lua-users home
lua-l archive

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


Sounds like a good use for references.
Just put the "attached" values in a table in the registry and keep a
reference in the userdata.
In the userdata's GC method, unlock the reference.

The "tricky" bit is to make GC work correctly.  As a "poor" solution you
could write a "lua_freeuserdatavalues(L, index_of_userdata)" and require
that userdata GC methods call this function.  As a "good" solution you could
write a new version of setmetatable that checked the GC method and if it was
not already "fixed" then "fix" it to reference a function that retrieves the
original GC function from somewhere (probably from the metatable or the
"associated values" table), calls that function, and then "frees" the
associated data values.

<Definitions of "poor" and "good" are purely my opinion, you have to decide
if "mucking" with the metatables is better than relying on the userdata GC
methods manually calling something.>

-----Original Message-----
Up values are the wrong term, but it would be nice if there were a simple
method to attach Lua values to a userdata item. I'm thinking of an API like
the following:

    lua_newuserdata( L, size, number_of_associated_values )

    lua_getuserdatavalue( L, index_of_userdata, index_of_value )

    lua_setuserdatavalue( L, index_of_userdata, index_of_value )

This would make it easy to have heavy userdata that referenced Lua values
properly for garbage collection. When the GC method runs for a userdata
object, the associated values would all be cleared first to avoid the usual
problems with finalization.