[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Prevent userdata from being marked for garbage collection?
- From: Peter Cawley <lua@...>
- Date: Tue, 3 Jul 2012 23:37:16 +0100
On Tue, Jul 3, 2012 at 11:24 PM, Carsten Fuchs <email@example.com> wrote:
> Is it possible to have a callback like __mark somehow?
> (Or anything else that achieves a similar result?)
If you're willing to modify the Lua VM source code and tie yourself to
that modified version, then yes. My high-level summary of how to do
this would be to look at the sweeplist function in lgc.c, and have it
resurrect userdata which are otherwise dead.
Alternatively, if you're willing to take advantage of some
implementation warts, you can resurrect objects from their __gc
handler. I've never had a need to do this, but I'd guess that if from
within a __gc handler you created a new table and assigned the
userdata into it, that would keep the userdata alive for another
collection cycle. This is considered a hack though, and still might
not be compatible with LuaJIT.
To round off, it probably doesn't need saying, but the cleanest
approach would be to work within the existing metamethods and garbage