Quick question about C/Lua integration

Is it possible to pass an anonymous Lua function to C that can be executed on? I noticed there is no "getArgFunction" or anything. What are other solutions to needing a "callback" type function?

1 Like

That's something I'd never considered! The only way to call a Lua function from C is by name, using pd->lua->callFunction(const char* name, int nargs, const char** outerr). The only thing that comes to mind is you could put the callback functions in a table indexed by a token of some sort and pass the token to your C code. To call back, the C code hands the token to a named function that then pulls the function out of the table and calls it with any arguments that were passed with the token.

I've filed an issue to add a way to call a lua function on the stack. Maybe I'll finally get around to rebuilding that entire interface, either figure out how to abstract away the stack entirely or just put all of lua.h in there. Dang. Now that I'm looking at it again.. lua.h isn't that big. I probably should have gone that way from the beginning :confused:


Yeah, I considered doing that and looks like I will end up needing to do something like that anyway. The context is that I have a tree of nodes with measurement functions on them, during a layout pass those measurement functions may get called and need to report their sizes back. Upon looking at the library I was integrating though, it seems that there is no good way to connect a function in general! So, the original question I had still wouldn't solve - on top of the fact that I know that calling Lua from C is expensive and these calls would happen often- it should be avoided. Since I only really need measurement for text nodes, I can instead store the text on the node that needs rendering and have the measurement function do all its work on the C side. Problem now is I am not super familiar with C and for some reason the pointers I am adding to the nodes seem to "disappear" lol. Assuming I probably need to push the pointers on a stack somewhere or in a static table somewhere.

Edit: ended up resolving my pointer issue, had to alloc to heap a struct instead of naked int/float/strings.

Now I just need getTextSizeForMaxWidth in the C lib, did not realize this was only in Lua! :cry: