- Push your lua callback function onto your lua state.
- Call your newly written register_lua_func, passing in the state. It would:
- Pop the function
- Push a new thread
- Put the new thread somewhere in the main state's registry so that it doesn't get garbage collected.
- Push the function onto the new thread.
- Pass the new lua_State as the userdata to the C register function.
With respect to steps 3 and 6; Is there an easy way to move the function from one lua_State's stack to another's? It's been a few months since I did any Lua. Maybe you could you do it via the registry?
You C callback would be trivial:
- Cast the void * to a lua_State
- lua_pushvalue(L, -1) // push a duplicate of the lua callback function
- Push any args
- lua_call(L, ...)
- Pop any results
- (original function remains on the stack)