lua-users home
lua-l archive

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

On Tue, Jul 3, 2012 at 11:24 PM, Carsten Fuchs <> 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
collection semantics.