lua-users home
lua-l archive

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




On Mon, Jun 17, 2019 at 12:20 PM Abhijit Nandy <abhijit.nandy@gmail.com> wrote:
Hi,

I have a long running chunk of Lua code which runs in the UI thread of my application. To keep the UI responsive, I sometimes run this in a coroutine and yield() in between so the UI can process pending messages. I can manually add the yields() if the code is under my control. However some of the code is submitted by my users and I would like to add the yields() later in the correct points by modifying the code string. But this is prone to errors, especially if there are multi-line strings in the code or statements are split into multiple lines.

Is there anyway to do this at regular intervals in Lua code that's certain not to break syntax?


The pattern I use to do this is to replace

return val, msg

with 

yield(val, msg)
return nil

This seems tractable to change in bytecode, though you'd have to research how for
a given Lua engine.

If you wanted to yield out of time-consuming loops, you can do a similar thing at the 
end:

for k,v in pairs(t) do
   --[[...]]
  yield()
end

Hope this helps! Can't guarantee it won't break your code. But it shouldn't.

cheers,
-Sam.