|
Sorry for all the newbie questions, but another one just occurred to me. In my original example code, have I put a subtle bug in there ? As I was restricted by what I could do from a swig function, I had passed in the luaTimerCallback1() function name as a string rather than a function. I then store this in the registry with /* the function name as a string onto the stack */ lua_getglobal(L1, funcName); // stores whatever is on the top of stack in registry, pops it from the stack too function_index = luaL_ref(L1, LUA_REGISTRYINDEX); Doesnt this mean that Lua will only protect the string from garbage collection, rather than the function itself ? Isnt my callback function potentially going to get garbage collected as it looks like it isnt really being used anywhere ? Regards Geoff From: spammealot1@live.co.uk To: lua-l@lists.lua.org Date: Mon, 21 Feb 2011 13:51:11 +0000 Subject: Timers, Callbacks & Swig 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 |