From bdaa2a67b491bfc1d9521778cb3d13d386f97f93 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 21 May 2017 18:56:10 +1000 Subject: Use same mod operator behaviour from both lvm.js and lobject.js --- src/llimit.js | 8 ++++++++ src/lobject.js | 6 +----- 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); } }; diff --git a/src/lvm.js b/src/lvm.js index 300b080..b00333a 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -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); } -- cgit v1.2.3-70-g09d2