From f41590e19c114a93b479460ac99e948a04087f3e Mon Sep 17 00:00:00 2001 From: Arvydas Sidorenko Date: Tue, 12 Jun 2012 13:32:40 +0200 Subject: [PATCH] Wrapped luaL_register Signed-off-by: Arvydas Sidorenko Signed-off-by: Julien Danjou --- common/luaclass.c | 8 ++++---- luaa.c | 16 ++++++++-------- luaa.h | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/common/luaclass.c b/common/luaclass.c index 3efaea65..6d7a006e 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -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); diff --git a/luaa.c b/luaa.c index 42c891e4..ef161bda 100644 --- a/luaa.c +++ b/luaa.c @@ -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); diff --git a/luaa.h b/luaa.h index 4a44ea32..d871964a 100644 --- a/luaa.h +++ b/luaa.h @@ -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) {