fix interface api in lua5.2

This commit is contained in:
jacob1 2017-08-20 18:13:36 -04:00
parent ce58c4aadf
commit 1ceae1ba43
3 changed files with 16 additions and 4 deletions

View File

@ -1,11 +1,19 @@
#include "LuaCompat.h" #include "LuaCompat.h"
#if LUA_VERSION_NUM >= 502 #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) 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)); const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg); 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 #endif

View File

@ -26,9 +26,10 @@ extern "C"
#if LUA_VERSION_NUM >= 502 #if LUA_VERSION_NUM >= 502
#define luaL_getn(L,i) lua_rawlen(L, (i)) #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); LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
#else
LUALIB_API void (lua_pushglobaltable) (lua_State *L);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -15,6 +15,9 @@ public:
lua_newtable(L); lua_newtable(L);
int methods = lua_gettop(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); luaL_newmetatable(L, T::className);
int metatable = lua_gettop(L); int metatable = lua_gettop(L);
@ -22,7 +25,7 @@ public:
// scripts can add functions written in Lua. // scripts can add functions written in Lua.
lua_pushstring(L, T::className); lua_pushstring(L, T::className);
lua_pushvalue(L, methods); lua_pushvalue(L, methods);
lua_settable(L, LUA_GLOBALSINDEX); lua_settable(L, -4);
lua_pushliteral(L, "__metatable"); lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methods); lua_pushvalue(L, methods);
@ -60,7 +63,7 @@ public:
lua_settable(L, methods); 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 // get userdata from Lua stack and return pointer to T object