lua-users home
lua-l archive

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


On Fri, Aug 21, 2015 at 04:39:31AM +0200, Philipp Janda wrote:
> Am 21.08.2015 um 04:18 schröbte Sean Conner:
> 
> >
> >1) Allow unique metatables for lightuserdata, that also respect __gc.
> 
> lightuserdata is a value type. `__gc` doesn't work well with value types ...
> 
>     lua_pushlightuserdata( L, 0x1234 );
>     luaL_getmetatable( L, "metatablewithgc" );
>     lua_setmetatable( L, -2 );
>     lua_pushvalue( L, -1 ); /* duplicate lightuserdata */
>     lua_pop( L, 2 );
>     /* should the __gc method really run twice now? */
> 

Yeah. And regardless of should, it _won't_. To be garbage collectable a
value needs many more members for the GC algorithm, and it has to be
allocated separately, with reference semantics rather than value semantics.
Full userdata, closures, and tables are all represented as GCObjects. For
__gc to be triggered when a lightuservalue values goes away, one would need
to make it a GCObject, effectively making it identical to full userdata.