Skip to content

Commit

Permalink
chore(obj): simplify obj metatable
Browse files Browse the repository at this point in the history
Signed-off-by: Xu Xingliang <[email protected]>
  • Loading branch information
XuNeo committed Jul 22, 2024
1 parent 921c5d3 commit c2c2640
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,27 +768,16 @@ static const luaL_Reg luavgl_obj_methods[] = {

static void luavgl_obj_init(lua_State *L)
{
luaL_newmetatable(L, "widgets");
luaL_newlib(L, widget_create_methods);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);

/* base lv_obj */
luavgl_obj_newmetatable(L, &lv_obj_class, "lv_obj", luavgl_obj_methods);
lua_pushcfunction(L, luavgl_obj_gc);
lua_setfield(L, -2, "__gc");

/**
* Widget creation functions is a metatable, so we can add extended widget to
* it.
*
* widgets = {}
* lib = {widget_create_methods}
* widgets.__index = lib
* obj.__metatable = widgets
*/
lua_getfield(L, -1, "__index");
luaL_newmetatable(L, "widgets");
luaL_newlib(L, widget_create_methods);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_pop(L, 1); /* remove obj.__index table */

lua_pop(L, 1); /* remove obj metatable */
}

Expand Down
6 changes: 6 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ LUALIB_API int luavgl_obj_createmetatable(lua_State *L,

/* setmetatable(t, b) */
lua_setmetatable(L, -2);
} else {
/* For base obj, add widgets creation method as metatable. So we can use
* obj:Object{} to create widgets.
*/
luaL_getmetatable(L, "widgets");
lua_setmetatable(L, -2);
}

lua_setfield(L, -2, "__index"); /* M.__index = t */
Expand Down

0 comments on commit c2c2640

Please sign in to comment.