Closed Bug 1958820 Opened 18 days ago Closed 7 days ago

%TypedArray%.prototype.sort should always normalise NaN values

Categories

(Core :: JavaScript Engine, defect, P3)

Firefox 137
defect

Tracking

()

RESOLVED INVALID

People

(Reporter: aapo.alasuutari, Unassigned)

References

(Blocks 1 open bug)

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0

Steps to reproduce:

Run the following script:

b = BigUint64Array.of(9221120237041091055n, 9221120237041090774n);
console.log(b[0], b[1]);
a = new Float64Array(b.buffer);
a.sort();
console.log(b[0], b[1]);
a.sort(() => Math.random() ? -1 : 1);
console.log(b[0], b[1]);

Actual results:

Output:

9221120237041091055n 9221120237041090774n
9221120237041090774n 9221120237041091055n
9221120237041090560n 9221120237041090560n

When calling %TypedArray%.prototype.sort with no comparator, Firefox performs sorting of floating point values according to some unknown ordering. When calling the method with a comparator function, Firefox normalises the NaN values.

Expected results:

Output:

9221120237041091055n 9221120237041090774n
9221120237041090560n 9221120237041090560n
9221120237041090560n 9221120237041090560n

Calling sort should always normalise NaN values, regardless of the comparator function being present or not. The specification defines sort as reading all values from the source buffer, converting them to JavaScript Values, sorting those, and finally writing them back into the source buffer. When converting to JavaScript Values, all floating point NaN must be canonicalised.

ECMA-262 and implementations don't quite agree on NaN canonicalisation, see:

For performance reasons it's preferable do to less NaN canonicalisation (bug 932767). JSC also recently changed their implementation to perform fewer NaN canonicalisations. V8 doesn't use NaN boxing, so they never had to perform NaN canonicalisation.

Component: Untriaged → JavaScript Engine
Product: Firefox → Core
Blocks: js-lang
Severity: -- → S4
Priority: -- → P3

Some more discussion was had in TC39 Matrix and it was suggested (though of course not yet accepted in a meeting) that the entire language regarding mention of "always choose the same encoding for each implementation distinguishable NaN value" should be removed entirely. With that, I believe, this bug also becomes fully not-a-bug (and I'm not even sure if this is a bug now, with some better reading comprehension).

So, I'll close this.

Status: UNCONFIRMED → RESOLVED
Closed: 7 days ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.