summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaState.h
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2017-06-09 12:16:31 +0200
committerLukas Pioch <lukas@zgow.de>2017-06-09 12:16:31 +0200
commit3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6 (patch)
tree51b2547f57ffda8523432cacf10788cedbef352f /src/Bindings/LuaState.h
parentFixes problems with windows: (diff)
downloadcuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar.gz
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar.bz2
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar.lz
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar.xz
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.tar.zst
cuberite-3c4e443ddc211b4ecdd2b990a3fa9b12b46efaf6.zip
Diffstat (limited to 'src/Bindings/LuaState.h')
-rw-r--r--src/Bindings/LuaState.h37
1 files changed, 36 insertions, 1 deletions
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 <typename FnT, typename... Args>
bool Call(const FnT & a_Function, Args &&... args)
{
- ASSERT_LUA_STACK_BALANCE(m_LuaState);
+ cStackBalancePopper balancer(*this);
m_NumCurrentFunctionArgs = -1;
if (!PushFunction(std::forward<const FnT &>(a_Function)))
{