lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I quite like the new-thread approach.  That means you only ever have a lua_State* on the C side of things, so no there's need to define and do housekeeping for any new structs.

It would be something like:
(Let's hope my formatting gets preserved!)

  1. Push your lua callback function onto your lua state.
  2. Call your newly written register_lua_func, passing in the state.  It would:
  3. Pop the function
  4. Push a new thread
  5. Put the new thread somewhere in the main state's registry so that it doesn't get garbage collected.
  6. Push the function onto the new thread.
  7. 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:
  1. Cast the void * to a lua_State
  2. lua_pushvalue(L, -1) // push a duplicate of the lua callback function
  3. Push any args
  4. lua_call(L, ...)
  5. Pop any results 
  6. (original function remains on the stack)
Peter