lua-users home
lua-l archive

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


2009/8/16 Chris Camacho <c.camacho@ntlworld.com>:
>> Be careful. Lightuserdata all share the same metatable. This may not be
>> your intent.
>
>
> By adding the "cpShape" metatable I can use the "cpShape" methods
> it is defiantly returning the correct pointer as the right object reacts
> when I use that userdata's methods, how can what I'm returning
> to lua be sharing the same metatable if I can call a method that's
> unique to cpShape???

By assigning cpShape as the metatable of one light userdata, all light
userdata now have that metatable. It means that if you create a light
userdata elsewhere that is not a shape, it will still have cpShape as
its metatable. And if you assign another metatable to that non-shape
light userdata, it will change the metatable or all you existing shape
light userdatas.

You don't see the problem because you only use one type of object. But
try with two or more and you will start getting very weird things.

>> My approach to your problem would be to make a userdata the size of your
>> pointer and then essentially use double indirection on any appropriate
>> pointer you receive.
>
> really? that seems a lot of work (there must be something easier and
> more elegant!)

The most elegant solution is to have a weak table in the registry, as
proposed earlier by Tony Finch. You can have your userdata constructor
function put them there, and not care about that table thereafter,
just use it to convert a light userdata to a full userdata.