|
Hi Newbie here, my first post to this list, hope I am doing it correctly ? I am trying to add to my embedded App. the ability for Lua to have up to say 20 timers the user can create with asscociated Lua callback functions. The timers will be implemented in C with my underlying OS (not Windows). So from Lua it would read something like .. function luaTimerCallback1() print ("Lua callback function One.\n") end function luaTimerCallback2() print ("Lua callback function Two.\n") end local timerID1 = myLib.createTimer("luaTimerCallback1", 3000) -- 3 Seconds local timerID2 = myLib.createTimer("luaTimerCallback2", 5000) -- 5 Seconds I am implementing the createTimer binding to C with SWIG, so my C function is int createTimer(char *funcName, unsigned int periodInMs) Question 1) Having to make the Callback function name a string, looks a bit wrong to me, I would have preferred to just pass it as a Lua function, but I couldnt figure out a way to do that with SWIG. Is it even possible to pass a Lua function back into C without having to pass it as a string ? If so any clues how to do it please ? In my C function I then do some API calls to store the LUA function name in the LUA registry, I then create the timer in C, and let it run. When the timer fires, I look up the specific Lua function from the registry and then call that with a pcall function. This works OK. Question 2) The API code I have put inside my C createTimer() function of course needs to get the Lua state variable L, for example a line such as lua_getglobal(L, funcName); etc But the snag I hit is how do I get the Lua state variable L ? It was passed into my Swig binding when I registered it with Lua with the line luaopen_myLib(L); // declare the wrapped module but I cant see how I can get at that variable from inside my createTimer() C function ? Anyone any ideas ? For the moment I have kludged it by saving it in a global. This works but dont want to use it as the final solution as it wont work if I need to have multiple Lua states running in different OS threads. Maybe I just need to implement this timer code without using SWIG ? But even if I could work out how to do that, I am not sure how I would then add this manual binding to my myLib Swig bindings ? Any ideas how to add manual bindings to the mylib Swig auto bindings ? Any thoughts or code examples would be appeciated, thanks Regards Geoff |