Skip to content

Commit

Permalink
fix interface api in lua5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Aug 20, 2017
1 parent ce58c4a commit 1ceae1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 9 additions & 1 deletion 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
3 changes: 2 additions & 1 deletion src/lua/LuaCompat.h
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/lua/LuaLuna.h
Expand Up @@ -15,14 +15,17 @@ template <typename T> class Luna
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);

// store method table in globals so that
// 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);
Expand Down Expand Up @@ -60,7 +63,7 @@ template <typename T> class Luna
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
Expand Down

0 comments on commit 1ceae1b

Please sign in to comment.