diff --git a/src/lua/LuaCompat.c b/src/lua/LuaCompat.c index 0ecec2714..7283b64c9 100644 --- a/src/lua/LuaCompat.c +++ b/src/lua/LuaCompat.c @@ -1,11 +1,19 @@ #include "LuaCompat.h" #if LUA_VERSION_NUM >= 502 -//implement missing luaL_typerror function +// Implement missing luaL_typerror function int luaL_typerror (lua_State *L, int narg, const char *tname) { const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg)); return luaL_argerror(L, narg, msg); } +#else + +// Implement function added in lua 5.2 that we now use +void lua_pushglobaltable(lua_State *L) +{ + lua_pushvalue(L, LUA_GLOBALSINDEX); +} + #endif diff --git a/src/lua/LuaCompat.h b/src/lua/LuaCompat.h index ec36f792c..b1db9d730 100644 --- a/src/lua/LuaCompat.h +++ b/src/lua/LuaCompat.h @@ -26,9 +26,10 @@ extern "C" #if LUA_VERSION_NUM >= 502 #define luaL_getn(L,i) lua_rawlen(L, (i)) -#define LUA_GLOBALSINDEX LUA_RIDX_GLOBALS LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +#else +LUALIB_API void (lua_pushglobaltable) (lua_State *L); #endif #ifdef __cplusplus diff --git a/src/lua/LuaLuna.h b/src/lua/LuaLuna.h index d16c1b203..7afcf3a2c 100644 --- a/src/lua/LuaLuna.h +++ b/src/lua/LuaLuna.h @@ -15,6 +15,9 @@ public: lua_newtable(L); int methods = lua_gettop(L); + // push global table to the stack, so we can add the component APIs to it + lua_pushglobaltable(L); + luaL_newmetatable(L, T::className); int metatable = lua_gettop(L); @@ -22,7 +25,7 @@ public: // scripts can add functions written in Lua. lua_pushstring(L, T::className); lua_pushvalue(L, methods); - lua_settable(L, LUA_GLOBALSINDEX); + lua_settable(L, -4); lua_pushliteral(L, "__metatable"); lua_pushvalue(L, methods); @@ -60,7 +63,7 @@ public: lua_settable(L, methods); } - lua_pop(L, 2); // drop metatable and method table + lua_pop(L, 3); // pop global table, metatable, and method table } // get userdata from Lua stack and return pointer to T object