Skip to content

Commit

Permalink
Runtime: explicit isNaN check
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Sep 26, 2024
1 parent 21392ad commit da599a3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
1 change: 0 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"noDoubleEquals": "off",
"noFallthroughSwitchClause": "off",
"noRedeclare": "off",
"noSelfCompare": "off",
"useDefaultSwitchClauseLast": "off"
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/bigarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Ml_Bigarray.prototype.compare = function (b, total) {
if (x > y) return 1;
if (x != y) {
if (!total) return Number.NaN;
if (x == x) return 1;
if (y == y) return -1;
if (!Number.isNaN(x)) return 1;
if (!Number.isNaN(y)) return -1;
}
}
break;
Expand Down
16 changes: 8 additions & 8 deletions runtime/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function caml_compare_val_number_custom(num, custom, swap, total) {
var comp = caml_compare_val_get_custom(custom);
if (comp) {
var x = swap > 0 ? comp(custom, num, total) : comp(num, custom, total);
if (total && x != x) return swap; // total && nan
if (+x != +x) return +x; // nan
if (total && Number.isNaN(x)) return swap; // total && nan
if (Number.isNan(+x)) return +x; // nan
if ((x | 0) != 0) return x | 0; // !nan
}
return swap;
Expand Down Expand Up @@ -159,7 +159,7 @@ function caml_compare_val(a, b, total) {
}
if (!comp) caml_invalid_argument("compare: abstract value");
var x = comp(a, b, total);
if (x != x) {
if (Number.isNaN(x)) {
// Protect against invalid UNORDERED
return total ? -1 : x;
}
Expand All @@ -171,7 +171,7 @@ function caml_compare_val(a, b, total) {
break;
case 1256: // compare function
var x = a.compare(b, total);
if (x != x) {
if (Number.isNaN(x)) {
// Protect against invalid UNORDERED
return total ? -1 : x;
}
Expand All @@ -188,8 +188,8 @@ function caml_compare_val(a, b, total) {
if (a > b) return 1;
if (a != b) {
if (!total) return Number.NaN;
if (a == a) return 1;
if (b == b) return -1;
if (!Number.isNaN(a)) return 1;
if (!Number.isNaN(b)) return -1;
}
break;
case 1001: // The rest
Expand All @@ -210,8 +210,8 @@ function caml_compare_val(a, b, total) {
if (a > b) return 1;
if (a != b) {
if (!total) return Number.NaN;
if (a == a) return 1;
if (b == b) return -1;
if (!Number.isNaN(a)) return 1;
if (!Number.isNaN(b)) return -1;
}
break;
case 1251: // JavaScript Symbol, no ordering.
Expand Down
22 changes: 7 additions & 15 deletions runtime/ieee_754.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ function caml_float_compare(x, y) {
if (x === y) return 0;
if (x < y) return -1;
if (x > y) return 1;
if (x === x) return 1;
if (y === y) return -1;
if (!Number.isNaN(x)) return 1;
if (!Number.isNaN(y)) return -1;
return 0;
}

Expand Down Expand Up @@ -412,22 +412,13 @@ function caml_fma_float(x, y, z) {
: x;
}

if (
x === 0 ||
x !== x ||
x === +1 / 0 ||
x === -1 / 0 ||
y === 0 ||
y !== y ||
y === +1 / 0 ||
y === -1 / 0
) {
if (x === 0 || y === 0 || !Number.isFinite(x) || !Number.isFinite(y)) {
return x * y + z;
}
if (z === 0) {
return x * y;
}
if (z !== z || z === +1 / 0 || z === -1 / 0) {
if (!Number.isFinite(z)) {
return z;
}

Expand Down Expand Up @@ -569,10 +560,11 @@ function caml_float_of_string(s) {
var res;
s = caml_jsbytes_of_string(s);
res = +s;
if (s.length > 0 && res === res) return res;
//Fast path
if (s.length > 0 && !Number.isNaN(res)) return res;
s = s.replace(/_/g, "");
res = +s;
if ((s.length > 0 && res === res) || /^[+-]?nan$/i.test(s)) return res;
if (s.length > 0 && (!Number.isNaN(res) || /^[+-]?nan$/i.test(s))) return res;
var m = /^ *([+-]?)0x([0-9a-f]+)\.?([0-9a-f]*)(p([+-]?[0-9]+))?/i.exec(s);
// 1 2 3 5
if (m) {
Expand Down

0 comments on commit da599a3

Please sign in to comment.