diff options
-rw-r--r-- | src/loadlib.js | 44 | ||||
-rw-r--r-- | src/lobject.js | 6 | ||||
-rw-r--r-- | src/lvm.js | 4 |
3 files changed, 38 insertions, 16 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? */ diff --git a/src/lobject.js b/src/lobject.js index 9790d23..680e293 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -523,7 +523,7 @@ const intarith = function(L, op, v1, v2) { case defs.LUA_OPSUB: return (v1 - v2)|0; case defs.LUA_OPMUL: return (v1 * v2)|0; case defs.LUA_OPMOD: return (v1 - Math.floor(v1 / v2) * v2)|0; // % semantic on negative numbers is different in js - case defs.LUA_OPIDIV: return (v1 / v2)|0; + case defs.LUA_OPIDIV: return Math.floor(v1 / v2)|0; case defs.LUA_OPBAND: return (v1 & v2); case defs.LUA_OPBOR: return (v1 | v2); case defs.LUA_OPBXOR: return (v1 ^ v2); @@ -531,6 +531,7 @@ const intarith = function(L, op, v1, v2) { case defs.LUA_OPSHR: return (v1 >> v2); case defs.LUA_OPUNM: return (0 - v1)|0; case defs.LUA_OPBNOT: return (~0 ^ v1); + default: assert(0); } }; @@ -542,9 +543,10 @@ const numarith = function(L, op, v1, v2) { case defs.LUA_OPMUL: return v1 * v2; case defs.LUA_OPDIV: return v1 / v2; case defs.LUA_OPPOW: return Math.pow(v1, v2); - case defs.LUA_OPIDIV: return (v1 / v2); + case defs.LUA_OPIDIV: return Math.floor(v1 / v2); case defs.LUA_OPUNM: return -v1; case defs.LUA_OPMOD: return v1 % v2; + default: assert(0); } }; @@ -713,8 +713,8 @@ const luaV_equalobj = function(L, t1, t2) { if (t1.ttnov() !== t2.ttnov() || t1.ttnov() !== CT.LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ - let i1, i2; /* compare them as integers */ - return (((i1 = luaV_tointeger(t1, 0)) !== false) && ((i2 = luaV_tointeger(t2, 0)) !== false) && (i1 === i2)) ? 1 : 0; + /* OPTIMIZATION: instead of calling luaV_tointeger we can just let JS do the comparison */ + return (t1.value === t2.value) ? 1 : 0; } } |