aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lapi.js5
-rw-r--r--src/ldebug.js9
-rw-r--r--src/ldo.js18
-rw-r--r--src/lobject.js6
-rw-r--r--src/ltm.js6
-rw-r--r--src/lvm.js34
6 files changed, 42 insertions, 36 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 92e1612..00e608f 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -163,7 +163,7 @@ const lua_pop = function(L, n) {
const reverse = function(L, from, to) {
for (; from < to; from++, to--) {
let temp = L.stack[from];
- L.stack[from] = L.stack[to];
+ lobject.setobjs2s(L, from, to);
L.stack[to] = temp;
}
};
@@ -892,7 +892,8 @@ const lua_arith = function(L, op) {
assert(2 < L.top - L.ci.funcOff, "not enough elements in the stack"); /* all other operations expect two operands */
else { /* for unary operations, add fake 2nd operand */
assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
- L.stack[L.top++] = L.stack[L.top - 1];
+ lobject.setobjs2s(L, L.top, L.top - 1);
+ L.top++;
}
/* first operand at top - 2, second at top - 1; result go to top - 2 */
lobject.luaO_arith(L, op, L.stack[L.top - 2], L.stack[L.top - 1], L.stack[L.top - 2]);
diff --git a/src/ldebug.js b/src/ldebug.js
index 0f387af..0231e67 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -155,7 +155,7 @@ const lua_setlocal = function(L, ar, n) {
let name = local.name;
let pos = local.pos;
if (name) {
- L.stack[pos] = L.stack[L.top - 1];
+ lobject.setobjs2s(L, pos, L.top - 1);
delete L.stack[--L.top]; /* pop value */
}
swapextra(L);
@@ -280,7 +280,8 @@ const lua_getinfo = function(L, what, ar) {
cl = func.ttisclosure() ? func.value : null;
status = auxgetinfo(L, what, ar, cl, ci);
if (what.indexOf('f'.charCodeAt(0)) >= 0) {
- L.stack[L.top++] = func;
+ lobject.setobjs2s(L, L.top, funcOff);
+ L.top++;
assert(L.top <= L.ci.top, "stack overflow");
}
@@ -590,8 +591,8 @@ const luaG_runerror = function(L, fmt, ...argp) {
const luaG_errormsg = function(L) {
if (L.errfunc !== 0) { /* is there an error handling function? */
let errfunc = L.errfunc;
- L.stack[L.top] = L.stack[L.top - 1];
- L.stack[L.top - 1] = L.stack[errfunc];
+ lobject.setobjs2s(L, L.top, L.top - 1); /* move argument */
+ lobject.setobjs2s(L, L.top - 1, errfunc); /* push function */
L.top++;
ldo.luaD_callnoyield(L, L.top - 2, 1);
}
diff --git a/src/ldo.js b/src/ldo.js
index 4c198ee..d236968 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -32,7 +32,7 @@ const seterrorobj = function(L, errcode, oldtop) {
break;
}
default: {
- L.stack[oldtop] = L.stack[L.top - 1];
+ lobject.setobjs2s(L, oldtop, L.top - 1);
}
}
@@ -186,12 +186,12 @@ const moveresults = function(L, firstResult, res, nres, wanted) {
case 1: {
if (nres === 0)
L.stack[firstResult] = lobject.luaO_nilobject;
- L.stack[res] = L.stack[firstResult];
+ lobject.setobjs2s(L, res, firstResult); /* move it to proper place */
break;
}
case defs.LUA_MULTRET: {
for (let i = 0; i < nres; i++)
- L.stack[res + i] = L.stack[firstResult + i];
+ lobject.setobjs2s(L, res + i, firstResult + i);
L.top = res + nres;
return false;
}
@@ -199,11 +199,11 @@ const moveresults = function(L, firstResult, res, nres, wanted) {
let i;
if (wanted <= nres) {
for (i = 0; i < wanted; i++) {
- L.stack[res + i] = L.stack[firstResult + i];
+ lobject.setobjs2s(L, res + i, firstResult + i);
}
} else {
for (i = 0; i < nres; i++)
- L.stack[res + i] = L.stack[firstResult + i];
+ lobject.setobjs2s(L, res + i, firstResult + i);
for (; i < wanted; i++)
L.stack[res + i] = new lobject.TValue(CT.LUA_TNIL, null);
}
@@ -252,7 +252,7 @@ const adjust_varargs = function(L, p, actual) {
let i;
for (i = 0; i < nfixargs && i < actual; i++) {
- L.stack[L.top++] = L.stack[fixed + i];
+ lobject.setobjs2s(L, L.top++, fixed + i);
L.stack[fixed + i] = new lobject.TValue(CT.LUA_TNIL, null);
}
@@ -268,7 +268,7 @@ const tryfuncTM = function(L, off, func) {
ldebug.luaG_typeerror(L, func, defs.to_luastring("call", true));
/* Open a hole inside the stack at 'func' */
for (let p = L.top; p > off; p--)
- L.stack[p] = L.stack[p-1];
+ lobject.setobjs2s(L, p, p-1);
L.top++; /* slot ensured by caller */
L.stack[off] = new lobject.TValue(tm.type, tm.value); /* tag method is the new function to be called */
};
@@ -351,8 +351,8 @@ const luaD_rawrunprotected = function(L, f, ud) {
/* copy of luaG_errormsg without the throw */
if (L.errfunc !== 0) { /* is there an error handling function? */
let errfunc = L.errfunc;
- L.stack[L.top] = L.stack[L.top - 1];
- L.stack[L.top - 1] = L.stack[errfunc];
+ lobject.setobjs2s(L, L.top, L.top - 1); /* move argument */
+ lobject.setobjs2s(L, L.top - 1, errfunc); /* push function */
L.top++;
luaD_callnoyield(L, L.top - 2, 1);
}
diff --git a/src/lobject.js b/src/lobject.js
index b6fd581..7b8a332 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -185,6 +185,11 @@ class TValue {
}
+/* from stack to (same) stack */
+const setobjs2s = function(L, newidx, oldidx) {
+ L.stack[newidx] = L.stack[oldidx];
+};
+
const luaO_nilobject = new TValue(CT.LUA_TNIL, null);
Object.freeze(luaO_nilobject);
module.exports.luaO_nilobject = luaO_nilobject;
@@ -667,3 +672,4 @@ module.exports.luaO_tostring = luaO_tostring;
module.exports.luaO_utf8desc = luaO_utf8desc;
module.exports.luaO_utf8esc = luaO_utf8esc;
module.exports.numarith = numarith;
+module.exports.setobjs2s = setobjs2s;
diff --git a/src/ltm.js b/src/ltm.js
index ad09c26..041dd19 100644
--- a/src/ltm.js
+++ b/src/ltm.js
@@ -108,7 +108,6 @@ const luaT_objtypename = function(L, o) {
};
const luaT_callTM = function(L, f, p1, p2, p3, hasres) {
- let result = p3;
let func = L.top;
L.stack[L.top] = new lobject.TValue(f.type, f.value); /* push function (assume EXTRA_STACK) */
@@ -124,9 +123,8 @@ const luaT_callTM = function(L, f, p1, p2, p3, hasres) {
else
ldo.luaD_callnoyield(L, func, hasres);
- if (hasres) {
- assert(typeof result === "number");
- L.stack[result] = L.stack[--L.top];
+ if (hasres) { /* if has result, move it to its place */
+ lobject.setobjs2s(L, p3, --L.top);
}
};
diff --git a/src/lvm.js b/src/lvm.js
index 07acb42..602fe5c 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -34,7 +34,7 @@ const luaV_finishOp = function(L) {
case OCi.OP_MOD: case OCi.OP_POW:
case OCi.OP_UNM: case OCi.OP_BNOT: case OCi.OP_LEN:
case OCi.OP_GETTABUP: case OCi.OP_GETTABLE: case OCi.OP_SELF: {
- L.stack[base + inst.A] = L.stack[--L.top];
+ lobject.setobjs2s(L, base + inst.A, --L.top);
break;
}
case OCi.OP_LE: case OCi.OP_LT: case OCi.OP_EQ: {
@@ -121,7 +121,7 @@ const luaV_execute = function(L) {
switch (opcode) {
case OCi.OP_MOVE: {
- L.stack[ra] = L.stack[RB(L, base, i)];
+ lobject.setobjs2s(L, ra, RB(L, base, i));
break;
}
case OCi.OP_LOADK: {
@@ -197,12 +197,11 @@ const luaV_execute = function(L) {
break;
}
case OCi.OP_SELF: {
- let table = L.stack[RB(L, base, i)];
- let key = RKC(L, base, k, i);
-
- L.stack[ra + 1] = table;
+ let rb = RB(L, base, i);
+ let rc = RKC(L, base, k, i);
+ lobject.setobjs2s(L, ra + 1, rb);
- gettable(L, table, key, ra);
+ gettable(L, L.stack[rb], rc, ra);
break;
}
case OCi.OP_ADD: {
@@ -409,7 +408,7 @@ const luaV_execute = function(L) {
L.top = base + c + 1; /* mark the end of concat operands */
luaV_concat(L, c - b + 1);
let rb = base + b;
- L.stack[ra] = L.stack[rb];
+ lobject.setobjs2s(L, ra, rb);
L.top = ci.top; /* restore top */
break;
}
@@ -446,11 +445,12 @@ const luaV_execute = function(L) {
break;
}
case OCi.OP_TESTSET: {
- let rb = L.stack[RB(L, base, i)];
+ let rbIdx = RB(L, base, i);
+ let rb = L.stack[rbIdx];
if (i.C ? rb.l_isfalse() : !rb.l_isfalse())
ci.l_savedpc++;
else {
- L.stack[ra] = rb;
+ lobject.setobjs2s(L, ra, rbIdx);
donextjump(L, ci);
}
break;
@@ -485,7 +485,7 @@ const luaV_execute = function(L) {
let lim = nci.l_base + nfunc.value.p.numparams;
if (cl.p.p.length > 0) lfunc.luaF_close(L, oci.l_base);
for (let aux = 0; nfuncOff + aux < lim; aux++)
- L.stack[ofuncOff + aux] = L.stack[nfuncOff + aux];
+ lobject.setobjs2s(L, ofuncOff + aux, nfuncOff + aux);
oci.func = nci.func;
oci.l_base = ofuncOff + (nci.l_base - nfuncOff);
oci.top = L.top = ofuncOff + (L.top - nfuncOff);
@@ -565,9 +565,9 @@ const luaV_execute = function(L) {
}
case OCi.OP_TFORCALL: {
let cb = ra + 3; /* call base */
- L.stack[cb + 2] = L.stack[ra + 2];
- L.stack[cb + 1] = L.stack[ra + 1];
- L.stack[cb] = L.stack[ra];
+ lobject.setobjs2s(L, cb+2, ra+2);
+ lobject.setobjs2s(L, cb+1, ra+1);
+ lobject.setobjs2s(L, cb, ra);
L.top = cb + 3; /* func. + 2 args (state and index) */
ldo.luaD_call(L, cb, i.C);
/* go straight to OP_TFORLOOP */
@@ -579,7 +579,7 @@ const luaV_execute = function(L) {
}
case OCi.OP_TFORLOOP: {
if (!L.stack[ra + 1].ttisnil()) { /* continue loop? */
- L.stack[ra] = L.stack[ra + 1]; /* save control variable */
+ lobject.setobjs2s(L, ra, ra + 1); /* save control variable */
ci.l_savedpc += i.sBx; /* jump back */
}
break;
@@ -625,7 +625,7 @@ const luaV_execute = function(L) {
}
for (j = 0; j < b && j < n; j++)
- L.stack[ra + j] = L.stack[base - n + j];
+ lobject.setobjs2s(L, ra + j, base - n + j);
for (; j < b; j++) /* complete required results with nil */
L.stack[ra + j] = new lobject.TValue(CT.LUA_TNIL, null);
@@ -996,7 +996,7 @@ const luaV_concat = function(L, total) {
tostring(L, top - 2);
delete L.stack[top - 1];
} else if (isemptystr(L.stack[top-2])) {
- L.stack[top - 2] = L.stack[top - 1];
+ lobject.setobjs2s(L, top - 2, top - 1);
delete L.stack[top - 1];
} else {
/* at least two non-empty string values; get as many as possible */