From 888c1286cfe25c1b21605c3443cc0a20b522972b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 23 May 2017 17:37:20 +1000 Subject: Introduce setobj2s --- src/lapi.js | 27 +++++++++++---------------- src/ldebug.js | 4 +++- src/ldo.js | 2 +- src/lobject.js | 5 +++++ src/ltable.js | 4 ++-- src/ltm.js | 8 ++++---- src/lvm.js | 8 ++++---- 7 files changed, 30 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 00e608f..2cf3a1c 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -112,7 +112,7 @@ const lua_xmove = function(from, to, n) { from.top -= n; for (let i = 0; i < n; i++) { - to.stack[to.top] = from.stack[from.top + i]; + lobject.setobj2s(to, to.top, from.stack[from.top + i]); to.top++; } }; @@ -135,8 +135,7 @@ const lua_gettop = function(L) { }; const lua_pushvalue = function(L, idx) { - L.stack[L.top] = index2addr(L, idx); - + lobject.setobj2s(L, L.top, index2addr(L, idx)); L.top++; assert(L.top <= L.ci.top, "stack overflow"); }; @@ -164,7 +163,7 @@ const reverse = function(L, from, to) { for (; from < to; from++, to--) { let temp = L.stack[from]; lobject.setobjs2s(L, from, to); - L.stack[to] = temp; + lobject.setobj2s(L, to, temp); } }; @@ -474,12 +473,10 @@ const auxgetstr = function(L, t, k) { const lua_rawgeti = function(L, idx, n) { let t = index2addr(L, idx); - assert(t.ttistable(), "table expected"); - - L.stack[L.top++] = ltable.luaH_getint(t.value, n); + lobject.setobj2s(L, L.top, ltable.luaH_getint(t.value, n)); + L.top++; assert(L.top <= L.ci.top, "stack overflow"); - return L.stack[L.top - 1].ttnov(); }; @@ -487,18 +484,16 @@ const lua_rawgetp = function(L, idx, p) { let t = index2addr(L, idx); assert(t.ttistable(), "table expected"); let k = new TValue(CT.LUA_TLIGHTUSERDATA, p); - L.stack[L.top++] = ltable.luaH_get(L, t.value, k); + lobject.setobj2s(L, L.top, ltable.luaH_get(L, t.value, k)); + L.top++; assert(L.top <= L.ci.top, "stack overflow"); return L.stack[L.top - 1].ttnov(); }; const lua_rawget = function(L, idx) { let t = index2addr(L, idx); - assert(t.ttistable(t), "table expected"); - - L.stack[L.top - 1] = ltable.luaH_get(L, t.value, L.stack[L.top - 1]); - + lobject.setobj2s(L, L.top - 1, ltable.luaH_get(L, t.value, L.stack[L.top - 1])); return L.stack[L.top - 1].ttnov(); }; @@ -552,9 +547,9 @@ const lua_getupvalue = function(L, funcindex, n) { if (up) { let name = up.name; let val = up.val; - - L.stack[L.top++] = new TValue(val.type, val.value); - + lobject.setobj2s(L, L.top, val); + L.top++; + assert(L.top <= L.ci.top, "stack overflow"); return name; } return null; diff --git a/src/ldebug.js b/src/ldebug.js index 0231e67..e78d3e7 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -140,7 +140,9 @@ const lua_getlocal = function(L, ar, n) { let local = findlocal(L, ar.i_ci, n); if (local) { name = local.name; - L.stack[L.top++] = L.stack[local.pos]; + lobject.setobj2s(L, L.top, L.stack[local.pos]); + L.top++; + assert(L.top <= L.ci.top, "stack overflow"); } else { name = null; } diff --git a/src/ldo.js b/src/ldo.js index d236968..5fa51db 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -270,7 +270,7 @@ const tryfuncTM = function(L, off, func) { for (let p = L.top; p > off; p--) 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 */ + lobject.setobj2s(L, off, tm); /* tag method is the new function to be called */ }; /* diff --git a/src/lobject.js b/src/lobject.js index 7b8a332..3a442ee 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -189,6 +189,10 @@ class TValue { const setobjs2s = function(L, newidx, oldidx) { L.stack[newidx] = L.stack[oldidx]; }; +/* to stack (not from same stack) */ +const setobj2s = function(L, newidx, oldtv) { + L.stack[newidx] = new TValue(oldtv.type, oldtv.value); +}; const luaO_nilobject = new TValue(CT.LUA_TNIL, null); Object.freeze(luaO_nilobject); @@ -673,3 +677,4 @@ module.exports.luaO_utf8desc = luaO_utf8desc; module.exports.luaO_utf8esc = luaO_utf8esc; module.exports.numarith = numarith; module.exports.setobjs2s = setobjs2s; +module.exports.setobj2s = setobj2s; diff --git a/src/ltable.js b/src/ltable.js index 5a31b48..53a5d79 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -261,8 +261,8 @@ const luaH_next = function(L, table, keyI) { } while (entry.key.ttisdeadkey()); } } - L.stack[keyI] = new lobject.TValue(entry.key.type, entry.key.value); - L.stack[keyI+1] = new lobject.TValue(entry.value.type, entry.value.value); + lobject.setobj2s(L, keyI, entry.key); + lobject.setobj2s(L, keyI+1, entry.value); return true; }; diff --git a/src/ltm.js b/src/ltm.js index 041dd19..c3efc1e 100644 --- a/src/ltm.js +++ b/src/ltm.js @@ -110,13 +110,13 @@ const luaT_objtypename = function(L, o) { const luaT_callTM = function(L, f, p1, p2, p3, hasres) { let func = L.top; - L.stack[L.top] = new lobject.TValue(f.type, f.value); /* push function (assume EXTRA_STACK) */ - L.stack[L.top + 1] = new lobject.TValue(p1.type, p1.value); /* 1st argument */ - L.stack[L.top + 2] = new lobject.TValue(p2.type, p2.value); /* 2nd argument */ + lobject.setobj2s(L, L.top, f); /* push function (assume EXTRA_STACK) */ + lobject.setobj2s(L, L.top + 1, p1); /* 1st argument */ + lobject.setobj2s(L, L.top + 2, p2); /* 2nd argument */ L.top += 3; if (!hasres) /* no result? 'p3' is third argument */ - L.stack[L.top++] = new lobject.TValue(p3.type, p3.value); /* 3rd argument */ + lobject.setobj2s(L, L.top++, p3); /* 3rd argument */ if (L.ci.callstatus & lstate.CIST_LUA) ldo.luaD_call(L, func, hasres); diff --git a/src/lvm.js b/src/lvm.js index 602fe5c..c7bb7ad 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -126,13 +126,13 @@ const luaV_execute = function(L) { } case OCi.OP_LOADK: { let konst = k[i.Bx]; - L.stack[ra] = new lobject.TValue(konst.type, konst.value); + lobject.setobj2s(L, ra, konst); break; } case OCi.OP_LOADKX: { assert(ci.l_code[ci.l_savedpc].opcode === OCi.OP_EXTRAARG); let konst = k[ci.l_code[ci.l_savedpc++].Ax]; - L.stack[ra] = new lobject.TValue(konst.type, konst.value); + lobject.setobj2s(L, ra, konst); break; } case OCi.OP_LOADBOOL: { @@ -150,7 +150,7 @@ const luaV_execute = function(L) { } case OCi.OP_GETUPVAL: { let o = cl.upvals[i.B].val(); - L.stack[ra] = new lobject.TValue(o.type, o.value); + lobject.setobj2s(L, ra, o); break; } case OCi.OP_SETUPVAL: { @@ -1033,7 +1033,7 @@ const gettable = function(L, t, key, ra) { } else { let slot = ltable.luaH_get(L, t.value, key); if (!slot.ttisnil()) { - L.stack[ra] = new lobject.TValue(slot.type, slot.value); + lobject.setobj2s(L, ra, slot); return; } else { /* 't' is a table */ tm = ltm.fasttm(L, t.value.metatable, ltm.TMS.TM_INDEX); /* table's metamethod */ -- cgit v1.2.3-54-g00ecf