1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TypedArrayConstants.h"
7 function ViewedArrayBufferIfReified(tarray) {
8 assert(IsTypedArray(tarray), "non-typed array asked for its buffer");
10 var buf = UnsafeGetReservedSlot(tarray, JS_TYPEDARRAYLAYOUT_BUFFER_SLOT);
15 (GuardToArrayBuffer(buf) !== null ||
16 GuardToSharedArrayBuffer(buf) !== null)),
17 "unexpected value in buffer slot"
19 return IsObject(buf) ? buf : null;
22 function IsDetachedBuffer(buffer) {
23 // A typed array with a null buffer has never had its buffer exposed,
24 // and so cannot have become detached.
25 if (buffer === null) {
30 GuardToArrayBuffer(buffer) !== null ||
31 GuardToSharedArrayBuffer(buffer) !== null,
32 "non-ArrayBuffer passed to IsDetachedBuffer"
35 // Shared array buffers are not detachable.
36 if ((buffer = GuardToArrayBuffer(buffer)) === null) {
40 var flags = UnsafeGetInt32FromReservedSlot(buffer, JS_ARRAYBUFFER_FLAGS_SLOT);
41 return (flags & JS_ARRAYBUFFER_DETACHED_FLAG) !== 0;
44 function GetAttachedArrayBuffer(tarray) {
45 var buffer = ViewedArrayBufferIfReified(tarray);
46 if (IsDetachedBuffer(buffer)) {
47 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
52 function GetAttachedArrayBufferMethod() {
53 return GetAttachedArrayBuffer(this);
56 // A function which ensures that the argument is either a typed array or a
57 // cross-compartment wrapper for a typed array and that the typed array involved
58 // has an attached array buffer. If one of those conditions doesn't hold (wrong
59 // kind of argument, or detached array buffer), an exception is thrown.
60 function EnsureTypedArrayWithArrayBuffer(arg) {
61 if (IsObject(arg) && IsTypedArray(arg)) {
62 GetAttachedArrayBuffer(arg);
67 CallTypedArrayMethodIfWrapped,
69 "GetAttachedArrayBufferMethod"
73 // ES2019 draft rev 85ce767c86a1a8ed719fe97e978028bff819d1f2
74 // 7.3.20 SpeciesConstructor ( O, defaultConstructor )
76 // SpeciesConstructor function optimized for TypedArrays to avoid calling
77 // ConstructorForTypedArray, a non-inlineable runtime function, in the normal
79 function TypedArraySpeciesConstructor(obj) {
81 assert(IsObject(obj), "not passed an object");
84 var ctor = obj.constructor;
87 if (ctor === undefined) {
88 return ConstructorForTypedArray(obj);
92 if (!IsObject(ctor)) {
93 ThrowTypeError(JSMSG_OBJECT_REQUIRED, "object's 'constructor' property");
97 var s = ctor[GetBuiltinSymbol("species")];
100 if (IsNullOrUndefined(s)) {
101 return ConstructorForTypedArray(obj);
105 if (IsConstructor(s)) {
111 JSMSG_NOT_CONSTRUCTOR,
112 "@@species property of object's constructor"
116 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
117 // 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
118 function ValidateTypedArray(obj) {
120 /* Steps 3-5 (non-wrapped typed arrays). */
121 if (IsTypedArray(obj)) {
122 // GetAttachedArrayBuffer throws for detached array buffers.
123 GetAttachedArrayBuffer(obj);
127 /* Steps 3-5 (wrapped typed arrays). */
128 if (IsPossiblyWrappedTypedArray(obj)) {
129 if (PossiblyWrappedTypedArrayHasDetachedBuffer(obj)) {
130 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
137 ThrowTypeError(JSMSG_NON_TYPED_ARRAY_RETURNED);
140 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
141 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
142 function TypedArrayCreateWithLength(constructor, length) {
144 var newTypedArray = constructContentFunction(
151 ValidateTypedArray(newTypedArray);
154 var len = PossiblyWrappedTypedArrayLength(newTypedArray);
157 ThrowTypeError(JSMSG_SHORT_TYPED_ARRAY_RETURNED, length, len);
161 return newTypedArray;
164 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
165 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
166 function TypedArrayCreateWithBuffer(constructor, buffer, byteOffset, length) {
168 var newTypedArray = constructContentFunction(
177 ValidateTypedArray(newTypedArray);
179 // We also need to make sure the length is in-bounds. This is checked by
180 // calling PossiblyWrappedTypedArrayLength, which throws for out-of-bounds.
181 PossiblyWrappedTypedArrayLength(newTypedArray);
183 // Step 3 (not applicable).
186 return newTypedArray;
189 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
190 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
191 function TypedArrayCreateWithResizableBuffer(constructor, buffer, byteOffset) {
193 var newTypedArray = constructContentFunction(
201 ValidateTypedArray(newTypedArray);
203 // We also need to make sure the length is in-bounds. This is checked by
204 // calling PossiblyWrappedTypedArrayLength, which throws for out-of-bounds.
205 PossiblyWrappedTypedArrayLength(newTypedArray);
207 // Step 3 (not applicable).
210 return newTypedArray;
213 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
214 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
215 function TypedArraySpeciesCreateWithLength(exemplar, length) {
219 var C = TypedArraySpeciesConstructor(exemplar);
222 return TypedArrayCreateWithLength(C, length);
225 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
226 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
227 function TypedArraySpeciesCreateWithBuffer(
236 var C = TypedArraySpeciesConstructor(exemplar);
239 return TypedArrayCreateWithBuffer(C, buffer, byteOffset, length);
242 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
243 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
244 function TypedArraySpeciesCreateWithResizableBuffer(
252 var C = TypedArraySpeciesConstructor(exemplar);
255 return TypedArrayCreateWithResizableBuffer(C, buffer, byteOffset);
258 // ES6 draft rev30 (2014/12/24) 22.2.3.6 %TypedArray%.prototype.entries()
259 function TypedArrayEntries() {
263 // We need to be a bit careful here, because in the Xray case we want to
264 // create the iterator in our current compartment.
266 // Before doing that, though, we want to check that we have a typed array
267 // and it does not have a detached array buffer. We do the latter by just
268 // calling GetAttachedArrayBuffer() and letting it throw if there isn't one.
269 // In the case when we're not sure we have a typed array (e.g. we might have
270 // a cross-compartment wrapper for one), we can go ahead and call
271 // GetAttachedArrayBuffer via EnsureTypedArrayWithArrayBuffer; that will
272 // throw if we're not actually a wrapped typed array, or if we have a
273 // detached array buffer.
276 EnsureTypedArrayWithArrayBuffer(O);
278 // We also need to make sure the length is in-bounds. This is checked by
279 // calling PossiblyWrappedTypedArrayLength, which throws for out-of-bounds.
280 PossiblyWrappedTypedArrayLength(O);
283 return CreateArrayIterator(O, ITEM_KIND_KEY_AND_VALUE);
286 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
287 // Plus <https://github.com/tc39/ecma262/pull/2221>
288 // 22.2.3.7 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] )
289 function TypedArrayEvery(callbackfn /*, thisArg*/) {
294 EnsureTypedArrayWithArrayBuffer(O);
296 // If we got here, `this` is either a typed array or a wrapper for one.
299 var len = PossiblyWrappedTypedArrayLength(O);
302 if (ArgumentsLength() === 0) {
303 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.every");
305 if (!IsCallable(callbackfn)) {
306 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
309 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
312 for (var k = 0; k < len; k++) {
317 var testResult = callContentFunction(callbackfn, thisArg, kValue, k, O);
328 // Inlining this enables inlining of the callback function.
329 SetIsInlinableLargeFunction(TypedArrayEvery);
331 // ES2024 draft rev b643e1d9bdc0e98d238a72f9988c01265306d09f
332 // Including the changes from <https://github.com/tc39/ecma262/pull/3116>.
334 // 23.2.3.9 %TypedArray%.prototype.fill ( value [ , start [ , end ] ] )
335 function TypedArrayFill(value, start = 0, end = undefined) {
336 // This function is not generic.
337 if (!IsObject(this) || !IsTypedArray(this)) {
339 CallTypedArrayMethodIfWrapped,
352 var buffer = GetAttachedArrayBuffer(this);
355 var len = TypedArrayLength(O);
358 var kind = GetTypedArrayKind(O);
359 if (kind === TYPEDARRAY_KIND_BIGINT64 || kind === TYPEDARRAY_KIND_BIGUINT64) {
360 value = ToBigInt(value);
362 value = ToNumber(value);
366 var relativeStart = ToInteger(start);
371 ? std_Math_max(len + relativeStart, 0)
372 : std_Math_min(relativeStart, len);
375 var relativeEnd = end === undefined ? len : ToInteger(end);
380 ? std_Math_max(len + relativeEnd, 0)
381 : std_Math_min(relativeEnd, len);
384 if (buffer === null) {
385 // A typed array previously using inline storage may acquire a
386 // buffer, so we must check with the source.
387 buffer = ViewedArrayBufferIfReified(O);
390 if (IsDetachedBuffer(buffer)) {
391 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
394 len = TypedArrayLength(O);
397 final = std_Math_min(final, len);
400 for (; k < final; k++) {
408 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
409 // %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
410 function TypedArrayFilter(callbackfn /*, thisArg*/) {
415 // This function is not generic.
416 // We want to make sure that we have an attached buffer, per spec prose.
417 EnsureTypedArrayWithArrayBuffer(O);
419 // If we got here, `this` is either a typed array or a wrapper for one.
422 var len = PossiblyWrappedTypedArrayLength(O);
425 if (ArgumentsLength() === 0) {
426 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.filter");
428 if (!IsCallable(callbackfn)) {
429 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
433 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
436 var kept = new_List();
442 for (var k = 0; k < len; k++) {
447 if (callContentFunction(callbackfn, T, kValue, k, O)) {
449 kept[captured++] = kValue;
454 var A = TypedArraySpeciesCreateWithLength(O, captured);
456 // Steps 11 and 12.b.
457 for (var n = 0; n < captured; n++) {
465 // Inlining this enables inlining of the callback function.
466 SetIsInlinableLargeFunction(TypedArrayFilter);
468 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
469 // Plus <https://github.com/tc39/ecma262/pull/2221>
470 // 22.2.3.10 %TypedArray%.prototype.find ( predicate [ , thisArg ] )
471 function TypedArrayFind(predicate /*, thisArg*/) {
476 EnsureTypedArrayWithArrayBuffer(O);
478 // If we got here, `this` is either a typed array or a wrapper for one.
481 var len = PossiblyWrappedTypedArrayLength(O);
484 if (ArgumentsLength() === 0) {
485 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.find");
487 if (!IsCallable(predicate)) {
488 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
491 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
494 for (var k = 0; k < len; k++) {
499 if (callContentFunction(predicate, thisArg, kValue, k, O)) {
507 // Inlining this enables inlining of the callback function.
508 SetIsInlinableLargeFunction(TypedArrayFind);
510 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
511 // Plus <https://github.com/tc39/ecma262/pull/2221>
512 // 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
513 function TypedArrayFindIndex(predicate /*, thisArg*/) {
518 EnsureTypedArrayWithArrayBuffer(O);
520 // If we got here, `this` is either a typed array or a wrapper for one.
523 var len = PossiblyWrappedTypedArrayLength(O);
526 if (ArgumentsLength() === 0) {
528 JSMSG_MISSING_FUN_ARG,
530 "%TypedArray%.prototype.findIndex"
533 if (!IsCallable(predicate)) {
534 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
537 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
540 for (var k = 0; k < len; k++) {
542 if (callContentFunction(predicate, thisArg, O[k], k, O)) {
550 // Inlining this enables inlining of the callback function.
551 SetIsInlinableLargeFunction(TypedArrayFindIndex);
553 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
554 // Plus <https://github.com/tc39/ecma262/pull/2221>
555 // 22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
556 function TypedArrayForEach(callbackfn /*, thisArg*/) {
561 EnsureTypedArrayWithArrayBuffer(O);
563 // If we got here, `this` is either a typed array or a wrapper for one.
566 var len = PossiblyWrappedTypedArrayLength(O);
569 if (ArgumentsLength() === 0) {
570 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "TypedArray.prototype.forEach");
572 if (!IsCallable(callbackfn)) {
573 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
576 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
579 for (var k = 0; k < len; k++) {
581 callContentFunction(callbackfn, thisArg, O[k], k, O);
587 // Inlining this enables inlining of the callback function.
588 SetIsInlinableLargeFunction(TypedArrayForEach);
590 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
591 // Plus <https://github.com/tc39/ecma262/pull/2221>
592 // 22.2.3.14 %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] )
593 function TypedArrayIndexOf(searchElement, fromIndex = 0) {
595 if (!IsObject(this) || !IsTypedArray(this)) {
597 CallTypedArrayMethodIfWrapped,
605 GetAttachedArrayBuffer(this);
611 var len = TypedArrayLength(O);
619 var n = ToInteger(fromIndex);
622 assert(fromIndex !== undefined || n === 0, "ToInteger(undefined) is zero");
624 // Reload O.[[ArrayLength]] in case ToInteger() detached or resized the ArrayBuffer.
625 // This lets us avoid executing the HasProperty operation in step 11.a.
626 len = std_Math_min(len, TypedArrayLengthZeroOnOutOfBounds(O));
629 len === 0 || !IsDetachedBuffer(ViewedArrayBufferIfReified(O)),
630 "TypedArrays with detached buffers have a length of zero"
639 // Steps 7-8 are handled implicitly.
655 for (; k < len; k++) {
656 // Step 11.a (not necessary in our implementation).
657 assert(k in O, "unexpected missing element");
660 if (O[k] === searchElement) {
669 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
670 // Plus <https://github.com/tc39/ecma262/pull/2221>
671 // 22.2.3.15 %TypedArray%.prototype.join ( separator )
672 function TypedArrayJoin(separator) {
674 if (!IsObject(this) || !IsTypedArray(this)) {
676 CallTypedArrayMethodIfWrapped,
683 GetAttachedArrayBuffer(this);
689 var len = TypedArrayLength(O);
692 var sep = separator === undefined ? "," : ToString(separator);
699 var limit = std_Math_min(len, TypedArrayLengthZeroOnOutOfBounds(O));
701 // ToString() might have detached or resized the underlying ArrayBuffer. To avoid
702 // checking for this condition when looping in step 8.c, do it once here.
704 return callFunction(String_repeat, sep, len - 1);
708 !IsDetachedBuffer(ViewedArrayBufferIfReified(O)),
709 "TypedArrays with detached buffers have a length of zero"
714 // Omit the 'if' clause in step 8.c, since typed arrays can't have undefined or null elements.
715 assert(element0 !== undefined, "unexpected undefined element");
718 var R = ToString(element0);
721 for (var k = 1; k < limit; k++) {
725 // Omit the 'if' clause in step 8.c, since typed arrays can't have undefined or null elements.
726 assert(element !== undefined, "unexpected undefined element");
728 // Steps 8.a and 8.c-d.
729 R += sep + ToString(element);
733 R += callFunction(String_repeat, sep, len - limit);
740 // ES6 draft (2016/1/11) 22.2.3.15 %TypedArray%.prototype.keys()
741 function TypedArrayKeys() {
745 // See the big comment in TypedArrayEntries for what we're doing here.
748 EnsureTypedArrayWithArrayBuffer(O);
749 PossiblyWrappedTypedArrayLength(O);
752 return CreateArrayIterator(O, ITEM_KIND_KEY);
755 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
756 // Plus <https://github.com/tc39/ecma262/pull/2221>
757 // 22.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
758 function TypedArrayLastIndexOf(searchElement /*, fromIndex*/) {
760 if (!IsObject(this) || !IsTypedArray(this)) {
761 if (ArgumentsLength() > 1) {
763 CallTypedArrayMethodIfWrapped,
767 "TypedArrayLastIndexOf"
771 CallTypedArrayMethodIfWrapped,
774 "TypedArrayLastIndexOf"
778 GetAttachedArrayBuffer(this);
784 var len = TypedArrayLength(O);
792 var n = ArgumentsLength() > 1 ? ToInteger(GetArgument(1)) : len - 1;
794 // Reload O.[[ArrayLength]] in case ToInteger() detached or resized the ArrayBuffer.
795 // This lets us avoid executing the HasProperty operation in step 9.a.
796 len = std_Math_min(len, TypedArrayLengthZeroOnOutOfBounds(O));
799 len === 0 || !IsDetachedBuffer(ViewedArrayBufferIfReified(O)),
800 "TypedArrays with detached buffers have a length of zero"
804 var k = n >= 0 ? std_Math_min(n, len - 1) : len + n;
807 for (; k >= 0; k--) {
808 // Step 9.a (not necessary in our implementation).
809 assert(k in O, "unexpected missing element");
812 if (O[k] === searchElement) {
821 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
822 // 22.2.3.19 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )
823 function TypedArrayMap(callbackfn /*, thisArg*/) {
828 // This function is not generic.
829 // We want to make sure that we have an attached buffer, per spec prose.
830 EnsureTypedArrayWithArrayBuffer(O);
832 // If we got here, `this` is either a typed array or a wrapper for one.
835 var len = PossiblyWrappedTypedArrayLength(O);
838 if (ArgumentsLength() === 0) {
839 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.map");
841 if (!IsCallable(callbackfn)) {
842 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
846 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
849 var A = TypedArraySpeciesCreateWithLength(O, len);
851 // Steps 7, 8.a (implicit) and 8.e.
852 for (var k = 0; k < len; k++) {
854 var mappedValue = callContentFunction(callbackfn, T, O[k], k, O);
863 // Inlining this enables inlining of the callback function.
864 SetIsInlinableLargeFunction(TypedArrayMap);
866 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
867 // Plus <https://github.com/tc39/ecma262/pull/2221>
868 // 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] )
869 function TypedArrayReduce(callbackfn /*, initialValue*/) {
874 EnsureTypedArrayWithArrayBuffer(O);
876 // If we got here, `this` is either a typed array or a wrapper for one.
879 var len = PossiblyWrappedTypedArrayLength(O);
882 if (ArgumentsLength() === 0) {
883 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.reduce");
885 if (!IsCallable(callbackfn)) {
886 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
890 if (len === 0 && ArgumentsLength() === 1) {
891 ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
898 var accumulator = ArgumentsLength() > 1 ? GetArgument(1) : O[k++];
901 for (; k < len; k++) {
902 accumulator = callContentFunction(
916 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
917 // Plus <https://github.com/tc39/ecma262/pull/2221>
918 // 22.2.3.21 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] )
919 function TypedArrayReduceRight(callbackfn /*, initialValue*/) {
924 EnsureTypedArrayWithArrayBuffer(O);
926 // If we got here, `this` is either a typed array or a wrapper for one.
929 var len = PossiblyWrappedTypedArrayLength(O);
932 if (ArgumentsLength() === 0) {
934 JSMSG_MISSING_FUN_ARG,
936 "%TypedArray%.prototype.reduceRight"
939 if (!IsCallable(callbackfn)) {
940 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
944 if (len === 0 && ArgumentsLength() === 1) {
945 ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
952 var accumulator = ArgumentsLength() > 1 ? GetArgument(1) : O[k--];
955 for (; k >= 0; k--) {
956 accumulator = callContentFunction(
970 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
971 // Plus <https://github.com/tc39/ecma262/pull/2221>
972 // 22.2.3.22 %TypedArray%.prototype.reverse ( )
973 function TypedArrayReverse() {
975 if (!IsObject(this) || !IsTypedArray(this)) {
977 CallTypedArrayMethodIfWrapped,
983 GetAttachedArrayBuffer(this);
989 var len = TypedArrayLength(O);
992 var middle = std_Math_floor(len / 2);
995 for (var lower = 0; lower !== middle; lower++) {
997 var upper = len - lower - 1;
1000 var lowerValue = O[lower];
1003 var upperValue = O[upper];
1006 O[lower] = upperValue;
1007 O[upper] = lowerValue;
1014 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
1015 // 22.2.3.24 %TypedArray%.prototype.slice ( start, end )
1016 function TypedArraySlice(start, end) {
1021 if (!IsObject(O) || !IsTypedArray(O)) {
1022 return callFunction(
1023 CallTypedArrayMethodIfWrapped,
1031 GetAttachedArrayBuffer(O);
1034 var len = TypedArrayLength(O);
1037 var relativeStart = ToInteger(start);
1042 ? std_Math_max(len + relativeStart, 0)
1043 : std_Math_min(relativeStart, len);
1046 var relativeEnd = end === undefined ? len : ToInteger(end);
1051 ? std_Math_max(len + relativeEnd, 0)
1052 : std_Math_min(relativeEnd, len);
1055 var count = std_Math_max(final - k, 0);
1058 var A = TypedArraySpeciesCreateWithLength(O, count);
1063 var sliced = TypedArrayBitwiseSlice(O, A, k, count);
1067 // Adjust |final| in case |O| has been resized.
1069 // https://tc39.es/proposal-resizablearraybuffer/#sec-%typedarray%.prototype.slice
1070 final = std_Math_min(final, TypedArrayLength(O));
1087 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
1088 // Plus <https://github.com/tc39/ecma262/pull/2221>
1089 // 22.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )
1090 function TypedArraySome(callbackfn /*, thisArg*/) {
1095 EnsureTypedArrayWithArrayBuffer(O);
1097 // If we got here, `this` is either a typed array or a wrapper for one.
1100 var len = PossiblyWrappedTypedArrayLength(O);
1103 if (ArgumentsLength() === 0) {
1104 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.some");
1106 if (!IsCallable(callbackfn)) {
1107 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
1110 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1113 for (var k = 0; k < len; k++) {
1118 var testResult = callContentFunction(callbackfn, thisArg, kValue, k, O);
1129 // Inlining this enables inlining of the callback function.
1130 SetIsInlinableLargeFunction(TypedArraySome);
1132 // To satisfy step 6.b from TypedArray SortCompare described in 23.2.3.29 the
1133 // user supplied comparefn is wrapped.
1134 function TypedArraySortCompare(comparefn) {
1135 return function(x, y) {
1137 var v = +callContentFunction(comparefn, undefined, x, y);
1149 // ES2019 draft rev 8a16cb8d18660a1106faae693f0f39b9f1a30748
1150 // 22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
1151 function TypedArraySort(comparefn) {
1152 // This function is not generic.
1155 if (comparefn !== undefined) {
1156 if (!IsCallable(comparefn)) {
1157 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, comparefn));
1165 EnsureTypedArrayWithArrayBuffer(obj);
1168 var len = PossiblyWrappedTypedArrayLength(obj);
1170 // Arrays with less than two elements remain unchanged when sorted.
1175 if (comparefn === undefined) {
1176 return TypedArrayNativeSort(obj);
1180 var wrappedCompareFn = TypedArraySortCompare(comparefn);
1183 var sorted = MergeSortTypedArray(obj, len, wrappedCompareFn);
1185 // Move the sorted elements into the array.
1186 for (var i = 0; i < len; i++) {
1193 // ES2017 draft rev f8a9be8ea4bd97237d176907a1e3080dce20c68f
1194 // 22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
1195 // ES2017 Intl draft rev 78bbe7d1095f5ff3760ac4017ed366026e4cb276
1196 // 13.4.1 Array.prototype.toLocaleString ([ locales [ , options ]])
1197 function TypedArrayToLocaleString(locales = undefined, options = undefined) {
1198 // ValidateTypedArray, then step 1.
1201 // This function is not generic.
1202 // We want to make sure that we have an attached buffer, per spec prose.
1203 EnsureTypedArrayWithArrayBuffer(array);
1205 // If we got here, `this` is either a typed array or a wrapper for one.
1208 var len = PossiblyWrappedTypedArrayLength(array);
1216 var firstElement = array[0];
1218 typeof firstElement === "number" || typeof firstElement === "bigint",
1219 "TypedArray elements are either Numbers or BigInts"
1223 // Omit the 'if' clause in step 6, since non-empty typed arrays can't have
1224 // undefined or null elements.
1227 callContentFunction(
1228 firstElement.toLocaleString,
1236 callContentFunction(firstElement.toLocaleString, firstElement)
1240 // Step 3 (reordered).
1241 // We don't (yet?) implement locale-dependent separators.
1242 var separator = ",";
1245 for (var k = 1; k < len; k++) {
1250 var nextElement = array[k];
1253 if (nextElement === undefined) {
1257 typeof nextElement === "number" || typeof nextElement === "bigint",
1258 "TypedArray elements are either Numbers or BigInts"
1264 callContentFunction(
1265 nextElement.toLocaleString,
1272 R += ToString(callContentFunction(nextElement.toLocaleString, nextElement));
1280 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1281 // 22.2.3.27 %TypedArray%.prototype.subarray ( begin, end )
1282 function TypedArraySubarray(begin, end) {
1287 // This function is not generic.
1288 if (!IsObject(obj) || !IsTypedArray(obj)) {
1289 return callFunction(
1290 CallTypedArrayMethodIfWrapped,
1294 "TypedArraySubarray"
1299 var buffer = ViewedArrayBufferIfReified(obj);
1300 if (buffer === null) {
1301 buffer = TypedArrayBuffer(obj);
1305 var srcLength = TypedArrayLengthZeroOnOutOfBounds(obj);
1307 // Step 13 (Reordered because otherwise it'd be observable that we reset
1308 // the byteOffset to zero when the underlying array buffer gets detached).
1309 var srcByteOffset = TypedArrayByteOffset(obj);
1312 var relativeBegin = ToInteger(begin);
1317 ? std_Math_max(srcLength + relativeBegin, 0)
1318 : std_Math_min(relativeBegin, srcLength);
1320 // Steps 11-12. (Reordered)
1321 var elementSize = TypedArrayElementSize(obj);
1323 // Step 14. (Reordered)
1324 var beginByteOffset = srcByteOffset + beginIndex * elementSize;
1326 if (end === undefined && TypedArrayIsAutoLength(obj)) {
1327 return TypedArraySpeciesCreateWithResizableBuffer(
1335 var relativeEnd = end === undefined ? srcLength : ToInteger(end);
1340 ? std_Math_max(srcLength + relativeEnd, 0)
1341 : std_Math_min(relativeEnd, srcLength);
1344 var newLength = std_Math_max(endIndex - beginIndex, 0);
1347 return TypedArraySpeciesCreateWithBuffer(
1355 // https://tc39.es/proposal-relative-indexing-method
1356 // %TypedArray%.prototype.at ( index )
1357 function TypedArrayAt(index) {
1362 // This function is not generic.
1363 if (!IsObject(obj) || !IsTypedArray(obj)) {
1364 return callFunction(
1365 CallTypedArrayMethodIfWrapped,
1371 GetAttachedArrayBuffer(obj);
1374 var len = TypedArrayLength(obj);
1377 var relativeIndex = ToInteger(index);
1381 if (relativeIndex >= 0) {
1384 k = len + relativeIndex;
1388 if (k < 0 || k >= len) {
1395 // This function is only barely too long for normal inlining.
1396 SetIsInlinableLargeFunction(TypedArrayAt);
1398 // https://github.com/tc39/proposal-array-find-from-last
1399 // %TypedArray%.prototype.findLast ( predicate, thisArg )
1400 function TypedArrayFindLast(predicate /*, thisArg*/) {
1405 EnsureTypedArrayWithArrayBuffer(O);
1407 // If we got here, `this` is either a typed array or a wrapper for one.
1410 var len = PossiblyWrappedTypedArrayLength(O);
1413 if (ArgumentsLength() === 0) {
1414 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.findLast");
1416 if (!IsCallable(predicate)) {
1417 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1420 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1423 for (var k = len - 1; k >= 0; k--) {
1428 if (callContentFunction(predicate, thisArg, kValue, k, O)) {
1436 // Inlining this enables inlining of the callback function.
1437 SetIsInlinableLargeFunction(TypedArrayFindLast);
1439 // https://github.com/tc39/proposal-array-find-from-last
1440 // %TypedArray%.prototype.findLastIndex ( predicate, thisArg )
1441 function TypedArrayFindLastIndex(predicate /*, thisArg*/) {
1446 EnsureTypedArrayWithArrayBuffer(O);
1448 // If we got here, `this` is either a typed array or a wrapper for one.
1451 var len = PossiblyWrappedTypedArrayLength(O);
1454 if (ArgumentsLength() === 0) {
1456 JSMSG_MISSING_FUN_ARG,
1458 "%TypedArray%.prototype.findLastIndex"
1461 if (!IsCallable(predicate)) {
1462 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1465 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1468 for (var k = len - 1; k >= 0; k--) {
1470 if (callContentFunction(predicate, thisArg, O[k], k, O)) {
1478 // Inlining this enables inlining of the callback function.
1479 SetIsInlinableLargeFunction(TypedArrayFindLastIndex);
1481 // ES6 draft rev30 (2014/12/24) 22.2.3.30 %TypedArray%.prototype.values()
1483 // Uncloned functions with `$` prefix are allocated as extended function
1484 // to store the original name in `SetCanonicalName`.
1485 function $TypedArrayValues() {
1489 // See the big comment in TypedArrayEntries for what we're doing here.
1490 EnsureTypedArrayWithArrayBuffer(O);
1491 PossiblyWrappedTypedArrayLength(O);
1494 return CreateArrayIterator(O, ITEM_KIND_VALUE);
1496 SetCanonicalName($TypedArrayValues, "values");
1498 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
1499 // Plus <https://github.com/tc39/ecma262/pull/2221>
1500 // 22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
1501 function TypedArrayIncludes(searchElement, fromIndex = 0) {
1503 if (!IsObject(this) || !IsTypedArray(this)) {
1504 return callFunction(
1505 CallTypedArrayMethodIfWrapped,
1509 "TypedArrayIncludes"
1513 GetAttachedArrayBuffer(this);
1519 var len = TypedArrayLength(O);
1527 var n = ToInteger(fromIndex);
1530 assert(fromIndex !== undefined || n === 0, "ToInteger(undefined) is zero");
1533 // Steps 7-8 are handled implicitly.
1551 if (SameValueZero(searchElement, O[k])) {
1563 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
1564 // 22.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )
1565 function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {
1570 if (!IsConstructor(C)) {
1571 ThrowTypeError(JSMSG_NOT_CONSTRUCTOR, typeof C);
1576 if (mapfn !== undefined) {
1578 if (!IsCallable(mapfn)) {
1579 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(1, mapfn));
1593 // Inlined: GetMethod, steps 1-2.
1594 var usingIterator = source[GetBuiltinSymbol("iterator")];
1597 // Inlined: GetMethod, step 3.
1598 if (usingIterator !== undefined && usingIterator !== null) {
1599 // Inlined: GetMethod, step 4.
1600 if (!IsCallable(usingIterator)) {
1601 ThrowTypeError(JSMSG_NOT_ITERABLE, DecompileArg(0, source));
1604 // Try to take a fast path when there's no mapper function and the
1605 // constructor is a built-in TypedArray constructor.
1606 if (!mapping && IsTypedArrayConstructor(C) && IsObject(source)) {
1607 // The source is a TypedArray using the default iterator.
1609 usingIterator === $TypedArrayValues &&
1610 IsTypedArray(source) &&
1611 ArrayIteratorPrototypeOptimizable()
1614 // Omitted but we still need to throw if |source| was detached.
1615 GetAttachedArrayBuffer(source);
1618 var len = TypedArrayLength(source);
1621 var targetObj = constructContentFunction(C, C, len);
1624 for (var k = 0; k < len; k++) {
1625 targetObj[k] = source[k];
1632 // The source is a packed array using the default iterator.
1634 usingIterator === $ArrayValues &&
1635 IsPackedArray(source) &&
1636 ArrayIteratorPrototypeOptimizable()
1639 var targetObj = constructContentFunction(C, C, source.length);
1641 // Steps 7.a, 7.d-f.
1642 TypedArrayInitFromPackedArray(targetObj, source);
1650 var values = IterableToList(source, usingIterator);
1653 var len = values.length;
1656 var targetObj = TypedArrayCreateWithLength(C, len);
1659 for (var k = 0; k < len; k++) {
1661 var kValue = values[k];
1663 // Steps 7.e.iii-iv.
1664 var mappedValue = mapping
1665 ? callContentFunction(mapfn, T, kValue, k)
1669 targetObj[k] = mappedValue;
1673 // Asserting that `values` is empty here would require removing them one by one from
1674 // the list's start in the loop above. That would introduce unacceptable overhead.
1675 // Additionally, the loop's logic is simple enough not to require the assert.
1681 // Step 8 is an assertion: items is not an Iterator. Testing this is
1682 // literally the very last thing we did, so we don't assert here.
1685 var arrayLike = ToObject(source);
1688 var len = ToLength(arrayLike.length);
1691 var targetObj = TypedArrayCreateWithLength(C, len);
1694 for (var k = 0; k < len; k++) {
1696 var kValue = arrayLike[k];
1699 var mappedValue = mapping
1700 ? callContentFunction(mapfn, T, kValue, k)
1704 targetObj[k] = mappedValue;
1711 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
1712 // 22.2.2.2 %TypedArray%.of ( ...items )
1713 function TypedArrayStaticOf(/*...items*/) {
1715 var len = ArgumentsLength();
1717 // Step 2 (implicit).
1723 if (!IsConstructor(C)) {
1724 ThrowTypeError(JSMSG_NOT_CONSTRUCTOR, typeof C);
1728 var newObj = TypedArrayCreateWithLength(C, len);
1731 for (var k = 0; k < len; k++) {
1732 newObj[k] = GetArgument(k);
1739 // ES 2016 draft Mar 25, 2016 22.2.2.4.
1740 function $TypedArraySpecies() {
1744 SetCanonicalName($TypedArraySpecies, "get [Symbol.species]");
1746 // ES2018 draft rev 0525bb33861c7f4e9850f8a222c89642947c4b9c
1747 // 22.2.2.1.1 Runtime Semantics: IterableToList( items, method )
1748 function IterableToList(items, method) {
1749 // Step 1 (Inlined GetIterator).
1751 // 7.4.1 GetIterator, step 1.
1752 assert(IsCallable(method), "method argument is a function");
1754 // 7.4.1 GetIterator, step 2.
1755 var iterator = callContentFunction(method, items);
1757 // 7.4.1 GetIterator, step 3.
1758 if (!IsObject(iterator)) {
1759 ThrowTypeError(JSMSG_GET_ITER_RETURNED_PRIMITIVE);
1762 // 7.4.1 GetIterator, step 4.
1763 var nextMethod = iterator.next;
1772 var next = callContentFunction(nextMethod, iterator);
1773 if (!IsObject(next)) {
1774 ThrowTypeError(JSMSG_ITER_METHOD_RETURNED_PRIMITIVE, "next");
1781 DefineDataProperty(values, i++, next.value);
1788 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1789 // 24.1.4.3 ArrayBuffer.prototype.slice ( start, end )
1790 function ArrayBufferSlice(start, end) {
1795 // This function is not generic.
1796 if (!IsObject(O) || (O = GuardToArrayBuffer(O)) === null) {
1797 return callFunction(
1798 CallArrayBufferMethodIfWrapped,
1807 if (IsDetachedBuffer(O)) {
1808 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
1812 var len = ArrayBufferByteLength(O);
1815 var relativeStart = ToInteger(start);
1820 ? std_Math_max(len + relativeStart, 0)
1821 : std_Math_min(relativeStart, len);
1824 var relativeEnd = end === undefined ? len : ToInteger(end);
1829 ? std_Math_max(len + relativeEnd, 0)
1830 : std_Math_min(relativeEnd, len);
1833 var newLen = std_Math_max(final - first, 0);
1836 var ctor = SpeciesConstructor(O, GetBuiltinConstructor("ArrayBuffer"));
1839 var new_ = constructContentFunction(ctor, ctor, newLen);
1842 var isWrapped = false;
1844 if ((newBuffer = GuardToArrayBuffer(new_)) !== null) {
1846 if (IsDetachedBuffer(newBuffer)) {
1847 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
1853 if (!IsWrappedArrayBuffer(newBuffer)) {
1854 ThrowTypeError(JSMSG_NON_ARRAY_BUFFER_RETURNED);
1862 CallArrayBufferMethodIfWrapped,
1864 "IsDetachedBufferThis"
1867 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
1872 if (newBuffer === O) {
1873 ThrowTypeError(JSMSG_SAME_ARRAY_BUFFER_RETURNED);
1877 var actualLen = PossiblyWrappedArrayBufferByteLength(newBuffer);
1878 if (actualLen < newLen) {
1879 ThrowTypeError(JSMSG_SHORT_ARRAY_BUFFER_RETURNED, newLen, actualLen);
1883 if (IsDetachedBuffer(O)) {
1884 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
1889 // Reacquire the length in case the buffer has been resized.
1890 var currentLen = ArrayBufferByteLength(O);
1892 if (first < currentLen) {
1893 var count = std_Math_min(newLen, currentLen - first);
1894 ArrayBufferCopyData(newBuffer, 0, O, first, count, isWrapped);
1901 function IsDetachedBufferThis() {
1902 return IsDetachedBuffer(this);
1905 // ES 2016 draft Mar 25, 2016 24.1.3.3.
1906 function $ArrayBufferSpecies() {
1910 SetCanonicalName($ArrayBufferSpecies, "get [Symbol.species]");
1912 // Shared memory and atomics proposal (30 Oct 2016)
1913 function $SharedArrayBufferSpecies() {
1917 SetCanonicalName($SharedArrayBufferSpecies, "get [Symbol.species]");
1919 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1920 // 24.2.4.3 SharedArrayBuffer.prototype.slice ( start, end )
1921 function SharedArrayBufferSlice(start, end) {
1926 // This function is not generic.
1927 if (!IsObject(O) || (O = GuardToSharedArrayBuffer(O)) === null) {
1928 return callFunction(
1929 CallSharedArrayBufferMethodIfWrapped,
1933 "SharedArrayBufferSlice"
1938 var len = SharedArrayBufferByteLength(O);
1941 var relativeStart = ToInteger(start);
1946 ? std_Math_max(len + relativeStart, 0)
1947 : std_Math_min(relativeStart, len);
1950 var relativeEnd = end === undefined ? len : ToInteger(end);
1955 ? std_Math_max(len + relativeEnd, 0)
1956 : std_Math_min(relativeEnd, len);
1959 var newLen = std_Math_max(final - first, 0);
1962 var ctor = SpeciesConstructor(O, GetBuiltinConstructor("SharedArrayBuffer"));
1965 var new_ = constructContentFunction(ctor, ctor, newLen);
1968 var isWrapped = false;
1970 if ((newObj = GuardToSharedArrayBuffer(new_)) === null) {
1971 if (!IsWrappedSharedArrayBuffer(new_)) {
1972 ThrowTypeError(JSMSG_NON_SHARED_ARRAY_BUFFER_RETURNED);
1979 if (newObj === O || SharedArrayBuffersMemorySame(newObj, O)) {
1980 ThrowTypeError(JSMSG_SAME_SHARED_ARRAY_BUFFER_RETURNED);
1984 var actualLen = PossiblyWrappedSharedArrayBufferByteLength(newObj);
1985 if (actualLen < newLen) {
1986 ThrowTypeError(JSMSG_SHORT_SHARED_ARRAY_BUFFER_RETURNED, newLen, actualLen);
1990 SharedArrayBufferCopyData(newObj, 0, O, first, newLen, isWrapped);
1996 // https://github.com/tc39/proposal-change-array-by-copy
1997 function TypedArrayCreateSameType(exemplar, length) {
1998 // Step 1. Assert: exemplar is an Object that has [[TypedArrayName]] and [[ContentType]] internal slots.
2000 IsPossiblyWrappedTypedArray(exemplar),
2001 "in TypedArrayCreateSameType, exemplar does not have a [[ContentType]] internal slot"
2004 // Step 2. Let constructor be the intrinsic object listed in column one of Table 63 for exemplar.[[TypedArrayName]].
2005 var constructor = ConstructorForTypedArray(exemplar);
2007 // Step 4 omitted. Assert: result has [[TypedArrayName]] and [[ContentType]] internal slots. - guaranteed by the TypedArray implementation
2008 // Step 5 omitted. Assert: result.[[ContentType]] is exemplar.[[ContentType]]. - guaranteed by the typed array implementation
2010 // Step 3. Let result be ? TypedArrayCreate(constructor, argumentList).
2011 // Step 6. Return result.
2012 return TypedArrayCreateWithLength(constructor, length);
2015 // https://github.com/tc39/proposal-change-array-by-copy
2016 // TypedArray.prototype.toReversed()
2017 function TypedArrayToReversed() {
2018 // Step 2. Perform ? ValidateTypedArray(O).
2019 if (!IsObject(this) || !IsTypedArray(this)) {
2020 return callFunction(
2021 CallTypedArrayMethodIfWrapped,
2023 "TypedArrayToReversed"
2027 GetAttachedArrayBuffer(this);
2029 // Step 1. Let O be the this value.
2032 // Step 3. Let length be O.[[ArrayLength]].
2033 var len = TypedArrayLength(O);
2035 // Step 4. Let A be ? TypedArrayCreateSameType(O, « 𝔽(length) »).
2036 var A = TypedArrayCreateSameType(O, len);
2038 // Step 5. Let k be 0.
2039 // Step 6. Repeat, while k < length,
2040 for (var k = 0; k < len; k++) {
2041 // Step 5.a. Let from be ! ToString(𝔽(length - k - 1)).
2042 var from = len - k - 1;
2043 // Step 5.b. omitted - Let Pk be ! ToString(𝔽(k)).
2044 // k coerced to String by property access
2045 // Step 5.c. Let fromValue be ! Get(O, from).
2046 var fromValue = O[from];
2047 // Step 5.d. Perform ! Set(A, k, kValue, true).
2051 // Step 7. Return A.
2055 // https://github.com/tc39/proposal-change-array-by-copy
2056 // TypedArray.prototype.with()
2057 function TypedArrayWith(index, value) {
2058 // Step 2. Perform ? ValidateTypedArray(O).
2059 if (!IsObject(this) || !IsTypedArray(this)) {
2060 return callFunction(
2061 CallTypedArrayMethodIfWrapped,
2069 GetAttachedArrayBuffer(this);
2071 // Step 1. Let O be the this value.
2074 // Step 3. Let len be O.[[ArrayLength]].
2075 var len = TypedArrayLength(O);
2077 // Step 4. Let relativeIndex be ? ToIntegerOrInfinity(index).
2078 var relativeIndex = ToInteger(index);
2081 if (relativeIndex >= 0) {
2082 // Step 5. If relativeIndex ≥ 0, let actualIndex be relativeIndex.
2083 actualIndex = relativeIndex;
2085 // Step 6. Else, let actualIndex be len + relativeIndex.
2086 actualIndex = len + relativeIndex;
2089 var kind = GetTypedArrayKind(O);
2090 if (kind === TYPEDARRAY_KIND_BIGINT64 || kind === TYPEDARRAY_KIND_BIGUINT64) {
2091 // Step 7. If O.[[ContentType]] is BigInt, set value to ? ToBigInt(value).
2092 value = ToBigInt(value);
2094 // Step 8. Else, set value to ? ToNumber(value).
2095 value = ToNumber(value);
2098 // Reload the array length in case the underlying buffer has been detached or resized.
2099 var currentLen = TypedArrayLengthZeroOnOutOfBounds(O);
2101 !IsDetachedBuffer(ViewedArrayBufferIfReified(O)) || currentLen === 0,
2102 "length is set to zero when the buffer has been detached"
2105 // Step 9. If ! IsValidIntegerIndex(O, 𝔽(actualIndex)) is false, throw a RangeError exception.
2106 // This check is an inlined version of the IsValidIntegerIndex abstract operation.
2107 if (actualIndex < 0 || actualIndex >= currentLen) {
2108 ThrowRangeError(JSMSG_BAD_INDEX);
2111 // Step 10. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
2112 var A = TypedArrayCreateSameType(O, len);
2114 // Step 11. Let k be 0.
2115 // Step 12. Repeat, while k < len,
2116 for (var k = 0; k < len; k++) {
2117 // Step 12.a. omitted - Let Pk be ! ToString(𝔽(k)).
2118 // k coerced to String by property access
2120 // Step 12.b. If k is actualIndex, let fromValue be value.
2121 // Step 12.c. Else, let fromValue be ! Get(O, Pk).
2122 var fromValue = k === actualIndex ? value : O[k];
2124 // Step 12.d. Perform ! Set(A, Pk, fromValue, true).
2132 // https://github.com/tc39/proposal-change-array-by-copy
2133 // TypedArray.prototype.toSorted()
2134 function TypedArrayToSorted(comparefn) {
2135 // Step 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
2136 if (comparefn !== undefined) {
2137 if (!IsCallable(comparefn)) {
2138 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, comparefn));
2142 // Step 2. Let O be the this value.
2145 // Step 3. Perform ? ValidateTypedArray(this).
2146 EnsureTypedArrayWithArrayBuffer(O);
2148 // Step 4. omitted. Let buffer be obj.[[ViewedArrayBuffer]].
2149 // FIXME: Draft spec not synched with https://github.com/tc39/ecma262/pull/2723
2151 // Step 5. Let len be O.[[ArrayLength]].
2152 var len = PossiblyWrappedTypedArrayLength(O);
2154 // Arrays with less than two elements remain unchanged when sorted.
2156 // Step 6. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
2157 var A = TypedArrayCreateSameType(O, len);
2168 if (comparefn === undefined) {
2169 // Step 6. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
2170 var A = TypedArrayCreateSameType(O, len);
2172 // Steps 7-11 not followed exactly; this implementation copies the list and then
2173 // sorts the copy, rather than calling a sort method that copies the list and then
2174 // copying the result again.
2176 // Equivalent to steps 10-11.
2177 for (var k = 0; k < len; k++) {
2181 // Equivalent to steps 7-9 and 12.
2182 return TypedArrayNativeSort(A);
2186 var wrappedCompareFn = TypedArraySortCompare(comparefn);
2188 // Steps 6 and 9-12.
2190 // MergeSortTypedArray returns a sorted copy - exactly what we need to return.
2191 return MergeSortTypedArray(O, len, wrappedCompareFn);