Cocos2d-x  v3.17
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
LuaStack Class Reference

LuaStack is used to manager the operation on the lua_State,eg., push data onto the lua_State, execute the function depended on the lua_State. More...

Inherits Ref.

Public Member Functions

virtual ~LuaStack ()
 Destructor. More...
 
lua_State * getLuaState (void)
 Method used to get a pointer to the lua_State that the script module is attached to. More...
 
virtual void addSearchPath (const char *path)
 Add a path to find lua files in. More...
 
virtual void addLuaLoader (lua_CFunction func)
 Add lua loader. More...
 
virtual int reload (const char *moduleFileName)
 Reload script code corresponding to moduleFileName. More...
 
virtual void removeScriptObjectByObject (Ref *object)
 Remove the related reference about the Ref object stored in the Lua table by set the value of corresponding key nil: The related Lua tables are toluafix_refid_ptr_mapping,toluafix_refid_type_mapping,tolua_value_root and object_Metatable["tolua_ubox"] or tolua_ubox. More...
 
virtual void removeScriptHandler (int nHandler)
 Remove Lua function reference by nHandler by setting toluafix_refid_function_mapping[nHandle] nil. More...
 
virtual int reallocateScriptHandler (int nHandler)
 Reallocate Lua function reference index to the Lua function pointer to add reference. More...
 
virtual int executeString (const char *codes)
 Execute script code contained in the given string. More...
 
virtual int executeScriptFile (const char *filename)
 Execute a script file. More...
 
virtual int executeGlobalFunction (const char *functionName)
 Execute a scripted global function. More...
 
virtual void clean (void)
 Set the stack top index 0.
 
virtual void pushInt (int intValue)
 Pushes a integer number with value intValue onto the stack. More...
 
virtual void pushFloat (float floatValue)
 Pushes a float number with value floatValue onto the stack. More...
 
virtual void pushLong (long longValue)
 Pushes a long number with value longValue onto the stack. More...
 
virtual void pushBoolean (bool boolValue)
 Pushes a bool value with boolValue onto the stack. More...
 
virtual void pushString (const char *stringValue)
 Pushes the zero-terminated string pointed to by stringValue onto the stack. More...
 
virtual void pushString (const char *stringValue, int length)
 Pushes the string pointed to by stringValue with size length onto the stack. More...
 
virtual void pushNil (void)
 Pushes a nil value onto the stack.
 
virtual void pushObject (Ref *objectValue, const char *typeName)
 Pushes a Ref object onto the stack. More...
 
virtual void pushLuaValue (const LuaValue &value)
 According to the type of LuaValue, it would called the other push function in the internal. More...
 
virtual void pushLuaValueDict (const LuaValueDict &dict)
 Pushes a lua table onto the stack. More...
 
virtual void pushLuaValueArray (const LuaValueArray &array)
 Pushes a lua array table onto the stack. More...
 
virtual bool pushFunctionByHandler (int nHandler)
 Get the lua function pointer from toluafix_refid_function_mapping table by giving nHanlder. More...
 
virtual int executeFunction (int numArgs)
 Execute the lua function on the -(numArgs + 1) index on the stack by the numArgs variables passed. More...
 
virtual int executeFunctionByHandler (int nHandler, int numArgs)
 Execute the lua function corresponding to the nHandler by the numArgs variables passed. More...
 
virtual int executeFunctionReturnArray (int handler, int numArgs, int numResults, __Array &resultArray)
 Execute the lua function corresponding to the handler by the numArgs variables passed. More...
 
virtual int executeFunction (int handler, int numArgs, int numResults, const std::function< void(lua_State *, int)> &func)
 Execute the lua function corresponding to the handler by the numArgs variables passed. More...
 
virtual bool handleAssert (const char *msg)
 Handle the assert message. More...
 
virtual void setXXTEAKeyAndSign (const char *key, int keyLen, const char *sign, int signLen)
 Set the key and sign for xxtea encryption algorithm. More...
 
virtual void cleanupXXTEAKeyAndSign ()
 free the key and sign for xxtea encryption algorithm.
 
int luaLoadBuffer (lua_State *L, const char *chunk, int chunkSize, const char *chunkName)
 Loads a buffer as a Lua chunk.This function uses lua_load to load the Lua chunk in the buffer pointed to by chunk with size chunkSize. More...
 
int loadChunksFromZIP (const char *zipFilePath)
 Load the Lua chunks from the zip file. More...
 
int luaLoadChunksFromZIP (lua_State *L)
 Load the Lua chunks from current lua_State. More...
 
- Public Member Functions inherited from Ref
void retain ()
 Retains the ownership. More...
 
void release ()
 Releases the ownership immediately. More...
 
local release ()
 Releases the ownership immediately. More...
 
Refautorelease ()
 Releases the ownership sometime soon automatically. More...
 
