Skip to content

Commit 30fd290

Browse files
avpfacebook-github-bot
authored andcommitted
Fix sort order of -0 in TypedArrays.
Summary: TypedArray sorting must put `-0` before `+0`, so add a special check for that. Reviewed By: dulinriley Differential Revision: D17632592 fbshipit-source-id: 97a966d2b196e629f4722f2831a14be5a0cb64d3
1 parent a3d4438 commit 30fd290

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/VM/JSLib/TypedArray.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,14 @@ class TypedArraySortModel : public SortModel {
323323
HermesValue aVal = JSObject::getOwnIndexed(*self_, runtime_, a);
324324
HermesValue bVal = JSObject::getOwnIndexed(*self_, runtime_, b);
325325
if (!WithCompareFn) {
326-
return aVal.getNumber() < bVal.getNumber();
326+
double a = aVal.getNumber();
327+
double b = bVal.getNumber();
328+
if (LLVM_UNLIKELY(a == 0) && LLVM_UNLIKELY(b == 0) &&
329+
LLVM_UNLIKELY(std::signbit(a)) && LLVM_UNLIKELY(!std::signbit(b))) {
330+
// -0 < +0, according to the spec.
331+
return true;
332+
}
333+
return a < b;
327334
}
328335
assert(compareFn_ && "Cannot use this version if the compareFn is null");
329336
// ES7 22.2.3.26 2a.

test/hermes/TypedArray.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,14 @@ cons.forEach(function(ta) {
934934
});
935935
}, TypeError);
936936
});
937+
938+
(function negativeZeroSort() {
939+
var x = new Float64Array([+0, -0]);
940+
x.sort();
941+
assert.equal(1 / x[0], -Infinity);
942+
assert.equal(1 / x[1], +Infinity);
943+
})();
944+
937945
/// @}
938946

939947
/// @name TypedArray.prototype.set

0 commit comments

Comments
 (0)