Skip to content

Commit 1381e18

Browse files
committed
refactor: optmize luaA_class_setup
1 parent 39143f0 commit 1381e18

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

common/luaclass.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,12 @@ luaA_class_gc(lua_State *L)
229229
* \param newindex_miss_property Function to call when an object of this class
230230
* receive a __newindex request on an unknown property.
231231
* \param methods The methods to set on the class table.
232+
* \param n_methods The number of methods.
232233
* \param meta The meta-methods to set on the class objects.
234+
* \param n_meta The number of meta-methods.
233235
*/
234236
void
235-
luaA_class_setup(lua_State *L, lua_class_t *class,
237+
luaA_class_setupx(lua_State *L, lua_class_t *class,
236238
const char *name,
237239
lua_class_t *parent,
238240
lua_class_allocator_t allocator,
@@ -241,10 +243,12 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
241243
lua_class_propfunc_t index_miss_property,
242244
lua_class_propfunc_t newindex_miss_property,
243245
const struct luaL_Reg methods[],
244-
const struct luaL_Reg meta[])
246+
int n_methods,
247+
const struct luaL_Reg meta[],
248+
int n_meta)
245249
{
246250
/* Create the object metatable */
247-
lua_newtable(L);
251+
lua_createtable(L, 0, n_meta + 2); /* The +2 is for __gc and __index. */
248252
/* Register it with class pointer as key in the registry
249253
* class-pointer -> metatable */
250254
lua_pushlightuserdata(L, class);
@@ -266,7 +270,11 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
266270
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
267271

268272
luaA_setfuncs(L, meta); /* 1 */
269-
luaA_registerlib(L, name, methods); /* 2 */
273+
lua_createtable(L, 0, n_methods); /* 2 */
274+
luaA_setfuncs(L, methods);
275+
lua_pushvalue(L, -1);
276+
lua_setglobal(L, name);
277+
270278
lua_pushvalue(L, -1); /* dup self as metatable 3 */
271279
lua_setmetatable(L, -2); /* set self as metatable 2 */
272280
lua_pop(L, 2);

common/luaclass.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,14 @@ void luaA_class_disconnect_signal_from_stack(lua_State *, lua_class_t *, const c
100100
void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int);
101101

102102
void luaA_openlib(lua_State *, const char *, const struct luaL_Reg[], const struct luaL_Reg[]);
103-
void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_t *,
103+
void luaA_class_setupx(lua_State *, lua_class_t *, const char *, lua_class_t *,
104104
lua_class_allocator_t, lua_class_collector_t,
105105
lua_class_checker_t,
106106
lua_class_propfunc_t, lua_class_propfunc_t,
107-
const struct luaL_Reg[], const struct luaL_Reg[]);
107+
const struct luaL_Reg[], int,
108+
const struct luaL_Reg[], int);
109+
#define luaA_class_setup(L, class, name, parent, allocator, collector, checker, index_miss_property, newindex_miss_property, methods, meta) \
110+
luaA_class_setupx(L, class, name, parent, allocator, collector, checker, index_miss_property, newindex_miss_property, methods, countof(methods) - 1, meta, countof(meta) - 1)
108111

109112
void luaA_class_add_property(lua_class_t *, const char *,
110113
lua_class_propfunc_t, lua_class_propfunc_t, lua_class_propfunc_t);

0 commit comments

Comments
 (0)