Skip to content

Commit

Permalink
Lua 5.2: no more LUA_GLOBALSINDEX
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbartholomew committed May 6, 2012
1 parent 3c36e49 commit db6b3d0
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/LmrModel.cpp
Expand Up @@ -1082,7 +1082,7 @@ LmrModel::LmrModel(const char *model_name)
s_curBuf = m_staticGeometry[i]; s_curBuf = m_staticGeometry[i];
lua_pushcfunction(sLua, pi_lua_panic); lua_pushcfunction(sLua, pi_lua_panic);
// call model static building function // call model static building function
lua_getfield(sLua, LUA_GLOBALSINDEX, (m_name+"_static").c_str()); lua_getglobal(sLua, (m_name+"_static").c_str());
// lod as first argument // lod as first argument
lua_pushnumber(sLua, i+1); lua_pushnumber(sLua, i+1);
lua_pcall(sLua, 1, 0, -3); lua_pcall(sLua, 1, 0, -3);
Expand Down Expand Up @@ -1267,7 +1267,7 @@ void LmrModel::Build(int lod, const LmrObjParams *params)
s_curParams = params; s_curParams = params;
lua_pushcfunction(sLua, pi_lua_panic); lua_pushcfunction(sLua, pi_lua_panic);
// call model dynamic bits // call model dynamic bits
lua_getfield(sLua, LUA_GLOBALSINDEX, (m_name+"_dynamic").c_str()); lua_getglobal(sLua, (m_name+"_dynamic").c_str());
// lod as first argument // lod as first argument
lua_pushnumber(sLua, lod+1); lua_pushnumber(sLua, lod+1);
lua_pcall(sLua, 1, 0, -3); lua_pcall(sLua, 1, 0, -3);
Expand Down
6 changes: 4 additions & 2 deletions src/LuaConstants.cpp
Expand Up @@ -103,14 +103,16 @@ static void _create_constant_table(lua_State *l, const char *ns, const EnumItem
{ {
LUA_DEBUG_START(l); LUA_DEBUG_START(l);


lua_getfield(l, LUA_GLOBALSINDEX, "Constants"); lua_getglobal(l, "Constants");
if (lua_isnil(l, -1)) { if (lua_isnil(l, -1)) {
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l); lua_newtable(l);
pi_lua_table_ro(l); pi_lua_table_ro(l);
lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
lua_pushstring(l, "Constants"); lua_pushstring(l, "Constants");
lua_pushvalue(l, -2); lua_pushvalue(l, -2);
lua_rawset(l, LUA_GLOBALSINDEX); lua_rawset(l, -3);
lua_pop(l, 1);
} }
assert(lua_istable(l, -1)); assert(lua_istable(l, -1));


Expand Down
2 changes: 1 addition & 1 deletion src/LuaEngine.cpp
Expand Up @@ -78,7 +78,7 @@ void LuaEngine::Register()


lua_setmetatable(l, -2); lua_setmetatable(l, -2);


lua_setfield(l, LUA_GLOBALSINDEX, "Engine"); lua_setglobal(l, "Engine");


LUA_DEBUG_END(l, 0); LUA_DEBUG_END(l, 0);
} }
4 changes: 2 additions & 2 deletions src/LuaEventQueue.cpp
Expand Up @@ -14,12 +14,12 @@ void LuaEventQueueBase::RegisterEventQueue()
LUA_DEBUG_START(l); LUA_DEBUG_START(l);


// get the eventqueue table, or create it if it doesn't exist // get the eventqueue table, or create it if it doesn't exist
lua_getfield(l, LUA_GLOBALSINDEX, "EventQueue"); lua_getglobal(l, "EventQueue");
if (lua_isnil(l, -1)) { if (lua_isnil(l, -1)) {
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l); lua_newtable(l);
lua_pushvalue(l, -1); lua_pushvalue(l, -1);
lua_setfield(l, LUA_GLOBALSINDEX, "EventQueue"); lua_setglobal(l, "EventQueue");
} }


lua_pushstring(l, m_name); lua_pushstring(l, m_name);
Expand Down
2 changes: 1 addition & 1 deletion src/LuaGame.cpp
Expand Up @@ -95,7 +95,7 @@ void LuaGame::Register()


lua_setmetatable(l, -2); lua_setmetatable(l, -2);


lua_setfield(l, LUA_GLOBALSINDEX, "Game"); lua_setglobal(l, "Game");


