From 71bb41ee86e262103909d8007dbc2ef876cd0b24 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 4 Aug 2013 23:11:25 +0200 Subject: LuaState refactoring: initial part. The cLuaState class is a wrapper for the lua_State * and for the common functions on it. The cPlugin_NewLua has been rewritten to use it instead of the raw pointer. Part of #33 --- source/LuaState.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 source/LuaState.h (limited to 'source/LuaState.h') diff --git a/source/LuaState.h b/source/LuaState.h new file mode 100644 index 000000000..10ff53911 --- /dev/null +++ b/source/LuaState.h @@ -0,0 +1,90 @@ + +// LuaState.h + +// Declares the cLuaState class representing the wrapper over lua_State *, provides associated helper functions + + + + + +#pragma once + + + + + +// fwd: lua.h +struct lua_State; + + + + + +class cLuaState +{ +public: + /** Creates a new instance. The LuaState is not initialized. + a_SubsystemName is used for reporting problems in the console, it is "plugin %s" for plugins, + or "LuaScript" for the cLuaScript template + */ + cLuaState(const AString & a_SubsystemName); + + ~cLuaState(); + + operator lua_State * (void) { return m_LuaState; } + + /// Creates the m_LuaState, if not closed already + void Create(void); + + /// Closes the m_LuaState, if not closed already + void Close(void); + + /// Returns true if the m_LuaState is valid + bool IsValid(void) const { return (m_LuaState != NULL); } + + /** Loads the specified file + Returns false and logs a warning to the console if not successful (but the LuaState is kept open). + m_SubsystemName is displayed in the warning log message. + */ + bool LoadFile(const AString & a_FileName); + + /** Pushes the function of the specified name onto the stack. + Returns true if successful. + If a_ShouldLogFail is true, logs a warning on failure (incl. m_SubsystemName) + */ + bool PushFunction(const char * a_FunctionName, bool a_ShouldLogFailure = true); + + /** Pushes a function that has been saved into the global registry, identified by a_FnRef. + Returns true if successful. Logs a warning on failure + */ + bool PushFunctionFromRegistry(int a_FnRef); + + /// Pushes a string vector, as a table, onto the stack + void PushStringVector(const AStringVector & a_Vector); + + /** + Calls the function that has been pushed onto the stack by PushFunction. + Returns true if successful, logs a warning on failure. + a_FunctionName is used only for the warning log message, the function + to be called must be pushed by PushFunction() beforehand. + */ + bool CallFunction(int a_NumArgs, int a_NumResults, const char * a_FunctionName); + + /// If the status is nonzero, prints the text on the top of Lua stack and returns true + bool ReportErrors(int status); + + /// If the status is nonzero, prints the text on the top of Lua stack and returns true + static bool ReportErrors(lua_State * a_LuaState, int status); + +protected: + lua_State * m_LuaState; + + /** The subsystem name is used for reporting errors to the console, it is either "plugin %s" or "LuaScript" + whatever is given to the constructor + */ + AString m_SubsystemName; +} ; + + + + -- cgit v1.2.3 From 2151bb8f5bad91975010ab1022177fdb94cc2a3e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 6 Aug 2013 08:01:00 +0200 Subject: cLuaState can now contain a detached LuaState, too. This will be useful for cases when we get a lua_State * from the outside and are asked to perform operations on it. --- source/LuaState.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'source/LuaState.h') diff --git a/source/LuaState.h b/source/LuaState.h index 10ff53911..fca532d89 100644 --- a/source/LuaState.h +++ b/source/LuaState.h @@ -3,6 +3,12 @@ // Declares the cLuaState class representing the wrapper over lua_State *, provides associated helper functions +/* +The contained lua_State can be either owned or attached. +Owned lua_State is created by calling Create() and the cLuaState automatically closes the state +Or, lua_State can be attached by calling Attach(), the cLuaState doesn't close such a state +Attaching a state will automatically close an owned state. +*/ @@ -29,16 +35,28 @@ public: */ cLuaState(const AString & a_SubsystemName); + /** Creates a new instance. The a_AttachState is attached. + Subsystem name is set to "". + */ + explicit cLuaState(lua_State * a_AttachState); + ~cLuaState(); + /// Allows this object to be used in the same way as a lua_State *, for example in the LuaLib functions operator lua_State * (void) { return m_LuaState; } - /// Creates the m_LuaState, if not closed already + /// Creates the m_LuaState, if not closed already. This state will be automatically closed in the destructor void Create(void); /// Closes the m_LuaState, if not closed already void Close(void); + /// Attaches the specified state. Operations will be carried out on this state, but it will not be closed in the destructor + void Attach(lua_State * a_State); + + /// Detaches a previously attached state. + void Detach(void); + /// Returns true if the m_LuaState is valid bool IsValid(void) const { return (m_LuaState != NULL); } @@ -79,6 +97,9 @@ public: protected: lua_State * m_LuaState; + /// If true, the state is owned by this object and will be auto-Closed. False => attached state + 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 */ -- cgit v1.2.3 From 2030bd47c823b47606e9567a1974761f80cf0d55 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 6 Aug 2013 08:59:54 +0200 Subject: cLuaState now tracks the function name and number of args --- source/LuaState.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'source/LuaState.h') diff --git a/source/LuaState.h b/source/LuaState.h index fca532d89..18c7c1354 100644 --- a/source/LuaState.h +++ b/source/LuaState.h @@ -8,6 +8,11 @@ The contained lua_State can be either owned or attached. Owned lua_State is created by calling Create() and the cLuaState automatically closes the state Or, lua_State can be attached by calling Attach(), the cLuaState doesn't close such a state Attaching a state will automatically close an owned state. + +Calling a Lua function is done by pushing the function, either by PushFunction() or PushFunctionFromRegistry(), +then pushing the arguments (PushString(), PushNumber(), PushUserData() etc.) and finally +executing CallFunction(). cLuaState automatically keeps track of the number of arguments and the name of the +function (for logging purposes), which makes the call less error-prone. */ @@ -22,6 +27,14 @@ Attaching a state will automatically close an owned state. // fwd: lua.h struct lua_State; +class cWorld; +class cPlayer; +class cEntity; +class cItem; +class cItems; +class cClientHandle; +class cPickup; + @@ -80,13 +93,36 @@ public: /// Pushes a string vector, as a table, onto the stack void PushStringVector(const AStringVector & a_Vector); + /// Pushes a usertype of the specified class type onto the stack + void PushUserType(void * a_Object, const char * a_Type); + + /// Pushes an integer onto the stack + void PushNumber(int a_Value); + + /// Pushes a double onto the stack + void PushNumber(double a_Value); + + /// Pushes a string onto the stack + void PushString(const char * a_Value); + + /// Pushes a bool onto the stack + void PushBool(bool a_Value); + + // Special family of functions that do PushUserType internally, but require one less parameter + void PushObject(cWorld * a_World); + void PushObject(cPlayer * a_Player); + void PushObject(cEntity * a_Entity); + void PushObject(cItem * a_Item); + void PushObject(cItems * a_Items); + void PushObject(cClientHandle * a_ClientHandle); + void PushObject(cPickup * a_Pickup); + /** - Calls the function that has been pushed onto the stack by PushFunction. + Calls the function that has been pushed onto the stack by PushFunction(), + with arguments pushed by PushXXX(). Returns true if successful, logs a warning on failure. - a_FunctionName is used only for the warning log message, the function - to be called must be pushed by PushFunction() beforehand. */ - bool CallFunction(int a_NumArgs, int a_NumResults, const char * a_FunctionName); + bool CallFunction(int a_NumReturnValues); /// If the status is nonzero, prints the text on the top of Lua stack and returns true bool ReportErrors(int status); @@ -104,6 +140,12 @@ protected: whatever is given to the constructor */ AString m_SubsystemName; + + /// Name of the currently pushed function (for the Push / Call chain) + AString m_CurrentFunctionName; + + /// Number of arguments currently pushed (for the Push / Call chain) + int m_NumCurrentFunctionArgs; } ; -- cgit v1.2.3 From 9b839aa32efe0287f5b28fd60285b10c6cf21d59 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 7 Aug 2013 14:26:18 +0200 Subject: cLuaState has reference management, param checking and a fixed destructor. References are now managed as RAII objects, cLuaState::cRef. Destructor now calls correct function, either Close() or Detach(), based on the owned-ness of the lua_State *. --- source/LuaState.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'source/LuaState.h') diff --git a/source/LuaState.h b/source/LuaState.h index 18c7c1354..6820f1777 100644 --- a/source/LuaState.h +++ b/source/LuaState.h @@ -13,6 +13,10 @@ Calling a Lua function is done by pushing the function, either by PushFunction() then pushing the arguments (PushString(), PushNumber(), PushUserData() etc.) and finally executing CallFunction(). cLuaState automatically keeps track of the number of arguments and the name of the function (for logging purposes), which makes the call less error-prone. + +Reference management is provided by the cLuaState::cRef class. This is used when you need to hold a reference to +any Lua object across several function calls; usually this is used for callbacks. The class is RAII-like, with +automatic resource management. */ @@ -20,12 +24,14 @@ function (for logging purposes), which makes the call less error-prone. #pragma once +extern "C" +{ + #include "lauxlib.h" +} -// fwd: lua.h -struct lua_State; class cWorld; class cPlayer; @@ -39,9 +45,31 @@ class cPickup; +/// Encapsulates a Lua state and provides some syntactic sugar for common operations class cLuaState { public: + + /// Used for storing references to object in the global registry + class cRef + { + public: + /// Creates a reference in the specified LuaState for object at the specified StackPos + cRef(cLuaState & a_LuaState, int a_StackPos); + ~cRef(); + + /// Returns true if the reference is valid + bool IsValid(void) const {return (m_Ref != LUA_REFNIL); } + + /// Allows to use this class wherever an int (i. e. ref) is to be used + operator int(void) { return m_Ref; } + + protected: + cLuaState & m_LuaState; + int m_Ref; + } ; + + /** Creates a new instance. The LuaState is not initialized. a_SubsystemName is used for reporting problems in the console, it is "plugin %s" for plugins, or "LuaScript" for the cLuaScript template @@ -90,6 +118,11 @@ public: */ bool PushFunctionFromRegistry(int a_FnRef); + /** Pushes a function that is stored in a table ref. + Returns true if successful, false on failure. Doesn't log failure. + */ + bool PushFunctionFromRefTable(cRef & a_TableRef, const char * a_FnName); + /// Pushes a string vector, as a table, onto the stack void PushStringVector(const AStringVector & a_Vector); @@ -123,7 +156,19 @@ public: Returns true if successful, logs a warning on failure. */ bool CallFunction(int a_NumReturnValues); - + + /// Returns true if the specified parameters on the stack are of the specified usertype; also logs warning if not + bool CheckParamUserType(int a_StartParam, const char * a_UserType, int a_EndParam = -1); + + /// Returns true if the specified parameters on the stack are a table; also logs warning if not + bool CheckParamTable(int a_StartParam, int a_EndParam = -1); + + /// Returns true if the specified parameters on the stack are a number; also logs warning if not + bool CheckParamNumber(int a_StartParam, int a_EndParam = -1); + + /// Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters) + bool CheckParamEnd(int a_Param); + /// If the status is nonzero, prints the text on the top of Lua stack and returns true bool ReportErrors(int status); -- cgit v1.2.3