Coroutines are a powerful notion to suspend and resume execution of lightweight threads. It can be used for instance for scheduling, as in copas, or for iteration.
When mixing these two uses, a problem arises: there is no way to specify
which coroutine.resume should intercept a coroutine.yield.
This module provides a thin wrapper around the standard coroutine
module to allow the definition of complex behaviors with nested
coroutines.
This module is available in luarocks:
luarocks install coronestEverywhere you are using the standard Lua coroutine module,
replace it with an instance of the nested coroutines:
local coroutine = require "coroutine.make" ()For an example, please look at examples/usage.lua.
Nested coroutines should be compatible with any version of Lua supporting
coroutines (well, at least from 5.1). As the module is written in pure Lua,
it also works with LuaJIT.
This module comes with some tests:
tests/nested.luaare important checks on the behavior of nested coroutines;tests/lua-5.*.luaare tests imported from the Lua testsuite, but usingcoroutine.makeinstead of standard coroutines; they allow to check that nested coroutines do not break anything.
This module has been built after this discussion on StackOverflow. Even if Alban Linard (saucisson) owns this repository, the main contributor is Philipp Janda (siffiejoe).