My previous example was wrong, so I attach a new simpler one. I hope that it is more readable. The question is still:
I just want to fully stop the lua VM, to process some data outside lua, and then to restart the VM exactly where it did stop. To do this transparently with the proposed code, a lot of auxiliary code is required, e.g. to decide if perform a `nestresume` or a normal one, to handle errors, to wrap the standard coroutine functions, and so on.
local function nestresume(nc, ...)
local res = table.pack(coroutine.resume(nc, ...))
-- Skip the yield/resume propagation as needed
if not coroutine.isyieldable() or coroutine.status(nc) ~= 'suspended' then
return table.unpack(res)
end
-- Propagate the yielding upstream
local res = table.pack(coroutine.yield(table.unpack(res)))
-- Propagate the resuming downstream
return nestresume(nc, table.unpack(res))
end
-- Usage example:
local outer, inner
local out = ''
outer = coroutine.create(function()
inner = coroutine.create(function()
out = out .. 'c'
coroutine.yield()
out = out .. 'e' -- with std resume, this whould have been skipped
end)
out = out .. 'b'
nestresume(inner)
out = out .. 'f'
end)
out = out .. 'a'
nestresume(outer)
out = out .. 'd' -- with std resume, this would have been run after 'f'
nestresume(outer) -- with std resume, this was not required
print(out)
assert(out=='abcdef')
```