diff options
author | Mattes D <github@xoft.cz> | 2016-03-16 10:58:28 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2016-03-17 19:29:03 +0100 |
commit | af200dfaae63e61bf0bbd237582e3cbbe08baed6 (patch) | |
tree | 10155f63ac6ba948d965853ae20a083f7f49ecba /src/Bindings/LuaState.cpp | |
parent | Removed the cPluginLua::cResettable class. (diff) | |
download | cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar.gz cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar.bz2 cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar.lz cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar.xz cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.tar.zst cuberite-af200dfaae63e61bf0bbd237582e3cbbe08baed6.zip |
Diffstat (limited to 'src/Bindings/LuaState.cpp')
-rw-r--r-- | src/Bindings/LuaState.cpp | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 91a9ef4d6..98507e96c 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -180,6 +180,25 @@ bool cLuaState::cCallback::IsValid(void) +bool cLuaState::cCallback::IsSameLuaState(cLuaState & a_LuaState) +{ + cCSLock lock(m_CS); + if (!m_Ref.IsValid()) + { + return false; + } + auto canonState = a_LuaState.QueryCanonLuaState(); + if (canonState == nullptr) + { + return false; + } + return (m_Ref.GetLuaState() == static_cast<lua_State *>(*canonState)); +} + + + + + void cLuaState::cCallback::Invalidate(void) { cCSLock Lock(m_CS); @@ -919,6 +938,18 @@ void cLuaState::Push(std::chrono::milliseconds a_Value) +void cLuaState::Pop(int a_NumValuesToPop) +{ + ASSERT(IsValid()); + + lua_pop(m_LuaState, a_NumValuesToPop); + m_NumCurrentFunctionArgs -= a_NumValuesToPop; +} + + + + + bool cLuaState::GetStackValue(int a_StackPos, AString & a_Value) { size_t len = 0; @@ -1647,7 +1678,7 @@ int cLuaState::CopyStackFrom(cLuaState & a_SrcLuaState, int a_SrcStart, int a_Sr LOGWARNING("%s: Unsupported value: '%s' at stack position %d. Can only copy numbers, strings, bools and classes!", __FUNCTION__, lua_typename(a_SrcLuaState, t), i ); - a_SrcLuaState.LogStack("Stack where copying failed:"); + a_SrcLuaState.LogStackValues("Stack where copying failed:"); lua_pop(m_LuaState, i - a_SrcStart); return -1; } @@ -1674,16 +1705,16 @@ void cLuaState::ToString(int a_StackPos, AString & a_String) -void cLuaState::LogStack(const char * a_Header) +void cLuaState::LogStackValues(const char * a_Header) { - LogStack(m_LuaState, a_Header); + LogStackValues(m_LuaState, a_Header); } -void cLuaState::LogStack(lua_State * a_LuaState, const char * a_Header) +void cLuaState::LogStackValues(lua_State * a_LuaState, const char * a_Header) { // Format string consisting only of %s is used to appease the compiler LOG("%s", (a_Header != nullptr) ? a_Header : "Lua C API Stack contents:"); @@ -1708,6 +1739,21 @@ void cLuaState::LogStack(lua_State * a_LuaState, const char * a_Header) +cLuaState * cLuaState::QueryCanonLuaState(void) +{ + // Get the CanonLuaState global from Lua: + auto cb = WalkToNamedGlobal(g_CanonLuaStateGlobalName); + if (!cb.IsValid()) + { + return nullptr; + } + return reinterpret_cast<cLuaState *>(lua_touserdata(m_LuaState, -1)); +} + + + + + int cLuaState::ReportFnCallErrors(lua_State * a_LuaState) { LOGWARNING("LUA: %s", lua_tostring(a_LuaState, -1)); @@ -1745,17 +1791,16 @@ int cLuaState::BreakIntoDebugger(lua_State * a_LuaState) void cLuaState::TrackCallback(cCallback & a_Callback) { // Get the CanonLuaState global from Lua: - auto cb = WalkToNamedGlobal(g_CanonLuaStateGlobalName); - if (!cb.IsValid()) + auto canonState = QueryCanonLuaState(); + if (canonState == nullptr) { LOGWARNING("%s: Lua state %p has invalid CanonLuaState!", __FUNCTION__, reinterpret_cast<void *>(m_LuaState)); return; } - auto & canonState = *reinterpret_cast<cLuaState *>(lua_touserdata(m_LuaState, -1)); // Add the callback: - cCSLock Lock(canonState.m_CSTrackedCallbacks); - canonState.m_TrackedCallbacks.push_back(&a_Callback); + cCSLock Lock(canonState->m_CSTrackedCallbacks); + canonState->m_TrackedCallbacks.push_back(&a_Callback); } @@ -1765,17 +1810,16 @@ void cLuaState::TrackCallback(cCallback & a_Callback) void cLuaState::UntrackCallback(cCallback & a_Callback) { // Get the CanonLuaState global from Lua: - auto cb = WalkToNamedGlobal(g_CanonLuaStateGlobalName); - if (!cb.IsValid()) + auto canonState = QueryCanonLuaState(); + if (canonState == nullptr) { LOGWARNING("%s: Lua state %p has invalid CanonLuaState!", __FUNCTION__, reinterpret_cast<void *>(m_LuaState)); return; } - auto & canonState = *reinterpret_cast<cLuaState *>(lua_touserdata(m_LuaState, -1)); // Remove the callback: - cCSLock Lock(canonState.m_CSTrackedCallbacks); - auto & trackedCallbacks = canonState.m_TrackedCallbacks; + cCSLock Lock(canonState->m_CSTrackedCallbacks); + auto & trackedCallbacks = canonState->m_TrackedCallbacks; trackedCallbacks.erase(std::remove_if(trackedCallbacks.begin(), trackedCallbacks.end(), [&a_Callback](cCallback * a_StoredCallback) { |