diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-26 17:55:37 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-26 18:06:57 +1000 |
commit | b688ef577a10e8b6f2cf948faaa8d1af70c7949c (patch) | |
tree | 0ff07d75f65442b92d8a95259b9ab6845e66b27a /src | |
parent | 53bd1fcc4b90f24427064419354ad07c552a93d5 (diff) | |
download | fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.tar.gz fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.tar.bz2 fengari-b688ef577a10e8b6f2cf948faaa8d1af70c7949c.zip |
Export lapi.js functions from lua.js
Diffstat (limited to 'src')
-rw-r--r-- | src/lauxlib.js | 220 | ||||
-rw-r--r-- | src/lbaselib.js | 187 | ||||
-rw-r--r-- | src/lcorolib.js | 75 | ||||
-rw-r--r-- | src/ldblib.js | 165 | ||||
-rw-r--r-- | src/lmathlib.js | 113 | ||||
-rw-r--r-- | src/loslib.js | 17 | ||||
-rw-r--r-- | src/lstrlib.js | 132 | ||||
-rw-r--r-- | src/ltablib.js | 82 | ||||
-rw-r--r-- | src/lua.js | 99 | ||||
-rw-r--r-- | src/lutf8lib.js | 32 |
10 files changed, 608 insertions, 514 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js index f3287f5..7b5f958 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -27,25 +27,25 @@ const LEVELS2 = 11; /* size of the second part of the stack */ ** return 1 + string at top if find a good name. */ const findfield = function(L, objidx, level) { - if (level === 0 || !lapi.lua_istable(L, -1)) + if (level === 0 || !lua.lua_istable(L, -1)) return 0; /* not found */ - lapi.lua_pushnil(L); /* start 'next' loop */ + lua.lua_pushnil(L); /* start 'next' loop */ - while (lapi.lua_next(L, -2)) { /* for each pair in table */ - if (lapi.lua_type(L, -2) === lua.LUA_TSTRING) { /* ignore non-string keys */ - if (lapi.lua_rawequal(L, objidx, -1)) { /* found object? */ - lapi.lua_pop(L, 1); /* remove value (but keep name) */ + while (lua.lua_next(L, -2)) { /* for each pair in table */ + if (lua.lua_type(L, -2) === lua.LUA_TSTRING) { /* ignore non-string keys */ + if (lua.lua_rawequal(L, objidx, -1)) { /* found object? */ + lua.lua_pop(L, 1); /* remove value (but keep name) */ return 1; } else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lapi.lua_remove(L, -2); /* remove table (but keep name) */ - lapi.lua_pushliteral(L, "."); - lapi.lua_insert(L, -2); /* place '.' between the two names */ - lapi.lua_concat(L, 3); + lua.lua_remove(L, -2); /* remove table (but keep name) */ + lua.lua_pushliteral(L, "."); + lua.lua_insert(L, -2); /* place '.' between the two names */ + lua.lua_concat(L, 3); return 1; } } - lapi.lua_pop(L, 1); /* remove value */ + lua.lua_pop(L, 1); /* remove value */ } return 0; /* not found */ @@ -55,20 +55,20 @@ const findfield = function(L, objidx, level) { ** Search for a name for a function in all loaded modules */ const pushglobalfuncname = function(L, ar) { - let top = lapi.lua_gettop(L); + let top = lua.lua_gettop(L); ldebug.lua_getinfo(L, ['f'.charCodeAt(0)], ar); /* push function */ - lapi.lua_getfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring(LUA_LOADED_TABLE, true)); + lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lua.to_luastring(LUA_LOADED_TABLE, true)); if (findfield(L, top + 1, 2)) { - let name = lapi.lua_tostring(L, -1); + let name = lua.lua_tostring(L, -1); if (lobject.jsstring(name).startsWith("_G.")) { /* name start with '_G.'? */ - lapi.lua_pushstring(L, name.slice(3)); /* push name without prefix */ - lapi.lua_remove(L, -2); /* remove original name */ + lua.lua_pushstring(L, name.slice(3)); /* push name without prefix */ + lua.lua_remove(L, -2); /* remove original name */ } - lapi.lua_copy(L, -1, top + 1); /* move name to proper place */ - lapi.lua_pop(L, 2); /* remove pushed values */ + lua.lua_copy(L, -1, top + 1); /* move name to proper place */ + lua.lua_pop(L, 2); /* remove pushed values */ return 1; } else { - lapi.lua_settop(L, top); /* remove function and global table */ + lua.lua_settop(L, top); /* remove function and global table */ return 0; } }; @@ -77,17 +77,17 @@ const sv = s => s ? s : []; const pushfuncname = function(L, ar) { if (pushglobalfuncname(L, ar)) { /* try first a global name */ - lapi.lua_pushstring(L, lua.to_luastring("function '", true).concat(lapi.lua_tostring(L, -1)).concat(["'".charCodeAt(0)])); - lapi.lua_remove(L, -2); /* remove name */ + lua.lua_pushstring(L, lua.to_luastring("function '", true).concat(lua.lua_tostring(L, -1)).concat(["'".charCodeAt(0)])); + lua.lua_remove(L, -2); /* remove name */ } else if (ar.namewhat) /* is there a name from code? */ - lapi.lua_pushstring(L, sv(ar.namewhat).concat(" ".charCodeAt(0), "'".charCodeAt(0), ...sv(ar.name.value), "'".charCodeAt(0))); /* use it */ + lua.lua_pushstring(L, sv(ar.namewhat).concat(" ".charCodeAt(0), "'".charCodeAt(0), ...sv(ar.name.value), "'".charCodeAt(0))); /* use it */ else if (ar.what && ar.what[0] === 'm'.charCodeAt(0)) /* main? */ - lapi.lua_pushliteral(L, "main chunk"); + lua.lua_pushliteral(L, "main chunk"); else if (ar.what && ar.what[0] != 'C'.charCodeAt(0)) /* for Lua functions, use <file:line> */ - lapi.lua_pushstring(L, lua.to_luastring("function <", true).concat(...sv(ar.short_src), ':'.charCodeAt(0), ...lua.to_luastring(`${ar.linedefined}>`))); + lua.lua_pushstring(L, lua.to_luastring("function <", true).concat(...sv(ar.short_src), ':'.charCodeAt(0), ...lua.to_luastring(`${ar.linedefined}>`))); else /* nothing left... */ - lapi.lua_pushliteral(L, "?"); + lua.lua_pushliteral(L, "?"); }; const lastlevel = function(L) { @@ -107,34 +107,34 @@ const lastlevel = function(L) { const luaL_traceback = function(L, L1, msg, level) { let ar = new lua.lua_Debug(); - let top = lapi.lua_gettop(L); + let top = lua.lua_gettop(L); let last = lastlevel(L1); let n1 = last - level > LEVELS1 + LEVELS2 ? LEVELS1 : -1; if (msg) - lapi.lua_pushstring(L, msg.concat('\n'.charCodeAt(0))); + lua.lua_pushstring(L, msg.concat('\n'.charCodeAt(0))); luaL_checkstack(L, 10, null); - lapi.lua_pushliteral(L, "stack traceback:"); + lua.lua_pushliteral(L, "stack traceback:"); while (ldebug.lua_getstack(L1, level++, ar)) { if (n1-- === 0) { /* too many levels? */ - lapi.lua_pushliteral(L, "\n\t..."); /* add a '...' */ + lua.lua_pushliteral(L, "\n\t..."); /* add a '...' */ level = last - LEVELS2 + 1; /* and skip to last ones */ } else { ldebug.lua_getinfo(L1, lua.to_luastring("Slnt", true), ar); - lapi.lua_pushstring(L, ['\n'.charCodeAt(0), '\t'.charCodeAt(0), '.'.charCodeAt(0), '.'.charCodeAt(0), '.'.charCodeAt(0)].concat(ar.short_src)); + lua.lua_pushstring(L, ['\n'.charCodeAt(0), '\t'.charCodeAt(0), '.'.charCodeAt(0), '.'.charCodeAt(0), '.'.charCodeAt(0)].concat(ar.short_src)); if (ar.currentline > 0) - lapi.lua_pushliteral(L, `${ar.currentline}:`); - lapi.lua_pushliteral(L, " in "); + lua.lua_pushliteral(L, `${ar.currentline}:`); + lua.lua_pushliteral(L, " in "); pushfuncname(L, ar); if (ar.istailcall) - lapi.lua_pushliteral(L, "\n\t(...tail calls..)"); - lapi.lua_concat(L, lapi.lua_gettop(L) - top); + lua.lua_pushliteral(L, "\n\t(...tail calls..)"); + lua.lua_concat(L, lua.lua_gettop(L) - top); } } - lapi.lua_concat(L, lapi.lua_gettop(L) - top); + lua.lua_concat(L, lua.lua_gettop(L) - top); }; const panic = function(L) { - throw new Error(`PANIC: unprotected error in call to Lua API (${lapi.lua_tojsstring(L, -1)})`); + throw new Error(`PANIC: unprotected error in call to Lua API (${lua.lua_tojsstring(L, -1)})`); }; const luaL_argerror = function(L, arg, extramsg) { @@ -152,7 +152,7 @@ const luaL_argerror = function(L, arg, extramsg) { } if (ar.name === null) - ar.name = pushglobalfuncname(L, ar) ? lapi.lua_tostring(L, -1) : ["?".charCodeAt(0)]; + ar.name = pushglobalfuncname(L, ar) ? lua.lua_tostring(L, -1) : ["?".charCodeAt(0)]; return luaL_error(L, lua.to_luastring(`bad argument #${arg} to '${lobject.jsstring(ar.name)}' (${lobject.jsstring(extramsg)})`)); }; @@ -160,13 +160,13 @@ const luaL_argerror = function(L, arg, extramsg) { const typeerror = function(L, arg, tname) { let typearg; if (luaL_getmetafield(L, arg, lua.to_luastring("__name", true)) === lua.LUA_TSTRING) - typearg = lapi.lua_tostring(L, -1); - else if (lapi.lua_type(L, arg) === lua.LUA_TLIGHTUSERDATA) + typearg = lua.lua_tostring(L, -1); + else if (lua.lua_type(L, arg) === lua.LUA_TLIGHTUSERDATA) typearg = lua.to_luastring("light userdata", true); else typearg = luaL_typename(L, arg); - let msg = lapi.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(tname)} expected, got ${lobject.jsstring(typearg)}`)); + let msg = lua.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(tname)} expected, got ${lobject.jsstring(typearg)}`)); return luaL_argerror(L, arg, msg); }; @@ -175,11 +175,11 @@ const luaL_where = function(L, level) { if (ldebug.lua_getstack(L, level, ar)) { ldebug.lua_getinfo(L, lua.to_luastring("Sl", true), ar); if (ar.currentline > 0) { - lapi.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(ar.short_src)}:${ar.currentline}:`)); + lua.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(ar.short_src)}:${ar.currentline}:`)); return; } } - lapi.lua_pushstring(L, []); + lua.lua_pushstring(L, []); }; const luaL_error = function(L, fmt, ...args) { @@ -192,24 +192,24 @@ const luaL_error = function(L, fmt, ...args) { }); fmt = lua.to_luastring(fmt); - lapi.lua_pushstring(L, fmt); + lua.lua_pushstring(L, fmt); - return lapi.lua_error(L); + return lua.lua_error(L); }; const tag_error = function(L, arg, tag) { - typeerror(L, arg, lapi.lua_typename(L, tag)); + typeerror(L, arg, lua.lua_typename(L, tag)); }; const luaL_newstate = function() { let L = lstate.lua_newstate(); - if (L) lapi.lua_atpanic(L, panic); + if (L) lua.lua_atpanic(L, panic); return L; }; const luaL_typename = function(L, i) { - return lapi.lua_typename(L, lapi.lua_type(L, i)); + return lua.lua_typename(L, lua.lua_type(L, i)); }; const luaL_argcheck = function(L, cond, arg, extramsg) { @@ -217,12 +217,12 @@ const luaL_argcheck = function(L, cond, arg, extramsg) { }; const luaL_checkany = function(L, arg) { - if (lapi.lua_type(L, arg) === lua.LUA_TNONE) + if (lua.lua_type(L, arg) === lua.LUA_TNONE) luaL_argerror(L, arg, lua.to_luastring("value expected", true)); }; const luaL_checktype = function(L, arg, t) { - if (lapi.lua_type(L, arg) !== t) + if (lua.lua_type(L, arg) !== t) tag_error(L, arg, t); }; @@ -231,13 +231,13 @@ const luaL_checkstring = function(L, n) { }; const luaL_checklstring = function(L, arg) { - let s = lapi.lua_tolstring(L, arg); + let s = lua.lua_tolstring(L, arg); if (s === null || s === undefined) tag_error(L, arg, lua.LUA_TSTRING); return s; }; const luaL_optlstring = function(L, arg, def) { - if (lapi.lua_type(L, arg) <= 0) { + if (lua.lua_type(L, arg) <= 0) { return def; } else return luaL_checklstring(L, arg); }; @@ -245,21 +245,21 @@ const luaL_optlstring = function(L, arg, def) { const luaL_optstring = luaL_optlstring; const interror = function(L, arg) { - if (lapi.lua_isnumber(L, arg)) + if (lua.lua_isnumber(L, arg)) luaL_argerror(L, arg, lua.to_luastring("number has no integer representation", true)); else tag_error(L, arg, lua.LUA_TNUMBER); }; const luaL_checknumber = function(L, arg) { - let d = lapi.lua_tonumber(L, arg); + let d = lua.lua_tonumber(L, arg); if (d === false) tag_error(L, arg, lua.LUA_TNUMBER); return d; }; const luaL_checkinteger = function(L, arg) { - let d = lapi.lua_tointeger(L, arg); + let d = lua.lua_tointeger(L, arg); if (d === false) interror(L, arg); return d; @@ -291,7 +291,7 @@ const luaL_addstring = luaL_addlstring; const luaL_pushresult = function(B) { let L = B.L; - lapi.lua_pushstring(L, B.b); + lua.lua_pushstring(L, B.b); }; const luaL_addchar = function(B, c) { @@ -300,14 +300,14 @@ const luaL_addchar = function(B, c) { const luaL_addvalue = function(B) { let L = B.L; - let s = lapi.lua_tostring(L, -1); + let s = lua.lua_tostring(L, -1); // TODO: buffonstack ? necessary ? luaL_addstring(B, s); - lapi.lua_remove(L, -1); + lua.lua_remove(L, -1); }; const luaL_opt = function(L, f, n, d) { - return lapi.lua_type(L, n) <= 0 ? d : f(L, n); + return lua.lua_type(L, n) <= 0 ? d : f(L, n); }; const getS = function(L, ud) { @@ -317,7 +317,7 @@ const getS = function(L, ud) { }; const luaL_loadbufferx = function(L, buff, size, name, mode) { - return lapi.lua_load(L, getS, {string: buff}, name, mode); + return lua.lua_load(L, getS, {string: buff}, name, mode); }; const luaL_loadbuffer = function(L, s, sz, n) { @@ -329,64 +329,64 @@ const luaL_loadstring = function(L, s) { }; const luaL_getmetafield = function(L, obj, event) { - if (!lapi.lua_getmetatable(L, obj)) + if (!lua.lua_getmetatable(L, obj)) return lua.LUA_TNIL; else { - lapi.lua_pushstring(L, event); - let tt = lapi.lua_rawget(L, -2); + lua.lua_pushstring(L, event); + let tt = lua.lua_rawget(L, -2); if (tt === lua.LUA_TNIL) - lapi.lua_pop(L, 2); + lua.lua_pop(L, 2); return tt; } }; const luaL_callmeta = function(L, obj, event) { - obj = lapi.lua_absindex(L, obj); + obj = lua.lua_absindex(L, obj); if (luaL_getmetafield(L, obj, event) === lua.LUA_TNIL) return false; - lapi.lua_pushvalue(L, obj); - lapi.lua_call(L, 1, 1); + lua.lua_pushvalue(L, obj); + lua.lua_call(L, 1, 1); return true; }; const luaL_len = function(L, idx) { - lapi.lua_len(L, idx); - let l = lapi.lua_tointegerx(L, -1); + lua.lua_len(L, idx); + let l = lua.lua_tointegerx(L, -1); if (l === false) luaL_error(L, lua.to_luastring("object length is not an integer", true)); - lapi.lua_pop(L, 1); /* remove object */ + lua.lua_pop(L, 1); /* remove object */ return l; }; const luaL_tolstring = function(L, idx) { if (luaL_callmeta(L, idx, lua.to_luastring("__tostring", true))) { - if (!lapi.lua_isstring(L, -1)) + if (!lua.lua_isstring(L, -1)) luaL_error(L, lua.to_luastring("'__tostring' must return a string", true)); } else { - switch(lapi.lua_type(L, idx)) { + switch(lua.lua_type(L, idx)) { case lua.LUA_TNUMBER: case lua.LUA_TBOOLEAN: - lapi.lua_pushstring(L, lua.to_luastring(`${lapi.index2addr(L, idx).value}`)); + lua.lua_pushstring(L, lua.to_luastring(`${lapi.index2addr(L, idx).value}`)); break; case lua.LUA_TSTRING: - lapi.lua_pushstring(L, lapi.index2addr(L, idx).value); + lua.lua_pushstring(L, lapi.index2addr(L, idx).value); break; case lua.LUA_TNIL: - lapi.lua_pushstring(L, lua.to_luastring(`nil`)); + lua.lua_pushstring(L, lua.to_luastring(`nil`)); break; default: let tt = luaL_getmetafield(L, idx, lua.to_luastring("__name", true)); - let kind = tt === lua.LUA_TSTRING ? lapi.lua_tostring(L, -1) : luaL_typename(L, idx); - lapi.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(kind)}: 0x${lapi.index2addr(L, -1).id.toString(16)}`)); + let kind = tt === lua.LUA_TSTRING ? lua.lua_tostring(L, -1) : luaL_typename(L, idx); + lua.lua_pushstring(L, lua.to_luastring(`${lobject.jsstring(kind)}: 0x${lapi.index2addr(L, -1).id.toString(16)}`)); if (tt !== lua.LUA_TNIL) - lapi.lua_remove(L, -2); + lua.lua_remove(L, -2); break; } } - return lapi.lua_tolstring(L, -1); + return lua.lua_tolstring(L, -1); }; /* @@ -397,19 +397,19 @@ const luaL_tolstring = function(L, idx) { */ const luaL_requiref = function(L, modname, openf, glb) { luaL_getsubtable(L, lua.LUA_REGISTRYINDEX, lua.to_luastring(LUA_LOADED_TABLE)); - lapi.lua_getfield(L, -1, modname); /* LOADED[modname] */ - if (!lapi.lua_toboolean(L, -1)) { /* package not already loaded? */ - lapi.lua_pop(L, 1); /* remove field */ - lapi.lua_pushcfunction(L, openf); - lapi.lua_pushstring(L, modname); /* argument to open function */ - lapi.lua_call(L, 1, 1); /* call 'openf' to open module */ - lapi.lua_pushvalue(L, -1); /* make copy of module (call result) */ - lapi.lua_setfield(L, -3, modname); /* LOADED[modname] = module */ + lua.lua_getfield(L, -1, modname); /* LOADED[modname] */ + if (!lua.lua_toboolean(L, -1)) { /* package not already loaded? */ + lua.lua_pop(L, 1); /* remove field */ + lua.lua_pushcfunction(L, openf); + lua.lua_pushstring(L, modname); /* argument to open function */ + lua.lua_call(L, 1, 1); /* call 'openf' to open module */ + lua.lua_pushvalue(L, -1); /* make copy of module (call result) */ + lua.lua_setfield(L, -3, modname); /* LOADED[modname] = module */ } - lapi.lua_remove(L, -2); /* remove LOADED table */ + lua.lua_remove(L, -2); /* remove LOADED table */ if (glb) { - lapi.lua_pushvalue(L, -1); /* copy of module */ - lapi.lua_setglobal(L, modname); /* _G[modname] = module */ + lua.lua_pushvalue(L, -1); /* copy of module */ + lua.lua_setglobal(L, modname); /* _G[modname] = module */ } }; @@ -418,14 +418,14 @@ const luaL_requiref = function(L, modname, openf, glb) { ** into the stack */ const luaL_getsubtable = function(L, idx, fname) { - if (lapi.lua_getfield(L, idx, fname) === lua.LUA_TTABLE) + if (lua.lua_getfield(L, idx, fname) === lua.LUA_TTABLE) return true; /* table already there */ else { - lapi.lua_pop(L, 1); /* remove previous result */ - idx = lapi.lua_absindex(L, idx); - lapi.lua_newtable(L); - lapi.lua_pushvalue(L, -1); /* copy to be left at top */ - lapi.lua_setfield(L, idx, fname); /* assign new table to field */ + lua.lua_pop(L, 1); /* remove previous result */ + idx = lua.lua_absindex(L, idx); + lua.lua_newtable(L); + lua.lua_pushvalue(L, -1); /* copy to be left at top */ + lua.lua_setfield(L, idx, fname); /* assign new table to field */ return false; /* false, because did not find table there */ } }; @@ -439,11 +439,11 @@ const luaL_setfuncs = function(L, l, nup) { luaL_checkstack(L, nup, lua.to_luastring("too many upvalues", true)); for (let lib in l) { /* fill the table with given functions */ for (let i = 0; i < nup; i++) /* copy upvalues to the top */ - lapi.lua_pushvalue(L, -nup); - lapi.lua_pushcclosure(L, l[lib], nup); /* closure with those upvalues */ - lapi.lua_setfield(L, -(nup + 2), lua.to_luastring(lib)); + lua.lua_pushvalue(L, -nup); + lua.lua_pushcclosure(L, l[lib], nup); /* closure with those upvalues */ + lua.lua_setfield(L, -(nup + 2), lua.to_luastring(lib)); } - lapi.lua_pop(L, nup); /* remove upvalues */ + lua.lua_pop(L, nup); /* remove upvalues */ }; /* @@ -454,7 +454,7 @@ const luaL_setfuncs = function(L, l, nup) { ** but without 'msg'.) */ const luaL_checkstack = function(L, space, msg) { - if (!lapi.lua_checkstack(L, space)) { + if (!lua.lua_checkstack(L, space)) { if (msg) luaL_error(L, lua.to_luastring(`stack overflow (${lobject.jsstring(msg)})`)); else @@ -463,7 +463,7 @@ const luaL_checkstack = function(L, space, msg) { }; const luaL_newlib = function(L, l) { - lapi.lua_createtable(L); + lua.lua_createtable(L); luaL_setfuncs(L, l, 0); }; @@ -510,9 +510,9 @@ if (typeof require === "function") { const errfile = function(L, what, fnameindex, error) { let serr = error.message; - let filename = lapi.lua_tostring(L, fnameindex).slice(1); - lapi.lua_pushstring(L, lua.to_luastring(`cannot ${what} ${lobject.jsstring(filename)}: ${serr}`)); - lapi.lua_remove(L, fnameindex); + let filename = lua.lua_tostring(L, fnameindex).slice(1); + lua.lua_pushstring(L, lua.to_luastring(`cannot ${what} ${lobject.jsstring(filename)}: ${serr}`)); + lua.lua_remove(L, fnameindex); return lua.LUA_ERRFILE; }; @@ -566,13 +566,13 @@ if (typeof require === "function") { const luaL_loadfilex = function(L, filename, mode) { let lf = new LoadF(); - let fnameindex = lapi.lua_gettop(L) + 1; /* index of filename on the stack */ + let fnameindex = lua.lua_gettop(L) + 1; /* index of filename on the stack */ if (filename === null) { - lapi.lua_pushliteral(L, "=stdin"); + lua.lua_pushliteral(L, "=stdin"); lf.f = process.stdin.fd; } else { let jsfilename = lobject.jsstring(filename); - lapi.lua_pushliteral(L, `@${jsfilename}`); + lua.lua_pushliteral(L, `@${jsfilename}`); try { lf.f = fs.openSync(jsfilename, "r"); } catch (e) { @@ -589,12 +589,12 @@ if (typeof require === "function") { lf.binary = true; } - let status = lapi.lua_load(L, getF, lf, lapi.lua_tostring(L, -1), mode); + let status = lua.lua_load(L, getF, lf, lua.lua_tostring(L, -1), mode); if (filename) fs.closeSync(lf.f); /* close file (even in case of errors) */ - lapi.lua_remove(L, fnameindex); + lua.lua_remove(L, fnameindex); return status; } catch (err) { - lapi.lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ + lua.lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ return errfile(L, "read", fnameindex, err); } }; diff --git a/src/lbaselib.js b/src/lbaselib.js index aa02908..a1a8925 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -3,25 +3,24 @@ const assert = require('assert'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const lobject = require('./lobject.js'); const luaB_print = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let str = []; - lapi.lua_getglobal(L, lua.to_luastring("tostring", true)); + lua.lua_getglobal(L, lua.to_luastring("tostring", true)); for (let i = 1; i <= n; i++) { - lapi.lua_pushvalue(L, -1); /* function to be called */ - lapi.lua_pushvalue(L, i); /* value to print */ - lapi.lua_call(L, 1, 1); - let s = lapi.lua_tolstring(L, -1); + lua.lua_pushvalue(L, -1); /* function to be called */ + lua.lua_pushvalue(L, i); /* value to print */ + lua.lua_call(L, 1, 1); + let s = lua.lua_tolstring(L, -1); if (s === null) return lauxlib.luaL_error(L, lua.to_luastring("'tostring' must return a string to 'print'", true)); if (i > 1) s = ["\t".charCodeAt(0)].concat(s); str = str.concat(s); - lapi.lua_pop(L, 1); + lua.lua_pop(L, 1); } // Don't use console.log if Node @@ -39,8 +38,8 @@ const luaB_tostring = function(L) { const luaB_getmetatable = function(L) { lauxlib.luaL_checkany(L, 1); - if (!lapi.lua_getmetatable(L, 1)) { - lapi.lua_pushnil(L); + if (!lua.lua_getmetatable(L, 1)) { + lua.lua_pushnil(L); return 1; /* no metatable */ } lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true)); @@ -48,35 +47,35 @@ const luaB_getmetatable = function(L) { }; const luaB_setmetatable = function(L) { - let t = lapi.lua_type(L, 2); + let t = lua.lua_type(L, 2); lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); lauxlib.luaL_argcheck(L, t === lua.LUA_TNIL || t === lua.LUA_TTABLE, 2, lua.to_luastring("nil or table expected", true)); if (lauxlib.luaL_getmetafield(L, 1, lua.to_luastring("__metatable", true)) !== lua.LUA_TNIL) return lauxlib.luaL_error(L, lua.to_luastring("cannot change a protected metatable", true)); - lapi.lua_settop(L, 2); - lapi.lua_setmetatable(L, 1); + lua.lua_settop(L, 2); + lua.lua_setmetatable(L, 1); return 1; }; const luaB_rawequal = function(L) { lauxlib.luaL_checkany(L, 1); lauxlib.luaL_checkany(L, 2); - lapi.lua_pushboolean(L, lapi.lua_rawequal(L, 1, 2)); + lua.lua_pushboolean(L, lua.lua_rawequal(L, 1, 2)); return 1; }; const luaB_rawlen = function(L) { - let t = lapi.lua_type(L, 1); + let t = lua.lua_type(L, 1); lauxlib.luaL_argcheck(L, t === lua.LUA_TTABLE || t === lua.LUA_TSTRING, 1, lua.to_luastring("table or string expected", true)); - lapi.lua_pushinteger(L, lapi.lua_rawlen(L, 1)); + lua.lua_pushinteger(L, lua.lua_rawlen(L, 1)); return 1; }; const luaB_rawget = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); lauxlib.luaL_checkany(L, 2); - lapi.lua_settop(L, 2); - lapi.lua_rawget(L, 1); + lua.lua_settop(L, 2); + lua.lua_rawget(L, 1); return 1; }; @@ -84,39 +83,39 @@ const luaB_rawset = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); lauxlib.luaL_checkany(L, 2); lauxlib.luaL_checkany(L, 3); - lapi.lua_settop(L, 3); - lapi.lua_rawset(L, 1); + lua.lua_settop(L, 3); + lua.lua_rawset(L, 1); return 1; }; const luaB_type = function(L) { - let t = lapi.lua_type(L, 1); + let t = lua.lua_type(L, 1); lauxlib.luaL_argcheck(L, t !== lua.LUA_TNONE, 1, lua.to_luastring("value expected", true)); - lapi.lua_pushstring(L, lapi.lua_typename(L, t)); + lua.lua_pushstring(L, lua.lua_typename(L, t)); return 1; }; const pairsmeta = function(L, method, iszero, iter) { lauxlib.luaL_checkany(L, 1); if (lauxlib.luaL_getmetafield(L, 1, method) === lua.LUA_TNIL) { /* no metamethod? */ - lapi.lua_pushcfunction(L, iter); /* will return generator, */ - lapi.lua_pushvalue(L, 1); /* state, */ - if (iszero) lapi.lua_pushinteger(L, 0); /* and initial value */ - else lapi.lua_pushnil(L); + lua.lua_pushcfunction(L, iter); /* will return generator, */ + lua.lua_pushvalue(L, 1); /* state, */ + if (iszero) lua.lua_pushinteger(L, 0); /* and initial value */ + else lua.lua_pushnil(L); } else { - lapi.lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lapi.lua_call(L, 1, 3); /* get 3 values from metamethod */ + lua.lua_pushvalue(L, 1); /* argument 'self' to metamethod */ + lua.lua_call(L, 1, 3); /* get 3 values from metamethod */ } return 3; }; const luaB_next = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); - lapi.lua_settop(L, 2); /* create a 2nd argument if there isn't one */ - if (lapi.lua_next(L, 1)) + lua.lua_settop(L, 2); /* create a 2nd argument if there isn't one */ + if (lua.lua_next(L, 1)) return 2; else { - lapi.lua_pushnil(L); + lua.lua_pushnil(L); return 1; } }; @@ -130,8 +129,8 @@ const luaB_pairs = function(L) { */ const ipairsaux = function(L) { let i = lauxlib.luaL_checkinteger(L, 2) + 1; - lapi.lua_pushinteger(L, i); - return lapi.lua_geti(L, 1, i) === lua.LUA_TNIL ? 1 : 2; + lua.lua_pushinteger(L, i); + return lua.lua_geti(L, 1, i) === lua.LUA_TNIL ? 1 : 2; }; /* @@ -143,66 +142,66 @@ const luaB_ipairs = function(L) { // return pairsmeta(L, "__ipairs", 1, ipairsaux); lauxlib.luaL_checkany(L, 1); - lapi.lua_pushcfunction(L, ipairsaux); /* iteration function */ - lapi.lua_pushvalue(L, 1); /* state */ - lapi.lua_pushinteger(L, 0); /* initial value */ + lua.lua_pushcfunction(L, ipairsaux); /* iteration function */ + lua.lua_pushvalue(L, 1); /* state */ + lua.lua_pushinteger(L, 0); /* initial value */ return 3; }; const luaB_tonumber = function(L) { - if (lapi.lua_type(L, 2) <= 0) { /* standard conversion? */ + if (lua.lua_type(L, 2) <= 0) { /* standard conversion? */ lauxlib.luaL_checkany(L, 1); - if (lapi.lua_type(L, 1) === lua.LUA_TNUMBER) { /* already a number? */ - lapi.lua_settop(L, 1); + if (lua.lua_type(L, 1) === lua.LUA_TNUMBER) { /* already a number? */ + lua.lua_settop(L, 1); return 1; } else { - let s = lapi.lua_tostring(L, 1); - if (s !== null && lapi.lua_stringtonumber(L, s) === s.length) + let s = lua.lua_tostring(L, 1); + if (s !== null && lua.lua_stringtonumber(L, s) === s.length) return 1; /* successful conversion to number */ } } else { let base = lauxlib.luaL_checkinteger(L, 2); lauxlib.luaL_checktype(L, 1, lua.LUA_TSTRING); /* no numbers as strings */ - let s = lapi.lua_tostring(L, 1); + let s = lua.lua_tostring(L, 1); lauxlib.luaL_argcheck(L, 2 <= base && base <= 36, 2, lua.to_luastring("base out of range", true)); let n = parseInt(lobject.jsstring(s), base); if (!isNaN(n)) { - lapi.lua_pushinteger(L, n); + lua.lua_pushinteger(L, n); return 1; } } - lapi.lua_pushnil(L); + lua.lua_pushnil(L); return 1; }; const luaB_error = function(L) { let level = lauxlib.luaL_optinteger(L, 2, 1); - lapi.lua_settop(L, 1); - if (lapi.lua_type(L, 1) === lua.LUA_TSTRING && level > 0) { + lua.lua_settop(L, 1); + if (lua.lua_type(L, 1) === lua.LUA_TSTRING && level > 0) { lauxlib.luaL_where(L, level); /* add extra information */ - lapi.lua_pushvalue(L, 1); - lapi.lua_concat(L, 2); + lua.lua_pushvalue(L, 1); + lua.lua_concat(L, 2); } - return lapi.lua_error(L); + return lua.lua_error(L); }; const luaB_assert = function(L) { - if (lapi.lua_toboolean(L, 1)) /* condition is true? */ - return lapi.lua_gettop(L); /* return all arguments */ + if (lua.lua_toboolean(L, 1)) /* condition is true? */ + return lua.lua_gettop(L); /* return all arguments */ else { lauxlib.luaL_checkany(L, 1); /* there must be a condition */ - lapi.lua_remove(L, 1); /* remove it */ - lapi.lua_pushliteral(L, "assertion failed!"); /* default message */ - lapi.lua_settop(L, 1); /* leave only message (default if no other one) */ + lua.lua_remove(L, 1); /* remove it */ + lua.lua_pushliteral(L, "assertion failed!"); /* default message */ + lua.lua_settop(L, 1); /* leave only message (default if no other one) */ return luaB_error(L); /* call 'error' */ } }; const luaB_select = function(L) { - let n = lapi.lua_gettop(L); - if (lapi.lua_type(L, 1) === lua.LUA_TSTRING && lapi.lua_tostring(L, 1)[0] === "#".charCodeAt(0)) { - lapi.lua_pushinteger(L, n - 1); + let n = lua.lua_gettop(L); + if (lua.lua_type(L, 1) === lua.LUA_TSTRING && lua.lua_tostring(L, 1)[0] === "#".charCodeAt(0)) { + lua.lua_pushinteger(L, n - 1); return 1; } else { let i = lauxlib.luaL_checkinteger(L, 1); @@ -222,18 +221,18 @@ const luaB_select = function(L) { */ const finishpcall = function(L, status, extra) { if (status !== lua.LUA_OK && status !== lua.LUA_YIELD) { /* error? */ - lapi.lua_pushboolean(L, 0); /* first result (false) */ - lapi.lua_pushvalue(L, -2); /* error message */ + lua.lua_pushboolean(L, 0); /* first result (false) */ + lua.lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ } else - return lapi.lua_gettop(L) - extra; + return lua.lua_gettop(L) - extra; }; const luaB_pcall = function(L) { lauxlib.luaL_checkany(L, 1); - lapi.lua_pushboolean(L, 1); /* first result if no errors */ - lapi.lua_insert(L, 1); /* put it in place */ - let status = lapi.lua_pcallk(L, lapi.lua_gettop(L) - 2, lua.LUA_MULTRET, 0, 0, finishpcall); + lua.lua_pushboolean(L, 1); /* first result if no errors */ + lua.lua_insert(L, 1); /* put it in place */ + let status = lua.lua_pcallk(L, lua.lua_gettop(L) - 2, lua.LUA_MULTRET, 0, 0, finishpcall); return finishpcall(L, status, 0); }; @@ -243,12 +242,12 @@ const luaB_pcall = function(L) { ** 2 to 'finishpcall' to skip the 2 first values when returning results. */ const luaB_xpcall = function(L) { - let n = lapi.lua_gettop(L); + let n = lua.lua_gettop(L); lauxlib.luaL_checktype(L, 2, lua.LUA_TFUNCTION); /* check error function */ - lapi.lua_pushboolean(L, 1); /* first result */ - lapi.lua_pushvalue(L, 1); /* function */ - lapi.lua_rotate(L, 3, 2); /* move them below function's arguments */ - let status = lapi.lua_pcallk(L, n - 2, lua.LUA_MULTRET, 2, 2, finishpcall); + lua.lua_pushboolean(L, 1); /* first result */ + lua.lua_pushvalue(L, 1); /* function */ + lua.lua_rotate(L, 3, 2); /* move them below function's arguments */ + let status = lua.lua_pcallk(L, n - 2, lua.LUA_MULTRET, 2, 2, finishpcall); return finishpcall(L, status, 2); }; @@ -256,14 +255,14 @@ const luaB_xpcall = function(L) { const load_aux = function(L, status, envidx) { if (status === lua.LUA_OK) { if (envidx !== 0) { /* 'env' parameter? */ - lapi.lua_pushvalue(L, envidx); /* environment for loaded function */ - if (!lapi.lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ - lapi.lua_pop(L, 1); /* remove 'env' if not used by previous call */ + lua.lua_pushvalue(L, envidx); /* environment for loaded function */ + if (!lua.lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ + lua.lua_pop(L, 1); /* remove 'env' if not used by previous call */ } return 1; } else { /* error (message is on top of the stack) */ - lapi.lua_pushnil(L); - lapi.lua_insert(L, -2); /* put before error message */ + lua.lua_pushnil(L); + lua.lua_insert(L, -2); /* put before error message */ return 2; /* return nil plus error message */ } }; @@ -283,21 +282,21 @@ const RESERVEDSLOT = 5; */ const generic_reader = function(L, ud) { lauxlib.luaL_checkstack(L, 2, lua.to_luastring("too many nested functions", true)); - lapi.lua_pushvalue(L, 1); /* get function */ - lapi.lua_call(L, 0, 1); /* call it */ - if (lapi.lua_isnil(L, -1)) { - lapi.lua_pop(L, 1); /* pop result */ + lua.lua_pushvalue(L, 1); /* get function */ + lua.lua_call(L, 0, 1); /* call it */ + if (lua.lua_isnil(L, -1)) { + lua.lua_pop(L, 1); /* pop result */ return null; - } else if (!lapi.lua_isstring(L, -1)) + } else if (!lua.lua_isstring(L, -1)) lauxlib.luaL_error(L, lua.to_luastring("reader function must return a string", true)); - lapi.lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ - return lapi.lua_tostring(L, RESERVEDSLOT); + lua.lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ + return lua.lua_tostring(L, RESERVEDSLOT); }; const luaB_load = function(L) { - let s = lapi.lua_tostring(L, 1); + let s = lua.lua_tostring(L, 1); let mode = lauxlib.luaL_optstring(L, 3, lua.to_luastring("bt", true)); - let env = !lapi.lua_isnone(L, 4) ? 4 : 0; /* 'env' index or 0 if no 'env' */ + let env = !lua.lua_isnone(L, 4) ? 4 : 0; /* 'env' index or 0 if no 'env' */ let status; if (s !== null) { /* loading a string? */ let chunkname = lauxlib.luaL_optstring(L, 2, s); @@ -305,8 +304,8 @@ const luaB_load = function(L) { } else { /* loading from a reader function */ let chunkname = lauxlib.luaL_optstring(L, 2, lua.to_luastring("=(load)", true)); lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION); - lapi.lua_settop(L, RESERVEDSLOT); /* create reserved slot */ - status = lapi.lua_load(L, generic_reader, null, chunkname, mode); + lua.lua_settop(L, RESERVEDSLOT); /* create reserved slot */ + status = lua.lua_load(L, generic_reader, null, chunkname, mode); } return load_aux(L, status, env); }; @@ -346,21 +345,21 @@ if (typeof require === "function") { const luaB_loadfile = function(L) { let fname = lauxlib.luaL_optstring(L, 1, null); let mode = lauxlib.luaL_optstring(L, 2, null); - let env = !lapi.lua_isnone(L, 3) ? 3 : 0; /* 'env' index or 0 if no 'env' */ + let env = !lua.lua_isnone(L, 3) ? 3 : 0; /* 'env' index or 0 if no 'env' */ let status = lauxlib.luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); }; const dofilecont = function(L, d1, d2) { - return lapi.lua_gettop(L) - 1; + return lua.lua_gettop(L) - 1; }; const luaB_dofile = function(L) { let fname = lauxlib.luaL_optstring(L, 1, null); - lapi.lua_settop(L, 1); + lua.lua_settop(L, 1); if (lauxlib.luaL_loadfile(L, fname) !== lua.LUA_OK) - return lapi.lua_error(L); - lapi.lua_callk(L, 0, lua.LUA_MULTRET, 0, dofilecont); + return lua.lua_error(L); + lua.lua_callk(L, 0, lua.LUA_MULTRET, 0, dofilecont); return dofilecont(L, 0, 0); }; @@ -372,14 +371,14 @@ if (typeof require === "function") { const luaopen_base = function(L) { /* open lib into global table */ - lapi.lua_pushglobaltable(L); + lua.lua_pushglobaltable(L); lauxlib.luaL_setfuncs(L, base_funcs, 0); /* set global _G */ - lapi.lua_pushvalue(L, -1); - lapi.lua_setfield(L, -2, lua.to_luastring("_G", true)); + lua.lua_pushvalue(L, -1); + lua.lua_setfield(L, -2, lua.to_luastring("_G", true)); /* set global _VERSION */ - lapi.lua_pushliteral(L, lua.LUA_VERSION); - lapi.lua_setfield(L, -2, lua.to_luastring("_VERSION", true)); + lua.lua_pushliteral(L, lua.LUA_VERSION); + lua.lua_setfield(L, -2, lua.to_luastring("_VERSION", true)); return 1; }; diff --git a/src/lcorolib.js b/src/lcorolib.js index 98eebac..9c05e40 100644 --- a/src/lcorolib.js +++ b/src/lcorolib.js @@ -3,7 +3,6 @@ const assert = require('assert'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const lstate = require('./lstate.js'); const ldo = require('./ldo.js'); @@ -11,65 +10,65 @@ const ldebug = require('./ldebug.js'); const lobject = require('./lobject.js'); const getco = function(L) { - let co = lapi.lua_tothread(L, 1); + let co = lua.lua_tothread(L, 1); lauxlib.luaL_argcheck(L, co, 1, lua.to_luastring("thread expected", true)); return co; }; const auxresume = function(L, co, narg) { - if (!lapi.lua_checkstack(co, narg)) { - lapi.lua_pushliteral(L, "too many arguments to resume"); + if (!lua.lua_checkstack(co, narg)) { + lua.lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } - if (lapi.lua_status(co) === lua.LUA_OK && lapi.lua_gettop(co) === 0) { - lapi.lua_pushliteral(L, "cannot resume dead coroutine"); + if (lua.lua_status(co) === lua.LUA_OK && lua.lua_gettop(co) === 0) { + lua.lua_pushliteral(L, "cannot resume dead coroutine"); return -1; /* error flag */ } - lapi.lua_xmove(L, co, narg); + lua.lua_xmove(L, co, narg); let status = ldo.lua_resume(co, L, narg); if (status === lua.LUA_OK || status === lua.LUA_YIELD) { - let nres = lapi.lua_gettop(co); - if (!lapi.lua_checkstack(L, nres + 1)) { - lapi.lua_pop(co, nres); /* remove results anyway */ - lapi.lua_pushliteral(L, "too many results to resume"); + let nres = lua.lua_gettop(co); + if (!lua.lua_checkstack(L, nres + 1)) { + lua.lua_pop(co, nres); /* remove results anyway */ + lua.lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ } - lapi.lua_xmove(co, L, nres); /* move yielded values */ + lua.lua_xmove(co, L, nres); /* move yielded values */ return nres; } else { - lapi.lua_xmove(co, L, 1); /* move error message */ + lua.lua_xmove(co, L, 1); /* move error message */ return -1; /* error flag */ } }; const luaB_coresume = function(L) { let co = getco(L); - let r = auxresume(L, co, lapi.lua_gettop(L) - 1); + let r = auxresume(L, co, lua.lua_gettop(L) - 1); if (r < 0) { - lapi.lua_pushboolean(L, 0); - lapi.lua_insert(L, -2); + lua.lua_pushboolean(L, 0); + lua.lua_insert(L, -2); return 2; /* return false + error message */ } else { - lapi.lua_pushboolean(L, 1); - lapi.lua_insert(L, -(r + 1)); + lua.lua_pushboolean(L, 1); + lua.lua_insert(L, -(r + 1)); return r + 1; /* return true + 'resume' returns */ } }; const luaB_auxwrap = function(L) { - let co = lapi.lua_tothread(L, lua.lua_upvalueindex(1)); - let r = auxresume(L, co, lapi.lua_gettop(L)); + let co = lua.lua_tothread(L, lua.lua_upvalueindex(1)); + let r = auxresume(L, co, lua.lua_gettop(L)); if (r < 0) { - if (lapi.lua_type(L, -1) === lua.LUA_TSTRING) { /* error object is a string? */ + if (lua.lua_type(L, -1) === lua.LUA_TSTRING) { /* error object is a string? */ lauxlib.luaL_where(L, 1); /* add extra info */ - lapi.lua_insert(L, -2); - lapi.lua_concat(L, 2); + lua.lua_insert(L, -2); + lua.lua_concat(L, 2); } - return lapi.lua_error(L); /* propagate error */ + return lua.lua_error(L); /* propagate error */ } return r; @@ -78,41 +77,41 @@ const luaB_auxwrap = function(L) { const luaB_cocreate = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION); let NL = lstate.lua_newthread(L); - lapi.lua_pushvalue(L, 1); /* move function to top */ - lapi.lua_xmove(L, NL, 1); /* move function from L to NL */ + lua.lua_pushvalue(L, 1); /* move function to top */ + lua.lua_xmove(L, NL, 1); /* move function from L to NL */ return 1; }; const luaB_cowrap = function(L) { luaB_cocreate(L); - lapi.lua_pushcclosure(L, luaB_auxwrap, 1); + lua.lua_pushcclosure(L, luaB_auxwrap, 1); return 1; }; const luaB_yield = function(L) { - return ldo.lua_yield(L, lapi.lua_gettop(L)); + return ldo.lua_yield(L, lua.lua_gettop(L)); }; const luaB_costatus = function(L) { let co = getco(L); - if (L === co) lapi.lua_pushliteral(L, "running"); + if (L === co) lua.lua_pushliteral(L, "running"); else { - switch (lapi.lua_status(co)) { + switch (lua.lua_status(co)) { case lua.LUA_YIELD: - lapi.lua_pushliteral(L, "suspended"); + lua.lua_pushliteral(L, "suspended"); break; case lua.LUA_OK: { let ar = new lua.lua_Debug(); if (ldebug.lua_getstack(co, 0, ar) > 0) /* does it have frames? */ - lapi.lua_pushliteral(L, "normal"); /* it is running */ - else if (lapi.lua_gettop(co) === 0) - lapi.lua_pushliteral(L, "dead"); + lua.lua_pushliteral(L, "normal"); /* it is running */ + else if (lua.lua_gettop(co) === 0) + lua.lua_pushliteral(L, "dead"); else - lapi.lua_pushliteral(L, "suspended"); /* initial state */ + lua.lua_pushliteral(L, "suspended"); /* initial state */ break; } default: /* some error occurred */ - lapi.lua_pushliteral(L, "dead"); + lua.lua_pushliteral(L, "dead"); break; } } @@ -121,12 +120,12 @@ const luaB_costatus = function(L) { }; const luaB_yieldable = function(L) { - lapi.lua_pushboolean(L, ldo.lua_isyieldable(L)); + lua.lua_pushboolean(L, ldo.lua_isyieldable(L)); return 1; }; const luaB_corunning = function(L) { - lapi.lua_pushboolean(L, lapi.lua_pushthread(L)); + lua.lua_pushboolean(L, lua.lua_pushthread(L)); return 2; }; diff --git a/src/ldblib.js b/src/ldblib.js index 62422a8..c5ef8d4 100644 --- a/src/ldblib.js +++ b/src/ldblib.js @@ -3,7 +3,6 @@ const assert = require('assert'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const ldebug = require('./ldebug.js'); @@ -13,36 +12,36 @@ const ldebug = require('./ldebug.js'); ** checked. */ const checkstack = function(L, L1, n) { - if (L !== L1 && !lapi.lua_checkstack(L1, n)) + if (L !== L1 && !lua.lua_checkstack(L1, n)) lauxlib.luaL_error(L, lua.to_luastring("stack overflow", true)); }; const db_getregistry = function(L) { - lapi.lua_pushvalue(L, lua.LUA_REGISTRYINDEX); + lua.lua_pushvalue(L, lua.LUA_REGISTRYINDEX); return 1; }; const db_getmetatable = function(L) { lauxlib.luaL_checkany(L, 1); - if (!lapi.lua_getmetatable(L, 1)) { - lapi.lua_pushnil(L); /* no metatable */ + if (!lua.lua_getmetatable(L, 1)) { + lua.lua_pushnil(L); /* no metatable */ } return 1; }; const db_setmetatable = function(L) { - const t = lapi.lua_type(L, 2); + const t = lua.lua_type(L, 2); lauxlib.luaL_argcheck(L, t == lua.LUA_TNIL || t == lua.LUA_TTABLE, 2, lua.to_luastring("nil or table expected", true)); - lapi.lua_settop(L, 2); - lapi.lua_setmetatable(L, 1); + lua.lua_settop(L, 2); + lua.lua_setmetatable(L, 1); return 1; /* return 1st argument */ }; const db_getuservalue = function(L) { - if (lapi.lua_type(L, 1) !== lua.LUA_TUSERDATA) - lapi.lua_pushnil(L); + if (lua.lua_type(L, 1) !== lua.LUA_TUSERDATA) + lua.lua_pushnil(L); else - lapi.lua_getuservalue(L, 1); + lua.lua_getuservalue(L, 1); return 1; }; @@ -50,8 +49,8 @@ const db_getuservalue = function(L) { const db_setuservalue = function(L) { lauxlib.luaL_checktype(L, 1, lua.LUA_TUSERDATA); lauxlib.luaL_checkany(L, 2); - lapi.lua_settop(L, 2); - lapi.lua_setuservalue(L, 1); + lua.lua_settop(L, 2); + lua.lua_setuservalue(L, 1); return 1; }; @@ -62,10 +61,10 @@ const db_setuservalue = function(L) { ** access their other arguments) */ const getthread = function(L) { - if (lapi.lua_isthread(L, 1)) { + if (lua.lua_isthread(L, 1)) { return { arg: 1, - thread: lapi.lua_tothread(L, 1) + thread: lua.lua_tothread(L, 1) }; } else { return { @@ -81,18 +80,18 @@ const getthread = function(L) { ** value can be a string, an int, or a boolean. */ const settabss = function(L, k, v) { - lapi.lua_pushstring(L, v); - lapi.lua_setfield(L, -2, k); + lua.lua_pushstring(L, v); + lua.lua_setfield(L, -2, k); }; const settabsi = function(L, k, v) { - lapi.lua_pushinteger(L, v); - lapi.lua_setfield(L, -2, k); + lua.lua_pushinteger(L, v); + lua.lua_setfield(L, -2, k); }; const settabsb = function(L, k, v) { - lapi.lua_pushboolean(L, v); - lapi.lua_setfield(L, -2, k); + lua.lua_pushboolean(L, v); + lua.lua_setfield(L, -2, k); }; @@ -105,10 +104,10 @@ const settabsb = function(L, k, v) { */ const treatstackoption = function(L, L1, fname) { if (L == L1) - lapi.lua_rotate(L, -2, 1); /* exchange object and table */ + lua.lua_rotate(L, -2, 1); /* exchange object and table */ else - lapi.lua_xmove(L1, L, 1); /* move object to the "main" stack */ - lapi.lua_setfield(L, -2, fname); /* put object into table */ + lua.lua_xmove(L1, L, 1); /* move object to the "main" stack */ + lua.lua_setfield(L, -2, fname); /* put object into table */ }; /* @@ -124,20 +123,20 @@ const db_getinfo = function(L) { let L1 = thread.thread; let options = lauxlib.luaL_optstring(L, arg + 2, lua.to_luastring("flnStu", true)); checkstack(L, L1, 3); - if (lapi.lua_isfunction(L, arg + 1)) { /* info about a function? */ + if (lua.lua_isfunction(L, arg + 1)) { /* info about a function? */ options = ['>'.charCodeAt(0)].concat(options); /* add '>' to 'options' */ - lapi.lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ - lapi.lua_xmove(L, L1, 1); + lua.lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ + lua.lua_xmove(L, L1, 1); } else { /* stack level */ if (!ldebug.lua_getstack(L1, lauxlib.luaL_checkinteger(L, arg + 1), ar)) { - lapi.lua_pushnil(L); /* level out of range */ + lua.lua_pushnil(L); /* level out of range */ return 1; } } if (!ldebug.lua_getinfo(L1, options, ar)) lauxlib.luaL_argerror(L, arg + 2, lua.to_luastring("invalid option", true)); - lapi.lua_newtable(L); /* table to collect results */ + lua.lua_newtable(L); /* table to collect results */ if (options.indexOf('S'.charCodeAt(0)) > -1) { settabss(L, lua.to_luastring("source", true), ar.source.value); settabss(L, lua.to_luastring("short_src", true), ar.short_src); @@ -170,24 +169,24 @@ const db_getlocal = function(L) { let arg = thread.arg; let ar = new lua.lua_Debug(); let nvar = lauxlib.luaL_checkinteger(L, arg + 2); /* local-variable index */ - if (lapi.lua_isfunction(L, arg + 1)) { - lapi.lua_pushvalue(L, arg + 1); /* push function */ - lapi.lua_pushstring(L, ldebug.lua_getlocal(L, null, nvar)); /* push local name */ + if (lua.lua_isfunction(L, arg + 1)) { + lua.lua_pushvalue(L, arg + 1); /* push function */ + lua.lua_pushstring(L, ldebug.lua_getlocal(L, null, nvar)); /* push local name */ return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ let level = lauxlib.luaL_checkinteger(L, arg + 1); if (!ldebug.lua_getstack(L1, level, ar)) /* out of range? */ - return lauxlib.luaL_argerror(L, arg+1, lapi.to_luastring("level out of range", true)); + return lauxlib.luaL_argerror(L, arg+1, lua.to_luastring("level out of range", true)); checkstack(L, L1, 1); let name = ldebug.lua_getlocal(L1, ar, nvar); if (name) { - lapi.lua_xmove(L1, L, 1); /* move local value */ - lapi.lua_pushstring(L, name.value); /* push name */ - lapi.lua_rotate(L, -2, 1); /* re-order */ + lua.lua_xmove(L1, L, 1); /* move local value */ + lua.lua_pushstring(L, name.value); /* push name */ + lua.lua_rotate(L, -2, 1); /* re-order */ return 2; } else { - lapi.lua_pushnil(L); /* no name (nor value) */ + lua.lua_pushnil(L); /* no name (nor value) */ return 1; } } @@ -203,13 +202,13 @@ const db_setlocal = function(L) { if (!ldebug.lua_getstack(L1, level, ar)) /* out of range? */ return lauxlib.luaL_argerror(L, arg + 1, "level out of range"); lauxlib.luaL_checkany(L, arg + 3); - lapi.lua_settop(L, arg + 3); + lua.lua_settop(L, arg + 3); checkstack(L, L1, 1); - lapi.lua_xmove(L, L1, 1); + lua.lua_xmove(L, L1, 1); let name = ldebug.lua_setlocal(L1, ar, nvar); if (name === null) - lapi.lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ - lapi.lua_pushstring(L, name.value); + lua.lua_pop(L1, 1); /* pop value (if not popped by 'lua_setlocal') */ + lua.lua_pushstring(L, name.value); return 1; }; @@ -219,10 +218,10 @@ const db_setlocal = function(L) { const auxupvalue = function(L, get) { let n = lauxlib.luaL_checkinteger(L, 2); /* upvalue index */ lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION); /* closure */ - let name = get ? lapi.lua_getupvalue(L, 1, n) : lapi.lua_setupvalue(L, 1, n); + let name = get ? lua.lua_getupvalue(L, 1, n) : lua.lua_setupvalue(L, 1, n); if (name === null) return 0; - lapi.lua_pushstring(L, name); - lapi.lua_insert(L, -(get+1)); /* no-op if get is false */ + lua.lua_pushstring(L, name); + lua.lua_insert(L, -(get+1)); /* no-op if get is false */ return get + 1; }; @@ -243,22 +242,22 @@ const db_setupvalue = function(L) { const checkupval = function(L, argf, argnup) { let nup = lauxlib.luaL_checkinteger(L, argnup); /* upvalue index */ lauxlib.luaL_checktype(L, argf, lua.LUA_TFUNCTION); /* closure */ - lauxlib.luaL_argcheck(L, (lapi.lua_getupvalue(L, argf, nup) !== null), argnup, lua.to_luastring("invalid upvalue index", true)); + lauxlib.luaL_argcheck(L, (lua.lua_getupvalue(L, argf, nup) !== null), argnup, lua.to_luastring("invalid upvalue index", true)); return nup; }; const db_upvalueid = function(L) { let n = checkupval(L, 1, 2); - lapi.lua_pushlightuserdata(L, lapi.lua_upvalueid(L, 1, n)); + lua.lua_pushlightuserdata(L, lua.lua_upvalueid(L, 1, n)); return 1; }; const db_upvaluejoin = function(L) { let n1 = checkupval(L, 1, 2); let n2 = checkupval(L, 3, 4); - lauxlib.luaL_argcheck(L, !lapi.lua_iscfunction(L, 1), 1, lua.to_luastring("Lua function expected", true)); - lauxlib.luaL_argcheck(L, !lapi.lua_iscfunction(L, 3), 3, lua.to_luastring("Lua function expected", true)); - lapi.lua_upvaluejoin(L, 1, n1, 3, n2); + lauxlib.luaL_argcheck(L, !lua.lua_iscfunction(L, 1), 1, lua.to_luastring("Lua function expected", true)); + lauxlib.luaL_argcheck(L, !lua.lua_iscfunction(L, 3), 3, lua.to_luastring("Lua function expected", true)); + lua.lua_upvaluejoin(L, 1, n1, 3, n2); return 0; }; @@ -275,15 +274,15 @@ const hooknames = ["call", "return", "line", "count", "tail call"].map(e => lua. ** thread (if there is one) */ const hookf = function(L, ar) { - lapi.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); - lapi.lua_pushthread(L); - if (lapi.lua_rawget(L, -2) === lua.LUA_TFUNCTION) { /* is there a hook function? */ - lapi.lua_pushstring(L, hooknames[ar.event]); /* push event name */ + lua.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); + lua.lua_pushthread(L); + if (lua.lua_rawget(L, -2) === lua.LUA_TFUNCTION) { /* is there a hook function? */ + lua.lua_pushstring(L, hooknames[ar.event]); /* push event name */ if (ar.currentline >= 0) - lapi.lua_pushinteger(L, ar.currentline); /* push current line */ - else lapi.lua_pushnil(L); + lua.lua_pushinteger(L, ar.currentline); /* push current line */ + else lua.lua_pushnil(L); assert(ldebug.lua_getinfo(L, ["l".charCodeAt(0), "S".charCodeAt(0)], ar)); - lapi.lua_call(L, 2, 0); /* call hook function */ + lua.lua_call(L, 2, 0); /* call hook function */ } }; @@ -315,8 +314,8 @@ const db_sethook = function(L) { let thread = getthread(L); let L1 = thread.thread; let arg = thread.arg; - if (lapi.lua_isnoneornil(L, arg+1)) { /* no hook? */ - lapi.lua_settop(L, arg+1); + if (lua.lua_isnoneornil(L, arg+1)) { /* no hook? */ + lua.lua_settop(L, arg+1); func = null; mask = 0; count = 0; /* turn off hooks */ } else { @@ -325,19 +324,19 @@ const db_sethook = function(L) { count = lauxlib.luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } - if (lapi.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY) === lua.LUA_TNIL) { - lapi.lua_createtable(L, 0, 2); /* create a hook table */ - lapi.lua_pushvalue(L, -1); - lapi.lua_rawsetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); /* set it in position */ - lapi.lua_pushstring(L, ["k".charCodeAt(0)]); - lapi.lua_setfield(L, -2, lua.to_luastring("__mode", true)); /** hooktable.__mode = "k" */ - lapi.lua_pushvalue(L, -1); - lapi.lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + if (lua.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY) === lua.LUA_TNIL) { + lua.lua_createtable(L, 0, 2); /* create a hook table */ + lua.lua_pushvalue(L, -1); + lua.lua_rawsetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); /* set it in position */ + lua.lua_pushstring(L, ["k".charCodeAt(0)]); + lua.lua_setfield(L, -2, lua.to_luastring("__mode", true)); /** hooktable.__mode = "k" */ + lua.lua_pushvalue(L, -1); + lua.lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ } checkstack(L, L1, 1); - lapi.lua_pushthread(L1); lapi.lua_xmove(L1, L, 1); /* key (thread) */ - lapi.lua_pushvalue(L, arg + 1); /* value (hook function) */ - lapi.lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ + lua.lua_pushthread(L1); lua.lua_xmove(L1, L, 1); /* key (thread) */ + lua.lua_pushvalue(L, arg + 1); /* value (hook function) */ + lua.lua_rawset(L, -3); /* hooktable[L1] = new Lua hook */ ldebug.lua_sethook(L1, func, mask, count); return 0; }; @@ -350,18 +349,18 @@ const db_gethook = function(L) { let mask = ldebug.lua_gethookmask(L1); let hook = ldebug.lua_gethook(L1); if (hook === null) /* no hook? */ - lapi.lua_pushnil(L); + lua.lua_pushnil(L); else if (hook !== hookf) /* external hook? */ - lapi.lua_pushliteral(L, "external hook"); + lua.lua_pushliteral(L, "external hook"); else { /* hook table must exist */ - lapi.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); + lua.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); checkstack(L, L1, 1); - lapi.lua_pushthread(L1); lapi.lua_xmove(L1, L, 1); - lapi.lua_rawget(L, -2); /* 1st result = hooktable[L1] */ - lapi.lua_remove(L, -2); /* remove hook table */ + lua.lua_pushthread(L1); lua.lua_xmove(L1, L, 1); + lua.lua_rawget(L, -2); /* 1st result = hooktable[L1] */ + lua.lua_remove(L, -2); /* remove hook table */ } - lapi.lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ - lapi.lua_pushinteger(L, ldebug.lua_gethookcount(L1)); /* 3rd result = count */ + lua.lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ + lua.lua_pushinteger(L, ldebug.lua_gethookcount(L1)); /* 3rd result = count */ return 3; }; @@ -369,9 +368,9 @@ const db_traceback = function(L) { let thread = getthread(L); let L1 = thread.thread; let arg = thread.arg; - let msg = lapi.lua_tostring(L, arg + 1); - if (msg === null && !lapi.lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ - lapi.lua_pushvalue(L, arg + 1); /* return it untouched */ + let msg = lua.lua_tostring(L, arg + 1); + if (msg === null && !lua.lua_isnoneornil(L, arg + 1)) /* non-string 'msg'? */ + lua.lua_pushvalue(L, arg + 1); /* return it untouched */ else { let level = lauxlib.luaL_optinteger(L, arg + 2, L === L1 ? 1 : 0); lauxlib.luaL_traceback(L, L1, msg, level); @@ -423,10 +422,10 @@ if (typeof require === "function") { let buffer = lua.to_luastring(input); if (lauxlib.luaL_loadbuffer(L, buffer, buffer.length, lua.to_luastring("=(debug command)", true)) - || lapi.lua_pcall(L, 0, 0, 0)) { - lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`); + || lua.lua_pcall(L, 0, 0, 0)) { + lauxlib.lua_writestringerror(`${lua.lua_tojsstring(L, -1)}\n`); } - lapi.lua_settop(L, 0); /* remove eventual returns */ + lua.lua_settop(L, 0); /* remove eventual returns */ } }; diff --git a/src/lmathlib.js b/src/lmathlib.js index 9603d2e..4f323c6 100644 --- a/src/lmathlib.js +++ b/src/lmathlib.js @@ -4,7 +4,6 @@ const assert = require('assert'); const seedrandom = require('seedrandom'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const lstate = require('./lstate.js'); const ldo = require('./ldo.js'); @@ -21,9 +20,9 @@ const math_randomseed = function(L) { const math_random = function(L) { let low, up; let r = RNG(); - switch (lapi.lua_gettop(L)) { /* check number of arguments */ + switch (lua.lua_gettop(L)) { /* check number of arguments */ case 0: - lapi.lua_pushnumber(L, r); /* Number between 0 and 1 */ + lua.lua_pushnumber(L, r); /* Number between 0 and 1 */ return 1; case 1: { low = 1; @@ -44,55 +43,55 @@ const math_random = function(L) { lua.to_luastring("interval too large", true)); r *= (up - low) + 1; - lapi.lua_pushinteger(L, r + low); + lua.lua_pushinteger(L, r + low); return 1; }; const math_abs = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_pushinteger(L, Math.abs(lapi.lua_tointeger(L, 1))); + if (lua.lua_isinteger(L, 1)) + lua.lua_pushinteger(L, Math.abs(lua.lua_tointeger(L, 1))); else - lapi.lua_pushnumber(L, Math.abs(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.abs(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_sin = function(L) { - lapi.lua_pushnumber(L, Math.sin(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.sin(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_cos = function(L) { - lapi.lua_pushnumber(L, Math.cos(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.cos(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_tan = function(L) { - lapi.lua_pushnumber(L, Math.tan(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.tan(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_asin = function(L) { - lapi.lua_pushnumber(L, Math.asin(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.asin(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_acos = function(L) { - lapi.lua_pushnumber(L, Math.acos(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.acos(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_atan = function(L) { - lapi.lua_pushnumber(L, Math.atan(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.atan(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_toint = function(L) { - let n = lapi.lua_tointegerx(L, 1); + let n = lua.lua_tointegerx(L, 1); if (n !== false) - lapi.lua_pushinteger(L, n); + lua.lua_pushinteger(L, n); else { lauxlib.luaL_checkany(L, 1); - lapi.lua_pushnil(L); /* value is not convertible to integer */ + lua.lua_pushnil(L); /* value is not convertible to integer */ } return 1; }; @@ -100,14 +99,14 @@ const math_toint = function(L) { const pushnumint = function(L, d) { let n = luaconf.lua_numbertointeger(d); if (n !== false) /* does 'd' fit in an integer? */ - lapi.lua_pushinteger(L, n); /* result is integer */ + lua.lua_pushinteger(L, n); /* result is integer */ else - lapi.lua_pushnumber(L, d); /* result is float */ + lua.lua_pushnumber(L, d); /* result is float */ }; const math_floor = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_settop(L, 1); + if (lua.lua_isinteger(L, 1)) + lua.lua_settop(L, 1); else pushnumint(L, Math.floor(lauxlib.luaL_checknumber(L, 1))); @@ -115,8 +114,8 @@ const math_floor = function(L) { }; const math_ceil = function(L) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_settop(L, 1); + if (lua.lua_isinteger(L, 1)) + lua.lua_settop(L, 1); else pushnumint(L, Math.ceil(lauxlib.luaL_checknumber(L, 1))); @@ -124,21 +123,21 @@ const math_ceil = function(L) { }; const math_sqrt = function(L) { - lapi.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.sqrt(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_ult = function(L) { let a = lauxlib.luaL_checkinteger(L, 1); let b = lauxlib.luaL_checkinteger(L, 2); - lapi.lua_pushboolean(L, Math.abs(a) < Math.abs(b)); + lua.lua_pushboolean(L, Math.abs(a) < Math.abs(b)); return 1; }; const math_log = function(L) { let x = lauxlib.luaL_checknumber(L, 1); let res; - if (lapi.lua_isnoneornil(L, 2)) + if (lua.lua_isnoneornil(L, 2)) res = Math.log(x); else { let base = lauxlib.luaL_checknumber(L, 2); @@ -149,87 +148,87 @@ const math_log = function(L) { else res = Math.log(x)/Math.log(base); } - lapi.lua_pushnumber(L, res); + lua.lua_pushnumber(L, res); return 1; }; const math_exp = function(L) { - lapi.lua_pushnumber(L, Math.exp(lauxlib.luaL_checknumber(L, 1))); + lua.lua_pushnumber(L, Math.exp(lauxlib.luaL_checknumber(L, 1))); return 1; }; const math_deg = function(L) { - lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (180 / Math.PI)); + lua.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (180 / Math.PI)); return 1; }; const math_rad = function(L) { - lapi.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (Math.PI / 180)); + lua.lua_pushnumber(L, lauxlib.luaL_checknumber(L, 1) * (Math.PI / 180)); return 1; }; const math_min = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let imin = 1; /* index of current minimum value */ lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true)); for (let i = 2; i <= n; i++){ - if (lapi.lua_compare(L, i, imin, lua.LUA_OPLT)) + if (lua.lua_compare(L, i, imin, lua.LUA_OPLT)) imin = i; } - lapi.lua_pushvalue(L, imin); + lua.lua_pushvalue(L, imin); return 1; }; const math_max = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let imax = 1; /* index of current minimum value */ lauxlib.luaL_argcheck(L, n >= 1, 1, lua.to_luastring("value expected", true)); for (let i = 2; i <= n; i++){ - if (lapi.lua_compare(L, imax, i, lua.LUA_OPLT)) + if (lua.lua_compare(L, imax, i, lua.LUA_OPLT)) imax = i; } - lapi.lua_pushvalue(L, imax); + lua.lua_pushvalue(L, imax); return 1; }; const math_type = function(L) { - if (lapi.lua_type(L, 1) === lua.LUA_TNUMBER) { - if (lapi.lua_isinteger(L, 1)) - lapi.lua_pushliteral(L, "integer"); + if (lua.lua_type(L, 1) === lua.LUA_TNUMBER) { + if (lua.lua_isinteger(L, 1)) + lua.lua_pushliteral(L, "integer"); else - lapi.lua_pushliteral(L, "float"); + lua.lua_pushliteral(L, "float"); } else { lauxlib.luaL_checkany(L, 1); - lapi.lua_pushnil(L); + lua.lua_pushnil(L); } return 1; }; const math_fmod = function(L) { - if (lapi.lua_isinteger(L, 1) && lapi.lua_isinteger(L, 2)) { - let d = lapi.lua_tointeger(L, 2); + if (lua.lua_isinteger(L, 1) && lua.lua_isinteger(L, 2)) { + let d = lua.lua_tointeger(L, 2); if (Math.abs(d) + 1 <= 1) { lauxlib.luaL_argcheck(L, d !== 0, 2, lua.to_luastring("zero", true)); - lapi.lua_pushinteger(L, 0); + lua.lua_pushinteger(L, 0); } else - lapi.lua_pushinteger(L, lapi.lua_tointeger(L, 1) % d); + lua.lua_pushinteger(L, lua.lua_tointeger(L, 1) % d); } else { let a = lauxlib.luaL_checknumber(L, 1); let b = lauxlib.luaL_checknumber(L, 2); - lapi.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8))); + lua.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8))); } return 1; }; const math_modf = function(L) { - if (lapi.lua_isinteger(L, 1)) { - lapi.lua_settop(L, 1); /* number is its own integer part */ - lapi.lua_pushnumber(L, 0); /* no fractional part */ + if (lua.lua_isinteger(L, 1)) { + lua.lua_settop(L, 1); /* number is its own integer part */ + lua.lua_pushnumber(L, 0); /* no fractional part */ } else { let n = lauxlib.luaL_checknumber(L, 1); let ip = n < 0 ? Math.ceil(n) : Math.floor(n); pushnumint(L, ip); - lapi.lua_pushnumber(L, n === ip ? 0 : n - ip); + lua.lua_pushnumber(L, n === ip ? 0 : n - ip); } return 2; }; @@ -262,14 +261,14 @@ const mathlib = { const luaopen_math = function(L) { lauxlib.luaL_newlib(L, mathlib); - lapi.lua_pushnumber(L, Math.PI); - lapi.lua_setfield(L, -2, lua.to_luastring("pi", true)); - lapi.lua_pushnumber(L, Number.MAX_VALUE); - lapi.lua_setfield(L, -2, lua.to_luastring("huge", true)); - lapi.lua_pushinteger(L, llimit.MAX_INT); - lapi.lua_setfield(L, -2, lua.to_luastring("maxinteger", true)); - lapi.lua_pushinteger(L, llimit.MIN_INT); - lapi.lua_setfield(L, -2, lua.to_luastring("mininteger", true)); + lua.lua_pushnumber(L, Math.PI); + lua.lua_setfield(L, -2, lua.to_luastring("pi", true)); + lua.lua_pushnumber(L, Number.MAX_VALUE); + lua.lua_setfield(L, -2, lua.to_luastring("huge", true)); + lua.lua_pushinteger(L, llimit.MAX_INT); + lua.lua_setfield(L, -2, lua.to_luastring("maxinteger", true)); + lua.lua_pushinteger(L, llimit.MIN_INT); + lua.lua_setfield(L, -2, lua.to_luastring("mininteger", true)); return 1; }; diff --git a/src/loslib.js b/src/loslib.js index 4a5c39d..348dcd6 100644 --- a/src/loslib.js +++ b/src/loslib.js @@ -3,14 +3,13 @@ const assert = require('assert'); const lua = require('./lua.js'); -const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const ldebug = require('./ldebug.js'); const llimit = require('./llimit.js'); const setfield = function(L, key, value) { - lapi.lua_pushinteger(L, value); - lapi.lua_setfield(L, -2, key); + lua.lua_pushinteger(L, value); + lua.lua_setfield(L, -2, key); }; const setallfields = function(L, time) { @@ -29,8 +28,8 @@ const setallfields = function(L, time) { const L_MAXDATEFIELD = (llimit.MAX_INT / 2); const getfield = function(L, key, d, delta) { - let t = lapi.lua_getfield(L, -1, lua.to_luastring(key)); /* get field and its type */ - let res = lapi.lua_tointegerx(L, -1); + let t = lua.lua_getfield(L, -1, lua.to_luastring(key)); /* get field and its type */ + let res = lua.lua_tointegerx(L, -1); if (res !== false) { /* field is not an integer? */ if (t != lua.LUA_TNIL) /* some other value? */ return lauxlib.luaL_error(L, lua.to_luastring(`field '${key}' is not an integer`), true); @@ -43,15 +42,15 @@ const getfield = function(L, key, d, delta) { return lauxlib.luaL_error(L, lua.to_luastring(`field '${key}' is out-of-bound`), true); res -= delta; } - lapi.lua_pop(L, 1); + lua.lua_pop(L, 1); return res; }; const os_time = function(L) { let t = new Date(); - if (!lapi.lua_isnoneornil(L, 1)) /* called with arg */{ + if (!lua.lua_isnoneornil(L, 1)) /* called with arg */{ lauxlib.luaL_checktype(L, 1, lua.LUA_TTABLE); /* make sure table is at the top */ - lapi.lua_settop(L, 1); + lua.lua_settop(L, 1); t.setSeconds(getfield(L, "sec", 0, 0)); t.setSeconds(getfield(L, "min", 0, 0)); t.setSeconds(getfield(L, "hour", 12, 0)); @@ -61,7 +60,7 @@ const os_time = function(L) { setallfields(L, t); } - lapi.lua_pushinteger(L, Math.floor(t / 1000)); + lua.lua_pushinteger(L, Math.floor(t / 1000)); return 1; }; diff --git a/src/lstrlib.js b/src/lstrlib.js index 2bfe2ee..9bbf26a 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -46,25 +46,25 @@ const str_sub = function(L) { if (start < 1) start = 1; if (end > l) end = l; if (start <= end) - lapi.lua_pushstring(L, ts.value.slice(start - 1, (start - 1) + (end - start + 1))); - else lapi.lua_pushliteral(L, ""); + lua.lua_pushstring(L, ts.value.slice(start - 1, (start - 1) + (end - start + 1))); + else lua.lua_pushliteral(L, ""); return 1; }; const str_len = function(L) { - lapi.lua_pushinteger(L, lauxlib.luaL_checkstring(L, 1).length); + lua.lua_pushinteger(L, lauxlib.luaL_checkstring(L, 1).length); return 1; }; const str_char = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ let p = []; for (let i = 1; i <= n; i++) { let c = lauxlib.luaL_checkinteger(L, i); lauxlib.luaL_argcheck(L, c >= 0 && c <= 255, "value out of range"); // Strings are 8-bit clean p.push(c); } - lapi.lua_pushstring(L, p); + lua.lua_pushstring(L, p); return 1; }; @@ -76,12 +76,12 @@ const writer = function(L, b, size, B) { const str_dump = function(L) { let b = []; - let strip = lapi.lua_toboolean(L, 2); + let strip = lua.lua_toboolean(L, 2); lauxlib.luaL_checktype(L, 1, lua.LUA_TFUNCTION); - lapi.lua_settop(L, 1); - if (lapi.lua_dump(L, writer, b, strip) !== 0) + lua.lua_settop(L, 1); + if (lua.lua_dump(L, writer, b, strip) !== 0) return lauxlib.luaL_error(L, lua.to_luastring("unable to dump given function")); - lapi.lua_pushstring(L, b); + lua.lua_pushstring(L, b); return 1; }; @@ -216,19 +216,19 @@ const checkdp = function(buff) { }; const addliteral = function(L, b, arg) { - switch(lapi.lua_type(L, arg)) { + switch(lua.lua_type(L, arg)) { case lua.LUA_TSTRING: { - let s = lapi.lua_tostring(L, arg); + let s = lua.lua_tostring(L, arg); addquoted(b, s, s.length); break; } case lua.LUA_TNUMBER: { - if (!lapi.lua_isinteger(L, arg)) { /* float? */ - let n = lapi.lua_tonumber(L, arg); /* write as hexa ('%a') */ + if (!lua.lua_isinteger(L, arg)) { /* float? */ + let n = lua.lua_tonumber(L, arg); /* write as hexa ('%a') */ concat(b, lua_number2strx(L, lua.to_luastring(`%${luaconf.LUA_INTEGER_FRMLEN}a`), n)); checkdp(b); /* ensure it uses a dot */ } else { /* integers */ - let n = lapi.lua_tointeger(L, arg); + let n = lua.lua_tointeger(L, arg); concat(b, lua.to_luastring(sprintf("%d", n))); } break; @@ -282,7 +282,7 @@ const addlenmod = function(form, lenmod) { }; const str_format = function(L) { - let top = lapi.lua_gettop(L); + let top = lua.lua_gettop(L); let arg = 1; let strfrmt = lauxlib.luaL_checkstring(L, arg); let b = []; @@ -340,17 +340,17 @@ const str_format = function(L) { let s = lauxlib.luaL_tolstring(L, arg); if (form.length <= 2 || form[2] === 0) { /* no modifiers? */ concat(b, s); /* keep entire string */ - lapi.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + lua.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } else { lauxlib.luaL_argcheck(L, s.length === strlen(s), arg, lua.to_luastring("string contains zeros", true)); if (form.indexOf('.'.charCodeAt(0)) < 0 && s.length >= 100) { /* no precision and string is too long to be formatted */ concat(b, s); /* keep entire string */ - lapi.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + lua.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } else { /* format the string into 'buff' */ // TODO: will failt if s is not valid UTF-8 concat(b, lua.to_luastring(sprintf(String.fromCharCode(...form), lobject.jsstring(s)))); - lapi.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ + lua.lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } } break; @@ -362,7 +362,7 @@ const str_format = function(L) { } } - lapi.lua_pushstring(L, b); + lua.lua_pushstring(L, b); return 1; }; @@ -569,7 +569,7 @@ const str_pack = function(L) { }; let arg = 1; /* current argument to pack */ let totalsize = 0; /* accumulate total size of result */ - lapi.lua_pushnil(L); /* mark to separate arguments from string buffer */ + lua.lua_pushnil(L); /* mark to separate arguments from string buffer */ while (fmt.s.length - 1 > 0) { let details = getdetails(h, totalsize, fmt); let opt = details.opt; @@ -634,24 +634,24 @@ const str_pack = function(L) { break; } } - lapi.lua_pushstring(L, b); + lua.lua_pushstring(L, b); return 1; }; const str_reverse = function(L) { - lapi.lua_pushstring(L, lauxlib.luaL_checkstring(L, 1).reverse()); + lua.lua_pushstring(L, lauxlib.luaL_checkstring(L, 1).reverse()); return 1; }; const str_lower = function(L) { // TODO: will fail on invalid UTF-8 - lapi.lua_pushstring(L, lua.to_luastring(lobject.jsstring(lauxlib.luaL_checkstring(L, 1)).toLowerCase())); + lua.lua_pushstring(L, lua.to_luastring(lobject.jsstring(lauxlib.luaL_checkstring(L, 1)).toLowerCase())); return 1; }; const str_upper = function(L) { // TODO: will fail on invalid UTF-8 - lapi.lua_pushstring(L, lua.to_luastring(lobject.jsstring(lauxlib.luaL_checkstring(L, 1)).toUpperCase())); + lua.lua_pushstring(L, lua.to_luastring(lobject.jsstring(lauxlib.luaL_checkstring(L, 1)).toUpperCase())); return 1; }; @@ -668,7 +668,7 @@ const str_rep = function(L) { r = r.concat(s.concat(sep)); r = r.concat(s); - lapi.lua_pushstring(L, n > 0 ? r : []); + lua.lua_pushstring(L, n > 0 ? r : []); return 1; }; @@ -687,7 +687,7 @@ const str_byte = function(L) { let n = (pose - posi) + 1; lauxlib.luaL_checkstack(L, n, lua.to_luastring("string slice too long", true)); for (let i = 0; i < n; i++) - lapi.lua_pushinteger(L, s[posi + i - 1]); + lua.lua_pushinteger(L, s[posi + i - 1]); return n; }; @@ -715,7 +715,7 @@ const str_packsize = function(L) { default: break; } } - lapi.lua_pushinteger(L, totalsize); + lua.lua_pushinteger(L, totalsize); return 1; }; @@ -786,28 +786,28 @@ const str_unpack = function(L) { case KOption.Kint: case KOption.Kuint: { let res = unpackint(L, data.slice(pos), h.islittle, size, opt === KOption.Kint); - lapi.lua_pushinteger(L, res); + lua.lua_pushinteger(L, res); break; } case KOption.Kfloat: { let res = unpacknum(L, data.slice(pos), h.islittle, size); - lapi.lua_pushnumber(L, res); + lua.lua_pushnumber(L, res); break; } case KOption.Kchar: { - lapi.lua_pushstring(L, data.slice(pos, pos + size)); + lua.lua_pushstring(L, data.slice(pos, pos + size)); break; } case KOption.Kstring: { let len = unpackint(L, data.slice(pos), h.islittle, size, 0); lauxlib.luaL_argcheck(L, pos + len + size <= ld, 2, lua.to_luastring("data string too short", true)); - lapi.lua_pushstring(L, data.slice(pos + size, pos + size + len)); + lua.lua_pushstring(L, data.slice(pos + size, pos + size + len)); pos += len; /* skip string */ break; } case KOption.Kzstr: { let len = data.slice(pos).indexOf(0); - lapi.lua_pushstring(L, data.slice(pos, pos + len)); + lua.lua_pushstring(L, data.slice(pos, pos + len)); pos += len + 1; /* skip string plus final '\0' */ break; } @@ -817,7 +817,7 @@ const str_unpack = function(L) { } pos += size; } - lapi.lua_pushinteger(L, pos + 1); /* next position */ + lua.lua_pushinteger(L, pos + 1); /* next position */ return n + 1; }; @@ -1112,16 +1112,16 @@ const match = function(ms, s, p) { const push_onecapture = function(ms, i, s, e) { if (i >= ms.level) { if (i === 0) - lapi.lua_pushlstring(ms.L, ms.src.slice(s), e - s); /* add whole match */ + lua.lua_pushlstring(ms.L, ms.src.slice(s), e - s); /* add whole match */ else lauxlib.luaL_error(ms.L, lua.to_luastring(`invalid capture index %${i + 1}`)); } else { let l = ms.capture[i].len; if (l === CAP_UNFINISHED) lauxlib.luaL_error(ms.L, lua.to_luastring("unfinished capture", true)); if (l === CAP_POSITION) - lapi.lua_pushinteger(ms.L, ms.src_init + 1); + lua.lua_pushinteger(ms.L, ms.src_init + 1); else - lapi.lua_pushlstring(ms.L, ms.src.slice(ms.capture[i].init), l); + lua.lua_pushlstring(ms.L, ms.src.slice(ms.capture[i].init), l); } }; @@ -1189,16 +1189,16 @@ const str_find_aux = function(L, find) { let init = posrelat(lauxlib.luaL_optinteger(L, 3, 1), ls); if (init < 1) init = 1; else if (init > ls + 1) { /* start after string's end? */ - lapi.lua_pushnil(L); /* cannot find anything */ + lua.lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ - if (find && (lapi.lua_toboolean(L, 4) || nospecials(p, lp))) { + if (find && (lua.lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ let f = find_subarray(s.slice(init - 1), p, 0); if (f > -1) { - lapi.lua_pushinteger(L, init + f); - lapi.lua_pushinteger(L, init + f + lp - 1); + lua.lua_pushinteger(L, init + f); + lua.lua_pushinteger(L, init + f + lp - 1); return 2; } } else { @@ -1214,15 +1214,15 @@ const str_find_aux = function(L, find) { reprepstate(ms); if ((res = match(ms, s1, 0)) !== null) { if (find) { - lapi.lua_pushinteger(L, s1 + 1); /* start */ - lapi.lua_pushinteger(L, res); /* end */ + lua.lua_pushinteger(L, s1 + 1); /* start */ + lua.lua_pushinteger(L, res); /* end */ return push_captures(ms, null, 0) + 2; } else return push_captures(ms, s1, res); } } while (s1++ < ms.src_end && !anchor); } - lapi.lua_pushnil(L); /* not found */ + lua.lua_pushnil(L); /* not found */ return 1; }; @@ -1245,7 +1245,7 @@ class GMatchState { } const gmatch_aux = function(L) { - let gm = lapi.lua_touserdata(L, lua.lua_upvalueindex(3)); + let gm = lua.lua_touserdata(L, lua.lua_upvalueindex(3)); gm.ms.L = L; for (let src = gm.src; src < gm.ms.src_end; src++) { reprepstate(gm.ms); @@ -1263,20 +1263,20 @@ const str_gmatch = function(L) { let p = lauxlib.luaL_checkstring(L, 2); let ls = s.length; let lp = p.length; - lapi.lua_settop(L, 2); /* keep them on closure to avoid being collected */ + lua.lua_settop(L, 2); /* keep them on closure to avoid being collected */ let gm = new GMatchState(); - lapi.lua_pushobject(L, gm); + lua.lua_pushobject(L, gm); prepstate(gm.ms, L, s, ls, p, lp); gm.src = 0; gm.p = 0; gm.lastmatch = null; - lapi.lua_pushcclosure(L, gmatch_aux, 3); + lua.lua_pushcclosure(L, gmatch_aux, 3); return 1; }; const add_s = function(ms, b, s, e) { let L = ms.L; - let news = lapi.lua_tostring(L, 3); + let news = lua.lua_tostring(L, 3); let l = news.length; for (let i = 0; i < l; i++) { if (news[i] !== L_ESC) @@ -1292,7 +1292,7 @@ const add_s = function(ms, b, s, e) { else { push_onecapture(ms, news[i] - '1'.charCodeAt(0), s, e); lauxlib.luaL_tolstring(L, -1); - lapi.lua_remove(L, -2); /* remove original value */ + lua.lua_remove(L, -2); /* remove original value */ lauxlib.luaL_addvalue(b); /* add capture to accumulated result */ } } @@ -1303,14 +1303,14 @@ const add_value = function(ms, b, s, e, tr) { let L = ms.L; switch (tr) { case lua.LUA_TFUNCTION: { - lapi.lua_pushvalue(L, 3); + lua.lua_pushvalue(L, 3); let n = push_captures(ms, s, e); - lapi.lua_call(L, n, 1); + lua.lua_call(L, n, 1); break; } case lua.LUA_TTABLE: { push_onecapture(ms, 0, s, e); - lapi.lua_gettable(L, 3); + lua.lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ @@ -1318,10 +1318,10 @@ const add_value = function(ms, b, s, e, tr) { return; } } - if (!lapi.lua_toboolean(L, -1)) { /* nil or false? */ - lapi.lua_pop(L, 1); - lapi.lua_pushlstring(L, s, e - s); /* keep original text */ - } else if (!lapi.lua_isstring(L, -1)) + if (!lua.lua_toboolean(L, -1)) { /* nil or false? */ + lua.lua_pop(L, 1); + lua.lua_pushlstring(L, s, e - s); /* keep original text */ + } else if (!lua.lua_isstring(L, -1)) lauxlib.luaL_error(L, lua.to_luastring(`invalid replacement value (a ${lobject.jsstring(lauxlib.luaL_typename(L, -1))})`)); lauxlib.luaL_addvalue(b); /* add result to accumulator */ }; @@ -1332,7 +1332,7 @@ const str_gsub = function(L) { let p = lauxlib.luaL_checkstring(L, 2); /* pattern */ let lp = p.length; let lastmatch = null; /* end of last match */ - let tr = lapi.lua_type(L, 3); /* replacement type */ + let tr = lua.lua_type(L, 3); /* replacement type */ let max_s = lauxlib.luaL_optinteger(L, 4, srcl + 1); /* max replacements */ let anchor = p[0] === '^'.charCodeAt(0); let n = 0; /* replacement count */ @@ -1360,7 +1360,7 @@ const str_gsub = function(L) { } lauxlib.luaL_addlstring(b, ms.src.slice(src), ms.src_end - src); lauxlib.luaL_pushresult(b); - lapi.lua_pushinteger(L, n); /* number of substitutions */ + lua.lua_pushinteger(L, n); /* number of substitutions */ return 2; }; @@ -1385,14 +1385,14 @@ const strlib = { }; const createmetatable = function(L) { - lapi.lua_createtable(L, 0, 1); /* table to be metatable for strings */ - lapi.lua_pushliteral(L, ""); /* dummy string */ - lapi.lua_pushvalue(L, -2); /* copy table */ - lapi.lua_setmetatable(L, -2); /* set table as metatable for strings */ - lapi.lua_pop(L, 1); /* pop dummy string */ - lapi.lua_pushvalue(L, -2); /* get string library */ - lapi.lua_setfield(L, -2, lua.to_luastring("__index", true)); /* lobject.table_index = string */ - lapi.lua_pop(L, 1); /* pop metatable */ + lua.lua_createtable(L, 0, 1); /* table to be metatable for strings */ + lua.lua_pushliteral(L, ""); /* dummy string */ + lua.lua_pushvalue(L, -2); /* copy table */ + lua.lua_setmetatable(L, -2); /* set table as metatable for strings */ + lua.lua_pop(L, 1); /* pop dummy string */ + lua.lua_pushvalue(L, -2); /* get string library */ + lua.lua_setfield(L, -2, lua.to_luastring("__index", true)); /* lobject.table_index = string */ + lua.lua_pop(L, 1); /* pop metatable */ }; const luaopen_string = function(L) { diff --git a/src/ltablib.js b/src/ltablib.js index 51f9810..5dc33d1 100644 --- a/src/ltablib.js +++ b/src/ltablib.js @@ -22,8 +22,8 @@ const TAB_L = 4; /* length */ const TAB_RW = (TAB_R | TAB_W); /* read/write */ const checkfield = function(L, key, n) { - lapi.lua_pushstring(L, key); - return lapi.lua_rawget(L, -n) !== lua.LUA_TNIL; + lua.lua_pushstring(L, key); + return lua.lua_rawget(L, -n) !== lua.LUA_TNIL; }; /* @@ -31,13 +31,13 @@ const checkfield = function(L, key, n) { ** has a metatable with the required metamethods) */ const checktab = function(L, arg, what) { - if (lapi.lua_type(L, arg) !== lua.LUA_TTABLE) { /* is it not a table? */ + if (lua.lua_type(L, arg) !== lua.LUA_TTABLE) { /* is it not a table? */ let n = 1; - if (lapi.lua_getmetatable(L, arg) && /* must have metatable */ + if (lua.lua_getmetatable(L, arg) && /* must have metatable */ (!(what & TAB_R) || checkfield(L, lua.to_luastring("__index", true), ++n)) && (!(what & TAB_W) || checkfield(L, lua.to_luastring("__newindex", true), ++n)) && (!(what & TAB_L) || checkfield(L, lua.to_luastring("__len", true), ++n))) { - lapi.lua_pop(L, n); /* pop metatable and tested metamethods */ + lua.lua_pop(L, n); /* pop metatable and tested metamethods */ } else lauxlib.luaL_checktype(L, arg, lua.LUA_TTABLE); /* force an error */ @@ -50,8 +50,8 @@ const aux_getn = function(L, n, w) { }; const addfield = function(L, b, i) { - lapi.lua_geti(L, 1, i); - if (!lapi.lua_isstring(L, -1)) + lua.lua_geti(L, 1, i); + if (!lua.lua_isstring(L, -1)) lauxlib.luaL_error(L, lua.to_luastring(`invalid value (${lobject.jsstring(lauxlib.luaL_typename(L, -1))}) at index ${i} in table for 'concat'`)); lauxlib.luaL_addvalue(b); @@ -60,7 +60,7 @@ const addfield = function(L, b, i) { const tinsert = function(L) { let e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ let pos; - switch (lapi.lua_gettop(L)) { + switch (lua.lua_gettop(L)) { case 2: pos = e; break; @@ -68,8 +68,8 @@ const tinsert = function(L) { pos = lauxlib.luaL_checkinteger(L, 2); /* 2nd argument is the position */ lauxlib.luaL_argcheck(L, 1 <= pos && pos <= e, 2, lua.to_luastring("position out of bounds", true)); for (let i = e; i > pos; i--) { /* move up elements */ - lapi.lua_geti(L, 1, i - 1); - lapi.lua_seti(L, 1, i); /* t[i] = t[i - 1] */ + lua.lua_geti(L, 1, i - 1); + lua.lua_seti(L, 1, i); /* t[i] = t[i - 1] */ } break; } @@ -78,7 +78,7 @@ const tinsert = function(L) { } } - lapi.lua_seti(L, 1, pos); /* t[pos] = v */ + lua.lua_seti(L, 1, pos); /* t[pos] = v */ return 0; }; @@ -87,13 +87,13 @@ const tremove = function(L) { let pos = lauxlib.luaL_optinteger(L, 2, size); if (pos !== size) /* validate 'pos' if given */ lauxlib.luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, lua.to_luastring("position out of bounds", true)); - lapi.lua_geti(L, 1, pos); /* result = t[pos] */ + lua.lua_geti(L, 1, pos); /* result = t[pos] */ for (; pos < size; pos++) { - lapi.lua_geti(L, 1, pos + 1); - lapi.lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ + lua.lua_geti(L, 1, pos + 1); + lua.lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } - lapi.lua_pushnil(L); - lapi.lua_seti(L, 1, pos); /* t[pos] = nil */ + lua.lua_pushnil(L); + lua.lua_seti(L, 1, pos); /* t[pos] = nil */ return 1; }; @@ -107,7 +107,7 @@ const tmove = function(L) { let f = lauxlib.luaL_checkinteger(L, 2); let e = lauxlib.luaL_checkinteger(L, 3); let t = lauxlib.luaL_checkinteger(L, 4); - let tt = !lapi.lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ + let tt = !lua.lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ checktab(L, 1, TAB_R); checktab(L, tt, TAB_W); if (e >= f) { /* otherwise, nothing to move */ @@ -115,20 +115,20 @@ const tmove = function(L) { let n = e - f + 1; /* number of elements to move */ lauxlib.luaL_argcheck(L, t <= llimit.LUA_MAXINTEGER - n + 1, 4, lua.to_luastring("destination wrap around", true)); - if (t > e || t <= f || (tt !== 1 && lapi.lua_compare(L, 1, tt, lua.LUA_OPEQ) !== 1)) { + if (t > e || t <= f || (tt !== 1 && lua.lua_compare(L, 1, tt, lua.LUA_OPEQ) !== 1)) { for (let i = 0; i < n; i++) { - lapi.lua_geti(L, 1, f + i); - lapi.lua_seti(L, tt, t + i); + lua.lua_geti(L, 1, f + i); + lua.lua_seti(L, tt, t + i); } } else { for (let i = n - 1; i >= 0; i--) { - lapi.lua_geti(L, 1, f + i); - lapi.lua_seti(L, tt, t + i); + lua.lua_geti(L, 1, f + i); + lua.lua_seti(L, tt, t + i); } } } - lapi.lua_pushvalue(L, tt); /* return destination table */ + lua.lua_pushvalue(L, tt); /* return destination table */ return 1; }; @@ -155,13 +155,13 @@ const tconcat = function(L) { }; const pack = function(L) { - let n = lapi.lua_gettop(L); /* number of elements to pack */ - lapi.lua_createtable(L, n, 1); /* create result table */ - lapi.lua_insert(L, 1); /* put it at index 1 */ + let n = lua.lua_gettop(L); /* number of elements to pack */ + lua.lua_createtable(L, n, 1); /* create result table */ + lua.lua_insert(L, 1); /* put it at index 1 */ for (let i = n; i >= 1; i--) /* assign elements */ - lapi.lua_seti(L, 1, i); - lapi.lua_pushinteger(L, n); - lapi.lua_setfield(L, 1, ["n".charCodeAt(0)]); /* t.n = number of elements */ + lua.lua_seti(L, 1, i); + lua.lua_pushinteger(L, n); + lua.lua_setfield(L, 1, ["n".charCodeAt(0)]); /* t.n = number of elements */ return 1; /* return table */ }; @@ -170,11 +170,11 @@ const unpack = function(L) { let e = lauxlib.luaL_opt(L, lauxlib.luaL_checkinteger, 3, lauxlib.luaL_len(L, 1)); if (i > e) return 0; /* empty range */ let n = e - i; /* number of elements minus 1 (avoid overflows) */ - if (n >= llimit.MAX_INT || !lapi.lua_checkstack(L, ++n)) + if (n >= llimit.MAX_INT || !lua.lua_checkstack(L, ++n)) return lauxlib.luaL_error(L, lua.to_luastring("too many results to unpack", true)); for (; i < e; i++) /* push arg[i..e - 1] (to avoid overflows) */ - lapi.lua_geti(L, 1, i); - lapi.lua_geti(L, 1, e); /* push last element */ + lua.lua_geti(L, 1, i); + lua.lua_geti(L, 1, e); /* push last element */ return n; }; @@ -183,7 +183,7 @@ const unpack = function(L) { const auxsort = function(L) { let t = lapi.index2addr(L, 1); - if (lapi.lua_type(L, 2) !== lua.LUA_TFUNCTION) { /* no function? */ + if (lua.lua_type(L, 2) !== lua.LUA_TFUNCTION) { /* no function? */ [...t.value.entries()] .sort(function (a, b) { if (typeof a[0] !== 'number') return 1; @@ -197,12 +197,12 @@ const auxsort = function(L) { if (typeof a[0] !== 'number') return 1; else if (typeof b[0] !== 'number') return -1; - lapi.lua_pushvalue(L, 2); /* push function */ - lapi.lua_pushtvalue(L, a[1]); /* since we use Map.sort, a and b are not on the stack */ - lapi.lua_pushtvalue(L, b[1]); - lapi.lua_call(L, 2, 1); /* call function */ - let res = lapi.lua_toboolean(L, -1); /* get result */ - lapi.lua_pop(L, 1); /* pop result */ + lua.lua_pushvalue(L, 2); /* push function */ + lua.lua_pushtvalue(L, a[1]); /* since we use Map.sort, a and b are not on the stack */ + lua.lua_pushtvalue(L, b[1]); + lua.lua_call(L, 2, 1); /* call function */ + let res = lua.lua_toboolean(L, -1); /* get result */ + lua.lua_pop(L, 1); /* pop result */ return res ? -1 : 1; }) .forEach((e, i) => typeof e[0] === 'number' ? t.value.set(i + 1, e[1]) : true); @@ -213,9 +213,9 @@ const sort = function(L) { let n = aux_getn(L, 1, TAB_RW); if (n > 1) { /* non-trivial interval? */ lauxlib.luaL_argcheck(L, n < llimit.MAX_INT, 1, lua.to_luastring("array too big", true)); - if (!lapi.lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ + if (!lua.lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ lauxlib.luaL_checktype(L, 2, lua.LUA_TFUNCTION); /* must be a function */ - lapi.lua_settop(L, 2); /* make sure there are two arguments */ + lua.lua_settop(L, 2); /* make sure there are two arguments */ auxsort(L); } return 0; @@ -2,6 +2,7 @@ "use strict"; const defs = require("./defs.js"); +const lapi = require("./lapi.js"); module.exports.FENGARI_AUTHORS = defs.FENGARI_AUTHORS; module.exports.FENGARI_COPYRIGHT = defs.FENGARI_COPYRIGHT; @@ -76,3 +77,101 @@ module.exports.LUA_YIELD = defs.thread_status.LUA_YIELD; module.exports.lua_Debug = defs.lua_Debug; module.exports.lua_upvalueindex = defs.lua_upvalueindex; module.exports.to_luastring = defs.to_luastring; + +module.exports.lua_absindex = lapi.lua_absindex; +module.exports.lua_atpanic = lapi.lua_atpanic; +module.exports.lua_call = lapi.lua_call; +module.exports.lua_callk = lapi.lua_callk; +module.exports.lua_checkstack = lapi.lua_checkstack; +module.exports.lua_compare = lapi.lua_compare; +module.exports.lua_concat = lapi.lua_concat; +module.exports.lua_copy = lapi.lua_copy; +module.exports.lua_createtable = lapi.lua_createtable; +module.exports.lua_dump = lapi.lua_dump; +module.exports.lua_error = lapi.lua_error; +module.exports.lua_gc = lapi.lua_gc; +module.exports.lua_getallocf = lapi.lua_getallocf; +module.exports.lua_getextraspace = lapi.lua_getextraspace; +module.exports.lua_getfield = lapi.lua_getfield; +module.exports.lua_getglobal = lapi.lua_getglobal; +module.exports.lua_geti = lapi.lua_geti; +module.exports.lua_getmetatable = lapi.lua_getmetatable; +module.exports.lua_gettable = lapi.lua_gettable; +module.exports.lua_gettop = lapi.lua_gettop; +module.exports.lua_getupvalue = lapi.lua_getupvalue; +module.exports.lua_getuservalue = lapi.lua_getuservalue; +module.exports.lua_insert = lapi.lua_insert; +module.exports.lua_iscfunction = lapi.lua_iscfunction; +module.exports.lua_isfunction = lapi.lua_isfunction; +module.exports.lua_isinteger = lapi.lua_isinteger; +module.exports.lua_isnil = lapi.lua_isnil; +module.exports.lua_isnone = lapi.lua_isnone; +module.exports.lua_isnoneornil = lapi.lua_isnoneornil; +module.exports.lua_isnumber = lapi.lua_isnumber; +module.exports.lua_isstring = lapi.lua_isstring; +module.exports.lua_istable = lapi.lua_istable; +module.exports.lua_isthread = lapi.lua_isthread; +module.exports.lua_isuserdata = lapi.lua_isuserdata; +module.exports.lua_len = lapi.lua_len; +module.exports.lua_load = lapi.lua_load; +module.exports.lua_newtable = lapi.lua_newtable; +module.exports.lua_newuserdata = lapi.lua_newuserdata; +module.exports.lua_next = lapi.lua_next; +module.exports.lua_pcall = lapi.lua_pcall; +module.exports.lua_pcallk = lapi.lua_pcallk; +module.exports.lua_pop = lapi.lua_pop; +module.exports.lua_pushboolean = lapi.lua_pushboolean; +module.exports.lua_pushcclosure = lapi.lua_pushcclosure; +module.exports.lua_pushcfunction = lapi.lua_pushcfunction; +module.exports.lua_pushglobaltable = lapi.lua_pushglobaltable; +module.exports.lua_pushinteger = lapi.lua_pushinteger; +module.exports.lua_pushjsclosure = lapi.lua_pushjsclosure; +module.exports.lua_pushjsfunction = lapi.lua_pushjsfunction; +module.exports.lua_pushlightuserdata = lapi.lua_pushlightuserdata; +module.exports.lua_pushliteral = lapi.lua_pushliteral; +module.exports.lua_pushlstring = lapi.lua_pushlstring; +module.exports.lua_pushnil = lapi.lua_pushnil; +module.exports.lua_pushnumber = lapi.lua_pushnumber; +module.exports.lua_pushobject = lapi.lua_pushobject; +module.exports.lua_pushstring = lapi.lua_pushstring; +module.exports.lua_pushthread = lapi.lua_pushthread; +module.exports.lua_pushtvalue = lapi.lua_pushtvalue; +module.exports.lua_pushvalue = lapi.lua_pushvalue; +module.exports.lua_rawequal = lapi.lua_rawequal; +module.exports.lua_rawget = lapi.lua_rawget; +module.exports.lua_rawgeti = lapi.lua_rawgeti; +module.exports.lua_rawgetp = lapi.lua_rawgetp; +module.exports.lua_rawlen = lapi.lua_rawlen; +module.exports.lua_rawset = lapi.lua_rawset; +module.exports.lua_rawsetp = lapi.lua_rawsetp; +module.exports.lua_remove = lapi.lua_remove; +module.exports.lua_replace = lapi.lua_replace; +module.exports.lua_rotate = lapi.lua_rotate; +module.exports.lua_setfield = lapi.lua_setfield; +module.exports.lua_setglobal = lapi.lua_setglobal; +module.exports.lua_seti = lapi.lua_seti; +module.exports.lua_setmetatable = lapi.lua_setmetatable; +module.exports.lua_settable = lapi.lua_settable; +module.exports.lua_settop = lapi.lua_settop; +module.exports.lua_setupvalue = lapi.lua_setupvalue; +module.exports.lua_setuservalue = lapi.lua_setuservalue; +module.exports.lua_status = lapi.lua_status; +module.exports.lua_stringtonumber = lapi.lua_stringtonumber; +module.exports.lua_toboolean = lapi.lua_toboolean; +module.exports.lua_todataview = lapi.lua_todataview; +module.exports.lua_tointeger = lapi.lua_tointeger; +module.exports.lua_tointegerx = lapi.lua_tointegerx; +module.exports.lua_tojsstring = lapi.lua_tojsstring; +module.exports.lua_toljsstring = lapi.lua_toljsstring; +module.exports.lua_tolstring = lapi.lua_tolstring; +module.exports.lua_tonumber = lapi.lua_tonumber; +module.exports.lua_topointer = lapi.lua_topointer; +module.exports.lua_tostring = lapi.lua_tostring; +module.exports.lua_tothread = lapi.lua_tothread; +module.exports.lua_touserdata = lapi.lua_touserdata; +module.exports.lua_type = lapi.lua_type; +module.exports.lua_typename = lapi.lua_typename; +module.exports.lua_upvalueid = lapi.lua_upvalueid; +module.exports.lua_upvaluejoin = lapi.lua_upvaluejoin; +module.exports.lua_version = lapi.lua_version; +module.exports.lua_xmove = lapi.lua_xmove; diff --git a/src/lutf8lib.js b/src/lutf8lib.js index 9d53a8c..af5f960 100644 --- a/src/lutf8lib.js +++ b/src/lutf8lib.js @@ -75,28 +75,28 @@ const utflen = function(L) { let s1 = dec ? dec.string : null; if (s1 === null) { /* conversion error? */ - lapi.lua_pushnil(L); /* return nil ... */ - lapi.lua_pushinteger(L, posi + 1); /* ... and current position */ + lua.lua_pushnil(L); /* return nil ... */ + lua.lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } posi = s.length - s1.length; n++; } - lapi.lua_pushinteger(L, n); + lua.lua_pushinteger(L, n); return 1; }; const pushutfchar = function(L, arg) { let code = lauxlib.luaL_checkinteger(L, arg); lauxlib.luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, lua.to_luastring("value out of range", true)); - lapi.lua_pushstring(L, lua.to_luastring(String.fromCharCode(code))); + lua.lua_pushstring(L, lua.to_luastring(String.fromCharCode(code))); }; /* ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... */ const utfchar = function(L) { - let n = lapi.lua_gettop(L); /* number of arguments */ + let n = lua.lua_gettop(L); /* number of arguments */ if (n === 1) /* optimize common case of single char */ pushutfchar(L, 1); else { @@ -150,9 +150,9 @@ const byteoffset = function(L) { } if (n === 0) /* did it find given character? */ - lapi.lua_pushinteger(L, posi + 1); + lua.lua_pushinteger(L, posi + 1); else /* no such character */ - lapi.lua_pushnil(L); + lua.lua_pushnil(L); return 1; }; @@ -182,7 +182,7 @@ const codepoint = function(L) { return lauxlib.luaL_error(L, lua.to_luastring("invalid UTF-8 code", true)); s = dec.string; let code = dec.code; - lapi.lua_pushinteger(L, code); + lua.lua_pushinteger(L, code); n++; } return n; @@ -192,7 +192,7 @@ const iter_aux = function(L) { let s = lauxlib.luaL_checkstring(L, 1); s = L.stack[lapi.index2addr_(L, 1)].value; let len = s.length; - let n = lapi.lua_tointeger(L, 2) - 1; + let n = lua.lua_tointeger(L, 2) - 1; if (n < 0) /* first iteration? */ n = 0; /* start from here */ @@ -209,17 +209,17 @@ const iter_aux = function(L) { let next = dec ? dec.string : null; if (next === null || iscont(next[0])) return lauxlib.luaL_error(L, lua.to_luastring("invalid UTF-8 code", true)); - lapi.lua_pushinteger(L, n + 1); - lapi.lua_pushinteger(L, code); + lua.lua_pushinteger(L, n + 1); + lua.lua_pushinteger(L, code); return 2; } }; const iter_codes = function(L) { lauxlib.luaL_checkstring(L, 1); - lapi.lua_pushcfunction(L, iter_aux); - lapi.lua_pushvalue(L, 1); - lapi.lua_pushinteger(L, 0); + lua.lua_pushcfunction(L, iter_aux); + lua.lua_pushvalue(L, 1); + lua.lua_pushinteger(L, 0); return 3; }; @@ -236,8 +236,8 @@ const UTF8PATT = "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"; const luaopen_utf8 = function(L) { lauxlib.luaL_newlib(L, funcs); - lapi.lua_pushstring(L, lua.to_luastring(UTF8PATT)); - lapi.lua_setfield(L, -2, lua.to_luastring("charpattern", true)); + lua.lua_pushstring(L, lua.to_luastring(UTF8PATT)); + lua.lua_setfield(L, -2, lua.to_luastring("charpattern", true)); return 1; }; |