Wrapped luaL_register

Signed-off-by: Arvydas Sidorenko <asido4@gmail.com>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Arvydas Sidorenko 2012-06-12 13:32:40 +02:00 committed by Julien Danjou
parent 508dce9c14
commit f41590e19c
3 changed files with 30 additions and 12 deletions

View file

@ -132,8 +132,8 @@ luaA_openlib(lua_State *L, const char *name,
lua_pushvalue(L, -1); /* dup metatable 2 */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaL_register(L, NULL, meta); /* 1 */
luaL_register(L, name, methods); /* 2 */
luaA_registerlib(L, NULL, 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 */
lua_pop(L, 2);
@ -232,8 +232,8 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaL_register(L, NULL, meta); /* 1 */
luaL_register(L, name, methods); /* 2 */
luaA_registerlib(L, NULL, 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 */
lua_pop(L, 2);

16
luaa.c
View file

@ -549,22 +549,22 @@ luaA_init(xdgHandle* xdg)
luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
/* Export root lib */
luaL_register(L, "root", awesome_root_lib);
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
luaA_registerlib(L, "root", awesome_root_lib);
lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
#ifdef WITH_DBUS
/* Export D-Bus lib */
luaL_register(L, "dbus", awesome_dbus_lib);
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
luaA_registerlib(L, "dbus", awesome_dbus_lib);
lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
#endif
/* Export keygrabber lib */
luaL_register(L, "keygrabber", awesome_keygrabber_lib);
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
luaA_registerlib(L, "keygrabber", awesome_keygrabber_lib);
lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
/* Export mousegrabber lib */
luaL_register(L, "mousegrabber", awesome_mousegrabber_lib);
lua_pop(L, 1); /* luaL_register() leaves the table on stack */
luaA_registerlib(L, "mousegrabber", awesome_mousegrabber_lib);
lua_pop(L, 1); /* luaA_registerlib() leaves the table on stack */
/* Export screen */
luaA_openlib(L, "screen", awesome_screen_methods, awesome_screen_meta);

18
luaa.h
View file

@ -104,6 +104,24 @@ luaA_rawlen(lua_State *L, int idx)
#endif
}
static inline void
luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
{
#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);
#else
luaL_register(L, libname, l);
#endif
}
static inline bool
luaA_checkboolean(lua_State *L, int n)
{