Skip to content

Commit

Permalink
feat(obj): add user_data support
Browse files Browse the repository at this point in the history
obj.user_data = {}
print(obj.user_data)

Signed-off-by: Neo Xu <[email protected]>
  • Loading branch information
XuNeo committed Aug 4, 2024
1 parent db49c1f commit 6cae749
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 55 deletions.
7 changes: 0 additions & 7 deletions src/luavgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand Down
46 changes: 18 additions & 28 deletions src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = {
Expand Down
20 changes: 0 additions & 20 deletions src/widgets/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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} },

Expand Down

0 comments on commit 6cae749

Please sign in to comment.