Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(obj): simplify obj metatable #55

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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