[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Feature suggestion: "Up values for userdata"
- From: Mark Hamburg <mhamburg@...>
- Date: Wed, 03 Sep 2003 10:18:04 -0700
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.
The "straightforward" implementation involves making the userdata items
bigger by a pointer.
Alternative implementations:
* Use metatables. I'm doing this now, but it makes doing type-checking
harder since every userdata with associated values has to have a distinct
metatable.
* Use weaktables. This doesn't actually work because of the problems with
the case where a value points back to its own key item.
* Maybe something with function closures, but my current thoughts in that
regard feel pretty convoluted. The idea would basically be to implement
objects with function closures instead of tables (ref. Structure and
Interpretation of Computer Programs), but that's pretty unlike how things
are generally done in Lua.
Mark