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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCLuaStack.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
#include "tolua++.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "tolua_fix.h"
|
|
|
|
}
|
|
|
|
|
2013-04-26 12:12:19 +08:00
|
|
|
#include "Cocos2dxLuaLoader.h"
|
2013-02-02 02:05:52 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
#include "platform/ios/CCLuaObjcBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 09:48:24 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
#include "platform/android/CCLuaJavaBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-17 10:15:25 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
#include "Lua_web_socket.h"
|
|
|
|
#endif
|
|
|
|
#include "LuaOpengl.h"
|
|
|
|
#include "LuaScriptHandlerMgr.h"
|
2013-08-09 10:07:10 +08:00
|
|
|
#include "lua_cocos2dx_auto.hpp"
|
|
|
|
#include "lua_cocos2dx_extension_auto.hpp"
|
|
|
|
#include "lua_cocos2dx_manual.hpp"
|
2013-08-20 13:44:37 +08:00
|
|
|
#include "LuaBasicConversions.h"
|
2013-08-22 10:16:57 +08:00
|
|
|
#include "lua_cocos2dx_extension_manual.h"
|
2013-08-23 16:12:59 +08:00
|
|
|
#include "lua_cocos2dx_deprecated.h"
|
2013-10-29 11:40:41 +08:00
|
|
|
#include "lua_xml_http_request.h"
|
2013-11-05 15:09:51 +08:00
|
|
|
#include "lua_cocos2dx_studio_auto.hpp"
|
2013-11-07 23:22:11 +08:00
|
|
|
#include "lua_cocos2dx_coco_studio_manual.hpp"
|
2013-07-17 10:15:25 +08:00
|
|
|
|
2013-04-02 01:12:58 +08:00
|
|
|
namespace {
|
|
|
|
int lua_print(lua_State * luastate)
|
|
|
|
{
|
|
|
|
int nargs = lua_gettop(luastate);
|
|
|
|
|
|
|
|
std::string t;
|
|
|
|
for (int i=1; i <= nargs; i++)
|
|
|
|
{
|
|
|
|
if (lua_istable(luastate, i))
|
|
|
|
t += "table";
|
|
|
|
else if (lua_isnone(luastate, i))
|
|
|
|
t += "none";
|
|
|
|
else if (lua_isnil(luastate, i))
|
|
|
|
t += "nil";
|
|
|
|
else if (lua_isboolean(luastate, i))
|
|
|
|
{
|
|
|
|
if (lua_toboolean(luastate, i) != 0)
|
|
|
|
t += "true";
|
|
|
|
else
|
|
|
|
t += "false";
|
|
|
|
}
|
|
|
|
else if (lua_isfunction(luastate, i))
|
|
|
|
t += "function";
|
|
|
|
else if (lua_islightuserdata(luastate, i))
|
|
|
|
t += "lightuserdata";
|
|
|
|
else if (lua_isthread(luastate, i))
|
|
|
|
t += "thread";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * str = lua_tostring(luastate, i);
|
|
|
|
if (str)
|
|
|
|
t += lua_tostring(luastate, i);
|
|
|
|
else
|
|
|
|
t += lua_typename(luastate, lua_type(luastate, i));
|
|
|
|
}
|
|
|
|
if (i!=nargs)
|
|
|
|
t += "\t";
|
|
|
|
}
|
|
|
|
CCLOG("[LUA-print] %s", t.c_str());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace {
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-15 13:48:34 +08:00
|
|
|
LuaStack::~LuaStack()
|
|
|
|
{
|
|
|
|
if (nullptr != _state)
|
|
|
|
{
|
|
|
|
lua_close(_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaStack *LuaStack::create(void)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaStack *stack = new LuaStack();
|
2013-02-02 02:05:52 +08:00
|
|
|
stack->init();
|
|
|
|
stack->autorelease();
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaStack *LuaStack::attach(lua_State *L)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaStack *stack = new LuaStack();
|
2013-02-02 02:05:52 +08:00
|
|
|
stack->initWithLuaState(L);
|
|
|
|
stack->autorelease();
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaStack::init(void)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = lua_open();
|
|
|
|
luaL_openlibs(_state);
|
|
|
|
toluafix_open(_state);
|
2013-02-02 02:05:52 +08:00
|
|
|
|
2013-04-02 01:12:58 +08:00
|
|
|
// Register our version of the global "print" function
|
|
|
|
const luaL_reg global_functions [] = {
|
|
|
|
{"print", lua_print},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
2013-06-15 14:03:30 +08:00
|
|
|
luaL_register(_state, "_G", global_functions);
|
2013-08-20 13:44:37 +08:00
|
|
|
g_luaType.clear();
|
2013-08-09 10:07:10 +08:00
|
|
|
register_all_cocos2dx(_state);
|
|
|
|
register_all_cocos2dx_extension(_state);
|
2013-08-23 16:12:59 +08:00
|
|
|
register_all_cocos2dx_deprecated(_state);
|
2013-08-22 10:16:57 +08:00
|
|
|
register_cocos2dx_extension_CCBProxy(_state);
|
2013-08-22 15:06:34 +08:00
|
|
|
tolua_opengl_open(_state);
|
2013-11-05 15:09:51 +08:00
|
|
|
register_all_cocos2dx_studio(_state);
|
2013-08-09 10:07:10 +08:00
|
|
|
register_all_cocos2dx_manual(_state);
|
2013-08-22 10:16:57 +08:00
|
|
|
register_all_cocos2dx_extension_manual(_state);
|
2013-08-23 16:12:59 +08:00
|
|
|
register_all_cocos2dx_manual_deprecated(_state);
|
2013-11-07 23:22:11 +08:00
|
|
|
register_all_cocos2dx_coco_studio_manual(_state);
|
2013-02-02 02:05:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaObjcBridge::luaopen_luaoc(_state);
|
2013-02-02 02:05:52 +08:00
|
|
|
#endif
|
2013-09-11 09:48:24 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
LuaJavaBridge::luaopen_luaj(_state);
|
|
|
|
#endif
|
2013-09-02 10:54:39 +08:00
|
|
|
|
2013-07-17 10:15:25 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
tolua_web_socket_open(_state);
|
|
|
|
#endif
|
2013-10-29 11:40:41 +08:00
|
|
|
|
|
|
|
register_xml_http_request(_state);
|
|
|
|
|
2013-07-17 10:15:25 +08:00
|
|
|
tolua_script_handler_mgr_open(_state);
|
2013-04-26 12:12:19 +08:00
|
|
|
|
|
|
|
// add cocos2dx loader
|
|
|
|
addLuaLoader(cocos2dx_lua_loader);
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaStack::initWithLuaState(lua_State *L)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_state = L;
|
2013-02-02 02:05:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::addSearchPath(const char* path)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_getglobal(_state, "package"); /* L: package */
|
|
|
|
lua_getfield(_state, -1, "path"); /* get package.path, L: package path */
|
|
|
|
const char* cur_path = lua_tostring(_state, -1);
|
|
|
|
lua_pushfstring(_state, "%s;%s/?.lua", cur_path, path); /* L: package path newpath */
|
|
|
|
lua_setfield(_state, -3, "path"); /* package.path = newpath, L: package path */
|
|
|
|
lua_pop(_state, 2); /* L: - */
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::addLuaLoader(lua_CFunction func)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
if (!func) return;
|
|
|
|
|
|
|
|
// stack content after the invoking of the function
|
|
|
|
// get loader table
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_getglobal(_state, "package"); /* L: package */
|
|
|
|
lua_getfield(_state, -1, "loaders"); /* L: package, loaders */
|
2013-02-02 02:05:52 +08:00
|
|
|
|
|
|
|
// insert loader into index 2
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushcfunction(_state, func); /* L: package, loaders, func */
|
|
|
|
for (int i = lua_objlen(_state, -2) + 1; i > 2; --i)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_rawgeti(_state, -2, i - 1); /* L: package, loaders, func, function */
|
2013-02-02 02:05:52 +08:00
|
|
|
// we call lua_rawgeti, so the loader table now is at -3
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_rawseti(_state, -3, i); /* L: package, loaders, func */
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_rawseti(_state, -2, 2); /* L: package, loaders */
|
2013-02-02 02:05:52 +08:00
|
|
|
|
|
|
|
// set loaders into package
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_setfield(_state, -2, "loaders"); /* L: package */
|
2013-02-02 02:05:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::removeScriptObjectByObject(Object* pObj)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
toluafix_remove_ccobject_by_refid(_state, pObj->_luaID);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::removeScriptHandler(int nHandler)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
toluafix_remove_function_by_refid(_state, nHandler);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::executeString(const char *codes)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
luaL_loadstring(_state, codes);
|
2013-04-25 21:29:31 +08:00
|
|
|
return executeFunction(0);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::executeScriptFile(const char* filename)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-04-25 21:29:31 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
std::string code("require \"");
|
|
|
|
code.append(filename);
|
|
|
|
code.append("\"");
|
|
|
|
return executeString(code.c_str());
|
|
|
|
#else
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
2013-06-15 14:03:30 +08:00
|
|
|
++_callFromLua;
|
|
|
|
int nRet = luaL_dofile(_state, fullPath.c_str());
|
|
|
|
--_callFromLua;
|
|
|
|
CC_ASSERT(_callFromLua >= 0);
|
|
|
|
// lua_gc(_state, LUA_GCCOLLECT, 0);
|
2013-02-02 02:05:52 +08:00
|
|
|
|
|
|
|
if (nRet != 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(_state, -1));
|
|
|
|
lua_pop(_state, 1);
|
2013-02-02 02:05:52 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-04-25 21:29:31 +08:00
|
|
|
#endif
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::executeGlobalFunction(const char* functionName)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_getglobal(_state, functionName); /* query function by name, stack: function */
|
|
|
|
if (!lua_isfunction(_state, -1))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", functionName);
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1);
|
2013-02-02 02:05:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
return executeFunction(0);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::clean(void)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_settop(_state, 0);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushInt(int intValue)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushinteger(_state, intValue);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushFloat(float floatValue)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushnumber(_state, floatValue);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-18 15:55:58 +08:00
|
|
|
void LuaStack::pushLong(long longValue)
|
|
|
|
{
|
|
|
|
lua_pushnumber(_state, longValue);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushBoolean(bool boolValue)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushboolean(_state, boolValue);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushString(const char* stringValue)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushstring(_state, stringValue);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushString(const char* stringValue, int length)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushlstring(_state, stringValue, length);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushNil(void)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushnil(_state);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushObject(Object* objectValue, const char* typeName)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
toluafix_pushusertype_ccobject(_state, objectValue->_ID, &objectValue->_luaID, objectValue, typeName);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushLuaValue(const LuaValue& value)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
const LuaValueType type = value.getType();
|
|
|
|
if (type == LuaValueTypeInt)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
return pushInt(value.intValue());
|
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeFloat)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
return pushFloat(value.floatValue());
|
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeBoolean)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
return pushBoolean(value.booleanValue());
|
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeString)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
return pushString(value.stringValue().c_str());
|
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeDict)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
pushLuaValueDict(value.dictValue());
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeArray)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
pushLuaValueArray(value.arrayValue());
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-06-20 14:33:59 +08:00
|
|
|
else if (type == LuaValueTypeObject)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
pushObject(value.ccobjectValue(), value.getObjectTypename().c_str());
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushLuaValueDict(const LuaValueDict& dict)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_newtable(_state); /* L: table */
|
2013-06-20 14:33:59 +08:00
|
|
|
for (LuaValueDictIterator it = dict.begin(); it != dict.end(); ++it)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushstring(_state, it->first.c_str()); /* L: table key */
|
2013-06-20 14:33:59 +08:00
|
|
|
pushLuaValue(it->second); /* L: table key value */
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_rawset(_state, -3); /* table.key = value, L: table */
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaStack::pushLuaValueArray(const LuaValueArray& array)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_newtable(_state); /* L: table */
|
2013-02-02 02:05:52 +08:00
|
|
|
int index = 1;
|
2013-06-20 14:33:59 +08:00
|
|
|
for (LuaValueArrayIterator it = array.begin(); it != array.end(); ++it)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
pushLuaValue(*it); /* L: table value */
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_rawseti(_state, -2, index); /* table[index] = value, L: table */
|
2013-02-02 02:05:52 +08:00
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaStack::pushFunctionByHandler(int nHandler)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
toluafix_get_function_by_refid(_state, nHandler); /* L: ... func */
|
|
|
|
if (!lua_isfunction(_state, -1))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", nHandler);
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1);
|
2013-02-02 02:05:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::executeFunction(int numArgs)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
int functionIndex = -(numArgs + 1);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!lua_isfunction(_state, functionIndex))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("value at stack [%d] is not function", functionIndex);
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, numArgs + 1); // remove function and arguments
|
2013-02-02 02:05:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int traceback = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */
|
|
|
|
if (!lua_isfunction(_state, -1))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */
|
2013-02-02 02:05:52 +08:00
|
|
|
traceback = functionIndex - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
++_callFromLua;
|
|
|
|
error = lua_pcall(_state, numArgs, 1, traceback); /* L: ... [G] ret */
|
|
|
|
--_callFromLua;
|
2013-02-02 02:05:52 +08:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (traceback == 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */
|
|
|
|
lua_pop(_state, 1); // remove error message from stack
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
else /* L: ... G error */
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack
|
2013-04-25 21:29:31 +08:00
|
|
|
}
|
2013-02-02 02:05:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value
|
|
|
|
int ret = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (lua_isnumber(_state, -1))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ret = lua_tointeger(_state, -1);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
else if (lua_isboolean(_state, -1))
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ret = lua_toboolean(_state, -1);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
// remove return value from stack
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1); /* L: ... [G] */
|
2013-04-25 21:29:31 +08:00
|
|
|
|
|
|
|
if (traceback)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */
|
2013-04-25 21:29:31 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::executeFunctionByHandler(int nHandler, int numArgs)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
if (pushFunctionByHandler(nHandler)) /* L: ... arg1 arg2 ... func */
|
|
|
|
{
|
|
|
|
if (numArgs > 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
ret = executeFunction(numArgs);
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_settop(_state, 0);
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaStack::handleAssert(const char *msg)
|
2013-02-02 02:05:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_callFromLua == 0) return false;
|
2013-02-02 02:05:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_pushfstring(_state, "ASSERT FAILED ON LUA EXECUTE: %s", msg ? msg : "unknown");
|
|
|
|
lua_error(_state);
|
2013-02-02 02:05:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaStack::reallocateScriptHandler(int nHandler)
|
2013-06-05 15:17:00 +08:00
|
|
|
{
|
|
|
|
LUA_FUNCTION nNewHandle = -1;
|
|
|
|
|
|
|
|
if (pushFunctionByHandler(nHandler))
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
nNewHandle = toluafix_ref_function(_state,lua_gettop(_state),0);
|
2013-06-05 15:17:00 +08:00
|
|
|
}
|
|
|
|
/*
|
2013-06-15 14:03:30 +08:00
|
|
|
toluafix_get_function_by_refid(_state,nNewHandle);
|
|
|
|
if (!lua_isfunction(_state, -1))
|
2013-06-05 15:17:00 +08:00
|
|
|
{
|
|
|
|
CCLOG("Error!");
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
lua_settop(_state, 0);
|
2013-06-05 15:17:00 +08:00
|
|
|
*/
|
|
|
|
return nNewHandle;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-04 14:02:22 +08:00
|
|
|
int LuaStack::executeFunctionReturnArray(int handler,int numArgs,int numResults,Array& resultArray)
|
|
|
|
{
|
|
|
|
if (pushFunctionByHandler(handler)) /* L: ... arg1 arg2 ... func */
|
|
|
|
{
|
|
|
|
if (numArgs > 0)
|
|
|
|
{
|
|
|
|
lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */
|
|
|
|
int functionIndex = -(numArgs + 1);
|
|
|
|
if (!lua_isfunction(_state, functionIndex))
|
|
|
|
{
|
|
|
|
CCLOG("value at stack [%d] is not function", functionIndex);
|
|
|
|
lua_pop(_state, numArgs + 1); // remove function and arguments
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int traceback = 0;
|
|
|
|
lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */
|
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */
|
|
|
|
traceback = functionIndex - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = 0;
|
|
|
|
++_callFromLua;
|
|
|
|
error = lua_pcall(_state, numArgs, numResults, traceback); /* L: ... [G] ret1 ret2 ... retResults*/
|
|
|
|
--_callFromLua;
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (traceback == 0)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */
|
|
|
|
lua_pop(_state, 1); // remove error message from stack
|
|
|
|
}
|
|
|
|
else /* L: ... G error */
|
|
|
|
{
|
|
|
|
lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value,don't pass LUA_MULTRET to numResults,
|
|
|
|
if (numResults <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int i = 0 ; i < numResults; i++)
|
|
|
|
{
|
|
|
|
if (lua_type(_state, -1) == LUA_TBOOLEAN) {
|
|
|
|
|
|
|
|
bool value = lua_toboolean(_state, -1);
|
|
|
|
resultArray.addObject(Bool::create(value)) ;
|
|
|
|
|
|
|
|
}else if (lua_type(_state, -1) == LUA_TNUMBER) {
|
|
|
|
|
|
|
|
double value = lua_tonumber(_state, -1);
|
|
|
|
resultArray.addObject(Double::create(value));
|
|
|
|
|
|
|
|
}else if (lua_type(_state, -1) == LUA_TSTRING) {
|
|
|
|
|
|
|
|
const char* value = lua_tostring(_state, -1);
|
|
|
|
resultArray.addObject(String::create(value));
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
resultArray.addObject(static_cast<Object*>(tolua_tousertype(_state, -1, NULL)));
|
|
|
|
}
|
|
|
|
// remove return value from stack
|
|
|
|
lua_pop(_state, 1); /* L: ... [G] ret1 ret2 ... ret*/
|
|
|
|
}
|
|
|
|
/* L: ... [G]*/
|
|
|
|
|
|
|
|
if (traceback)
|
|
|
|
{
|
|
|
|
lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_settop(_state, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
NS_CC_END
|