/************************************************************************* Call operator *************************************************************************/ bool LuaFunctor::operator()(const EventArgs& args) const { // named error handler needs binding? if ((d_errFuncIndex == LUA_NOREF) && !d_errFuncName.empty()) { pushNamedFunction(L, d_errFuncName); d_errFuncIndex = luaL_ref(L, LUA_REGISTRYINDEX); d_ourErrFuncIndex = true; } // is this a late binding? if (needs_lookup) { pushNamedFunction(L, function_name); // reference function index = luaL_ref(L, LUA_REGISTRYINDEX); needs_lookup = false; CEGUI_LOGINSANE("Late binding of callback '"+function_name+"' performed"); function_name.clear(); } // put error handler on stack if we're using such a thing int err_idx = 0; if (d_errFuncIndex != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, d_errFuncIndex); err_idx = lua_gettop(L); } // retrieve function lua_rawgeti(L, LUA_REGISTRYINDEX, index); // possibly self as well? int nargs = 1; if (self != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, self); ++nargs; } // push EventArgs parameter tolua_pushusertype(L, (void*)&args, "const CEGUI::EventArgs"); // call it int error = lua_pcall(L, nargs, 1, err_idx); // handle errors if (error) { String errStr(lua_tostring(L, -1)); lua_pop(L, 1); CEGUI_THROW(ScriptException("Unable to call Lua event handler:\n\n"+errStr+"\n")); } // retrieve result bool ret = lua_isboolean(L, -1) ? lua_toboolean(L, -1 ) : true; lua_pop(L, 1); return ret; }
int ConnectorBase::resolveLuaFunction(lua_State* state) { if (mLuaFunctionIndex == LUA_NOREF || EmberServices::getSingleton().getScriptingService().getAlwaysLookup()) { //If we've already resolved the function we should release the reference before getting a new one. if (mLuaFunctionIndex != LUA_NOREF) { luaL_unref(state, LUA_REGISTRYINDEX, mLuaFunctionIndex); } pushNamedFunction(state); mLuaFunctionIndex = luaL_ref(state, LUA_REGISTRYINDEX); } //get the lua function lua_rawgeti(state, LUA_REGISTRYINDEX, mLuaFunctionIndex); //Check if there's a "self" table specified. If so, prepend that as the first argument and increase the number of arguments counter. if (mLuaSelfIndex != LUA_NOREF) { lua_rawgeti(state, LUA_REGISTRYINDEX, mLuaSelfIndex); return 1; } return 0; }