From 3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 Jun 2017 12:16:31 +0200 Subject: Fixed handling Lua errors in nested callbacks (#3755) --- src/Bindings/LuaState.h | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/Bindings/LuaState.h') diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index ac911557d..9cbfbf69d 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -105,6 +105,41 @@ public: #define ASSERT_LUA_STACK_BALANCE(...) #endif + + /** Makes sure that the Lua state's stack has the same number of elements on destruction as it had on construction of this object (RAII). + Can only remove elements, if there are less than expected, throws. */ + class cStackBalancePopper + { + public: + cStackBalancePopper(cLuaState & a_LuaState): + m_LuaState(a_LuaState), + m_Count(lua_gettop(a_LuaState)) + { + } + + ~cStackBalancePopper() + { + auto curTop = lua_gettop(m_LuaState); + if (curTop > m_Count) + { + // There are some leftover elements, adjust the stack: + m_LuaState.LogStackValues(Printf("Re-balancing Lua stack, expected %d values, got %d:", m_Count, curTop).c_str()); + lua_pop(m_LuaState, curTop - m_Count); + } + else if (curTop < m_Count) + { + // This is an irrecoverable error, rather than letting the Lua engine crash undefinedly later on, abort now: + LOGERROR("Unable to re-balance Lua stack, there are elements missing. Expected at least %d elements, got %d.", m_Count, curTop); + throw std::runtime_error(Printf("Unable to re-balance Lua stack, there are elements missing. Expected at least %d elements, got %d.", m_Count, curTop)); + } + } + + protected: + cLuaState & m_LuaState; + int m_Count; + }; + + /** Provides a RAII-style locking for the LuaState. Used mainly by the cPluginLua internals to provide the actual locking for interface operations, such as callbacks. */ class cLock @@ -704,7 +739,7 @@ public: template bool Call(const FnT & a_Function, Args &&... args) { - ASSERT_LUA_STACK_BALANCE(m_LuaState); + cStackBalancePopper balancer(*this); m_NumCurrentFunctionArgs = -1; if (!PushFunction(std::forward(a_Function))) { -- cgit v1.2.3