Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lua 5.2: no more LUA_GLOBALSINDEX

  • Loading branch information...
commit db6b3d05a136cc692fd050cd172b2ea7c28655a5 1 parent 3c36e49
John Bartholomew johnbartholomew authored
4 src/LmrModel.cpp
View
@@ -1082,7 +1082,7 @@ LmrModel::LmrModel(const char *model_name)
s_curBuf = m_staticGeometry[i];
lua_pushcfunction(sLua, pi_lua_panic);
// 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
lua_pushnumber(sLua, i+1);
lua_pcall(sLua, 1, 0, -3);
@@ -1267,7 +1267,7 @@ void LmrModel::Build(int lod, const LmrObjParams *params)
s_curParams = params;
lua_pushcfunction(sLua, pi_lua_panic);
// 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
lua_pushnumber(sLua, lod+1);
lua_pcall(sLua, 1, 0, -3);
6 src/LuaConstants.cpp
View
@@ -103,14 +103,16 @@ static void _create_constant_table(lua_State *l, const char *ns, const EnumItem
{
LUA_DEBUG_START(l);
- lua_getfield(l, LUA_GLOBALSINDEX, "Constants");
+ lua_getglobal(l, "Constants");
if (lua_isnil(l, -1)) {
lua_pop(l, 1);
lua_newtable(l);
pi_lua_table_ro(l);
+ lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
lua_pushstring(l, "Constants");
lua_pushvalue(l, -2);
- lua_rawset(l, LUA_GLOBALSINDEX);
+ lua_rawset(l, -3);
+ lua_pop(l, 1);
}
assert(lua_istable(l, -1));
2  src/LuaEngine.cpp
View
@@ -78,7 +78,7 @@ void LuaEngine::Register()
lua_setmetatable(l, -2);
- lua_setfield(l, LUA_GLOBALSINDEX, "Engine");
+ lua_setglobal(l, "Engine");
LUA_DEBUG_END(l, 0);
}
4 src/LuaEventQueue.cpp
View
@@ -14,12 +14,12 @@ void LuaEventQueueBase::RegisterEventQueue()
LUA_DEBUG_START(l);
// 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)) {
lua_pop(l, 1);
lua_newtable(l);
lua_pushvalue(l, -1);
- lua_setfield(l, LUA_GLOBALSINDEX, "EventQueue");
+ lua_setglobal(l, "EventQueue");
}
lua_pushstring(l, m_name);
2  src/LuaGame.cpp
View
@@ -95,7 +95,7 @@ void LuaGame::Register()
lua_setmetatable(l, -2);
- lua_setfield(l, LUA_GLOBALSINDEX, "Game");
+ lua_setglobal(l, "Game");
LUA_DEBUG_END(l, 0);
}
2  src/LuaNameGen.cpp
View
@@ -13,7 +13,7 @@ static bool GetNameGenFunc(lua_State *l, const char *func)
{
LUA_DEBUG_START(l);
- lua_getfield(l, LUA_GLOBALSINDEX, "NameGen");
+ lua_getglobal(l, "NameGen");
if (lua_isnil(l, -1)) {
lua_pop(l, 1);
LUA_DEBUG_END(l, 0);
30 src/LuaObject.cpp
View
@@ -187,8 +187,14 @@ static int dispatch_index(lua_State *l)
// sanity check. it should be a userdatum
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
- 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
while (!lua_isnil(l, -1)) {
@@ -196,27 +202,27 @@ static int dispatch_index(lua_State *l)
// 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
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_rawget(l, -2); // object, key, metatable, method table, method
+ lua_rawget(l, -2); // object, key, globals, metatable, method table, method
// found something, return it
if (!lua_isnil(l, -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
// the attribute table
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)) {
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
// method call we have to do the call ourselves
@@ -226,25 +232,25 @@ static int dispatch_index(lua_State *l)
return 1;
}
- lua_pop(l, 2); // object, key, metatable
+ lua_pop(l, 2); // object, key, globals, metatable
}
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
// there instead
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
if (lua_isnil(l, -1))
break;
// 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
- 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));
13 src/LuaSerializer.cpp
View
@@ -81,7 +81,7 @@ void LuaSerializer::pickle(lua_State *l, int idx, std::string &out, const char *
const char *cl = lua_tostring(l, -1);
snprintf(buf, sizeof(buf), "o%s\n", cl);
- lua_getfield(l, LUA_GLOBALSINDEX, cl);
+ lua_getglobal(l, cl);
if (lua_isnil(l, -1))
luaL_error(l, "No Serialize method found for class '%s'\n", cl);
@@ -365,12 +365,15 @@ const char *LuaSerializer::unpickle(lua_State *l, const char *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);
- 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)) {
lua_pop(l, 2);
break;
2  src/galaxy/CustomSystem.cpp
View
@@ -390,7 +390,7 @@ static void register_class(lua_State *L, const char *tname, luaL_Reg *meta)
lua_setfield(L, -2, "__index");
// publish the metatable
- lua_setfield(L, LUA_GLOBALSINDEX, tname);
+ lua_setglobal(L, tname);
LUA_DEBUG_END(L, 0);
}
Please sign in to comment.
Something went wrong with that request. Please try again.