diff options
author | daurnimator <quae@daurnimator.com> | 2017-05-21 18:56:10 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-05-21 19:12:50 +1000 |
commit | bdaa2a67b491bfc1d9521778cb3d13d386f97f93 (patch) | |
tree | 8ce64bbf6a390250547d6427bc10595664bd1ced | |
parent | d229ba6542aa009185b28530c55887d14c76b163 (diff) | |
download | fengari-bdaa2a67b491bfc1d9521778cb3d13d386f97f93.tar.gz fengari-bdaa2a67b491bfc1d9521778cb3d13d386f97f93.tar.bz2 fengari-bdaa2a67b491bfc1d9521778cb3d13d386f97f93.zip |
Use same mod operator behaviour from both lvm.js and lobject.js
-rw-r--r-- | src/llimit.js | 8 | ||||
-rw-r--r-- | src/lobject.js | 6 | ||||
-rw-r--r-- | src/lvm.js | 2 |
3 files changed, 10 insertions, 6 deletions
diff --git a/src/llimit.js b/src/llimit.js index c8455b8..31862d0 100644 --- a/src/llimit.js +++ b/src/llimit.js @@ -8,6 +8,14 @@ module.exports.LUA_MAXINTEGER = LUA_MAXINTEGER; const LUA_MININTEGER = -2147483648; module.exports.LUA_MININTEGER = LUA_MININTEGER; +const luai_nummod = function(L, a, b) { + let m = a % b; + if ((m*b) < 0) + m += b; + return m; +}; +module.exports.luai_nummod = luai_nummod; + // If later integers are more than 32bit, LUA_MAXINTEGER will then be != MAX_INT const MAX_INT = 2147483647; module.exports.MAX_INT = MAX_INT; diff --git a/src/lobject.js b/src/lobject.js index ec88e34..6d86852 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -545,11 +545,7 @@ const numarith = function(L, op, v1, v2) { case defs.LUA_OPPOW: return Math.pow(v1, v2); case defs.LUA_OPIDIV: return Math.floor(v1 / v2); case defs.LUA_OPUNM: return -v1; - case defs.LUA_OPMOD: - let m = v1 % v2; - if ((m*v2) < 0) - m += v2; - return m; + case defs.LUA_OPMOD: return llimit.luai_nummod(L, v1, v2); default: assert(0); } }; @@ -259,7 +259,7 @@ const luaV_execute = function(L) { if (op1.ttisinteger() && op2.ttisinteger()) { L.stack[ra] = new lobject.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 lobject.TValue(CT.LUA_TNUMFLT, (numberop1 - Math.floor(numberop1 / numberop2) * numberop2)); + L.stack[ra] = new lobject.TValue(CT.LUA_TNUMFLT, llimit.luai_nummod(L, numberop1, numberop2)); } else { ltm.luaT_trybinTM(L, op1, op2, ra, ltm.TMS.TM_MOD); } |