lua-users home
lua-l archive

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


> On the other hand, it might be a fun exercise to implement this
> as a C extension.  The car would be stored as a ref, which is
> something the reference manual doesn't emphasize enough.
> (Somebody might wanna write a tutorial for the wiki on
> references).  If you allow improper lists, you'll have to store
> the cdr as a ref as well.
>
> > Beware of what amount to reference-counting cycles when
> > creating refs. It's actually a bit of a pain to get userdata
> > to refer safely to other Lua data objects and seems to
> > require the use of per-object metatables rather than sharing
> > the metatables across a type.

Agreed, refs should NOT be emphasized in Lua5.

For per userdata instance Lua data I recommend using a weak keyed table
inside the metatable for the userdata "type".  This way the metatable
uniqueness is still a "type identifier", but per instance data is easily
accessed.

Alternatively, you can "front" the per instance data Lua table as the
"object itself", and back it with the userdata as necessary rather than the
other way around.  This has some performance advantages for raw Lua access,
and some "security" disadvantages in terms of allowing Lua code to use
rawset to bypass/override your properties and methods.