1
0

cLuaState: Added direct support for pushing a nil constant.

This commit is contained in:
Mattes D 2016-08-16 13:02:08 +02:00
parent 89c9c6fe46
commit 9493488e48
5 changed files with 29 additions and 26 deletions

View File

@ -82,7 +82,7 @@ void PushJsonValue(const Json::Value & a_Value, cLuaState & a_LuaState)
{ {
case Json::nullValue: case Json::nullValue:
{ {
a_LuaState.PushNil(); a_LuaState.Push(cLuaState::Nil);
break; break;
} }
@ -228,8 +228,7 @@ static int tolua_cJson_Parse(lua_State * a_LuaState)
Json::Reader reader; Json::Reader reader;
if (!reader.parse(input, root, false)) if (!reader.parse(input, root, false))
{ {
L.PushNil(); L.Push(cLuaState::Nil, Printf("Parsing Json failed: %s", reader.getFormattedErrorMessages().c_str()));
L.Push(Printf("Parsing Json failed: %s", reader.getFormattedErrorMessages().c_str()));
return 2; return 2;
} }

View File

@ -42,6 +42,7 @@ extern "C"
const cLuaState::cRet cLuaState::Return = {}; const cLuaState::cRet cLuaState::Return = {};
const cLuaState::cNil cLuaState::Nil = {};
/** Each Lua state stores a pointer to its creating cLuaState in Lua globals, under this name. /** Each Lua state stores a pointer to its creating cLuaState in Lua globals, under this name.
This way any cLuaState can reference the main cLuaState's TrackedCallbacks, mutex etc. */ This way any cLuaState can reference the main cLuaState's TrackedCallbacks, mutex etc. */
@ -751,18 +752,6 @@ bool cLuaState::PushFunction(const cRef & a_TableRef, const char * a_FnName)
void cLuaState::PushNil(void)
{
ASSERT(IsValid());
lua_pushnil(m_LuaState);
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::Push(const AString & a_String) void cLuaState::Push(const AString & a_String)
{ {
ASSERT(IsValid()); ASSERT(IsValid());
@ -860,6 +849,18 @@ void cLuaState::Push(const cItems & a_Items)
void cLuaState::Push(const cNil & a_Nil)
{
ASSERT(IsValid());
lua_pushnil(m_LuaState);
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::Push(const cPlayer * a_Player) void cLuaState::Push(const cPlayer * a_Player)
{ {
ASSERT(IsValid()); ASSERT(IsValid());

View File

@ -327,14 +327,21 @@ public:
T & m_Dest; T & m_Dest;
}; };
/** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */ /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */
class cRet class cRet
{ {
} ; } ;
static const cRet Return; // Use this constant to delimit function args from return values for cLuaState::Call() static const cRet Return; // Use this constant to delimit function args from return values for cLuaState::Call()
/** A dummy class that's used only to push a constant nil as a function parameter in Call(). */
class cNil
{
};
static const cNil Nil; // Use this constant to give a function a nil parameter in Call()
/** A RAII class for values pushed onto the Lua stack. /** A RAII class for values pushed onto the Lua stack.
Will pop the value off the stack in the destructor. */ Will pop the value off the stack in the destructor. */
class cStackValue class cStackValue
@ -489,8 +496,6 @@ public:
Push(std::forward<Arg2>(a_Arg2), std::forward<Args>(a_Args)...); Push(std::forward<Arg2>(a_Arg2), std::forward<Args>(a_Args)...);
} }
void PushNil(void);
// Push a const value onto the stack (keep alpha-sorted): // Push a const value onto the stack (keep alpha-sorted):
void Push(const AString & a_String); void Push(const AString & a_String);
void Push(const AStringMap & a_Dictionary); void Push(const AStringMap & a_Dictionary);
@ -499,6 +504,7 @@ public:
void Push(const cCraftingRecipe * a_Recipe); void Push(const cCraftingRecipe * a_Recipe);
void Push(const char * a_Value); void Push(const char * a_Value);
void Push(const cItems & a_Items); void Push(const cItems & a_Items);
void Push(const cNil & a_Nil);
void Push(const cPlayer * a_Player); void Push(const cPlayer * a_Player);
void Push(const cRef & a_Ref); void Push(const cRef & a_Ref);
void Push(const HTTPRequest * a_Request); void Push(const HTTPRequest * a_Request);

View File

@ -2058,8 +2058,7 @@ static int tolua_cUrlParser_Parse(lua_State * a_LuaState)
if (!res.first) if (!res.first)
{ {
// Error, return nil and error msg: // Error, return nil and error msg:
L.PushNil(); L.Push(cLuaState::Nil, res.second);
L.Push(res.second);
return 2; return 2;
} }
L.Push(scheme, username, password, host, port, path, query, fragment); L.Push(scheme, username, password, host, port, path, query, fragment);
@ -2099,8 +2098,7 @@ static int tolua_cUrlParser_ParseAuthorityPart(lua_State * a_LuaState)
if (!res.first) if (!res.first)
{ {
// Error, return nil and error msg: // Error, return nil and error msg:
L.PushNil(); L.Push(cLuaState::Nil, res.second);
L.Push(res.second);
return 2; return 2;
} }
L.Push(username, password, host, port); L.Push(username, password, host, port);

View File

@ -649,8 +649,7 @@ static int tolua_cTCPLink_StartTLSClient(lua_State * L)
AString res = Link->StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword); AString res = Link->StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword);
if (!res.empty()) if (!res.empty())
{ {
S.PushNil(); S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
S.Push(Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
return 2; return 2;
} }
return 1; return 1;
@ -695,10 +694,10 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L)
AString res = Link->StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData); AString res = Link->StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData);
if (!res.empty()) if (!res.empty())
{ {
S.PushNil(); S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
S.Push(Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str()));
return 2; return 2;
} }
S.Push(true);
return 1; return 1;
} }