Skip to content
Snippets Groups Projects
Commit ea6c18c5 authored by André Bargull's avatar André Bargull Committed by Leo Balter
Browse files

Don't compute NaN values in functions (#1503)

`function nan() { return /* expression to compute NaN */ }` may not return the same implementation-distinguishable NaN value before and after JIT compilation.
parent 500e48e6
No related branches found
No related tags found
No related merge requests found
Showing
with 42 additions and 42 deletions
...@@ -9,13 +9,13 @@ description: | ...@@ -9,13 +9,13 @@ description: |
---*/ ---*/
var NaNs = [ var NaNs = [
() => NaN, NaN,
() => Number.NaN, Number.NaN,
() => NaN * 0, NaN * 0,
() => 0/0, 0/0,
() => Infinity/Infinity, Infinity/Infinity,
() => -(0/0), -(0/0),
() => Math.pow(-1, 0.5), Math.pow(-1, 0.5),
() => -Math.pow(-1, 0.5), -Math.pow(-1, 0.5),
() => Number("Not-a-Number"), Number("Not-a-Number"),
]; ];
...@@ -57,12 +57,12 @@ for (var idx = 0; idx < len; ++idx) { ...@@ -57,12 +57,12 @@ for (var idx = 0; idx < len; ++idx) {
for (var jdx = 0; jdx < len; ++jdx) { for (var jdx = 0; jdx < len; ++jdx) {
var a = {}; var a = {};
a.prop = NaNs[idx](); a.prop = NaNs[idx];
a.prop = NaNs[jdx](); a.prop = NaNs[jdx];
assert( assert(
a.prop !== a.prop, a.prop !== a.prop,
`Object property value reassigned to NaN produced by (${NaNs[idx].toString()}) results in a valid NaN` `Object property value reassigned to NaN produced by (index=${idx}) results in a valid NaN`
); );
} }
} }
...@@ -59,17 +59,17 @@ for (var idx = 0; idx < len; ++idx) { ...@@ -59,17 +59,17 @@ for (var idx = 0; idx < len; ++idx) {
var b = {}; var b = {};
Object.defineProperty(a, "prop", { Object.defineProperty(a, "prop", {
value: NaNs[idx](), value: NaNs[idx],
configurable: true, configurable: true,
}); });
Object.defineProperty(a, "prop", { Object.defineProperty(a, "prop", {
value: NaNs[jdx](), value: NaNs[jdx],
}); });
assert( assert(
a.prop !== a.prop, a.prop !== a.prop,
`Object property value reconfigured to NaN produced by (${NaNs[idx].toString()}) results in a valid NaN` `Object property value reconfigured to NaN produced by (index=${idx}) results in a valid NaN`
); );
} }
} }
...@@ -20,7 +20,7 @@ function body(FloatArray) { ...@@ -20,7 +20,7 @@ function body(FloatArray) {
var subject = new FloatArray(NaNs.length * 2); var subject = new FloatArray(NaNs.length * 2);
NaNs.forEach(function(v, i) { NaNs.forEach(function(v, i) {
subject[i] = v(); subject[i] = v;
}); });
var originalBytes, copiedBytes; var originalBytes, copiedBytes;
......
...@@ -78,7 +78,7 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -78,7 +78,7 @@ testWithTypedArrayConstructors(function(FA) {
var controls, idx, aNaN; var controls, idx, aNaN;
for (idx = 0; idx < NaNs.length; ++idx) { for (idx = 0; idx < NaNs.length; ++idx) {
aNaN = NaNs[idx](); aNaN = NaNs[idx];
controls = new Float32Array([aNaN, aNaN, aNaN]); controls = new Float32Array([aNaN, aNaN, aNaN]);
samples.fill(aNaN); samples.fill(aNaN);
...@@ -89,12 +89,12 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -89,12 +89,12 @@ testWithTypedArrayConstructors(function(FA) {
assert( assert(
samples[i] !== samples[i], samples[i] !== samples[i],
`samples (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `samples (index=${idx}) produces a valid NaN (${precision} precision)`
); );
assert( assert(
controls[i] !== controls[i], controls[i] !== controls[i],
`controls (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `controls (index=${idx}) produces a valid NaN (${precision} precision)`
); );
} }
} }
......
...@@ -45,12 +45,12 @@ features: [TypedArray] ...@@ -45,12 +45,12 @@ features: [TypedArray]
---*/ ---*/
function body(FloatArray) { function body(FloatArray) {
var sample = new FloatArray(NaNs.map(n => n())); var sample = new FloatArray(NaNs);
var sampleBytes, resultBytes; var sampleBytes, resultBytes;
var i = 0; var i = 0;
var result = sample.map(function() { var result = sample.map(function() {
return NaNs[i++](); return NaNs[i++];
}); });
sampleBytes = new Uint8Array(sample.buffer); sampleBytes = new Uint8Array(sample.buffer);
......
...@@ -20,7 +20,7 @@ features: [TypedArray] ...@@ -20,7 +20,7 @@ features: [TypedArray]
---*/ ---*/
function body(FA) { function body(FA) {
var source = new FA(NaNs.map(n => n())); var source = new FA(NaNs);
var target = new FA(NaNs.length); var target = new FA(NaNs.length);
var sourceBytes, targetBytes; var sourceBytes, targetBytes;
......
...@@ -25,7 +25,7 @@ features: [TypedArray] ...@@ -25,7 +25,7 @@ features: [TypedArray]
---*/ ---*/
function body(FloatArray) { function body(FloatArray) {
var subject = new FloatArray(NaNs.map(n => n())); var subject = new FloatArray(NaNs);
var sliced, subjectBytes, slicedBytes; var sliced, subjectBytes, slicedBytes;
sliced = subject.slice(); sliced = subject.slice();
......
...@@ -50,8 +50,8 @@ features: [TypedArray] ...@@ -50,8 +50,8 @@ features: [TypedArray]
---*/ ---*/
function body(FloatArray) { function body(FloatArray) {
var first = new FloatArray(NaNs.map(n => n())); var first = new FloatArray(NaNs);
var second = new FloatArray(NaNs.map(n => n())); var second = new FloatArray(NaNs);
var firstBytes = new Uint8Array(first.buffer); var firstBytes = new Uint8Array(first.buffer);
var secondBytes = new Uint8Array(second.buffer); var secondBytes = new Uint8Array(second.buffer);
......
...@@ -74,7 +74,7 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -74,7 +74,7 @@ testWithTypedArrayConstructors(function(FA) {
var controls, idx, aNaN; var controls, idx, aNaN;
for (idx = 0; idx < NaNs.length; ++idx) { for (idx = 0; idx < NaNs.length; ++idx) {
aNaN = NaNs[idx](); aNaN = NaNs[idx];
controls = new FA([aNaN, aNaN, aNaN]); controls = new FA([aNaN, aNaN, aNaN]);
Object.defineProperty(samples, "0", { value: aNaN }); Object.defineProperty(samples, "0", { value: aNaN });
...@@ -85,12 +85,12 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -85,12 +85,12 @@ testWithTypedArrayConstructors(function(FA) {
assert( assert(
samples[i] !== samples[i], samples[i] !== samples[i],
`samples (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `samples (index=${idx}) produces a valid NaN (${precision} precision)`
); );
assert( assert(
controls[i] !== controls[i], controls[i] !== controls[i],
`controls (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `controls (index=${idx}) produces a valid NaN (${precision} precision)`
); );
} }
} }
......
...@@ -72,7 +72,7 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -72,7 +72,7 @@ testWithTypedArrayConstructors(function(FA) {
var controls, idx, aNaN; var controls, idx, aNaN;
for (idx = 0; idx < NaNs.length; ++idx) { for (idx = 0; idx < NaNs.length; ++idx) {
aNaN = NaNs[idx](); aNaN = NaNs[idx];
controls = new FA([aNaN, aNaN, aNaN]); controls = new FA([aNaN, aNaN, aNaN]);
samples[0] = aNaN; samples[0] = aNaN;
...@@ -83,12 +83,12 @@ testWithTypedArrayConstructors(function(FA) { ...@@ -83,12 +83,12 @@ testWithTypedArrayConstructors(function(FA) {
assert( assert(
samples[i] !== samples[i], samples[i] !== samples[i],
`samples (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `samples (index=${idx}) produces a valid NaN (${precision} precision)`
); );
assert( assert(
controls[i] !== controls[i], controls[i] !== controls[i],
`controls (${NaNs[idx].toString()}) produces a valid NaN (${precision} precision)` `controls (index=${idx}) produces a valid NaN (${precision} precision)`
); );
} }
} }
......
...@@ -15,5 +15,5 @@ includes: [nans.js] ...@@ -15,5 +15,5 @@ includes: [nans.js]
---*/ ---*/
NaNs.forEach(function(v, i) { NaNs.forEach(function(v, i) {
assert.sameValue(isNaN(v()), true, "value on position: " + i); assert.sameValue(isNaN(v), true, "value on position: " + i);
}); });
...@@ -5,20 +5,20 @@ description: > ...@@ -5,20 +5,20 @@ description: >
Including nans.js will expose: Including nans.js will expose:
var NaNs = [ var NaNs = [
() => NaN, NaN,
() => Number.NaN, Number.NaN,
() => NaN * 0, NaN * 0,
() => 0/0, 0/0,
() => Infinity/Infinity, Infinity/Infinity,
() => -(0/0), -(0/0),
() => Math.pow(-1, 0.5), Math.pow(-1, 0.5),
() => -Math.pow(-1, 0.5), -Math.pow(-1, 0.5),
() => Number("Not-a-Number"), Number("Not-a-Number"),
]; ];
includes: [nans.js] includes: [nans.js]
---*/ ---*/
for (var i = 0; i < NaNs.length; i++) { for (var i = 0; i < NaNs.length; i++) {
assert.sameValue(Number.isNaN(NaNs[i]()), true, NaNs[i].toString()); assert.sameValue(Number.isNaN(NaNs[i]), true, "index: " + i);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment