lua-users home
lua-l archive

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


> I was just wondering: is there any special reason why
> lua_setuservalue() will only accept either a table or nil (according
> to the manual)?

Yes. First, it saves memory, as the userdata only needs to store a
pointer, instead of a full Lua value (a double plus a tag).

Second, and more importantly, it simplifies the garbage collector.
Currently, userdata are never grey; when it is visited, its uservalue is
visited too. Because the uservalue is a table, this visit is fast and
simple (the table is marked grey for future traversal). If the uservalue
could be another userdata, then its visit could trigger another visit,
which potentially could overflow the C stack. The option would be to
allow userdata to be grey, which would need yet more space for each
userdata (a pointer 'gclist' for the grey list).

-- Roberto