LuaTable LuaTable::SubTable(const string& mixedKey) const { const string key = !(parser ? parser->lowerCppKeys : true) ? mixedKey : StringToLower(mixedKey); LuaTable subTable; subTable.path = path + "." + key; if (!PushTable()) { return subTable; } lua_pushstring(L, key.c_str()); lua_gettable(L, -2); if (!lua_istable(L, -1)) { lua_pop(L, 1); return subTable; } subTable.parser = parser; subTable.L = L; subTable.refnum = luaL_ref(L, LUA_REGISTRYINDEX); subTable.isValid = (subTable.refnum != LUA_NOREF); parser->AddTable(&subTable); return subTable; }
int LuaTable::GetLength() const { if (!PushTable()) { return 0; } return lua_objlen(L, -1); }
LuaTable LuaTable::SubTable(int key) const { LuaTable subTable; char buf[32]; SNPRINTF(buf, 32, "[%i]", key); subTable.path = path + buf; if (!PushTable()) { return subTable; } lua_pushnumber(L, key); lua_gettable(L, -2); if (!lua_istable(L, -1)) { lua_pop(L, 1); return subTable; } subTable.parser = parser; subTable.L = L; subTable.refnum = luaL_ref(L, LUA_REGISTRYINDEX); subTable.isValid = (subTable.refnum != LUA_NOREF); parser->AddTable(&subTable); return subTable; }
void OnTimer() override { if (PushTable()) { lua_getfield(L, -1, "callback"); lua_getfield(L, -2, "timer"); if (lua_pcall(L, 1, 0, 0)) Lua::ThrowError(L, Lua::PopError(L)); Lua::CheckPersistent(L); } }
bool LuaTable::PushValue(int key) const { if (!PushTable()) { return false; } lua_pushnumber(L, key); lua_gettable(L, -2); if (lua_isnoneornil(L, -1)) { lua_pop(L, 1); return false; } return true; }
bool LuaTable::PushValue(const string& mixedKey) const { const string key = !(parser ? parser->lowerCppKeys : true) ? mixedKey : StringToLower(mixedKey); if (!PushTable()) { return false; } lua_pushstring(L, key.c_str()); lua_gettable(L, -2); if (lua_isnoneornil(L, -1)) { lua_pop(L, 1); return false; } return true; }
bool LuaTable::GetKeys(vector<int>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2)) { const int value = lua_toint(L, -2); data.push_back(value); } } std::sort(data.begin(), data.end()); return true; }
bool LuaTable::GetMap(map<string, string>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwstring(L, -2) && lua_isstring(L, -1)) { const string key = lua_tostring(L, -2); const string value = lua_tostring(L, -1); data[key] = value; } } return true; }
bool LuaTable::GetMap(map<int, float>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2) && lua_isnumber(L, -1)) { const int key = lua_toint(L, -2); const float value = lua_tofloat(L, -1); data[key] = value; } } return true; }
int lua_getmetatable(lua_State* L, int index) { const Value* object = GetValueForIndex(L, index); Table* metatable = Value_GetMetatable(L, object); if (metatable == NULL) { return 0; } PushTable(L, metatable); return 1; }
LuaTable::LuaTable(LuaParser* _parser) { assert(_parser != NULL); isValid = _parser->IsValid(); path = "ROOT"; parser = _parser; L = parser->L; refnum = parser->rootRef; if (PushTable()) { lua_pushvalue(L, -1); // copy refnum = luaL_ref(L, LUA_REGISTRYINDEX); } else { refnum = LUA_NOREF; } isValid = (refnum != LUA_NOREF); parser->AddTable(this); }
bool LuaTable::GetMap(map<int, string>& data) const { if (!PushTable()) { return false; } const int table = lua_gettop(L); for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2) && lua_isstring(L, -1)) { if (lua_isstring(L, -1)) { const int key = lua_toint(L, -2); const string value = lua_tostring(L, -1); data[key] = value; } else if (lua_isboolean(L, -1)) { const int key = lua_toint(L, -2); const string value = lua_toboolean(L, -1) ? "1" : "0"; data[key] = value; } } } return true; }