Merge pull request 'Implement TypedArray.toSorted' (#25) from 13.2-release into main

Reviewed-on: https://repo.dactyloidae.xyz/Dactyloidae/UXP/pulls/25
This commit is contained in:
wuggy 2026-07-14 02:05:07 +01:00
commit 414ce93828
3 changed files with 70 additions and 0 deletions

View file

@ -903,6 +903,73 @@ function SetFromNonTypedArray(target, array, targetOffset, targetLength, targetB
return undefined;
}
// ES2023 23.2.3.33 %TypedArray%.prototype.toSorted ( comparator )
function TypedArrayToSorted(comparator) {
// Step 1.
if (comparator !== undefined && !IsCallable(comparator))
ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, comparator));
// Step 2.
var O = this;
// Step 3.
// This function is not generic.
// We want to make sure that we have an attached buffer, per spec prose.
var isTypedArray = IsTypedArrayEnsuringArrayBuffer(O);
// If we got here, `this` is either a typed array or a wrapper for one.
// Step 4.
var len;
if (isTypedArray)
len = TypedArrayLength(O);
else
len = callFunction(CallTypedArrayMethodIfWrapped, O, "TypedArrayLengthMethod");
// Step 5.
var A = TypedArrayCreateSameType(O, len);
// Step 8.
var sortedList = new List();
for (var k = 0; k < len; k++) {
sortedList[k] = O[k];
}
if (len > 1) {
// Steps 6-7.
var sortCompare;
if (comparator === undefined) {
var isBigIntContentType;
if (isTypedArray) {
isBigIntContentType = callFunction(TypedArrayContentTypeIsBigIntMethod, O);
} else {
isBigIntContentType = callFunction(CallTypedArrayMethodIfWrapped, O,
"TypedArrayContentTypeIsBigIntMethod");
}
sortCompare = isBigIntContentType ? TypedArrayCompareBigInt : TypedArrayCompare;
} else {
var wrappedComparator = comparator;
sortCompare = function(x, y) {
// CompareTypedArrayElements step 2.a.
var v = ToNumber(callContentFunction(wrappedComparator, undefined, x, y));
// CompareTypedArrayElements step 2.b.
return Number_isNaN(v) ? 0 : v;
};
}
MergeSort(sortedList, len, sortCompare);
}
// Steps 9-10.
for (var j = 0; j < len; j++) {
A[j] = sortedList[j];
}
// Step 11.
return A;
}
// ES6 draft 20150220 22.2.3.22.2 %TypedArray%.prototype.set(typedArray [, offset])
function SetFromTypedArray(target, typedArray, targetOffset, targetLength) {
assert(IsPossiblyWrappedTypedArray(typedArray),

View file

@ -1643,6 +1643,9 @@ TypedArrayObject::protoFunctions[] = {
/* ES2022 additions */
JS_SELF_HOSTED_FN("at", "TypedArrayAt", 1, 0),
/* ES2023 (WIP) */
JS_SELF_HOSTED_FN("toSorted", "TypedArrayToSorted", 1, 0),
JS_FS_END
};

Binary file not shown.