static void pushCCObject(CCObject *v, const char *t){ #if COCOS2D_VERSION >= 0x00030000 luaStack()->pushObject(v, t); #else luaStack()->pushCCObject(v, t); #endif }
static bool pushLuaFunction(int nHandler){ lua_State *l = luaStack()->getLuaState(); toluafix_get_function_by_refid(l, nHandler); /* stack: ... func */ if(!lua_isfunction(l, -1)){ CCLog("[LUA ERROR] function refid '%d' does not reference a Lua function", nHandler); lua_pop(l, 1); return false; } return true; }
CLuaScript::~CLuaScript(void) { CLuaStack luaStack(m_luaBridge); LUA_CODE_CHUNK_ENTER(m_luaBridge) lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef); lua_pushnil(state); lua_rawseti(state, -2, 0); LUA_CODE_CHUNK_LEAVE }
CLuaScript::CLuaScript(CLuaBridge & luaBridge) : m_luaBridge(luaBridge), m_nMethods(DEFAULT_INDEX_INITIALIZER), m_nThisRef(DEFAULT_INDEX_INITIALIZER), m_nArgs(DEFAULT_INDEX_INITIALIZER) { LUA_CODE_CHUNK_ENTER(luaBridge) lua_newtable(state); m_nThisRef = luaL_ref(state, LUA_REGISTRYINDEX); CLuaStack luaStack(luaBridge); lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef); lua_pushlightuserdata(state, reinterpret_cast<void*>(this)); lua_rawseti(state, -2, 0); LUA_CODE_CHUNK_LEAVE }
bool CLuaScript::ScriptHasFunction(const char * szFunction) { CLuaStack luaStack(m_luaBridge); bool bRetVal = false; LUA_CODE_CHUNK_ENTER(m_luaBridge) lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef); lua_pushstring(state, szFunction); lua_rawget(state, -2); lua_remove(state, -2); bRetVal = (lua_isfunction(state, -1) != FALSE); LUA_CODE_CHUNK_LEAVE return bRetVal; }
int CLuaScript::RegisterFunction(const char *szFuncName) { int nMethod = -1; CLuaStack luaStack(m_luaBridge); LUA_CODE_CHUNK_ENTER(m_luaBridge) nMethod = ++m_nMethods; lua_rawgeti(state, LUA_REGISTRYINDEX, m_nThisRef); lua_pushstring(state, szFuncName); lua_pushnumber(state, (lua_Number)nMethod); lua_pushcclosure(state, CLuaScript::LuaCallback, 1); lua_settable(state, -3); LUA_CODE_CHUNK_LEAVE return nMethod; }
// Run lua function but do not get return value. // If (retInt) is true, try to return int and bool value, and auto finish function call. // Else, should manual call finishRunLuaFunction() static int runLuaFunction(int h, int numArgs, bool retInt = false){ lua_State *l = luaStack()->getLuaState(); if(pushLuaFunction(h)){ /* stack: ... arg1 arg2 ... func */ if(numArgs > 0){ lua_insert(l, -(numArgs + 1)); /* stack: ... func arg1 arg2 ... */ } int traceback = 0; lua_getglobal(l, "__G__TRACKBACK__"); /* stack: ... func arg1 arg2 ... G */ if (!lua_isfunction(l, -1)){ lua_pop(l, 1); /* stack: ... func arg1 arg2 ... */ }else{ traceback = -(numArgs + 2); lua_insert(l, traceback); /* stack: ... G func arg1 arg2 ... */ } int error = 0; error = lua_pcall(l, numArgs, 1, traceback); /* stack: ... ret */ if (error){ if (traceback == 0){ CCLog("[LUA ERROR] %s", lua_tostring(l, - 1)); /* stack: ... error */ lua_pop(l, 1); // remove error message from stack } return 0; } // get return value int ret = 1; if(retInt){ if (lua_isnumber(l, -1)){ ret = lua_tointeger(l, -1); }else if (lua_isboolean(l, -1)){ ret = lua_toboolean(l, -1); } lua_pop(l, 1); // remove return value from stack } return ret; }else{ lua_pop(l, numArgs); // remove args from stack return 0; } }
static void pushInt(int v){ luaStack()->pushInt(v);}
static void runString(const char *s){ luaL_dostring(luaStack()->getLuaState(), s); }
static int runFunctionHandler(int hnd, int argNum, bool retInt = false){ int r = 0; if(retInt){ r = runLuaFunction(hnd, argNum, true);} else{ luaStack()->executeFunctionByHandler(hnd, argNum);} return r; }
static void pushNil(){ luaStack()->pushNil();}
static void pushUsertype(void *v, const char *t){ tolua_pushusertype(luaStack()->getLuaState(), v, t); }
static void pushString(const char *v){ luaStack()->pushString(v);}
static void pushBoolean(bool v){ luaStack()->pushBoolean(v);}
static void pushFloat(float v){ luaStack()->pushFloat(v);}