From 6cae7494b4c2ba31b836ee2cd4730bde00d6683d Mon Sep 17 00:00:00 2001 From: Neo Xu Date: Sun, 4 Aug 2024 16:47:48 +0800 Subject: [PATCH] feat(obj): add user_data support obj.user_data = {} print(obj.user_data) Signed-off-by: Neo Xu --- src/luavgl.h | 7 ------- src/obj.c | 46 ++++++++++++++++++--------------------------- src/widgets/label.c | 20 -------------------- 3 files changed, 18 insertions(+), 55 deletions(-) diff --git a/src/luavgl.h b/src/luavgl.h index 199272a..a3557f9 100644 --- a/src/luavgl.h +++ b/src/luavgl.h @@ -178,13 +178,6 @@ LUALIB_API int luavgl_obj_createmetatable(lua_State *L, const char *name, const rotable_Reg *l, int n); -/** - * @brief Get user value of userdata of lua lvgl object, create if not exists - * - * @return type of the uservalue, LUA_TTABLE - */ -LUALIB_API int luavgl_obj_getuserdatauv(lua_State *L, int idx); - /* helper to get value from stack */ /** diff --git a/src/obj.c b/src/obj.c index ec2a29e..3e9317c 100644 --- a/src/obj.c +++ b/src/obj.c @@ -43,6 +43,9 @@ static void obj_delete_cb(lv_event_t *e) goto pop_exit; } + lua_pushnil(L); + lua_setuservalue(L, -2); + luavgl_obj_t *lobj = luavgl_to_lobj(L, -1); if (lobj->lua_created) goto pop_exit; @@ -81,31 +84,6 @@ static void obj_delete_cb(lv_event_t *e) return; } -/** - * get the obj userdata and uservalue, if uservalue is not a table, then add - * one. result stack: table(from uservalue) - * return uservalue type: LUA_TTABLE - */ -LUALIB_API int luavgl_obj_getuserdatauv(lua_State *L, int idx) -{ - int type = lua_getuservalue(L, idx); - if (type == LUA_TTABLE) - return type; - - lua_pop(L, 1); - /* initial element: 1 */ - lua_createtable(L, 0, 1); - -#if 0 /* reserved slot, not used for now */ - lua_pushinteger(L, 1); - lua_pushnil(L); - lua_rawset(L, -3); -#endif - lua_pushvalue(L, -1); /* leave one on stack */ - lua_setuservalue(L, idx > 0 ? idx : idx - 3); - return LUA_TTABLE; -} - static int luavgl_obj_create(lua_State *L) { return luavgl_obj_create_helper(L, lv_obj_create); @@ -831,10 +809,22 @@ static int obj_property_h(lua_State *L, lv_obj_t *obj, bool set) return 1; } +static int obj_property_user_data(lua_State *L, lv_obj_t *obj, bool set) +{ + if (set) { + lua_pushvalue(L, -1); + lua_setuservalue(L, 1); + } else { + lua_getuservalue(L, 1); + } + return 1; +} + static const luavgl_property_ops_t obj_property_ops[] = { - {.name = "align", .ops = obj_property_align}, - {.name = "h", .ops = obj_property_h }, - {.name = "w", .ops = obj_property_w }, + {.name = "align", .ops = obj_property_align }, + {.name = "h", .ops = obj_property_h }, + {.name = "w", .ops = obj_property_w }, + {.name = "user_data", .ops = obj_property_user_data}, }; static const luavgl_table_t luavgl_obj_property_table = { diff --git a/src/widgets/label.c b/src/widgets/label.c index d3e7616..230591e 100644 --- a/src/widgets/label.c +++ b/src/widgets/label.c @@ -27,25 +27,6 @@ static int luavgl_label_cut_text(lua_State *L) return 0; } -/* demo purpose, there is no need to use set_text_static */ -static int luavgl_label_set_text_static(lua_State *L) -{ - const char *str = lua_tostring(L, 2); - luavgl_obj_t *lobj = luavgl_to_lobj(L, 1); - if (lobj->obj == NULL) { - return luaL_error(L, "obj null."); - } - - luavgl_obj_getuserdatauv(L, 1); - - /* uservalue is on top */ - lua_pushvalue(L, 2); - lua_setfield(L, -2, "text_static"); - - lv_label_set_text_static(lobj->obj, str); - return 0; -} - static int luavgl_label_tostring(lua_State *L) { lv_obj_t *obj = luavgl_to_obj(L, 1); @@ -54,7 +35,6 @@ static int luavgl_label_tostring(lua_State *L) } static const rotable_Reg luavgl_label_methods[] = { - {"set_text_static", LUA_TFUNCTION, {luavgl_label_set_text_static}}, {"ins_text", LUA_TFUNCTION, {luavgl_label_ins_text} }, {"cut_text", LUA_TFUNCTION, {luavgl_label_cut_text} },