lua-users home
lua-l archive

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


Hi, list!

Lua 5.1.4

I need to delay my script execution until I get an event from a host
program. To do this I wrap my script into a coroutine, and pass a code
to resume it into a callback. Engine stores callback in Lua registry
and calls it when ready.

A pseudocode:

      local coro -- A forward declaration
      coro = coroutine.create(function()
        ENGINE.call_me_later(function() assert(coroutine.resume(coro)) end)
        coroutine.yield()
      end)
      assert(coroutine.resume(coro))

When I run it, coroutine.resume() inside the callback fails with
message "cannot resume running coroutine". Coroutine status is
"running" and coroutine.running() points to the coroutine as well.
Stack trace is like this:

      assert()
      callback()
      coroutine.yield()
      coroutine.resume()

All works fine when I change code to this (that is, no calls to
coroutine.resume() before callback is called):

      local coro -- A forward declaration
      coro = coroutine.create(function()
        coroutine.yield()
      end)
      ENGINE.call_me_later(function() assert(coroutine.resume(coro)) end)

Am I missing something? I thought the first method should work.

If needed, I may provide a minimal-enough working example.

Alexander.