diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-03-24 12:23:30 +0100 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-03-24 12:23:30 +0100 |
commit | f893be002aebd215b499b0c72803dd1346f4eb05 (patch) | |
tree | a2c83ccfacfb50e3dd1487e2d8b7562fafc4bb5c | |
parent | 16cdb860e9e8e5550c1178a41eabb52e461ffa46 (diff) | |
download | fengari-f893be002aebd215b499b0c72803dd1346f4eb05.tar.gz fengari-f893be002aebd215b499b0c72803dd1346f4eb05.tar.bz2 fengari-f893be002aebd215b499b0c72803dd1346f4eb05.zip |
Fixed bad lua_tointegerx
-rw-r--r-- | src/lapi.js | 6 | ||||
-rw-r--r-- | src/lobject.js | 3 | ||||
-rw-r--r-- | src/luaconf.js | 2 | ||||
-rw-r--r-- | src/lvm.js | 6 | ||||
-rw-r--r-- | tests/single.lua | 7 |
5 files changed, 15 insertions, 9 deletions
diff --git a/src/lapi.js b/src/lapi.js index 3520b15..d1ca02f 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -652,11 +652,7 @@ const lua_stringtonumber = function(L, s) { // TODO: pisnum const lua_tointegerx = function(L, idx) { - let o = index2addr(L, idx); - let res = lvm.tointeger(o); - if (res === false) - res = 0; /* call to 'tointeger' may change 'n' even if it fails */ - return res; + return lvm.tointeger(index2addr(L, idx)); }; const f_call = function(L, ud) { diff --git a/src/lobject.js b/src/lobject.js index 3cbde26..bdbccb9 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -342,7 +342,8 @@ const l_str2dloc = function(s, mode) { }; const l_str2d = function(s) { - let pidx = /[.xXnN]/g.exec(s).index; + let pidx = /[.xXnN]/g.exec(s); + pidx = pidx ? pidx.index : null; let pmode = pidx ? s[pidx] : null; let mode = pmode ? pmode.toLowerCase() : 0; if (mode === 'n') /* reject 'inf' and 'nan' */ diff --git a/src/luaconf.js b/src/luaconf.js index a965954..190893a 100644 --- a/src/luaconf.js +++ b/src/luaconf.js @@ -17,7 +17,7 @@ const LUAI_MAXSTACK = 1000000; const LUA_IDSIZE = 60; const lua_numbertointeger = function(n) { - return n; + return n >= Number.MIN_SAFE_INTEGER && n < -Number.MIN_SAFE_INTEGER ? n : 0; }; const LUA_INTEGER_FRMLEN = ""; @@ -6,6 +6,7 @@ const assert = require('assert'); const BytecodeParser = require('./lundump.js'); const OC = require('./lopcodes.js'); const lua = require('./lua.js'); +const luaconf = require('./luaconf.js'); const CT = lua.constant_types; const LUA_MULTRET = lua.LUA_MULTRET; const lobject = require('./lobject.js'); @@ -843,7 +844,7 @@ const forlimit = function(obj, step) { const luaV_tointeger = function(obj, mode) { if (obj.ttisfloat()) { let n = obj.value; - let f = n; + let f = Math.floor(n); if (n !== f) { /* not an integral value? */ if (mode === 0) @@ -852,7 +853,8 @@ const luaV_tointeger = function(obj, mode) { f += 1; /* convert floor to ceil (remember: n !== f) */ } - return f; + let res = luaconf.lua_numbertointeger(f); + return res !== 0 ? res : (n === 0 ? 0 : false); } else if (obj.ttisinteger()) { return obj.value; } else if (obj.ttisstring()) { diff --git a/tests/single.lua b/tests/single.lua index 4960b11..f74c096 100644 --- a/tests/single.lua +++ b/tests/single.lua @@ -131,3 +131,10 @@ assert(tostring(-1203) == "-1203") assert(tostring(1203.125) == "1203.125") assert(tostring(-0.5) == "-0.5") assert(tostring(-32767) == "-32767") +if math.tointeger(2147483647) then -- no overflow? (32 bits) + assert(tostring(-2147483647) == "-2147483647") +end +if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) + assert(tostring(4611686018427387904) == "4611686018427387904") + assert(tostring(-4611686018427387904) == "-4611686018427387904") +end |