summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-10 13:47:01 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-10 13:54:58 +0200
commit204ab9fba5a7a1e2e6981c11e0a6c67764332d21 (patch)
tree4976b9295969a7b0a2b4b124710d235771f325e4
parent89e4a76a812499cc108bb77fb5c9ae1877123df6 (diff)
downloadfengari-204ab9fba5a7a1e2e6981c11e0a6c67764332d21.tar.gz
fengari-204ab9fba5a7a1e2e6981c11e0a6c67764332d21.tar.bz2
fengari-204ab9fba5a7a1e2e6981c11e0a6c67764332d21.zip
nan, -inf, inf
-rw-r--r--src/lobject.js4
-rw-r--r--src/lstrlib.js8
-rw-r--r--src/lvm.js2
-rw-r--r--tests/single.lua24
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