lua-users home
lua-l archive

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


Quoth Josh Haberman <jhaberman@gmail.com>, on 2010-07-19 02:37:30 +0000:
> > Interestingly enough, I just ran into the opposite problem.  It appears
> > that my __gc method isn't respected when I do the C equivalent of:
> > 
> > setmetatable({}, {__index = {__gc = foo}})
> > 
> > Lua only seems to find __gc when it is set directly on the metatable.
> > It doesn't find it via __index.
> 
> To clarify, in C it is indeed a userdata I am setting the metatable on,
> not a table as in the faux Lua example above.

That's correct behavior.  Lua does not chain lookups for metamethods;
they are independent of indexing on the target object.  I.e., looking
up __gc on the userdata itself would return foo above, but the cleanup
process does a raw lookup of __gc on the metatable, not a plain lookup
on the userdata.

(This is a reason I prefer not to conflate metatables and method
tables, FWVLIW; people get confused and do stuff like the above.)

> Josh

   ---> Drake Wilson