lua-users home
lua-l archive

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


Our application currently uses lua_pcall to evaluate scripts. The script instances have control inputs and outputs and the user can register lua function callbacks to be called when the values on the input changes. Because we have our own control frame-rate/threading in the application any operation that might take time uses an Asynchronous Programming Model where each operation has a Begin* and End* method with a callback when the operation is done. 

This approach has been working fine so far except using an APM can really make for some spaghetti code if you need to chain one async call to another. Because of this I'm thinking of changing our 'long' methods to use yield() instead. That way the script can look essentially linear but it doesn't hold up our entire control thread waiting for an operation to complete. I am curious if the approach I'm taking is a safe one or if there's a better way.

Since I don't want my users to have to deal with coroutines it seems like I need to create the threads in C. Previously I would do something like

   lua_State* L = luaL_newstate();
   luaL_loadbuffer(st, code, strlen(code), "=");
   lua_pcall(L, 0, LUA_MULTRET, 0);

and when a control would change I would just call pcall

   lua_pcall( L, 1, 0, 0 );

Since it appears as though I need to create a thread to enable yield, I changed my code to 

   lua_State* L = luaL_newstate();
   lua_State* th = lua_newthread(L);
   luaL_loadbuffer(th, code, strlen(code), "=");
   lua_resume (th, 0);

and would do the same thing for the callback for control changes

   lua_resume (th, 0);

Assuming this is the correct way to accomplish this, is there a way around needing to keep track of 2 lua_States? I'm assuming it's not ok to call lua_close on the state return from newstate and still use the one I got from newthread. 

Any other suggestions/comments on what I'm trying to do?

Thanks-

John