lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Consider storing your list items in a Lua table.
Associate that table with your list userdata by using lua_newuserdatauv:

struct mylist *p = lua_newuserdatauv(L, sizeof(mylist), 1); /* 1 is how many user values you'll bind to your ud */
lua_newtable(L); /* new empty table to store your list items */
lua_setiuservalue(L, index_of_your_list, 1); /* 1 is uservalue index in your userdata */

then when you want to add a list item:

lua_getiuservalue(L, index_of_your_list, 1);
lua_pushvalue(L, index_of_your_list_item);
lua_rawseti(L, index_of_your_list, luaL_len(L, index_of_your_list) + 1);

so this way all your list items would not be garbage collected because their refs would exist in a uservalue of your list userdata.
and when your list would be garbage collected, your list items would be also (unless you don't have more refs to them).