From 89c9c6fe46ce4a273a15b7f882c3d0935a868145 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 11:55:12 +0200 Subject: cLuaState: Added support for optional params and AStringMap values. --- src/Bindings/LuaState.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index fd29e8e34..b5832802d 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -341,6 +341,33 @@ void cLuaState::cStackTable::ForEachArrayElement(std::function a_ElementCallback) const +{ + #ifdef _DEBUG + auto stackTop = lua_gettop(m_LuaState); + #endif + lua_pushvalue(m_LuaState, m_StackPos); // Stk: + lua_pushnil(m_LuaState); // Stk:
nil + while (lua_next(m_LuaState, -2)) // Stk:
+ { + auto shouldAbort = a_ElementCallback(m_LuaState); + ASSERT(lua_gettop(m_LuaState) == stackTop + 3); // The element callback must not change the Lua stack below the value + lua_pop(m_LuaState, 1); // Stk:
+ if (shouldAbort) + { + // The callback wants to abort + lua_pop(m_LuaState, 2); // Stk: empty + return; + } + } + // Stk:
+ lua_pop(m_LuaState, 1); // Stk: empty +} + + + + + //////////////////////////////////////////////////////////////////////////////// // cLuaState: @@ -748,6 +775,24 @@ void cLuaState::Push(const AString & a_String) +void cLuaState::Push(const AStringMap & a_Dictionary) +{ + ASSERT(IsValid()); + + lua_createtable(m_LuaState, 0, static_cast(a_Dictionary.size())); + int newTable = lua_gettop(m_LuaState); + for (const auto & item: a_Dictionary) + { + Push(item.first); // key + Push(item.second); // value + lua_rawset(m_LuaState, newTable); + } +} + + + + + void cLuaState::Push(const AStringVector & a_Vector) { ASSERT(IsValid()); @@ -1113,6 +1158,37 @@ bool cLuaState::GetStackValue(int a_StackPos, AString & a_Value) +bool cLuaState::GetStackValue(int a_StackPos, AStringMap & a_Value) +{ + // Retrieve all values in a string => string dictionary table: + if (!lua_istable(m_LuaState, a_StackPos)) + { + return false; + } + cStackTable tbl(*this, a_StackPos); + bool isValid = true; + tbl.ForEachElement([&isValid, &a_Value](cLuaState & a_LuaState) + { + AString key, val; + if (a_LuaState.GetStackValues(-2, key, val)) + { + a_Value[key] = val; + } + else + { + isValid = false; + return true; + } + return false; + } + ); + return isValid; +} + + + + + bool cLuaState::GetStackValue(int a_StackPos, bool & a_ReturnedVal) { a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0); -- cgit v1.2.3 From 9493488e48c49a54e9b1ec892107735de6736c13 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 13:02:08 +0200 Subject: cLuaState: Added direct support for pushing a nil constant. --- src/Bindings/LuaState.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/Bindings/LuaState.cpp') 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()); -- cgit v1.2.3 From 11682d1386299d78bab39f77884797981950edee Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 16 Aug 2016 14:05:03 +0200 Subject: cLuaState: Moved function param counting to PushCallPop() template. The Push() functions can be used not only for function params, but also returns or temporaries, so it doesn't make sense to count the params there. --- src/Bindings/LuaState.cpp | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 2e1415519..ec6bdb48a 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -757,7 +757,6 @@ void cLuaState::Push(const AString & a_String) ASSERT(IsValid()); lua_pushlstring(m_LuaState, a_String.data(), a_String.size()); - m_NumCurrentFunctionArgs += 1; } @@ -794,7 +793,6 @@ void cLuaState::Push(const AStringVector & a_Vector) tolua_pushstring(m_LuaState, itr->c_str()); lua_rawseti(m_LuaState, newTable, index); } - m_NumCurrentFunctionArgs += 1; } @@ -806,7 +804,6 @@ void cLuaState::Push(const cCraftingGrid * a_Grid) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Grid)), "cCraftingGrid"); - m_NumCurrentFunctionArgs += 1; } @@ -818,7 +815,6 @@ void cLuaState::Push(const cCraftingRecipe * a_Recipe) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Recipe)), "cCraftingRecipe"); - m_NumCurrentFunctionArgs += 1; } @@ -830,7 +826,6 @@ void cLuaState::Push(const char * a_Value) ASSERT(IsValid()); tolua_pushstring(m_LuaState, a_Value); - m_NumCurrentFunctionArgs += 1; } @@ -842,7 +837,6 @@ void cLuaState::Push(const cItems & a_Items) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(&a_Items)), "cItems"); - m_NumCurrentFunctionArgs += 1; } @@ -854,7 +848,6 @@ void cLuaState::Push(const cNil & a_Nil) ASSERT(IsValid()); lua_pushnil(m_LuaState); - m_NumCurrentFunctionArgs += 1; } @@ -866,7 +859,6 @@ void cLuaState::Push(const cPlayer * a_Player) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Player)), "cPlayer"); - m_NumCurrentFunctionArgs += 1; } @@ -878,7 +870,6 @@ void cLuaState::Push(const cLuaState::cRef & a_Ref) ASSERT(IsValid()); lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, static_cast(a_Ref)); - m_NumCurrentFunctionArgs += 1; } @@ -890,7 +881,6 @@ void cLuaState::Push(const HTTPRequest * a_Request) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Request)), "HTTPRequest"); - m_NumCurrentFunctionArgs += 1; } @@ -902,7 +892,6 @@ void cLuaState::Push(const HTTPTemplateRequest * a_Request) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Request)), "HTTPTemplateRequest"); - m_NumCurrentFunctionArgs += 1; } @@ -914,7 +903,6 @@ void cLuaState::Push(const Vector3d & a_Vector) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(&a_Vector)), "Vector3"); - m_NumCurrentFunctionArgs += 1; } @@ -926,7 +914,6 @@ void cLuaState::Push(const Vector3d * a_Vector) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Vector)), "Vector3"); - m_NumCurrentFunctionArgs += 1; } @@ -938,7 +925,6 @@ void cLuaState::Push(const Vector3i & a_Vector) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(&a_Vector)), "Vector3"); - m_NumCurrentFunctionArgs += 1; } @@ -950,7 +936,6 @@ void cLuaState::Push(const Vector3i * a_Vector) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, reinterpret_cast(const_cast(a_Vector)), "Vector3"); - m_NumCurrentFunctionArgs += 1; } @@ -962,7 +947,6 @@ void cLuaState::Push(bool a_Value) ASSERT(IsValid()); tolua_pushboolean(m_LuaState, a_Value ? 1 : 0); - m_NumCurrentFunctionArgs += 1; } @@ -1027,8 +1011,6 @@ void cLuaState::Push(cEntity * a_Entity) } } // switch (EntityType) } - - m_NumCurrentFunctionArgs += 1; } @@ -1040,7 +1022,6 @@ void cLuaState::Push(cLuaServerHandle * a_ServerHandle) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, a_ServerHandle, "cServerHandle"); - m_NumCurrentFunctionArgs += 1; } @@ -1052,7 +1033,6 @@ void cLuaState::Push(cLuaTCPLink * a_TCPLink) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, a_TCPLink, "cTCPLink"); - m_NumCurrentFunctionArgs += 1; } @@ -1064,7 +1044,6 @@ void cLuaState::Push(cLuaUDPEndpoint * a_UDPEndpoint) ASSERT(IsValid()); tolua_pushusertype(m_LuaState, a_UDPEndpoint, "cUDPEndpoint"); - m_NumCurrentFunctionArgs += 1; } @@ -1076,7 +1055,6 @@ void cLuaState::Push(double a_Value) ASSERT(IsValid()); tolua_pushnumber(m_LuaState, a_Value); - m_NumCurrentFunctionArgs += 1; } @@ -1088,7 +1066,6 @@ void cLuaState::Push(int a_Value) ASSERT(IsValid()); tolua_pushnumber(m_LuaState, a_Value); - m_NumCurrentFunctionArgs += 1; } @@ -1100,7 +1077,6 @@ void cLuaState::Push(long a_Value) ASSERT(IsValid()); tolua_pushnumber(m_LuaState, static_cast(a_Value)); - m_NumCurrentFunctionArgs += 1; } @@ -1112,7 +1088,6 @@ void cLuaState::Push(UInt32 a_Value) ASSERT(IsValid()); tolua_pushnumber(m_LuaState, a_Value); - m_NumCurrentFunctionArgs += 1; } @@ -1124,7 +1099,6 @@ void cLuaState::Push(std::chrono::milliseconds a_Value) ASSERT(IsValid()); tolua_pushnumber(m_LuaState, static_cast(a_Value.count())); - m_NumCurrentFunctionArgs += 1; } @@ -1136,7 +1110,6 @@ void cLuaState::Pop(int a_NumValuesToPop) ASSERT(IsValid()); lua_pop(m_LuaState, a_NumValuesToPop); - m_NumCurrentFunctionArgs -= a_NumValuesToPop; } -- cgit v1.2.3