diff options
author | Mattes D <github@xoft.cz> | 2016-03-21 10:40:38 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2016-03-21 10:40:38 +0100 |
commit | fa53e98419152a83b2a74c5ea45a60449abc59c2 (patch) | |
tree | 766f4703a8c30a01bfc5148fe54c6d6b4a3e3cc7 /src/Bindings/LuaState.h | |
parent | Merge pull request #3084 from cuberite/seadragon91-patch-1 (diff) | |
parent | Revert "Lua callback" (diff) | |
download | cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.gz cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.bz2 cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.lz cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.xz cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.zst cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.zip |
Diffstat (limited to 'src/Bindings/LuaState.h')
-rw-r--r-- | src/Bindings/LuaState.h | 117 |
1 files changed, 5 insertions, 112 deletions
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 0509b09ed..215980033 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -80,20 +80,8 @@ public: /** Allows to use this class wherever an int (i. e. ref) is to be used */ explicit operator int(void) const { return m_Ref; } - /** Returns the Lua state associated with the value. */ - lua_State * GetLuaState(void) { return m_LuaState; } - - /** Creates a Lua reference to the specified object instance in the specified Lua state. - This is useful to make anti-GC references for objects that were created by Lua and need to stay alive longer than Lua GC would normally guarantee. */ - template <typename T> void CreateFromObject(cLuaState & a_LuaState, T && a_Object) - { - a_LuaState.Push(std::forward<T>(a_Object)); - RefStack(a_LuaState, -1); - a_LuaState.Pop(); - } - protected: - lua_State * m_LuaState; + cLuaState * m_LuaState; int m_Ref; // Remove the copy-constructor: @@ -124,76 +112,6 @@ public: } ; - /** Represents a callback to Lua that C++ code can call. - Is thread-safe and unload-safe. - When the Lua state is unloaded, the callback returns an error instead of calling into non-existent code. - To receive the callback instance from the Lua side, use RefStack() or (better) cLuaState::GetStackValue(). - Note that instances of this class are tracked in the canon LuaState instance, so that they can be invalidated - when the LuaState is unloaded; due to multithreading issues they can only be tracked by-ptr, which has - an unfortunate effect of disabling the copy and move constructors. */ - class cCallback - { - public: - /** Creates an unbound callback instance. */ - cCallback(void) = default; - - ~cCallback() - { - Clear(); - } - - /** Calls the Lua callback, if still available. - Returns true if callback has been called. - Returns false if the Lua state isn't valid anymore. */ - template <typename... Args> - bool Call(Args &&... args) - { - cCSLock Lock(m_CS); - if (!m_Ref.IsValid()) - { - return false; - } - cLuaState(m_Ref.GetLuaState()).Call(m_Ref, std::forward<Args>(args)...); - return true; - } - - /** Set the contained callback to the function in the specified Lua state's stack position. - If a callback has been previously contained, it is freed first. */ - bool RefStack(cLuaState & a_LuaState, int a_StackPos); - - /** Frees the contained callback, if any. */ - void Clear(void); - - /** Returns true if the contained callback is valid. */ - bool IsValid(void); - - /** Returns true if the callback resides in the specified Lua state. - Internally, compares the callback's canon Lua state. */ - bool IsSameLuaState(cLuaState & a_LuaState); - - protected: - friend class cLuaState; - - /** The mutex protecting m_Ref against multithreaded access */ - cCriticalSection m_CS; - - /** Reference to the Lua callback */ - cRef m_Ref; - - - /** Invalidates the callback, without untracking it from the cLuaState. - Called only from cLuaState when closing the Lua state. */ - void Invalidate(void); - - /** This class cannot be copied, because it is tracked in the LuaState by-ptr. */ - cCallback(const cCallback &) = delete; - - /** This class cannot be moved, because it is tracked in the LuaState by-ptr. */ - cCallback(cCallback &&) = delete; - }; - typedef SharedPtr<cCallback> cCallbackPtr; - - /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */ class cRet { @@ -343,16 +261,11 @@ public: void Push(const UInt32 a_Value); void Push(std::chrono::milliseconds a_time); - /** Pops the specified number of values off the top of the Lua stack. */ - void Pop(int a_NumValuesToPop = 1); - // GetStackValue() retrieves the value at a_StackPos, if it is a valid type. If not, a_Value is unchanged. // Returns whether value was changed // Enum values are checked for their allowed values and fail if the value is not assigned. bool GetStackValue(int a_StackPos, AString & a_Value); bool GetStackValue(int a_StackPos, bool & a_Value); - bool GetStackValue(int a_StackPos, cCallback & a_Callback); - bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback); bool GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result); bool GetStackValue(int a_StackPos, cRef & a_Ref); bool GetStackValue(int a_StackPos, double & a_Value); @@ -515,14 +428,10 @@ public: void ToString(int a_StackPos, AString & a_String); /** Logs all the elements' types on the API stack, with an optional header for the listing. */ - void LogStackValues(const char * a_Header = nullptr); + void LogStack(const char * a_Header = nullptr); /** Logs all the elements' types on the API stack, with an optional header for the listing. */ - static void LogStackValues(lua_State * a_LuaState, const char * a_Header = nullptr); - - /** Returns the canon Lua state (the primary cLuaState instance that was used to create, rather than attach, the lua_State structure). - Returns nullptr if the canon Lua state cannot be queried. */ - cLuaState * QueryCanonLuaState(void); + static void LogStack(lua_State * a_LuaState, const char * a_Header = nullptr); protected: @@ -532,7 +441,8 @@ protected: bool m_IsOwned; /** The subsystem name is used for reporting errors to the console, it is either "plugin %s" or "LuaScript" - whatever is given to the constructor. */ + whatever is given to the constructor + */ AString m_SubsystemName; /** Name of the currently pushed function (for the Push / Call chain) */ @@ -541,15 +451,6 @@ protected: /** Number of arguments currently pushed (for the Push / Call chain) */ int m_NumCurrentFunctionArgs; - /** The tracked callbacks. - This object will invalidate all of these when it is about to be closed. - Protected against multithreaded access by m_CSTrackedCallbacks. */ - std::vector<cCallback *> m_TrackedCallbacks; - - /** Protects m_TrackedTallbacks against multithreaded access. */ - cCriticalSection m_CSTrackedCallbacks; - - /** Variadic template terminator: If there's nothing more to push / pop, just call the function. Note that there are no return values either, because those are prefixed by a cRet value, so the arg list is never empty. */ bool PushCallPop(void) @@ -632,14 +533,6 @@ protected: /** Tries to break into the MobDebug debugger, if it is installed. */ static int BreakIntoDebugger(lua_State * a_LuaState); - - /** Adds the specified callback to tracking. - The callback will be invalidated when this Lua state is about to be closed. */ - void TrackCallback(cCallback & a_Callback); - - /** Removes the specified callback from tracking. - The callback will no longer be invalidated when this Lua state is about to be closed. */ - void UntrackCallback(cCallback & a_Callback); } ; |