lua-users home
lua-l archive

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


2009/8/17 Chris Camacho <c.camacho@ntlworld.com>:
>> 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.
>
> slight problem... when I create the user data on the stack
> if I pop it off to save it to a table there's no way to push
> userdata onto the stack

You could eventually have indexed the weak table again to retrieve the
userdata reference, but since the weak reference would have been the
last one, it could have been collected in between (any call to the Lua
C API can trigger a collection cycle).

> How would I "peek" into the stack and save the new userdata
> without disturbing the values I'm assembling on the stack for
> the return?

You can use lua_pushvalue to duplicate the reference to the userdata
on the stack. Then you can save one in the weak table (with the light
userdata key), and do whatever you want with the other one.