lua-users home
lua-l archive

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

I don't know if this is useful to anyone else, but I have had this problem
of needing to include() files from one lua script to the next. The problem
is that our underlying file system is asynchronous, so we cannot just do a
dofile() whenever we need to load dependant code. I have worked around this
by implementing a new kind of non-fatal error in Lua, which I've named
LUA_EAGAIN. I've also added API functions lua_tryagain() and tryagain() to
throw this error.

When I need to include a new file, I first check to see if it has been
cached in memory, if so, I eval it. Otherwise, I schedule a new file read
request and call lua_tryagain(). Whenever the async file I/O completes, I
cache the code in memory, and attempt to re-evaluate the dependant file,
which may or may not include additional files and cause additional
LUA_EAGAIN errors.

As I said, I don't know if this feature is useful to anyone else, but I'm
happy to share source with anyone who would like. Also, if there is a more
"official" Lua way of accomplishing this same task (which does not require
patching Lua source), I'd love to hear about it.