2013-02-02 02:05:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_LUA_STACK_H_
|
|
|
|
#define __CC_LUA_STACK_H_
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "ccTypes.h"
|
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "CCLuaValue.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-04-27 18:24:08 +08:00
|
|
|
class CCLuaStack : public CCObject
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static CCLuaStack *create(void);
|
|
|
|
static CCLuaStack *attach(lua_State *L);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Method used to get a pointer to the lua_State that the script module is attached to.
|
|
|
|
@return A pointer to the lua_State that the script module is attached to.
|
|
|
|
*/
|
|
|
|
lua_State* getLuaState(void) {
|
2013-06-15 14:03:30 +08:00
|
|
|
return _state;
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add a path to find lua files in
|
|
|
|
@param path to be added to the Lua path
|
|
|
|
*/
|
|
|
|
virtual void addSearchPath(const char* path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add lua loader, now it is used on android
|
|
|
|
*/
|
|
|
|
virtual void addLuaLoader(lua_CFunction func);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove CCObject from lua state
|
|
|
|
@param object to remove
|
|
|
|
*/
|
|
|
|
virtual void removeScriptObjectByCCObject(CCObject* pObj);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove Lua function reference
|
|
|
|
*/
|
|
|
|
virtual void removeScriptHandler(int nHandler);
|
|
|
|
|
2013-06-05 15:17:00 +08:00
|
|
|
/**
|
|
|
|
@brief Remove Lua function reference
|
|
|
|
*/
|
|
|
|
virtual int reallocateScriptHandler(int nHandler);
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
/**
|
|
|
|
@brief Execute script code contained in the given string.
|
|
|
|
@param codes holding the valid script code that should be executed.
|
|
|
|
@return 0 if the string is excuted correctly.
|
|
|
|
@return other if the string is excuted wrongly.
|
|
|
|
*/
|
|
|
|
virtual int executeString(const char* codes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute a script file.
|
|
|
|
@param filename String object holding the filename of the script file that is to be executed
|
|
|
|
*/
|
|
|
|
virtual int executeScriptFile(const char* filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute a scripted global function.
|
|
|
|
@brief The function should not take any parameters and should return an integer.
|
|
|
|
@param functionName String object holding the name of the function, in the global script environment, that is to be executed.
|
|
|
|
@return The integer value returned from the script function.
|
|
|
|
*/
|
|
|
|
virtual int executeGlobalFunction(const char* functionName);
|
|
|
|
|
|
|
|
virtual void clean(void);
|
|
|
|
virtual void pushInt(int intValue);
|
|
|
|
virtual void pushFloat(float floatValue);
|
|
|
|
virtual void pushBoolean(bool boolValue);
|
|
|
|
virtual void pushString(const char* stringValue);
|
|
|
|
virtual void pushString(const char* stringValue, int length);
|
|
|
|
virtual void pushNil(void);
|
|
|
|
virtual void pushCCObject(CCObject* objectValue, const char* typeName);
|
|
|
|
virtual void pushCCLuaValue(const CCLuaValue& value);
|
|
|
|
virtual void pushCCLuaValueDict(const CCLuaValueDict& dict);
|
|
|
|
virtual void pushCCLuaValueArray(const CCLuaValueArray& array);
|
|
|
|
virtual bool pushFunctionByHandler(int nHandler);
|
|
|
|
virtual int executeFunction(int numArgs);
|
|
|
|
|
|
|
|
virtual int executeFunctionByHandler(int nHandler, int numArgs);
|
2013-04-15 22:50:07 +08:00
|
|
|
|
|
|
|
virtual bool handleAssert(const char *msg);
|
2013-02-02 02:05:52 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
CCLuaStack(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _state(NULL)
|
|
|
|
, _callFromLua(0)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(void);
|
|
|
|
bool initWithLuaState(lua_State *L);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_State *_state;
|
|
|
|
int _callFromLua;
|
2013-02-02 02:05:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CC_LUA_STACK_H_
|