lua-users home
lua-l archive

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


2012/1/3 Sean Conner <sean@conman.org>:
> It was thus said that the Great Dirk Laurie once stated:
>> The Four Laws of a userdata:
>>
>> 1. The closest a full userdata ever gets to your C code is the Lua stack.
>> 2. You can create a new userdata on the stack.
>> 3. You can receive an old userdata on the stack.
>> 4. There is no other way to return a userdata to Lua.
>>
>> You can check the registry type of a userdata on the stack, get its
>> block address, modify its contents, push the block address as a light
>> userdata — but you can't transform that light userdata back into a
>> full one.  Once it's off the stack, it is gone.
>>
>> Right?
>
>  I'm not so sure ...
>
...
> int foo_ligh_to_full(lua_Stack *L)
> {
>  foo__t *pfooa;
>  foo__t *pfoob;
>
>  if (!lua_islightuserdata(L,1))
>    return luaL_error(L,"not a light user data");
>
>  pfooa = lua_touserdata(L,1);
>  pfoob = lua_newuserdata(L,sizeof(foo__t));
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^

This demonstrates only that the contents of the memory block can be
copied into a newly created userdata.  What I'm claiming is that you
can't tell Lua that a particular block of memory is to be treated as a
userdata, even if Lua itself gave you that block last time you asked
for a userdata.