class

Lua::Coroutine

Inherits Lua::Object / Reference / Object

Constructors

new(stack, function : Function | Nil = nil)

Creates new Coroutine with it's own stack and a function to execute.

lua = Lua.load

f = lua.run %q{
  return function()
    print("before yield")

    coroutine.yield()

    print("after yield")
  end
}

co = lua.newthread(f.as Lua::Function)
co.resume # before yield
co.status # => YIELD
co.resume # after yield
co.status # => OK

Stack may return coroutine. In this case we do not need to pass a function 'cause coroutine should already have it:

t = lua.run %q {
  function s(x)
    return coroutine.yield(x) * 10
  end

  return coroutine.create(s)
}

co = t.as(Lua::Coroutine)
co.resume      # => nil
co.resume(4.2) # => 42.0
Source

Instance methods

resume(*args)
Source
status
Source