Trying to make an engine that calls other language from lua code. And the problem that I stuck into is callbacks. Converting strings, ints and so on is easy, but what to do with functions? Lua API has lua_isfunction to check that function argument is function, but how do I store it to call it later when called script needs it? There is lua_topointer, that can store function, but I don't see how to call that stored pointer.
You need to retrieve these functions as values that live in the Lua state. You must not store pointers to values managed by Lua. These may become invalid at any point because the GC does not know you are holding them.
For example one option are global callbacks, or global callback tables. In this case the function is identified by the "path" of names that leads for it. On the C side of things, you then just index _G["callback"]
and call that, or you get _G["myapitable"]["mycallbacks"]
, iterate that and call each callback.
If you want to "hide" these functions for encapsulation purposes, use the registry.
As soon as you've decided for a table to store your functions in, you can manage them however you please, using names or numbers as keys into this table, and removing or adding "callbacks" to this table as you please.