diff --git a/js/src/builtin/TypedArray.js b/js/src/builtin/TypedArray.js index a8510faa71..c831b74614 100644 --- a/js/src/builtin/TypedArray.js +++ b/js/src/builtin/TypedArray.js @@ -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), diff --git a/js/src/vm/TypedArrayObject.cpp b/js/src/vm/TypedArrayObject.cpp index 3e9e82c844..faf3dc40df 100644 --- a/js/src/vm/TypedArrayObject.cpp +++ b/js/src/vm/TypedArrayObject.cpp @@ -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 }; diff --git a/other-licenses/7zstub/dactyloidae/7zSD.sfx b/other-licenses/7zstub/dactyloidae/7zSD.sfx new file mode 100644 index 0000000000..01163de64a Binary files /dev/null and b/other-licenses/7zstub/dactyloidae/7zSD.sfx differ