unsigned int getReferenceCount () const
 Returns the Ref's current reference count. More...
 
local getReferenceCount ()
 Returns the Ref's current reference count. More...
 
virtual ~Ref ()
 Destructor. More...
 

Static Public Member Functions

static LuaStackcreate (void)
 Create a LuaStack object, it will new a lua_State.
 
static LuaStackattach (lua_State *L)
 Create a LuaStack object with the existed lua_State.
 

Additional Inherited Members

- Public Attributes inherited from Ref
unsigned int _ID
 object id, ScriptSupport need public _ID
 
local _ID
 object id, ScriptSupport need public _ID
 
int _luaID
 Lua reference id.
 
local _luaID
 Lua reference id.
 
void * _scriptObject
 scriptObject, support for swift
 
local _scriptObject
 scriptObject, support for swift
 
bool _rooted
 When true, it means that the object was already rooted.
 
local _rooted
 When true, it means that the object was already rooted.
 

Detailed Description

LuaStack is used to manager the operation on the lua_State,eg., push data onto the lua_State, execute the function depended on the lua_State.

In the current mechanism, there is only one lua_State in one LuaStack object.

Constructor & Destructor Documentation

virtual ~LuaStack ( )
virtual

Destructor.

Member Function Documentation

lua_State* getLuaState ( void  )
inline

Method used to get a pointer to the lua_State that the script module is attached to.

Returns
A pointer to the lua_State that the script module is attached to.
virtual void addSearchPath ( const char *  path)
virtual

Add a path to find lua files in.

Parameters
pathto be added to the Lua search path.
virtual void addLuaLoader ( lua_CFunction  func)
virtual

Add lua loader.

Parameters
funca function pointer point to the loader function.
virtual int reload ( const char *  moduleFileName)
virtual

Reload script code corresponding to moduleFileName.

If value of package["loaded"][moduleFileName] is existed, it would set the value nil.Then,it calls executeString function.

Parameters
moduleFileNameString object holding the filename of the script file that is to be executed.
Returns
0 if the string is executed correctly or other if the string is executed wrongly.
virtual void
removeScriptObjectByObject
( Ref object)
virtual

Remove the related reference about the Ref object stored in the Lua table by set the value of corresponding key nil: The related Lua tables are toluafix_refid_ptr_mapping,toluafix_refid_type_mapping,tolua_value_root and object_Metatable["tolua_ubox"] or tolua_ubox.

Meanwhile set the corresponding userdata nullptr and remove the all the lua function reference corresponding to this object.

In current mechanism, this function is called in the destructor of Ref object, developer don't call this functions.

Parameters
objectthe key object to remove script object.
virtual void removeScriptHandler ( int  nHandler)
virtual

Remove Lua function reference by nHandler by setting toluafix_refid_function_mapping[nHandle] nil.

Parameters
nHandlerthe function reference index to find the corresponding Lua function pointer.
virtual int reallocateScriptHandler ( int  nHandler)
virtual

Reallocate Lua function reference index to the Lua function pointer to add reference.

Parameters
nHandlerthe function reference index to find the corresponding Lua function pointer.
virtual int executeString ( const char *  codes)
virtual

Execute script code contained in the given string.

Parameters
codesholding the valid script code that should be executed.
Returns
0 if the string is executed correctly, other if the string is executed wrongly.
virtual int executeScriptFile ( const char *  filename)
virtual

Execute a script file.

Parameters
filenameString object holding the filename of the script file that is to be executed.
Returns
the return values by calling executeFunction.
virtual int executeGlobalFunction ( const char *  functionName)
virtual

Execute a scripted global function.

The function should not take any parameters and should return an integer.

Parameters
functionNameString object holding the name of the function, in the global script environment, that is to be executed.
Returns
The integer value returned from the script function.
virtual void pushInt ( int  intValue)
virtual

Pushes a integer number with value intValue onto the stack.

Parameters
intValuea integer number.
virtual void pushFloat ( float  floatValue)
virtual

Pushes a float number with value floatValue onto the stack.

Parameters
floatValuea float number.
virtual void pushLong ( long  longValue)
virtual

Pushes a long number with value longValue onto the stack.

Parameters
longValuea long number.
virtual void pushBoolean ( bool  boolValue)
virtual

Pushes a bool value with boolValue onto the stack.

Parameters
boolValuea bool value.
virtual void pushString ( const char *  stringValue)
virtual

Pushes the zero-terminated string pointed to by stringValue onto the stack.

Parameters
stringValuea pointer point to a zero-terminated string stringValue.
virtual void pushString ( const char *  stringValue,
int  length 
)
virtual

Pushes the string pointed to by stringValue with size length onto the stack.

Parameters
stringValuea pointer point to the string stringValue.
lengththe size.
virtual void pushObject ( Ref objectValue,
const char *  typeName 
)
virtual

