From 310a25c45697dbf3b1de6b0363f529691c422bf1 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 9 Feb 2014 18:39:22 +0100 Subject: cLuaState::cRef can be unbound and re-bound. This will allow us to store Lua references as member variables in classes and initialize those later than in the constructor. --- src/Bindings/LuaState.cpp | 52 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index ac5ce47e1..22f342467 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -1240,13 +1240,21 @@ int cLuaState::ReportFnCallErrors(lua_State * a_LuaState) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cLuaState::cRef: +cLuaState::cRef::cRef(void) : + m_LuaState(NULL), + m_Ref(LUA_REFNIL) +{ +} + + + + + cLuaState::cRef::cRef(cLuaState & a_LuaState, int a_StackPos) : - m_LuaState(a_LuaState) + m_LuaState(NULL), + m_Ref(LUA_REFNIL) { - ASSERT(m_LuaState.IsValid()); - - lua_pushvalue(m_LuaState, a_StackPos); // Push a copy of the value at a_StackPos onto the stack - m_Ref = luaL_ref(m_LuaState, LUA_REGISTRYINDEX); + RefStack(a_LuaState, a_StackPos); } @@ -1255,12 +1263,42 @@ cLuaState::cRef::cRef(cLuaState & a_LuaState, int a_StackPos) : cLuaState::cRef::~cRef() { - ASSERT(m_LuaState.IsValid()); + if (m_LuaState != NULL) + { + UnRef(); + } +} + + + + + +void cLuaState::cRef::RefStack(cLuaState & a_LuaState, int a_StackPos) +{ + ASSERT(a_LuaState.IsValid()); + if (m_LuaState != NULL) + { + UnRef(); + } + m_LuaState = &a_LuaState; + lua_pushvalue(a_LuaState, a_StackPos); // Push a copy of the value at a_StackPos onto the stack + m_Ref = luaL_ref(a_LuaState, LUA_REGISTRYINDEX); +} + + + + + +void cLuaState::cRef::UnRef(void) +{ + ASSERT(m_LuaState->IsValid()); // The reference should be destroyed before destroying the LuaState if (IsValid()) { - luaL_unref(m_LuaState, LUA_REGISTRYINDEX, m_Ref); + luaL_unref(*m_LuaState, LUA_REGISTRYINDEX, m_Ref); } + m_LuaState = NULL; + m_Ref = LUA_REFNIL; } -- cgit v1.2.3 From 589a4839dfe05810235a16f430e72f5e622b09ee Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 10 Feb 2014 22:44:56 +0100 Subject: cLuaState: Stack traces don't include ghost 0-th element. --- src/Bindings/LuaState.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 22f342467..8f2a70a60 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -1206,7 +1206,7 @@ void cLuaState::LogStack(const char * a_Header) void cLuaState::LogStack(lua_State * a_LuaState, const char * a_Header) { LOGD((a_Header != NULL) ? a_Header : "Lua C API Stack contents:"); - for (int i = lua_gettop(a_LuaState); i >= 0; i--) + for (int i = lua_gettop(a_LuaState); i > 0; i--) { AString Value; int Type = lua_type(a_LuaState, i); -- cgit v1.2.3 From b41bb3bb44bbc098622e45e17ed3adf34eadf2aa Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 11 Feb 2014 08:52:14 +0100 Subject: Fixed nested plugin function calls. --- src/Bindings/LuaState.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/Bindings/LuaState.cpp') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 8f2a70a60..24e64d9b2 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -735,17 +735,20 @@ bool cLuaState::CallFunction(int a_NumResults) ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 1)); // The function to call ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 2)); // The error handler - int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, -m_NumCurrentFunctionArgs - 2); + // Save the current "stack" state and reset, in case the callback calls another function: + AString CurrentFunctionName; + std::swap(m_CurrentFunctionName, CurrentFunctionName); + int NumArgs = m_NumCurrentFunctionArgs; + m_NumCurrentFunctionArgs = -1; + + // Call the function: + int s = lua_pcall(m_LuaState, NumArgs, a_NumResults, -NumArgs - 2); if (s != 0) { // The error has already been printed together with the stacktrace - LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName.c_str()); - m_NumCurrentFunctionArgs = -1; - m_CurrentFunctionName.clear(); + LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), CurrentFunctionName.c_str()); return false; } - m_NumCurrentFunctionArgs = -1; - m_CurrentFunctionName.clear(); // Remove the error handler from the stack: lua_remove(m_LuaState, -a_NumResults - 1); -- cgit v1.2.3