summaryrefslogtreecommitdiffstats
path: root/source/LuaState.cpp
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2013-08-06 08:01:00 +0200
committermadmaxoft <github@xoft.cz>2013-08-06 08:01:00 +0200
commit2151bb8f5bad91975010ab1022177fdb94cc2a3e (patch)
tree897b195dad7deae2726b55491f68522d428548ac /source/LuaState.cpp
parentMerge branch 'master' into BlockTracing (diff)
downloadcuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar.gz
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar.bz2
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar.lz
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar.xz
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.tar.zst
cuberite-2151bb8f5bad91975010ab1022177fdb94cc2a3e.zip
Diffstat (limited to '')
-rw-r--r--source/LuaState.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/source/LuaState.cpp b/source/LuaState.cpp
index da86a4b20..117023755 100644
--- a/source/LuaState.cpp
+++ b/source/LuaState.cpp
@@ -37,6 +37,7 @@ extern "C"
cLuaState::cLuaState(const AString & a_SubsystemName) :
m_LuaState(NULL),
+ m_IsOwned(false),
m_SubsystemName(a_SubsystemName)
{
}
@@ -45,6 +46,17 @@ cLuaState::cLuaState(const AString & a_SubsystemName) :
+cLuaState::cLuaState(lua_State * a_AttachState) :
+ m_LuaState(a_AttachState),
+ m_IsOwned(false),
+ m_SubsystemName("<attached>")
+{
+}
+
+
+
+
+
cLuaState::~cLuaState()
{
if (IsValid())
@@ -70,6 +82,7 @@ void cLuaState::Create(void)
ManualBindings::Bind(m_LuaState);
luaopen_lsqlite3(m_LuaState);
luaopen_lxp(m_LuaState);
+ m_IsOwned = true;
}
@@ -83,8 +96,62 @@ void cLuaState::Close(void)
LOGWARNING("%s: Trying to close an invalid LuaState, ignoring.", __FUNCTION__);
return;
}
+ if (!m_IsOwned)
+ {
+ LOGWARNING(
+ "%s: Detected mis-use, calling Close() on an attached state (0x%p). Detaching instead.",
+ __FUNCTION__, m_LuaState
+ );
+ Detach();
+ return;
+ }
lua_close(m_LuaState);
m_LuaState = NULL;
+ m_IsOwned = false;
+}
+
+
+
+
+
+void cLuaState::Attach(lua_State * a_State)
+{
+ if (m_LuaState != NULL)
+ {
+ LOGINFO("%s: Already contains a LuaState (0x%p), will be closed / detached.", __FUNCTION__, m_LuaState);
+ if (m_IsOwned)
+ {
+ Close();
+ }
+ else
+ {
+ Detach();
+ }
+ }
+ m_LuaState = a_State;
+ m_IsOwned = false;
+}
+
+
+
+
+
+void cLuaState::Detach(void)
+{
+ if (m_LuaState == NULL)
+ {
+ return;
+ }
+ if (m_IsOwned)
+ {
+ LOGWARNING(
+ "%s: Detected a mis-use, calling Detach() when the state is owned. Closing the owned state (0x%p).",
+ __FUNCTION__, m_LuaState
+ );
+ Close();
+ return;
+ }
+ m_LuaState = NULL;
}