lua-users home
lua-l archive

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


Mark Hamburg preguntó:

> How complicated would it be to allow userdata to consist of two 
sections?
> The first section would be a series of TObjects of a length generally
> specified in the userdata constructor. The rest of the object would 
consist
> of arbitrary data as it does now. Even better would be a way to specify 
an
> offset and count for the pointers so that one could position them a bit 
more
> freely, but this comes with extra overhead. The garbage collector could 
then
> trace the references from a user data just as it traces references from 
Lua
> tables.

As I understand it, the difficulty here is handling finalizers for 
userdata.
The current finalizer implementation assumes that userdata have no Lua
references, so there cannot be circular references between finalizable
objects.

This is really a problem of semantics: what should a finalizer do when
confronted with a collection of inter-referencing objects? There does
not seem to be any good answer to this question.

Leaving that aside, there is a fairly simple way of letting a userdata
get at a collection of Lua objects: put the Lua objects in the userdata's
metatable. Of course, this means creating a unique metatable for each
userdata, which is a bit of overhead. But if you use integer keys
starting from 0 or 1, it is not too much overhead.

R.