lua-users home
lua-l archive

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


On Mar 12, 2012, at 8:37 PM, Kevin Martin wrote:

> I am about to hit a similar problem, I haven't implemented it yet, but my intended solution is to create a table with all of the callbacks within lua and then call a set_handler function passing the table as an argument. The set_handler function is implemented in C and sets a field of the lua registry equal to that table. I have a single callback function in C, it looks up the value in the registry to get the callback table in lua, it then indexes that table to find the appropriate function to call, and calls it.

An alternative is to store the table of callbacks as a shared upvalue of the functions in the C library. This makes them private to the library. You can install callbacks from Lua or C, and reference them with integers, or light userdata, for example.

Another issue is what Lua thread to use when invoking the callbacks. Lua 5.2 makes this easier with LUA_RIDX_MAINTHREAD. In Lua 5.1 I use the thread that loads the C library.

e