lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
> More often than not, programs set the metatable of a userdata right
> after creating the userdata. Does anyone know of relevant
> counterexamples to this pattern?  
> 
> More specifically: if Lua puts a cost on setting the metatable of an
> object long after its creation (the longer the more expensive), do
> you know of programs that would be hurt?  
> 
> Yet more specifically, I am interested only in cases where the
> metatable has a __gc metamethod. 

I did that at some point for a userdata which is an hybrid between a
pointer and a dynamic array. That is the same userdata contain just a
pointer, and can be owner or not of the pointed data. So whether or not
we are the owner we have or not the __gc metamethod, and that can change
over time.

I don't use that system anymore, because I needed at some point to have
per-class metatables rather than per object (for luaL_checkudata). With
my new system, I allocate all buffers through lua_newuserdata instead of
malloc, and I store the buffer ud as the environment of the pointer ud
to prevent its collection as long as the pointer exists and points to
it.