From 9493488e48c49a54e9b1ec892107735de6736c13 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 13:02:08 +0200 Subject: [PATCH] cLuaState: Added direct support for pushing a nil constant. --- src/Bindings/LuaJson.cpp | 5 ++--- src/Bindings/LuaState.cpp | 25 +++++++++++++------------ src/Bindings/LuaState.h | 12 +++++++++--- src/Bindings/ManualBindings.cpp | 6 ++---- src/Bindings/ManualBindings_Network.cpp | 7 +++---- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/Bindings/LuaJson.cpp b/src/Bindings/LuaJson.cpp index 39a720319..7a0ddb961 100644 --- a/src/Bindings/LuaJson.cpp +++ b/src/Bindings/LuaJson.cpp @@ -82,7 +82,7 @@ void PushJsonValue(const Json::Value & a_Value, cLuaState & a_LuaState) { case Json::nullValue: { - a_LuaState.PushNil(); + a_LuaState.Push(cLuaState::Nil); break; } @@ -228,8 +228,7 @@ static int tolua_cJson_Parse(lua_State * a_LuaState) Json::Reader reader; if (!reader.parse(input, root, false)) { - L.PushNil(); - L.Push(Printf("Parsing Json failed: %s", reader.getFormattedErrorMessages().c_str())); + L.Push(cLuaState::Nil, Printf("Parsing Json failed: %s", reader.getFormattedErrorMessages().c_str())); return 2; } diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index b5832802d..2e1415519 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -42,6 +42,7 @@ extern "C" 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. 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) { 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) { ASSERT(IsValid()); diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 303a59327..3c07ac5d2 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -327,14 +327,21 @@ public: T & m_Dest; }; + /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */ class cRet { } ; - 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. Will pop the value off the stack in the destructor. */ class cStackValue @@ -489,8 +496,6 @@ public: Push(std::forward(a_Arg2), std::forward(a_Args)...); } - void PushNil(void); - // Push a const value onto the stack (keep alpha-sorted): void Push(const AString & a_String); void Push(const AStringMap & a_Dictionary); @@ -499,6 +504,7 @@ public: void Push(const cCraftingRecipe * a_Recipe); void Push(const char * a_Value); void Push(const cItems & a_Items); + void Push(const cNil & a_Nil); void Push(const cPlayer * a_Player); void Push(const cRef & a_Ref); void Push(const HTTPRequest * a_Request); diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 8927b0b19..038c67995 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -2058,8 +2058,7 @@ static int tolua_cUrlParser_Parse(lua_State * a_LuaState) if (!res.first) { // Error, return nil and error msg: - L.PushNil(); - L.Push(res.second); + L.Push(cLuaState::Nil, res.second); return 2; } 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) { // Error, return nil and error msg: - L.PushNil(); - L.Push(res.second); + L.Push(cLuaState::Nil, res.second); return 2; } L.Push(username, password, host, port); diff --git a/src/Bindings/ManualBindings_Network.cpp b/src/Bindings/ManualBindings_Network.cpp index 68eba5870..c8565d23d 100644 --- a/src/Bindings/ManualBindings_Network.cpp +++ b/src/Bindings/ManualBindings_Network.cpp @@ -649,8 +649,7 @@ static int tolua_cTCPLink_StartTLSClient(lua_State * L) AString res = Link->StartTLSClient(OwnCert, OwnPrivKey, OwnPrivKeyPassword); if (!res.empty()) { - S.PushNil(); - S.Push(Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str())); + S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str())); return 2; } return 1; @@ -695,10 +694,10 @@ static int tolua_cTCPLink_StartTLSServer(lua_State * L) AString res = Link->StartTLSServer(OwnCert, OwnPrivKey, OwnPrivKeyPassword, StartTLSData); if (!res.empty()) { - S.PushNil(); - S.Push(Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str())); + S.Push(cLuaState::Nil, Printf("Cannot start TLS on link to %s:%d: %s", Link->GetRemoteIP().c_str(), Link->GetRemotePort(), res.c_str())); return 2; } + S.Push(true); return 1; }