From b9f9cde1c18ace3930be4cb7f6d9527178544d8b Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 24 Apr 2017 15:00:52 +0200 Subject: Force 32bit integer with |0 --- src/lapi.js | 3 +-- src/lobject.js | 10 +++++----- src/lvm.js | 10 +++++----- 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 0231597..51b869f 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -213,7 +213,7 @@ const lua_pushnumber = function(L, n) { const lua_pushinteger = function(L, n) { assert(typeof n === "number"); - L.stack[L.top++] = new TValue(CT.LUA_TNUMINT, n); + L.stack[L.top++] = new TValue(CT.LUA_TNUMINT, n|0); assert(L.top <= L.ci.top, "stack overflow"); }; @@ -726,7 +726,6 @@ const lua_stringtonumber = function(L, s) { return s.length; }; -// TODO: pisnum const lua_tointegerx = function(L, idx) { return lvm.tointeger(index2addr(L, idx)); }; diff --git a/src/lobject.js b/src/lobject.js index 7409415..0f71405 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -524,11 +524,11 @@ const luaO_int2fb = function(x) { const intarith = function(L, op, v1, v2) { switch (op) { - case lua.LUA_OPADD: return (v1 + v2); - case lua.LUA_OPSUB: return (v1 - v2); - case lua.LUA_OPMUL: return (v1 * v2); - case lua.LUA_OPMOD: return (v1 - Math.floor(v1 / v2) * v2); // % semantic on negative numbers is different in js - case lua.LUA_OPIDIV: return (v1 / v2); + case lua.LUA_OPADD: return (v1 + v2)|0; + case lua.LUA_OPSUB: return (v1 - v2)|0; + case lua.LUA_OPMUL: return (v1 * v2)|0; + case lua.LUA_OPMOD: return (v1 - Math.floor(v1 / v2) * v2)|0; // % semantic on negative numbers is different in js + case lua.LUA_OPIDIV: return (v1 / v2)|0; case lua.LUA_OPBAND: return (v1 & v2); case lua.LUA_OPBOR: return (v1 | v2); case lua.LUA_OPBXOR: return (v1 ^ v2); diff --git a/src/lvm.js b/src/lvm.js index 209cd6f..43ea9b2 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -232,7 +232,7 @@ 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 + op2.value)|0); } else if (numberop1 !== false && numberop2 !== false) { L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 + numberop2); } else { @@ -248,7 +248,7 @@ 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 - op2.value)|0); } else if (numberop1 !== false && numberop2 !== false) { L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 - numberop2); } else { @@ -264,7 +264,7 @@ 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 * op2.value)|0); } else if (numberop1 !== false && numberop2 !== false) { L.stack[ra] = new TValue(CT.LUA_TNUMFLT, numberop1 * numberop2); } else { @@ -280,7 +280,7 @@ const luaV_execute = function(L) { let numberop2 = tonumber(op2); if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value - Math.floor(op1.value / op2.value) * op2.value)); + L.stack[ra] = new TValue(CT.LUA_TNUMINT, (op1.value - Math.floor(op1.value / op2.value) * op2.value)|0); } else if (numberop1 !== false && numberop2 !== false) { L.stack[ra] = new TValue(CT.LUA_TNUMFLT, (numberop1 - Math.floor(numberop1 / numberop2) * numberop2)); } else { @@ -324,7 +324,7 @@ const luaV_execute = function(L) { let numberop2 = tonumber(op2); if (op1.ttisinteger() && op2.ttisinteger()) { - L.stack[ra] = new TValue(CT.LUA_TNUMINT, Math.floor(op1.value / op2.value)); + L.stack[ra] = new TValue(CT.LUA_TNUMINT, Math.floor(op1.value / op2.value)|0); } else if (numberop1 !== false && numberop2 !== false) { L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.floor(numberop1 / numberop2)); } else { -- cgit v1.2.3-54-g00ecf