lua-users home
lua-l archive

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


I confused terms here. Sorry. I'm going to edit my own words:

On Tue, Sep 16, 2014 at 1:19 PM, Andrew Starks <andrew.starks@trms.com> wrote:

I think that the important difference is that Lua allocates userdata, returning a pointer that it allocates: `void * lua_newuserdata (lua_State *L, size_t

I meant to say returning a pointer to the buffer that it allocated for you.
 
size);` You can't stash this pointer in a C struct and then call into Lua and then later retrieve it, once the C `L*` goes out of scope.

With lightuserdata, you give the pointer to it: `void lua_pushlightuserdata (lua_State *L, void *p);` In this use case, that's important, because you need to find the C object (Window handle) inside of Lua. You can't do that with userdata, unless you want to manually do that using some other kind of uuid magic.


Not the C object (Window handle), but instead the *pointer*, because the handle that you receive from Windows can be pushed on the stack, like so:

```
lua_rawgetp(L, LUA_REGISTRYINDEX, (void *) ptr_my_whandle);

* ud_myobj =(window_obj *) lua_touserdata(L, -1);

luaL_checkudata( L, -1, MY_LIB_NAME);
```
Hopefully that's somewhat clearer

-Andrew