void CScriptSerializableNode::LinkChildren() { lua_State* L = GetScriptState(); lua_getref(L, m_table); int i = 1; for (; i <= (int)m_children.size(); i++) { m_children[i - 1]->LinkChildren(); lua_getref(L, m_children[i - 1]->m_table); lua_rawseti(L, -2, i); } for (;; i++) { lua_rawgeti(L, -1, i); bool isNil = lua_isnil(L, -1); lua_pop(L, 1); if (isNil) { break; } lua_pushnil(L); lua_rawseti(L, -2, i); } lua_pop(L, 1); }
void LuaBase::setTextObjectParams(TextObjectCommon *textObject, lua_Object tableObj) { lua_Object keyObj; lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectX)); keyObj = lua_gettable(); if (keyObj) { if (lua_isnumber(keyObj)) { float num = lua_getnumber(keyObj); if (g_grim->getGameType() == GType_MONKEY4) textObject->setX((int)(num * 320)); else textObject->setX((int)num); } } lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectY)); keyObj = lua_gettable(); if (keyObj) { if (lua_isnumber(keyObj)) { float num = lua_getnumber(keyObj); if (g_grim->getGameType() == GType_MONKEY4) textObject->setY((int)(num * 240)); else textObject->setY((int)num); } } lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectFont)); keyObj = lua_gettable(); if (keyObj) { if (g_grim->getGameType() == GType_MONKEY4 && lua_isstring(keyObj)) { const char *str = lua_getstring(keyObj); Font *font = 0; foreach (Font *f, Font::getPool()) { if (f->getFilename() == str) { font = f; } } if (!font) { font = g_resourceloader->loadFont(str); } textObject->setFont(font); } else if (lua_isuserdata(keyObj) && lua_tag(keyObj) == MKTAG('F','O','N','T')) { textObject->setFont(getfont(keyObj)); } else if (g_grim->getGameType() == GType_MONKEY4 && !textObject->getFont() && g_grim->getGamePlatform() == Common::kPlatformPS2) { // HACK: warning("HACK: No default font set for PS2-version, just picking one for now"); textObject->setFont(*Font::getPool().begin()); } }
void LuaBase::setTextObjectParams(TextObjectCommon *textObject, lua_Object tableObj) { lua_Object keyObj; lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectX)); keyObj = lua_gettable(); if (keyObj) { if (lua_isnumber(keyObj)) { float num = lua_getnumber(keyObj); if (g_grim->getGameType() == GType_MONKEY4) textObject->setX((int)(num * 640)); else textObject->setX((int)num); } } lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectY)); keyObj = lua_gettable(); if (keyObj) { if (lua_isnumber(keyObj)) { float num = lua_getnumber(keyObj); if (g_grim->getGameType() == GType_MONKEY4) textObject->setY((int)(num * 480)); else textObject->setY((int)num); } } lua_pushobject(tableObj); lua_pushobject(lua_getref(refTextObjectFont)); keyObj = lua_gettable(); if (keyObj) { if (g_grim->getGameType() == GType_MONKEY4 && lua_isstring(keyObj)) { const char *str = lua_getstring(keyObj); Font *font = 0; foreach (Font *f, Font::getPool()) { if (f->getFilename() == str) { font = f; } } if (!font) { font = g_resourceloader->loadFont(str); } textObject->setFont(font); } else if (lua_isuserdata(keyObj) && lua_tag(keyObj) == MKTAG('F','O','N','T')) { textObject->setFont(getfont(keyObj)); } }
void LuaEngineMgr::CallFunctionByReference(int ref) { CREATE_L_PTR; lua_getref(L, ref); if(lua_pcall(L, 0, 0, 0)) report(L); }
bool CScriptSystem::ExecuteString(const char* sBuffer, bool useEnv) { SCRIPT_STACK_GUARD(m_pLS); PushErrorHandler(m_pLS); int nRes = luaL_loadstring(m_pLS, sBuffer); if (nRes) { HandleErrorCode(m_pLS, nRes); return false; } if (useEnv) { lua_getref(m_pLS, m_environmentTable); lua_setfenv(m_pLS, -2); } nRes = lua_pcall(m_pLS, 0, 0, -2); if (nRes) { HandleErrorCode(m_pLS, nRes); return false; } return true; }
bool CScriptSystem::ExecuteBuffer(const char* sBuffer, int nSize, const char* name, bool useEnv) { SCRIPT_STACK_GUARD(m_pLS); PushErrorHandler(m_pLS); int nRes = luaL_loadbuffer(m_pLS, sBuffer, nSize, name ? name : "__script_buffer__"); if (nRes) { HandleErrorCode(m_pLS, nRes); return false; } if (useEnv) { lua_getref(m_pLS, m_environmentTable); lua_setfenv(m_pLS, -2); } nRes = lua_pcall(m_pLS, 0, 0, -2); if (nRes) { HandleErrorCode(m_pLS, nRes); return false; } return true; }
int CScriptSystem::GetFunctionParamName(HSCRIPTFUNCTION hFunc, XmlNodeRef& funcNode) { lua_getref(m_pLS, (int)hFunc); StkId func = m_pLS->top - 1; Closure* cl; if (ttype(func) != LUA_TFUNCTION) { lua_pop(m_pLS, 1); return -1; } cl = clvalue(func); int numParams = cl->l.p->numparams; int validParams = numParams; for (int j = 0, i = 0 ; j < numParams; j++) { const char* name = luaF_getlocalname(cl->l.p, j + 1, 0); BEHAVIAC_ASSERT(name); if (!string_icmp(name, "self")) { validParams--; continue; } // There is no value for now, so store the index as the value funcNode->setAttr(name, i); ++i; } lua_pop(m_pLS, 1); // lua_getref pop return validParams; }
void L1_typeOverride() { lua_Object data = lua_getparam(1); if (lua_isuserdata(data)) { switch (lua_tag(data)) { case MKTAG('A','C','T','R'): lua_pushstring("actor"); lua_pushnumber(lua_tag(data)); return; case MKTAG('C','O','S','T'): lua_pushstring("costume"); lua_pushnumber(lua_tag(data)); return; case MKTAG('S','E','T',' '): lua_pushstring("set"); lua_pushnumber(lua_tag(data)); return; case MKTAG('K','E','Y','F'): lua_pushstring("keyframe"); lua_pushnumber(lua_tag(data)); return; default: break; } } lua_pushobject(data); lua_callfunction(lua_getref(refTypeOverride)); lua_Object param1 = lua_getresult(1); lua_Object param2 = lua_getresult(2); lua_pushobject(param1); lua_pushobject(param2); }
static void _get_scores_cb(DArray* data) { assert(live_scores_req); if(data) { GameCenterScore* scores = DARRAY_DATA_PTR(*data, GameCenterScore); lua_createtable(cb_l, data->size, 0); int tbl = lua_gettop(cb_l); for(uint i = 0; i < data->size; ++i) { GameCenterScore* s = &scores[i]; _score_to_tbl(cb_l, s); lua_rawseti(cb_l, tbl, i+1); } } else { // Call with empty table lua_createtable(cb_l, 0, 0); } lua_getref(cb_l, cb_score_ref); assert(lua_isfunction(cb_l, -1)); lua_call(cb_l, 1, 0); lua_unref(cb_l, cb_score_ref); live_scores_req = false; }
static void _get_achievements_cb(DArray* data) { assert(live_achievements_req); lua_getref(cb_l, cb_achievement_ref); assert(lua_isfunction(cb_l, -1)); if(data) { GameCenterAchievement* achievements = DARRAY_DATA_PTR(*data, GameCenterAchievement); lua_createtable(cb_l, data->size, 0); int tbl = lua_gettop(cb_l); for(uint i = 0; i < data->size; ++i) { GameCenterAchievement* a = &achievements[i]; _achievement_to_tbl(cb_l, a); lua_rawseti(cb_l, tbl, i+1); } } else { lua_createtable(cb_l, 0, 0); } lua_call(cb_l, 1, 0); lua_unref(cb_l, cb_achievement_ref); live_achievements_req = false; }
LUALIB_API void tolua_pushuint64(lua_State *L, uint64_t n) { uint64_t* p = (uint64_t*)lua_newuserdata(L, sizeof(uint64_t)); *p = n; lua_getref(L, LUA_RIDX_UINT64); lua_setmetatable(L, -2); }
bool LuaCallBase::valid()const { lua_getref( _L, _ref ); bool ret = (_ref != LUA_REFNIL && lua_type( _L, -1 ) > LUA_TNIL ); lua_pop(_L,1); return ret; }
LuaCallBase::LuaCallBase( const LuaCallBase& rsh ) { _L = rsh._L; lua_getref( rsh._L, rsh._ref ); _ref = luaL_ref( rsh._L, LUA_REGISTRYINDEX ); lua_pop(_L,1); }
void GrimEngine::handleUserPaint() { lua_Object func; lua_beginblock(); lua_pushobject(lua_getref(refSystemTable)); lua_pushstring("userPaintHandler"); lua_Object handler = lua_gettable(); if (lua_istable(handler)) { lua_pushobject(handler); lua_pushstring("userPaintHandler"); func = lua_gettable(); if (!lua_isfunction(func)) error("handleUserPaint: handler not a function"); lua_pushobject(handler); } else if (lua_isfunction(handler)) { func = handler; } else if (!lua_isnil(handler)) { error("handleUserPaint: invalid handler"); return; } else { lua_endblock(); return; } lua_callfunction(func); lua_endblock(); }
void GrimEngine::savegameCallback() { lua_Object funcParam1; lua_beginblock(); lua_pushobject(lua_getref(refSystemTable)); lua_pushstring("saveGameCallback"); lua_Object funcParam2 = lua_gettable(); if (lua_istable(funcParam2)) { lua_pushobject(funcParam2); lua_pushstring("saveGameCallback"); funcParam1 = lua_gettable(); if (lua_isfunction(funcParam1)) { lua_pushobject(funcParam2); lua_callfunction(funcParam1); } else { error("GrimEngine::savegameCallback: handler is not a function"); } } else if (lua_isfunction(funcParam2)) { funcParam1 = funcParam2; lua_callfunction(funcParam1); } else if (!lua_isnil(funcParam2)) { error("GrimEngine::savegameCallback: invalid handler"); } lua_endblock(); }
static void IupSetIdle(void) { lua_Object obj = lua_getparam(1); lua_Object old; if (idle_ref != 0) old = lua_getref(idle_ref); else old = LUA_NOOBJECT; if (lua_isnil(obj)) { lua_unref(idle_ref); idle_ref = 0; IupSetFunction("IDLE_ACTION", (Icallback) NULL); } else { if (!lua_isfunction(obj) && !lua_iscfunction(obj)) lua_error("IupSetIdle: parameter must be a function or a cfunction"); lua_pushobject(obj); idle_ref = lua_ref(1); IupSetFunction("IDLE_ACTION", (Icallback) default_idle); } if (old == LUA_NOOBJECT) lua_pushnil(); else lua_pushobject(old); }
static const char* tbl_getstringi_worker(int arr, int* index) { int i; const char* result = NULL; lua_getref(L, arr); for (i = 1; i <= luaL_getn(L, -1) && *index > 0; ++i) { lua_rawgeti(L, -1, i); if (lua_istable(L, -1)) { int ref = lua_ref(L, -1); result = tbl_getstringi_worker(ref, index); } else if (*index == 1) { result = lua_tostring(L, -1); lua_pop(L, 1); (*index)--; } else { lua_pop(L, 1); (*index)--; } } lua_pop(L, 1); return result; }
Object* TiledMap::addObject(int x, int y, const char* type) { console.log(CON_LOG, CON_VDEBUG, "Adding object of type \"%s\"...", type); lua_getglobal(L, type); if (!lua_istable(L, -1)) { console.log(CON_LOG | CON_CONSOLE, CON_ALWAYS, "Error: object type \"%s\" not defined.", type); } lua_pop(L, 1); lua_newtable(L); Object* newObject = new Object(lua_ref(L, 1), this); newObject->x = x; newObject->y = y; lua_getglobal(L, "inherit"); lua_getref(L, newObject->tableRef); lua_getglobal(L, type); if (lua_istable(L, -1)) { lua_call(L, 2, 0); } objects.push_back(newObject); newObject->initialize(); return newObject; }
/***************************************************************************\ * CGM CD_SCLMDE. * \***************************************************************************/ static int cgm_sclmdecb(cdCanvas *canvas, short scl_mde, short *draw_mode_i, double *factor_f) { lua_Object func, result, draw_mode, factor; int result_i; lua_beginblock(); func = lua_getref(cdluacgmcb[CD_CGMSCLMDECB].lock); cdlua_pushcanvas(canvas); lua_pushnumber( scl_mde); lua_callfunction(func); result = lua_getresult(1); if (!lua_isnumber(result)) lua_error("cdPlay: CD_CGMSCLMDECB: invalid return value!"); result_i = (int) lua_getnumber(result); if (result_i == 1) { lua_endblock(); return 1; } draw_mode = lua_getresult(2); if (!lua_isnumber(draw_mode)) lua_error("cdPlay: CD_CGMSCLMDECB: invalid draw_mode return value!"); *draw_mode_i = (short) lua_getnumber(draw_mode); factor = lua_getresult(3); if (!lua_isnumber(factor)) lua_error("cdPlay: CD_CGMSCLMDECB: invalid factor return value!"); *factor_f = (double) lua_getnumber(factor); lua_endblock(); return result_i; }
static void dohook (int ref) { lua_LHFunction oldlinehook = lua_setlinehook(NULL); lua_CHFunction oldcallhook = lua_setcallhook(NULL); lua_callfunction(lua_getref(ref)); lua_setlinehook(oldlinehook); lua_setcallhook(oldcallhook); }
bool LuaBase::callback(const char *name, const LuaObjects &objects) { lua_beginblock(); lua_pushobject(lua_getref(refSystemTable)); lua_pushstring(name); lua_Object table = lua_gettable(); if (lua_istable(table)) { lua_pushobject(table); lua_pushstring(name); lua_Object func = lua_gettable(); if (lua_isfunction(func)) { lua_pushobject(table); objects.pushObjects(); lua_callfunction(func); } else { lua_endblock(); return false; } } else if (lua_isfunction(table)) { objects.pushObjects(); lua_callfunction(table); } else if (!lua_isnil(table)) { lua_endblock(); return false; } lua_endblock(); return true; }
bool LuaObject::isfunction()const { lua_getref( m_state, m_luaobject ); // t bool ret = lua_isfunction( m_state, -1 ) != 0; lua_pop( m_state, 1 ); return ret; }
static int TreeSetUserId(lua_State *L) { Ihandle *ih = iuplua_checkihandle(L,1); int id = luaL_checkinteger(L,2); int ref = (int)IupTreeGetUserId(ih, id); if (ref != 0) /* userid is not NULL */ { if (ref > 0) ref--; /* only positive references are shifted */ /* release the previous object referenced there */ lua_getref(L, ref); tree_settableref(L, 4, LUA_NOREF); lua_unref(L, ref); lua_pop(L, 1); } if (lua_isnil(L, 3)) IupTreeSetUserId(ih, id, NULL); else { /* add a new reference */ lua_pushvalue(L, 3); ref = lua_ref(L, 1); tree_settableref(L, 3, ref); if (ref >= 0) ref++; /* only positive references are shifted */ IupTreeSetUserId(ih, id, (void*)ref); } return 0; }
void LuaObject::push()const { if( isvalid() ) { lua_getref( m_state, m_luaobject ); } }
void CMapEventManager::GetHandles ( CLuaMain* pLuaMain, const char* szName, lua_State* luaVM ) { unsigned int uiIndex = 0; EventsIterPair itPair = m_EventsMap.equal_range ( szName ); for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter ) { CMapEvent* pMapEvent = iter->second; // Is it not being destroyed? if ( !pMapEvent->IsBeingDestroyed () ) { // Same lua main? if ( pMapEvent->GetVM () == pLuaMain ) { // Same name? dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 ); { lua_pushnumber ( luaVM, ++uiIndex ); lua_getref ( luaVM, pMapEvent->GetLuaFunction ().ToInt() ); lua_settable ( luaVM, -3 ); } } } } }
int Table::size() const { assert( m_ref >= 0 ); RestoreStack rs( m_lua ); lua_getref( m_lua, m_ref ); return lua_getn( m_lua, -1 ); }
void Table::pushMember( int index ) const { assert( m_ref >= 0 ); lua_getref( m_lua, m_ref ); lua_rawgeti( m_lua, -1, index ); lua_remove( m_lua, -2 ); }
bool LuaObject::isboolean()const { lua_getref( m_state, m_luaobject ); // t //const char* tn = lua_typename( m_state, -1 ); bool ret = lua_isboolean( m_state, -1 ) != 0; lua_pop( m_state, 1 ); return ret; }
void Stack::pushFunction(const FunctionRef &value) { const int ref = value.getRef(); if (ref != LUA_REFNIL) { lua_getref(&_luaState, ref); } else { lua_pushnil(&_luaState); } }
template <typename T> void CScriptSerializableNode::setAttrTemplateRef(const CSerializationID& keyID, const T& value) { lua_State* L = GetScriptState(); lua_getref(L, m_table); CScriptMarshal::PushData(L, value); lua_setfield(L, -2, keyID.GetString()); lua_pop(L, 1); }