I’m using Lua’s C API to extend Lua. In my module, I want to populate a table using luaL_ref, and remove fields using luaL_unref. I also want to be able to iterate over this table, hopefully using lua_next.
Iterating over the table is a problem because of luaL_unref. In Lua it is common to “delete” table fields by assigning nil (because uninitialized table fields evaluate to nil). The next function is smart enough to skip over nil. I would have expected luaL_unref to assign nil to unreferenced table fields, but it seems to assign an integer. The value of this integer seems to be undocumented.
Consider the following code:
/* tableDump prints a table: */
/* key: value, key: value, ... */
lua_newtable(L);
lua_pushboolean(L, 0);
int ref1 = luaL_ref(L, -2);
lua_pushinteger(L, 7);
int ref2 = luaL_ref(L, -2);
lua_pushstring(L, "test");
int ref3 = luaL_ref(L, -2);
tableDump(L, -1);
luaL_unref(L, -1, ref1);
tableDump(L, -1);
luaL_unref(L, -1, ref3);
tableDump(L, -1);
luaL_unref(L, -1, ref2);
tableDump(L, -1);
printf("done.\n");
Output:
1: false, 2: 7, 3: `test',
3: `test', 2: 7, 0: 1,
3: 1, 2: 7, 0: 3,
3: 1, 2: 3, 0: 2,
done.
What’s going on here? How could I work around this? Is there some trick to iterate over references and ignore the unreferenced? Do I have to stop using luaL_ref and luaL_unref?
Edit
First off, thank you for your responses!
Maybe I’ve asked the wrong question.
Allow me to be a little more specific. I have a client userdata which needs to manage many subscription userdatas. Subscriptions are created by the client’s subscribe method. Subscriptions are removed by the client’s unsubscribe method. The subscription userdatas are basically an implementation detail, so they are not exposed in the client API. Instead the client API uses subscription references, hence the use of luaL_ref to populate a subscription table.
ref = client:sub(channel, func)
cleint:unsub(ref)
Here’s the catch. I would like the client to automatically unsubscribe all remaining subscriptions on __gc (or else the user will get a segfault). So it seems I need to iterate over the subscriptions. Am I really abusing the API here? Is there a better way to do this?
The
luaL_refandluaL_unreffunctions are defined inlauxlib.c. They work by keeping track of a free reference list, which they store in the table they are operating on. These functions are relatively short, so I will include them here.These functions are really clever because they require no additional storage (besides the table they are operating on). However, it is occasionally undesirable to have the free reference list stored in the same table as the referenced values, such as when it is necessary to iterate over referenced values.
I’ve written
luaX_refandluaX_unrefto solve this problem. They work nearly the same asluaL_refandluaL_unref, except they store their free reference list in a separate table,lfor list. (For my project, I’ve put these in a separate source file and have included them as necessary. I do not recommend modifyinglauxlib.c.)Now see the usage:
Output: