[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: GC method for objects
- From: "Adam D. Moss" <adam@...>
- Date: Tue, 19 Apr 2005 14:54:48 +0100
Roberto Ierusalimschy wrote:
In short, what you do is create a weak-valued table which maps
from dummy created-on-demant heavyuserdata to your {}s - the __gc
sits on the userdata and thus indirectly gets called when the {}
becomes collectable.
A simpler way is to store the dummy created-on-demant heavyuserdata
directly in your {}, using a reserved key (e.g., __userdata).
That's possible but pollutes the original table (but that
might not be a problem for objects, where you wouldn't
usually for example want to do a pairs() over the table).
You then need a way to map back from the userdata to the table
Since this userdata is dummy anyway, you can store the table as the
userdata's metatable.
That's a very nice idea (though then, surely, the __gc field has
to go into the table itself and the same pollution occurs as above).
> Lua 5.1 offers a more proper way to do this mapping.
Neat. Any examples?
--Adam
--
Adam D. Moss - adam@gimp.org