lua-users home
lua-l archive

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


Lua's __gc metamethod is actually based on the notion that the universe can
be divided into reachable objects and unreachable objects and all that the
__gc metamethod tells one is that the object made it's first and possibly
only transition from reachable to unreachable. If you don't resurrect the
object, this is for all intents and purposes a finalization notice.

Viewed in this light, it makes reasonable sense for tables as well as
userdata.

On the other hand, there is an expense to supporting it -- one has to check,
for example, whether or not an object has a __gc metamethod.

The implementation also seems to assume that relatively little work will
happen during a __gc metamethod. The Lua 5.1 garbage collector, for example,
can get into a very deep recursion if the work done by a __gc metamethod
drives the collector forward. That can be fixed by not driving the collector
forward while running __gc metamethods, but that probably adds to the
pressure to do relatively little during collection. Not supporting such
methods too widely probably helps discourage work being done in them.

Finally, one thing I have found myself wanting on occasion is a way to
report resurrection in a __gc metamethod so that I would get another call if
the object became unreachable again. This should just be the matter of
setting a flag back. Unfortunately, I can't think right now of why I thought
this might be useful.

Mark