lua-users home
lua-l archive

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

2011/1/10 Roberto Ierusalimschy <>:
>> > Does anyone still use references in real applications? For what?
>> I use it when I need to pass a callback to a C library, that could be
>> triggered in a subsequent lib call (ie. I can't just let a value on
>> the stack). I allocate a small struct { lua_State* L; int ref },
>> create a ref to the callback (a Lua "function" passed by the binding
>> user) and pass its address as the callback void*.
> Instead of a reference, can't you use the address of this small
> structure as the key to the callback (as a light userdata)? Or the
> address of a dummy 'ref' field, if you must keep more than one
> reference?

Yes, of course, that's possible, and with proper encapsulation just as
easy to use.

> (PS: I am aware I am not being strick to my question: there is a
> difference between whether people use references and whether people need
> references...)

The real need is to be able to retrieve a Lua value (be it a number or
a function I will pcall) from C, extracted from a void*. The couple
lua_State+int is just very easy to manipulate, and relatively easy to
grasp from a outsider point of view. But there are many possibilities
to achieve that without luaL_ref, we just need the registry (to keep
our trickery private).