lua-users home
lua-l archive

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


On Sat, Aug 15, 2009 at 3:47 PM, David Given<dg@cowlark.com> wrote:
> Michael Newberry wrote:
> [...]
>> The missing concept in precisely what has to happen for the userdata
>> I've created to be referenced? Do I push the userdata ptr onto the
>> return stack using lua_pushlightuserdata( ptr ) and then, in the script,
>> set a table element to the return value?

Sorry, Michael, I thought you said you'd be binding code into lua for years.

Your code ends with pushlightuserdata(), then does "return 1". So, it
is returning only 1 thing, and that thing is not the userdata you'd
created earlier.

> A userdata is just another Lua object type, like a lightuserdata. So
> just push it onto the stack and return it. You don't need to use
> lightuserdatas at all.
>
> Basically:
>
> lightuserdata: 4 bytes only, no metatable, no notification when GCd
>
> userdata: any number of bytes, metatable, notification when GCd
>
> Check out the example here: http://www.lua.org/pil/28.1.html

The fundamentals of this are covered in the above, and the succeeding
chapters which cover __gc (and I also posted a link to), and explained
much better than I can.

If are comfortable in C, and sit down and spend a few hours reading
carefully through those chapters, and write a small example to start
with, then build up, you should be OK.

Cheers,
Sam