Skip to content

Commit dcfd5f6

Browse files
committed
feat: add luaA_newlib
1 parent 39143f0 commit dcfd5f6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

common/luaclass.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,12 @@ luaA_class_gc(lua_State *L)
207207
* this object. This is needed since other __gc methods can still use this.
208208
* We also make sure that `item.valid == false`.
209209
*/
210-
lua_newtable(L);
211-
lua_pushcfunction(L, luaA_class_index_invalid);
212-
lua_setfield(L, -2, "__index");
213-
lua_pushcfunction(L, luaA_class_newindex_invalid);
214-
lua_setfield(L, -2, "__newindex");
210+
const luaL_Reg meta[] = {
211+
{"__index", luaA_class_index_invalid},
212+
{"__newindex", luaA_class_newindex_invalid},
213+
{}
214+
};
215+
luaA_newlib(L, meta);
215216
lua_setmetatable(L, 1);
216217
return 0;
217218
}

luaa.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ luaA_setfuncs(lua_State *L, const luaL_Reg *l)
153153
#endif
154154
}
155155

156+
#if LUA_VERSION_NUM >= 502
157+
#define luaA_newlibtable(L, l) luaL_newlibtable(L, l)
158+
#define luaA_newlib(L, l) luaL_newlib(L, l)
159+
#else
160+
#define luaA_newlibtable(L, l) \
161+
lua_createtable(L, 0, sizeof(l) / sizeof(l[0]) - 1)
162+
#define luaA_newlib(L, l) (luaA_newlibtable(L, l), luaA_setfuncs(L, l))
163+
#endif
164+
156165
static inline bool
157166
luaA_checkboolean(lua_State *L, int n)
158167
{

0 commit comments

Comments
 (0)