Browse Source

Migrate a bit away from luaL_register().

luaL_register() only exists in Lua 5.1 and was removed in never
versions. It has two completely different behaviours depending on
whether its second argument is NULL or not.

This commit splits that up by adding a wrapper for luaL_setfuncs()
(which does not exist in Lua 5.1) and using that everywhere instead of
our wrapper for luaL_register().

No behavioural changes are intended. This is simply meant as a cleanup.

Signed-off-by: Uli Schlachter <psychon@znc.in>
Uli Schlachter 8 months ago
parent
commit
4cd05133f7
2 changed files with 17 additions and 11 deletions
  1. 2
    2
      common/luaclass.c
  2. 15
    9
      luaa.h

+ 2
- 2
common/luaclass.c View File

@@ -131,7 +131,7 @@ luaA_openlib(lua_State *L, const char *name,
131 131
     lua_pushvalue(L, -1);           /* dup metatable                      2 */
132 132
     lua_setfield(L, -2, "__index"); /* metatable.__index = metatable      1 */
133 133
 
134
-    luaA_registerlib(L, NULL, meta);                                   /* 1 */
134
+    luaA_setfuncs(L, meta);                                            /* 1 */
135 135
     luaA_registerlib(L, name, methods);                                /* 2 */
136 136
     lua_pushvalue(L, -1);           /* dup self as metatable              3 */
137 137
     lua_setmetatable(L, -2);        /* set self as metatable              2 */
@@ -267,7 +267,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
267 267
 
268 268
     lua_setfield(L, -2, "__index"); /* metatable.__index = metatable      1 */
269 269
 
270
-    luaA_registerlib(L, NULL, meta);                                   /* 1 */
270
+    luaA_setfuncs(L, meta);                                            /* 1 */
271 271
     luaA_registerlib(L, name, methods);                                /* 2 */
272 272
     lua_pushvalue(L, -1);           /* dup self as metatable              3 */
273 273
     lua_setmetatable(L, -2);        /* set self as metatable              2 */

+ 15
- 9
luaa.h View File

@@ -128,21 +128,27 @@ luaA_rawlen(lua_State *L, int idx)
128 128
 static inline void
129 129
 luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
130 130
 {
131
+    assert(libname);
131 132
 #if LUA_VERSION_NUM >= 502
132
-    if (libname)
133
-    {
134
-        lua_newtable(L);
135
-        luaL_setfuncs(L, l, 0);
136
-        lua_pushvalue(L, -1);
137
-        lua_setglobal(L, libname);
138
-    }
139
-    else
140
-        luaL_setfuncs(L, l, 0);
133
+    lua_newtable(L);
134
+    luaL_setfuncs(L, l, 0);
135
+    lua_pushvalue(L, -1);
136
+    lua_setglobal(L, libname);
141 137
 #else
142 138
     luaL_register(L, libname, l);
143 139
 #endif
144 140
 }
145 141
 
142
+static inline void
143
+luaA_setfuncs(lua_State *L, const luaL_Reg *l)
144
+{
145
+#if LUA_VERSION_NUM >= 502
146
+    luaL_setfuncs(L, l, 0);
147
+#else
148
+    luaL_register(L, NULL, l);
149
+#endif
150
+}
151
+
146 152
 static inline bool
147 153
 luaA_checkboolean(lua_State *L, int n)
148 154
 {

Loading…
Cancel
Save