From 204ab9fba5a7a1e2e6981c11e0a6c67764332d21 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 10 Apr 2017 13:47:01 +0200 Subject: nan, -inf, inf --- src/lobject.js | 4 ++-- src/lstrlib.js | 8 ++++++-- src/lvm.js | 2 +- tests/single.lua | 24 ++++++++++++------------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/lobject.js b/src/lobject.js index b2cffe3..66a0dcb 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -556,8 +556,8 @@ const intarith = function(L, op, v1, v2) { case lua.LUA_OPBXOR: return (v1 ^ v2); case lua.LUA_OPSHL: return (v1 << v2); case lua.LUA_OPSHR: return (v1 >> v2); - case lua.LUA_OPUNM: return (-v1); - case lua.LUA_OPBNOT: return (~v1); + case lua.LUA_OPUNM: return (0 - v1); + case lua.LUA_OPBNOT: return (~0 ^ v1); } }; diff --git a/src/lstrlib.js b/src/lstrlib.js index f4f254f..73fc068 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -102,8 +102,12 @@ const adddigit = function(buff, n, x) { const num2straux = function(x) { let buff = []; /* if 'inf' or 'NaN', format it like '%g' */ - if (x === Infinity || isNaN(x)) - return sprintf(luaconf.LUA_NUMBER_FMT, x).split('').map(e => e.charCodeAt(0)); + if (Object.is(x, Infinity)) + return lua.to_luastring('inf'); + else if (Object.is(x, -Infinity)) + return lua.to_luastring('-inf'); + else if (Number.isNaN(x)) + return lua.to_luastring('nan'); else if (x === 0) { /* can be -0... */ /* create "0" or "-0" followed by exponent */ let zero = sprintf(luaconf.LUA_NUMBER_FMT + "x0p+0", x).split('').map(e => e.charCodeAt(0)); diff --git a/src/lvm.js b/src/lvm.js index 4786da4..300e622 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -301,7 +301,7 @@ const luaV_execute = function(L) { let numberop2 = tonumber(op2); 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_DIV); base = ci.u.l.base; diff --git a/tests/single.lua b/tests/single.lua index c770d22..c167196 100644 --- a/tests/single.lua +++ b/tests/single.lua @@ -271,17 +271,17 @@ do print("testing 'format %a %A'") assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) - -- if not _port then -- test inf, -inf, NaN, and -0.0 - -- assert(string.find(string.format("%a", 1/0), "^inf")) - -- assert(string.find(string.format("%A", -1/0), "^%-INF")) - -- assert(string.find(string.format("%a", 0/0), "^%-?nan")) - -- assert(string.find(string.format("%a", -0.0), "^%-0x0")) - -- end + if not _port then -- test inf, -inf, NaN, and -0.0 + assert(string.find(string.format("%a", 1/0), "^inf")) + assert(string.find(string.format("%A", -1/0), "^%-INF")) + assert(string.find(string.format("%a", 0/0), "^%-?nan")) + assert(string.find(string.format("%a", -0.0), "^%-0x0")) + end - -- if not pcall(string.format, "%.3a", 0) then - -- (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") - -- else - -- assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) - -- assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) - -- end + if not pcall(string.format, "%.3a", 0) then + (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") + else + assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) + assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) + end end -- cgit v1.2.3-70-g09d2