lua-users home
lua-l archive

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

> -----Original Message-----
> From: [] On
> Behalf Of Steven Degutis
> Sent: maandag 14 juli 2014 17:52
> To: Lua mailing list
> Subject: How do you usually mix Lua and C callbacks?
> I've got some C functions that take callback which has a void* pointer
> for programmer-use. The technique I've been using is making a little
> struct that holds the lua_State* and an int, which I store the return
> value of luaL_ref'ing the given function. Then inside the C callback,
> I take the Lua state and lua_rawgeti the function out of
> LUA_REGISTRYINDEX, and call it.
> But this seems very cumbersome and awkward. (Plus it means my function
> won't be garbage-collected until I luaL_unref it out of there, but I'm
> not sure that can be solved without making use of an explicit
> "unregister" method for this C callback). Is there a better way to do
> this that is less cumbersome and awkward? Thanks.
> -Steven

If your application is not multithreaded, the reference can be stored in a global variable, and the lua pieces can remain on the stack.

If you do have a multithreaded environment, then I don't know simpler solutions