lua-users home
lua-l archive

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



You may be interested to know that the Nmap Scripting Engine (NSE)
also does this:

https://github.com/nmap/nmap/blob/5325fa143c8a2a1f0ad7aa77b856c5e5c036ac46/nse_main.lua#L176-L213

Nice to learn that the problem of nested coroutines appears in
other software too!

It's good to see you generalized it into a library. I'm curious, do
you have an instance where there is more than one level of nesting?

No, currently i only use nested coroutines with two levels: iterators in
redis-lua (https://github.com/nrk/redis-lua) and copas
(https://github.com/keplerproject/copas) in the underlying sockets.
To make them work together, i had to patch both libraries.

The main problem in using the nested coroutines library is that,
usually, Lua scripts use the global `coroutine` variable, instead of
creating a local copy of it. So we can't just replace `_G.coroutine`
with a nestable one before requiring the module.

Search for instance the use of `coroutine` in:
* https://github.com/keplerproject/copas/blob/master/src/copas.lua
* https://github.com/nrk/redis-lua/blob/version-2.0/src/redis.lua

If modules used a local `coroutine` variable, we could at least
monkey-patch libraries as below:

    local coromake = require "coroutine.make"
    _G.coroutine = coromake ()
    local copas = require "copas"
    _G.coroutine = coromake ()
    local redis = require "redis"
    _G.coroutine = coromake ()

The best solution being, of course, to use coronest in those libraries ;-)