diff options
Diffstat (limited to 'src/lvm.js')
-rw-r--r-- | src/lvm.js | 88 |
1 files changed, 48 insertions, 40 deletions
@@ -123,8 +123,15 @@ const luaV_execute = function(L) { base = ci.u.l.base; i = ci.u.l.savedpc[ci.pcOff++]; + + if (L.hookmask & (lua.LUA_MASKLINE | lua.LUA_MASKCOUNT)) { + ldebug.luaG_traceexec(L); + base = ci.u.l.base; + } + + ra = RA(L, base, i); - opcode = i.opcode + opcode = i.opcode; } if (i.breakpoint) // TODO: remove, used until lapi @@ -228,7 +235,7 @@ const luaV_execute = function(L) { if (op1.ttisinteger() && op2.ttisinteger()) { L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value + op2.value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, op1.value + op2.value); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 + numberop2); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_ADD); base = ci.u.l.base; @@ -244,7 +251,7 @@ const luaV_execute = function(L) { if (op1.ttisinteger() && op2.ttisinteger()) { L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value - op2.value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, op1.value - op2.value); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 - numberop2); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_SUB); base = ci.u.l.base; @@ -260,7 +267,7 @@ const luaV_execute = function(L) { if (op1.ttisinteger() && op2.ttisinteger()) { L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value * op2.value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value * op2.value); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 * numberop2); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_MUL); base = ci.u.l.base; @@ -274,9 +281,9 @@ const luaV_execute = function(L) { let numberop2 = tonumber(op2); if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value % op2.value)); + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value - Math.floor(op1.value / op2.value) * op2.value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value % op2.value); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, (numberop1 - Math.floor(numberop1 / numberop2) * numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_MOD); base = ci.u.l.base; @@ -290,7 +297,7 @@ const luaV_execute = function(L) { let numberop2 = tonumber(op2); if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.pow(op1.value, op2.value)); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.pow(numberop1, numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_POW); base = ci.u.l.base; @@ -320,7 +327,7 @@ const luaV_execute = function(L) { if (op1.ttisinteger() && op2.ttisinteger()) { L.stack[ra] = new TValue(CT.LUA_TNUMINT, Math.floor(op1.value / op2.value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.floor(op1.value / op2.value)); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.floor(numberop1 / numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_IDIV); base = ci.u.l.base; @@ -330,11 +337,11 @@ const luaV_execute = function(L) { case OCi.OP_BAND: { let op1 = RKB(L, base, k, i); let op2 = RKC(L, base, k, i); - let numberop1 = tonumber(op1); - let numberop2 = tonumber(op2); + let numberop1 = tointeger(op1); + let numberop2 = tointeger(op2); - if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value & op2.value)); + if (numberop1 !== false && numberop2) { + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (numberop1 & numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_BAND); base = ci.u.l.base; @@ -344,11 +351,11 @@ const luaV_execute = function(L) { case OCi.OP_BOR: { let op1 = RKB(L, base, k, i); let op2 = RKC(L, base, k, i); - let numberop1 = tonumber(op1); - let numberop2 = tonumber(op2); + let numberop1 = tointeger(op1); + let numberop2 = tointeger(op2); - if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value | op2.value)); + if (numberop1 !== false && numberop2) { + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (numberop1 | numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_BOR); base = ci.u.l.base; @@ -358,11 +365,11 @@ const luaV_execute = function(L) { case OCi.OP_BXOR: { let op1 = RKB(L, base, k, i); let op2 = RKC(L, base, k, i); - let numberop1 = tonumber(op1); - let numberop2 = tonumber(op2); + let numberop1 = tointeger(op1); + let numberop2 = tointeger(op2); - if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value ^ op2.value)); + if (numberop1 !== false && numberop2) { + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (numberop1 ^ numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_BXOR); base = ci.u.l.base; @@ -372,11 +379,11 @@ const luaV_execute = function(L) { case OCi.OP_SHL: { let op1 = RKB(L, base, k, i); let op2 = RKC(L, base, k, i); - let numberop1 = tonumber(op1); - let numberop2 = tonumber(op2); + let numberop1 = tointeger(op1); + let numberop2 = tointeger(op2); - if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value << op2.value)); + if (numberop1 !== false && numberop2) { + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (numberop1 << numberop2)); // TODO: luaV_shiftl ? } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_SHL); base = ci.u.l.base; @@ -386,11 +393,11 @@ const luaV_execute = function(L) { case OCi.OP_SHR: { let op1 = RKB(L, base, k, i); let op2 = RKC(L, base, k, i); - let numberop1 = tonumber(op1); - let numberop2 = tonumber(op2); + let numberop1 = tointeger(op1); + let numberop2 = tointeger(op2); - if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value >> op2.value)); + if (numberop1 !== false && numberop2) { + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (numberop1 >> numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_SHR); base = ci.u.l.base; @@ -404,7 +411,7 @@ const luaV_execute = function(L) { if (op.ttisinteger()) { L.stack[ra] = new TValue(CT.LUA_TNUMINT, -op.value); } else if (numberop !== false) { - L.stack[ra] = new TValue(CT.LUA_TNUMFLT, -op.value); + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, -numberop); } else { ltm.luaT_trybinTM(L, op, op, ra, ltm.TMS.TM_UNM); base = ci.u.l.base; @@ -595,19 +602,19 @@ const luaV_execute = function(L) { let nstep = tonumber(pstep); if (nlimit === false) - ldebug.luaG_runerror(L, lua.to_luastring("'for' limit must be a number")); + ldebug.luaG_runerror(L, lua.to_luastring("'for' limit must be a number", true)); plimit.type = CT.LUA_TNUMFLT; plimit.value = nlimit; if (nstep === false) - ldebug.luaG_runerror(L, lua.to_luastring("'for' step must be a number")); + ldebug.luaG_runerror(L, lua.to_luastring("'for' step must be a number", true)); pstep.type = CT.LUA_TNUMFLT; pstep.value = nstep; if (ninit === false) - ldebug.luaG_runerror(L, lua.to_luastring("'for' initial value must be a number")); + ldebug.luaG_runerror(L, lua.to_luastring("'for' initial value must be a number", true)); init.type = CT.LUA_TNUMFLT; init.value = ninit - nstep; @@ -768,9 +775,10 @@ const luaV_equalobj = function(L, t1, t2) { switch(t1.ttype()) { case CT.LUA_TNIL: return 1; + case CT.LUA_TBOOLEAN: + return t1.value == t2.value ? 1 : 0; // Might be 1 or true case CT.LUA_TNUMINT: case CT.LUA_TNUMFLT: - case CT.LUA_TBOOLEAN: case CT.LUA_TLCF: return t1.value === t2.value ? 1 : 0; case CT.LUA_TSHRSTR: @@ -861,7 +869,7 @@ const luaV_tointeger = function(obj, mode) { } else if (obj.ttisinteger()) { return obj.value; } else if (obj.ttisstring()) { - return luaV_tointeger(new TValue(CT.LUA_TNUMFLT, parseFloat(obj.jsstring())), mode); // TODO: luaO_str2num + return luaV_tointeger(lobject.luaO_str2num(obj.value), mode); } return false; @@ -876,7 +884,7 @@ const tonumber = function(v) { return v.value; if (v.ttnov() === CT.LUA_TSTRING) - return parseFloat(v.value); // TODO: luaO_str2num + return lobject.luaO_str2num(v.value); return false; }; @@ -980,7 +988,7 @@ const luaV_objlen = function(L, ra, rb) { default: { tm = ltm.luaT_gettmbyobj(L, rb, ltm.TMS.TM_LEN); if (tm.ttisnil()) - ldebug.luaG_typeerror(L, rb, lua.to_luastring("get length of")); + ldebug.luaG_typeerror(L, rb, lua.to_luastring("get length of", true)); break; } } @@ -1045,7 +1053,7 @@ const gettable = function(L, table, key, ra, recur) { recur = recur ? recur : 0; if (recur >= MAXTAGRECUR) - ldebug.luaG_runerror(L, lua.to_luastring("'__index' chain too long; possible loop")); + ldebug.luaG_runerror(L, lua.to_luastring("'__index' chain too long; possible loop", true)); if (table.ttistable()) { let element = table.__index(table, key); @@ -1066,7 +1074,7 @@ const luaV_finishget = function(L, t, key, val, slot, recur) { assert(!t.ttistable()); tm = ltm.luaT_gettmbyobj(L, t, ltm.TMS.TM_INDEX); if (tm.ttisnil()) - ldebug.luaG_typeerror(L, t, lua.to_luastring('index')); + ldebug.luaG_typeerror(L, t, lua.to_luastring('index', true)); } else { /* 't' is a table */ assert(slot.ttisnil()); tm = ltm.luaT_gettmbyobj(L, t, ltm.TMS.TM_INDEX); // TODO: fasttm @@ -1088,7 +1096,7 @@ const settable = function(L, table, key, v, recur) { recur = recur ? recur : 0; if (recur >= MAXTAGRECUR) - ldebug.luaG_runerror(L, lua.to_luastring("'__newindex' chain too long; possible loop")); + ldebug.luaG_runerror(L, lua.to_luastring("'__newindex' chain too long; possible loop", true)); if (table.ttistable()) { let element = table.__index(table, key); @@ -1115,7 +1123,7 @@ const luaV_finishset = function(L, t, key, val, slot, recur) { } else { /* not a table; check metamethod */ tm = ltm.luaT_gettmbyobj(L, t, ltm.TMS.TM_NEWINDEX); if (tm.ttisnil()) - ldebug.luaG_typeerror(L, t, lua.to_luastring('index')); + ldebug.luaG_typeerror(L, t, lua.to_luastring('index', true)); } if (tm.ttisfunction()) { |