Pushes a Ref object onto the stack.

See also
toluafix_pushusertype_ccobject.
virtual void pushLuaValue ( const LuaValue value)
virtual

According to the type of LuaValue, it would called the other push function in the internal.

type function LuaValueTypeInt pushInt LuaValueTypeFloat pushFloat LuaValueTypeBoolean pushBoolean LuaValueTypeString pushString LuaValueTypeDict pushLuaValueDict LuaValueTypeArray pushLuaValueArray LuaValueTypeObject pushObject

Parameters
valuea LuaValue object.
virtual void pushLuaValueDict ( const LuaValueDict &  dict)
virtual

Pushes a lua table onto the stack.

The key of table is the key of LuaValueDict which is std::map. The value of table is according to the type of LuaValue of LuaValueDict by calling pushLuaValue,

See also
pushLuaValue.
Parameters
dicta LuaValueDict object.
virtual void pushLuaValueArray ( const LuaValueArray &  array)
virtual

Pushes a lua array table onto the stack.

The index of array table is begin at 1. The value of array table is according to the type of LuaValue of LuaValueDict by calling pushLuaValue,

See also
pushLuaValue.
virtual bool pushFunctionByHandler ( int  nHandler)
virtual

Get the lua function pointer from toluafix_refid_function_mapping table by giving nHanlder.

If the lua function pointer corresponding to the nHanlder isn't found, it would push nil on the top index of stack, then it would output the error log in the debug model.

Returns
true if get the no-null function pointer otherwise false.
virtual int executeFunction ( int  numArgs)
virtual

Execute the lua function on the -(numArgs + 1) index on the stack by the numArgs variables passed.

Parameters
numArgsthe number of variables.
Returns
0 if it happen the error or it hasn't return value, otherwise it return the value by calling the lua function.
virtual int
executeFunctionByHandler
( int  nHandler,
int  numArgs 
)
virtual

Execute the lua function corresponding to the nHandler by the numArgs variables passed.

Parameters
nHandlerthe index count corresponding to the lua function.
numArgsthe number of variables.
Returns
the return value is the same as executeFunction,please
See also
executeFunction.
virtual int
executeFunctionReturnArray
( int  handler,
int  numArgs,
int  numResults,
__Array &  resultArray 
)
virtual

Execute the lua function corresponding to the handler by the numArgs variables passed.

By calling this function, the number of return value is numResults(may be > 1). All the return values are stored in the resultArray.

Parameters
handlerthe index count corresponding to the lua function.
numArgsthe number of variables.
numResultsthe number of return value.
resultArraya array used to store the return value.
Returns
0 if it happen error or it hasn't return value, otherwise return 1.
virtual int executeFunction ( int  handler,
int  numArgs,
int  numResults,
const std::function< void(lua_State *, int)> &  func 
)
virtual

Execute the lua function corresponding to the handler by the numArgs variables passed.

By calling this function, the number of return value is numResults(may be > 1). All the return values are used in the callback func.

Parameters
handlerthe index count corresponding to the lua function.
numArgsthe number of variables.
numResultsthe number of return value.
funccallback function which is called if the numResults > 0.
Returns
0 if it happen error or it hasn't return value, otherwise return 1.
virtual bool handleAssert ( const char *  msg)
virtual

Handle the assert message.

Returns
return true if current _callFromLua of LuaStack is not equal to 0 otherwise return false.
virtual void setXXTEAKeyAndSign ( const char *  key,
int  keyLen,
const char *  sign,
int  signLen 
)
virtual

Set the key and sign for xxtea encryption algorithm.

Parameters
keya string pointer
keyLenthe length of key
signa string sign
signLenthe length of sign
int luaLoadBuffer ( lua_State *  L,
const char *  chunk,
int  chunkSize,
const char *  chunkName 
)

Loads a buffer as a Lua chunk.This function uses lua_load to load the Lua chunk in the buffer pointed to by chunk with size chunkSize.

If it supports xxtea encryption algorithm, the chunk and the chunkSize would be processed by calling xxtea_decrypt to the real buffer and buffer size.

Parameters
Lthe current lua_State.
chunkthe buffer pointer.
chunkSizethe size of buffer.
chunkNamethe name of chunk pointer.
Returns
0, LUA_ERRSYNTAX or LUA_ERRMEM:.
int loadChunksFromZIP ( const char *  zipFilePath)

Load the Lua chunks from the zip file.

Parameters
zipFilePathfile path to zip file.
Returns
1 if load successfully otherwise 0.
int luaLoadChunksFromZIP ( lua_State *  L)

Load the Lua chunks from current lua_State.

Parameters
Lthe current lua_State.
Returns
1 if load successfully otherwise 0.

The documentation for this class was generated from the following file: