summaryrefslogtreecommitdiff
path: root/src/loadlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-05-17 07:51:06 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-05-17 07:51:06 +0200
commit3a7f9d85b91530f7f151f536a0aab2455486262d (patch)
treec2e7d6cdbb0d453f4d6657d943555b3c3f14addd /src/loadlib.js
parent32bb4b116bab3df193f1ca1db022c217bd5f2cda (diff)
parent8e81d13f37230fa8a5aefd77d8b2372882077190 (diff)
downloadfengari-3a7f9d85b91530f7f151f536a0aab2455486262d.tar.gz
fengari-3a7f9d85b91530f7f151f536a0aab2455486262d.tar.bz2
fengari-3a7f9d85b91530f7f151f536a0aab2455486262d.zip
Merge remote-tracking branch 'daurnimator/yieldable-require'
Diffstat (limited to 'src/loadlib.js')
-rw-r--r--src/loadlib.js44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/loadlib.js b/src/loadlib.js
index c43f498..4e5ee60 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -321,30 +321,40 @@ const searcher_preload = function(L) {
return 1;
};
-const findloader = function(L, name) {
+const findloader = function(L, name, ctx, k) {
let msg = []; /* to build error message */
/* push 'package.searchers' to index 3 in the stack */
if (lua.lua_getfield(L, lua.lua_upvalueindex(1), lua.to_luastring("searchers", true)) !== lua.LUA_TTABLE)
lauxlib.luaL_error(L, lua.to_luastring("'package.searchers' must be a table"));
+ let ctx2 = {name: name, i: 1, msg: msg, ctx: ctx, k: k};
+ return findloader_cont(L, lua.LUA_OK, ctx2);
+};
+
+const findloader_cont = function(L, status, ctx) {
/* iterate over available searchers to find a loader */
- for (let i = 1; ; i++) {
- if (lua.lua_rawgeti(L, 3, i) === lua.LUA_TNIL) { /* no more searchers? */
- lua.lua_pop(L, 1); /* remove nil */
- lua.lua_pushstring(L, msg); /* create error message */
- lauxlib.luaL_error(L, lua.to_luastring("module '%s' not found:%s"), name, lua.lua_tostring(L, -1));
+ for (; ; ctx.i++) {
+ if (status === lua.LUA_OK) {
+ if (lua.lua_rawgeti(L, 3, ctx.i) === lua.LUA_TNIL) { /* no more searchers? */
+ lua.lua_pop(L, 1); /* remove nil */
+ lua.lua_pushstring(L, ctx.msg); /* create error message */
+ lauxlib.luaL_error(L, lua.to_luastring("module '%s' not found:%s"), ctx.name, lua.lua_tostring(L, -1));
+ }
+ lua.lua_pushstring(L, ctx.name);
+ lua.lua_callk(L, 1, 2, ctx, findloader_cont); /* call it */
+ } else {
+ status = lua.LUA_OK;
}
- lua.lua_pushstring(L, name);
- lua.lua_call(L, 1, 2); /* call it */
if (lua.lua_isfunction(L, -2)) /* did it find a loader? */
- return; /* module loader found */
+ break; /* module loader found */
else if (lua.lua_isstring(L, -2)) { /* searcher returned error message? */
lua.lua_pop(L, 1); /* remove extra return */
- msg.push(...lua.lua_tostring(L, -1));
+ ctx.msg.push(...lua.lua_tostring(L, -1));
lua.lua_remove(L, -1); /* concatenate error message */
}
else
lua.lua_pop(L, 2); /* remove both returns */
}
+ return ctx.k(L, lua.LUA_OK, ctx.ctx);
};
const ll_require = function(L) {
@@ -356,10 +366,20 @@ const ll_require = function(L) {
return 1; /* package is already loaded */
/* else must load package */
lua.lua_pop(L, 1); /* remove 'getfield' result */
- findloader(L, name);
+ let ctx = name;
+ return findloader(L, name, ctx, ll_require_cont);
+};
+
+const ll_require_cont = function(L, status, ctx) {
+ let name = ctx;
lua.lua_pushstring(L, name); /* pass name as argument to module loader */
lua.lua_insert(L, -2); /* name is 1st argument (before search data) */
- lua.lua_call(L, 2, 1); /* run loader to load module */
+ lua.lua_callk(L, 2, 1, ctx, ll_require_cont2)
+ return ll_require_cont2(L, lua.LUA_OK, ctx); /* run loader to load module */
+};
+
+const ll_require_cont2 = function(L, status, ctx) {
+ let name = ctx;
if (!lua.lua_isnil(L, -1)) /* non-nil return? */
lua.lua_setfield(L, 2, name); /* LOADED[name] = returned value */
if (lua.lua_getfield(L, 2, name) == lua.LUA_TNIL) { /* module set no value? */