lua-users home
lua-l archive

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


Am 30.09.2015 um 15:54 schröbte Alban Linard:

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 ()

You could temporarily replace the Lua searcher in `package.searchers[ 2 ]` (or insert a new one in `package.searchers[ 1 ]`) to load certain libraries with a custom environment that has the coroutines library replaced and an `__index`/`__newindex` to the real global environment.


Philipp