Merge pull request #2659 from psychon/luaA_setfuncs

Migrate a bit away from luaL_register().
This commit is contained in:
mergify[bot] 2019-02-18 01:43:31 +00:00 committed by GitHub
commit 052f6fb89d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -131,7 +131,7 @@ luaA_openlib(lua_State *L, const char *name,
lua_pushvalue(L, -1); /* dup metatable 2 */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaA_registerlib(L, NULL, meta); /* 1 */
luaA_setfuncs(L, meta); /* 1 */
luaA_registerlib(L, name, methods); /* 2 */
lua_pushvalue(L, -1); /* dup self as metatable 3 */
lua_setmetatable(L, -2); /* set self as metatable 2 */
@ -267,7 +267,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaA_registerlib(L, NULL, meta); /* 1 */
luaA_setfuncs(L, meta); /* 1 */
luaA_registerlib(L, name, methods); /* 2 */
lua_pushvalue(L, -1); /* dup self as metatable 3 */
lua_setmetatable(L, -2); /* set self as metatable 2 */

24
luaa.h
View File

@ -128,21 +128,27 @@ luaA_rawlen(lua_State *L, int idx)
static inline void
luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
{
assert(libname);
#if LUA_VERSION_NUM >= 502
if (libname)
{
lua_newtable(L);
luaL_setfuncs(L, l, 0);
lua_pushvalue(L, -1);
lua_setglobal(L, libname);
}
else
luaL_setfuncs(L, l, 0);
lua_newtable(L);
luaL_setfuncs(L, l, 0);
lua_pushvalue(L, -1);
lua_setglobal(L, libname);
#else
luaL_register(L, libname, l);
#endif
}
static inline void
luaA_setfuncs(lua_State *L, const luaL_Reg *l)
{
#if LUA_VERSION_NUM >= 502
luaL_setfuncs(L, l, 0);
#else
luaL_register(L, NULL, l);
#endif
}
static inline bool
luaA_checkboolean(lua_State *L, int n)
{