lua-users home
lua-l archive

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


Hi,

I'm relatively new to Lua, and I've hit a conceptual block about which I'd really appreciate some more experienced input.

I want to create objects in Lua that are tied to some C data structure (created from my own allocated memory pool), but also to user-defined Lua functions. At a later point, I will need to iterate through my allocated C objects and call the associated Lua functions in turn (along with associated upvalues, and passing in the C data structure as an argument). I'm looking for the most efficient solution to this problem, scalable to hundreds (or more) such objects. All calls will be made in a determinate order, so there should be no thread-unsafe issues. The objects shouldn't be allocated or gc'd by Lua, since I'm using a memory pool, but some kind of equivalent gc notification to trigger recycling might be useful on either the Lua or C side.

I understand (I think) the difference between light and full userdata, but I haven't figured out yet how I could associate a reference to a Lua function into my self-allocated C data structure. I've seen examples of storing function references as strings, but that seems both unsafe and inefficient for my purposes (and prevents storing anonymous functions/closures). I can see how to push and read C functions from the Lua stack, but not Lua functions/closures. I also read something about the registry (but haven't totally understood it yet) - would that help? Could I use a light userdata as a key in the registry?

Or is there a simpler method I'm overlooking?

Thanks in advance!

Graham