From e7605840613fcbc084de8b92dc6f68414deb757d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 18 Jan 2018 07:22:36 +1100 Subject: src/lapi.js: Use destructuring assignment where sensible --- src/lapi.js | 212 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 120 insertions(+), 92 deletions(-) diff --git a/src/lapi.js b/src/lapi.js index 4218338..ec643b3 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -1,13 +1,44 @@ "use strict"; -const defs = require('./defs.js'); +const { + LUA_MULTRET, + LUA_OPBNOT, + LUA_OPEQ, + LUA_OPLE, + LUA_OPLT, + LUA_OPUNM, + LUA_REGISTRYINDEX, + LUA_RIDX_GLOBALS, + LUA_VERSION_NUM, + constant_types: { + LUA_NUMTAGS, + LUA_TBOOLEAN, + LUA_TCCL, + LUA_TFUNCTION, + LUA_TLCF, + LUA_TLCL, + LUA_TLIGHTUSERDATA, + LUA_TLNGSTR, + LUA_TNIL, + LUA_TNONE, + LUA_TNUMFLT, + LUA_TNUMINT, + LUA_TSHRSTR, + LUA_TTABLE, + LUA_TTHREAD, + LUA_TUSERDATA + }, + thread_status: { LUA_OK }, + from_userstring, + to_luastring, +} = require('./defs.js'); const { api_check, lua_assert } = require('./llimits.js'); const ldebug = require('./ldebug.js'); const ldo = require('./ldo.js'); -const ldump = require('./ldump.js'); +const { luaU_dump } = require('./ldump.js'); const lfunc = require('./lfunc.js'); const lobject = require('./lobject.js'); const lstate = require('./lstate.js'); @@ -17,13 +48,10 @@ const { luaS_newliteral } = require('./lstring.js'); const ltm = require('./ltm.js'); -const luaconf = require('./luaconf.js'); +const { LUAI_MAXSTACK } = require('./luaconf.js'); const lvm = require('./lvm.js'); const ltable = require('./ltable.js'); -const lzio = require('./lzio.js'); -const MAXUPVAL = lfunc.MAXUPVAL; -const CT = defs.constant_types; -const TS = defs.thread_status; +const { ZIO } = require('./lzio.js'); const TValue = lobject.TValue; const CClosure = lobject.CClosure; @@ -45,7 +73,7 @@ const isvalid = function(o) { }; const lua_version = function(L) { - if (L === null) return defs.LUA_VERSION_NUM; + if (L === null) return LUA_VERSION_NUM; else return L.l_G.version; }; @@ -69,14 +97,14 @@ const index2addr = function(L, idx) { api_check(L, idx <= ci.top - (ci.funcOff + 1), "unacceptable index"); if (o >= L.top) return lobject.luaO_nilobject; else return L.stack[o]; - } else if (idx > defs.LUA_REGISTRYINDEX) { + } else if (idx > LUA_REGISTRYINDEX) { api_check(L, idx !== 0 && -idx <= L.top, "invalid index"); return L.stack[L.top + idx]; - } else if (idx === defs.LUA_REGISTRYINDEX) { + } else if (idx === LUA_REGISTRYINDEX) { return L.l_G.l_registry; } else { /* upvalues */ - idx = defs.LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); + idx = LUA_REGISTRYINDEX - idx; + api_check(L, idx <= lfunc.MAXUPVAL + 1, "upvalue index too large"); if (ci.func.ttislcf()) /* light C function? */ return lobject.luaO_nilobject; /* it has no upvalues */ else { @@ -93,7 +121,7 @@ const index2addr_ = function(L, idx) { api_check(L, idx <= ci.top - (ci.funcOff + 1), "unacceptable index"); if (o >= L.top) return null; else return o; - } else if (idx > defs.LUA_REGISTRYINDEX) { + } else if (idx > LUA_REGISTRYINDEX) { api_check(L, idx !== 0 && -idx <= L.top, "invalid index"); return L.top + idx; } else { /* registry or upvalue */ @@ -109,7 +137,7 @@ const lua_checkstack = function(L, n) { res = true; else { /* no; need to grow stack */ let inuse = L.top + lstate.EXTRA_STACK; - if (inuse > luaconf.LUAI_MAXSTACK - n) /* can grow without overflow? */ + if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ res = false; /* no */ else { /* try to grow stack */ ldo.luaD_growstack(L, n); @@ -145,7 +173,7 @@ const lua_xmove = function(from, to, n) { ** convert an acceptable stack index into an absolute index */ const lua_absindex = function(L, idx) { - return (idx > 0 || idx <= defs.LUA_REGISTRYINDEX) + return (idx > 0 || idx <= LUA_REGISTRYINDEX) ? idx : (L.top - L.ci.funcOff) + idx; }; @@ -193,7 +221,7 @@ const lua_rotate = function(L, idx, n) { let t = L.top - 1; let pIdx = index2addr_(L, idx); let p = L.stack[pIdx]; - api_check(L, isvalid(p) && idx > defs.LUA_REGISTRYINDEX, "index not in the stack"); + api_check(L, isvalid(p) && idx > LUA_REGISTRYINDEX, "index not in the stack"); api_check(L, (n >= 0 ? n : -n) <= (t - pIdx + 1), "invalid 'n'"); let m = n >= 0 ? t - n : pIdx - n - 1; /* end of prefix */ reverse(L, pIdx, m); @@ -225,19 +253,19 @@ const lua_replace = function(L, idx) { */ const lua_pushnil = function(L) { - L.stack[L.top] = new TValue(CT.LUA_TNIL, null); + L.stack[L.top] = new TValue(LUA_TNIL, null); api_incr_top(L); }; const lua_pushnumber = function(L, n) { fengari_argcheck(L, typeof n === "number"); - L.stack[L.top] = new TValue(CT.LUA_TNUMFLT, n); + L.stack[L.top] = new TValue(LUA_TNUMFLT, n); api_incr_top(L); }; const lua_pushinteger = function(L, n) { fengari_argcheck(L, typeof n === "number" && (n|0) === n); - L.stack[L.top] = new TValue(CT.LUA_TNUMINT, n); + L.stack[L.top] = new TValue(LUA_TNUMINT, n); api_incr_top(L); }; @@ -245,10 +273,10 @@ const lua_pushlstring = function(L, s, len) { fengari_argcheck(L, typeof len === "number"); let ts; if (len === 0) { - s = defs.to_luastring("", true); + s = to_luastring("", true); ts = luaS_bless(L, s); } else { - s = defs.from_userstring(s); + s = from_userstring(s); api_check(L, s.length >= len, "invalid length to lua_pushlstring"); ts = luaS_new(L, s.subarray(0, len)); } @@ -259,10 +287,10 @@ const lua_pushlstring = function(L, s, len) { const lua_pushstring = function (L, s) { if (s === undefined || s === null) { - L.stack[L.top] = new TValue(CT.LUA_TNIL, null); + L.stack[L.top] = new TValue(LUA_TNIL, null); L.top++; } else { - let ts = luaS_new(L, defs.from_userstring(s)); + let ts = luaS_new(L, from_userstring(s)); lobject.pushsvalue2s(L, ts); s = ts.getstr(); /* internal copy */ } @@ -271,19 +299,19 @@ const lua_pushstring = function (L, s) { }; const lua_pushvfstring = function (L, fmt, argp) { - fmt = defs.from_userstring(fmt); + fmt = from_userstring(fmt); return lobject.luaO_pushvfstring(L, fmt, argp); }; const lua_pushfstring = function (L, fmt, ...argp) { - fmt = defs.from_userstring(fmt); + fmt = from_userstring(fmt); return lobject.luaO_pushvfstring(L, fmt, argp); }; /* Similar to lua_pushstring, but takes a JS string */ const lua_pushliteral = function (L, s) { if (s === undefined || s === null) { - L.stack[L.top] = new TValue(CT.LUA_TNIL, null); + L.stack[L.top] = new TValue(LUA_TNIL, null); L.top++; } else { fengari_argcheck(typeof s === "string", "lua_pushliteral expects a JS string"); @@ -299,10 +327,10 @@ const lua_pushliteral = function (L, s) { const lua_pushcclosure = function(L, fn, n) { fengari_argcheck(typeof fn === "function" || typeof n === "number"); if (n === 0) - L.stack[L.top] = new TValue(CT.LUA_TLCF, fn); + L.stack[L.top] = new TValue(LUA_TLCF, fn); else { api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); + api_check(L, n <= lfunc.MAXUPVAL, "upvalue index too large"); let cl = new CClosure(L, fn, n); for (let i=0; i= 1) { /* does it have an upvalue? */ /* get global table from registry */ - let gt = ltable.luaH_getint(L.l_G.l_registry.value, defs.LUA_RIDX_GLOBALS); + let gt = ltable.luaH_getint(L.l_G.l_registry.value, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ f.upvals[0].v.setfrom(gt); } @@ -912,7 +940,7 @@ const lua_dump = function(L, writer, data, strip) { api_checknelems(L, 1); let o = L.stack[L.top -1]; if (o.ttisLclosure()) - return ldump.luaU_dump(L, o.value.p, writer, data, strip); + return luaU_dump(L, o.value.p, writer, data, strip); return 1; }; @@ -929,14 +957,14 @@ const lua_setuservalue = function(L, idx) { }; const checkresults = function(L,na,nr) { - api_check(L, (nr) == defs.LUA_MULTRET || (L.ci.top - L.top >= (nr) - (na)), + api_check(L, (nr) == LUA_MULTRET || (L.ci.top - L.top >= (nr) - (na)), "results from function overflow current stack size"); }; const lua_callk = function(L, nargs, nresults, ctx, k) { api_check(L, k === null || !(L.ci.callstatus & lstate.CIST_LUA), "cannot use continuations inside hooks"); api_checknelems(L, nargs + 1); - api_check(L, L.status === TS.LUA_OK, "cannot do calls on non-normal thread"); + api_check(L, L.status === LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); let func = L.top - (nargs + 1); if (k !== null && L.nny === 0) { /* need to prepare continuation? */ @@ -947,7 +975,7 @@ const lua_callk = function(L, nargs, nresults, ctx, k) { ldo.luaD_callnoyield(L, func, nresults); } - if (nresults === defs.LUA_MULTRET && L.ci.top < L.top) + if (nresults === LUA_MULTRET && L.ci.top < L.top) L.ci.top = L.top; }; @@ -958,7 +986,7 @@ const lua_call = function(L, n, r) { const lua_pcallk = function(L, nargs, nresults, errfunc, ctx, k) { api_check(L, k === null || !(L.ci.callstatus & lstate.CIST_LUA), "cannot use continuations inside hooks"); api_checknelems(L, nargs + 1); - api_check(L, L.status === TS.LUA_OK, "cannot do calls on non-normal thread"); + api_check(L, L.status === LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); let c = { func: null, @@ -995,10 +1023,10 @@ const lua_pcallk = function(L, nargs, nresults, errfunc, ctx, k) { ldo.luaD_call(L, c.funcOff, nresults); /* do the call */ ci.callstatus &= ~lstate.CIST_YPCALL; L.errfunc = ci.c_old_errfunc; - status = TS.LUA_OK; + status = LUA_OK; } - if (nresults === defs.LUA_MULTRET && L.ci.top < L.top) + if (nresults === LUA_MULTRET && L.ci.top < L.top) L.ci.top = L.top; return status; @@ -1037,7 +1065,7 @@ const lua_concat = function(L, n) { if (n >= 2) lvm.luaV_concat(L, n); else if (n === 0) { - lobject.pushsvalue2s(L, luaS_bless(L, defs.to_luastring("", true))); + lobject.pushsvalue2s(L, luaS_bless(L, to_luastring("", true))); api_check(L, L.top <= L.ci.top, "stack overflow"); } }; @@ -1065,10 +1093,10 @@ const getupvalref = function(L, fidx, n) { const lua_upvalueid = function(L, fidx, n) { let fi = index2addr(L, fidx); switch (fi.ttype()) { - case CT.LUA_TLCL: { /* lua closure */ + case LUA_TLCL: { /* lua closure */ return getupvalref(L, fidx, n).upval; } - case CT.LUA_TCCL: { /* C closure */ + case LUA_TCCL: { /* C closure */ let f = fi.value; api_check(L, 1 <= n && n <= f.nupvalues, "invalid upvalue index"); return f.upvalue[n - 1]; -- cgit v1.2.3-70-g09d2