LUA_DEBUG_END(l, 0); LUA_DEBUG_END(l, 0);
} }
2 changes: 1 addition & 1 deletion src/LuaNameGen.cpp
Expand Up @@ -13,7 +13,7 @@ static bool GetNameGenFunc(lua_State *l, const char *func)
{ {
LUA_DEBUG_START(l); LUA_DEBUG_START(l);


lua_getfield(l, LUA_GLOBALSINDEX, "NameGen"); lua_getglobal(l, "NameGen");
if (lua_isnil(l, -1)) { if (lua_isnil(l, -1)) {
lua_pop(l, 1); lua_pop(l, 1);
LUA_DEBUG_END(l, 0); LUA_DEBUG_END(l, 0);
Expand Down
30 changes: 18 additions & 12 deletions src/LuaObject.cpp
Expand Up @@ -187,36 +187,42 @@ static int dispatch_index(lua_State *l)
// sanity check. it should be a userdatum // sanity check. it should be a userdatum
assert(lua_isuserdata(l, 1)); assert(lua_isuserdata(l, 1));


// ensure we have enough stack space
luaL_checkstack(l, 8, 0);

// each type has a global method table, which we need access to
lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);

// everything we need is in the metatable, so lets start with that // everything we need is in the metatable, so lets start with that
lua_getmetatable(l, 1); // object, key, metatable lua_getmetatable(l, 1); // object, key, globals, metatable


// loop until we find what we're looking for or we run out of metatables // loop until we find what we're looking for or we run out of metatables
while (!lua_isnil(l, -1)) { while (!lua_isnil(l, -1)) {


// first is method lookup. we get the object type from the metatable and // first is method lookup. we get the object type from the metatable and
// use it to look up the method table and from there, the method itself // use it to look up the method table and from there, the method itself
lua_pushstring(l, "type"); lua_pushstring(l, "type");
lua_rawget(l, -2); // object, key, metatable, type lua_rawget(l, -2); // object, key, globals, metatable, type


lua_rawget(l, LUA_GLOBALSINDEX); // object, key, metatable, method table lua_rawget(l, -3); // object, key, globals, metatable, method table


lua_pushvalue(l, 2); lua_pushvalue(l, 2);
lua_rawget(l, -2); // object, key, metatable, method table, method lua_rawget(l, -2); // object, key, globals, metatable, method table, method


// found something, return it // found something, return it
if (!lua_isnil(l, -1)) if (!lua_isnil(l, -1))
return 1; return 1;


lua_pop(l, 2); // object, key, metatable lua_pop(l, 2); // object, key, globals, metatable


// didn't find a method, so now we go looking for an attribute handler in // didn't find a method, so now we go looking for an attribute handler in
// the attribute table // the attribute table
lua_pushstring(l, "attrs"); lua_pushstring(l, "attrs");
lua_rawget(l, -2); // object, key, metatable, attr table lua_rawget(l, -2); // object, key, globals, metatable, attr table


if (lua_istable(l, -1)) { if (lua_istable(l, -1)) {
lua_pushvalue(l, 2); lua_pushvalue(l, 2);
lua_rawget(l, -2); // object, key, metatable, attr table, attr handler lua_rawget(l, -2); // object, key, globals, metatable, attr table, attr handler


// found something. since its likely a regular attribute lookup and not a // found something. since its likely a regular attribute lookup and not a
// method call we have to do the call ourselves // method call we have to do the call ourselves
Expand All @@ -226,25 +232,25 @@ static int dispatch_index(lua_State *l)
return 1; return 1;
} }


lua_pop(l, 2); // object, key, metatable lua_pop(l, 2); // object, key, globals, metatable
} }
else else
lua_pop(l, 1); // object, key, metatable lua_pop(l, 1); // object, key, globals, metatable


// didn't find anything. if the object has a parent object then we look // didn't find anything. if the object has a parent object then we look
// there instead // there instead
lua_pushstring(l, "parent"); lua_pushstring(l, "parent");
lua_rawget(l, -2); // object, key, metatable, parent type lua_rawget(l, -2); // object, key, globals, metatable, parent type


// not found means we have no parents and we can't search any further // not found means we have no parents and we can't search any further
if (lua_isnil(l, -1)) if (lua_isnil(l, -1))
break; break;


// clean up the stack // clean up the stack
lua_remove(l, -2); // object, key, parent type lua_remove(l, -2); // object, key, globals, parent type


// get the parent metatable // get the parent metatable
lua_rawget(l, LUA_REGISTRYINDEX); // object, key, parent metatable lua_rawget(l, LUA_REGISTRYINDEX); // object, key, globals, parent metatable
} }


luaL_error(l, "unable to resolve method or attribute '%s'", lua_tostring(l, 2)); luaL_error(l, "unable to resolve method or attribute '%s'", lua_tostring(l, 2));
Expand Down
13 changes: 8 additions & 5 deletions src/LuaSerializer.cpp
Expand Up @@ -81,7 +81,7 @@ void LuaSerializer::pickle(lua_State *l, int idx, std::string &out, const char *
const char *cl = lua_tostring(l, -1); const char *cl = lua_tostring(l, -1);
snprintf(buf, sizeof(buf), "o%s\n", cl); snprintf(buf, sizeof(buf), "o%s\n", cl);


lua_getfield(l, LUA_GLOBALSINDEX, cl); lua_getglobal(l, cl);
if (lua_isnil(l, -1)) if (lua_isnil(l, -1))
luaL_error(l, "No Serialize method found for class '%s'\n", cl); luaL_error(l, "No Serialize method found for class '%s'\n", cl);


Expand Down Expand Up @@ -365,12 +365,15 @@ const char *LuaSerializer::unpickle(lua_State *l, const char *pos)


const char *cl = pos; const char *cl = pos;


lua_pushlstring(l, pos, len); // unpickle the object, and insert it beneath the method table value

pos = unpickle(l, end); pos = unpickle(l, end);
lua_insert(l, -2);


lua_gettable(l, LUA_GLOBALSINDEX); // get _G[typename]
lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
lua_pushlstring(l, cl, len);
lua_gettable(l, -2);
lua_remove(l, -2);

if (lua_isnil(l, -1)) { if (lua_isnil(l, -1)) {
lua_pop(l, 2); lua_pop(l, 2);
break; break;
Expand Down
2 changes: 1 addition & 1 deletion src/galaxy/CustomSystem.cpp
Expand Up @@ -390,7 +390,7 @@ static void register_class(lua_State *L, const char *tname, luaL_Reg *meta)
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");


// publish the metatable // publish the metatable
lua_setfield(L, LUA_GLOBALSINDEX, tname); lua_setglobal(L, tname);


LUA_DEBUG_END(L, 0); LUA_DEBUG_END(L, 0);
} }
Expand Down

0 comments on commit db6b3d0

Please sign in to comment.