Debian Codegraph @

js/src/builtin/TypedArray.js

raw
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/. */
4
5 #include "TypedArrayConstants.h"
6
7 function ViewedArrayBufferIfReified(tarray) {
8 assert(IsTypedArray(tarray), "non-typed array asked for its buffer");
9
10 var buf = UnsafeGetReservedSlot(tarray, JS_TYPEDARRAYLAYOUT_BUFFER_SLOT);
11 assert(
12 buf === false ||
13 buf === true ||
14 (IsObject(buf) &&
15 (GuardToArrayBuffer(buf) !== null ||
16 GuardToSharedArrayBuffer(buf) !== null)),
17 "unexpected value in buffer slot"
18 );
19 return IsObject(buf) ? buf : null;
20 }
21
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) {
26 return false;
27 }
28
29 assert(
30 GuardToArrayBuffer(buffer) !== null ||
31 GuardToSharedArrayBuffer(buffer) !== null,
32 "non-ArrayBuffer passed to IsDetachedBuffer"
33 );
34
35 // Shared array buffers are not detachable.
36 if ((buffer = GuardToArrayBuffer(buffer)) === null) {
37 return false;
38 }
39
40 var flags = UnsafeGetInt32FromReservedSlot(buffer, JS_ARRAYBUFFER_FLAGS_SLOT);
41 return (flags & JS_ARRAYBUFFER_DETACHED_FLAG) !== 0;
42 }
43
44 function GetAttachedArrayBuffer(tarray) {
45 var buffer = ViewedArrayBufferIfReified(tarray);
46 if (IsDetachedBuffer(buffer)) {
47 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
48 }
49 return buffer;
50 }
51
52 function GetAttachedArrayBufferMethod() {
53 return GetAttachedArrayBuffer(this);
54 }
55
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);
63 return;
64 }
65
66 callFunction(
67 CallTypedArrayMethodIfWrapped,
68 arg,
69 "GetAttachedArrayBufferMethod"
70 );
71 }
72
73 // ES2019 draft rev 85ce767c86a1a8ed719fe97e978028bff819d1f2
74 // 7.3.20 SpeciesConstructor ( O, defaultConstructor )
75 //
76 // SpeciesConstructor function optimized for TypedArrays to avoid calling
77 // ConstructorForTypedArray, a non-inlineable runtime function, in the normal
78 // case.
79 function TypedArraySpeciesConstructor(obj) {
80 // Step 1.
81 assert(IsObject(obj), "not passed an object");
82
83 // Step 2.
84 var ctor = obj.constructor;
85
86 // Step 3.
87 if (ctor === undefined) {
88 return ConstructorForTypedArray(obj);
89 }
90
91 // Step 4.
92 if (!IsObject(ctor)) {
93 ThrowTypeError(JSMSG_OBJECT_REQUIRED, "object's 'constructor' property");
94 }
95
96 // Steps 5.
97 var s = ctor[GetBuiltinSymbol("species")];
98
99 // Step 6.
100 if (IsNullOrUndefined(s)) {
101 return ConstructorForTypedArray(obj);
102 }
103
104 // Step 7.
105 if (IsConstructor(s)) {
106 return s;
107 }
108
109 // Step 8.
110 ThrowTypeError(
111 JSMSG_NOT_CONSTRUCTOR,
112 "@@species property of object's constructor"
113 );
114 }
115
116 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
117 // 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )
118 function ValidateTypedArray(obj) {
119 if (IsObject(obj)) {
120 /* Steps 3-5 (non-wrapped typed arrays). */
121 if (IsTypedArray(obj)) {
122 // GetAttachedArrayBuffer throws for detached array buffers.
123 GetAttachedArrayBuffer(obj);
124 return;
125 }
126
127 /* Steps 3-5 (wrapped typed arrays). */
128 if (IsPossiblyWrappedTypedArray(obj)) {
129 if (PossiblyWrappedTypedArrayHasDetachedBuffer(obj)) {
130 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
131 }
132 return;
133 }
134 }
135
136 /* Steps 1-2. */
137 ThrowTypeError(JSMSG_NON_TYPED_ARRAY_RETURNED);
138 }
139
140 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
141 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
142 function TypedArrayCreateWithLength(constructor, length) {
143 // Step 1.
144 var newTypedArray = constructContentFunction(
145 constructor,
146 constructor,
147 length
148 );
149
150 // Step 2.
151 ValidateTypedArray(newTypedArray);
152
153 // Step 3.
154 var len = PossiblyWrappedTypedArrayLength(newTypedArray);
155
156 if (len < length) {
157 ThrowTypeError(JSMSG_SHORT_TYPED_ARRAY_RETURNED, length, len);
158 }
159
160 // Step 4.
161 return newTypedArray;
162 }
163
164 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
165 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
166 function TypedArrayCreateWithBuffer(constructor, buffer, byteOffset, length) {
167 // Step 1.
168 var newTypedArray = constructContentFunction(
169 constructor,
170 constructor,
171 buffer,
172 byteOffset,
173 length
174 );
175
176 // Step 2.
177 ValidateTypedArray(newTypedArray);
178
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);
182
183 // Step 3 (not applicable).
184
185 // Step 4.
186 return newTypedArray;
187 }
188
189 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
190 // 22.2.4.6 TypedArrayCreate ( constructor, argumentList )
191 function TypedArrayCreateWithResizableBuffer(constructor, buffer, byteOffset) {
192 // Step 1.
193 var newTypedArray = constructContentFunction(
194 constructor,
195 constructor,
196 buffer,
197 byteOffset
198 );
199
200 // Step 2.
201 ValidateTypedArray(newTypedArray);
202
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);
206
207 // Step 3 (not applicable).
208
209 // Step 4.
210 return newTypedArray;
211 }
212
213 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
214 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
215 function TypedArraySpeciesCreateWithLength(exemplar, length) {
216 // Step 1 (omitted).
217
218 // Steps 2-3.
219 var C = TypedArraySpeciesConstructor(exemplar);
220
221 // Step 4.
222 return TypedArrayCreateWithLength(C, length);
223 }
224
225 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
226 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
227 function TypedArraySpeciesCreateWithBuffer(
228 exemplar,
229 buffer,
230 byteOffset,
231 length
232 ) {
233 // Step 1 (omitted).
234
235 // Steps 2-3.
236 var C = TypedArraySpeciesConstructor(exemplar);
237
238 // Step 4.
239 return TypedArrayCreateWithBuffer(C, buffer, byteOffset, length);
240 }
241
242 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
243 // 22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
244 function TypedArraySpeciesCreateWithResizableBuffer(
245 exemplar,
246 buffer,
247 byteOffset
248 ) {
249 // Step 1 (omitted).
250
251 // Steps 2-3.
252 var C = TypedArraySpeciesConstructor(exemplar);
253
254 // Step 4.
255 return TypedArrayCreateWithResizableBuffer(C, buffer, byteOffset);
256 }
257
258 // ES6 draft rev30 (2014/12/24) 22.2.3.6 %TypedArray%.prototype.entries()
259 function TypedArrayEntries() {
260 // Step 1.
261 var O = this;
262
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.
265 //
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.
274
275 // Step 2-6.
276 EnsureTypedArrayWithArrayBuffer(O);
277
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);
281
282 // Step 7.
283 return CreateArrayIterator(O, ITEM_KIND_KEY_AND_VALUE);
284 }
285
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*/) {
290 // Step 1.
291 var O = this;
292
293 // Step 2.
294 EnsureTypedArrayWithArrayBuffer(O);
295
296 // If we got here, `this` is either a typed array or a wrapper for one.
297
298 // Step 3.
299 var len = PossiblyWrappedTypedArrayLength(O);
300
301 // Step 4.
302 if (ArgumentsLength() === 0) {
303 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.every");
304 }
305 if (!IsCallable(callbackfn)) {
306 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
307 }
308
309 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
310
311 // Steps 5-6.
312 for (var k = 0; k < len; k++) {
313 // Steps 6.b-d.
314 var kValue = O[k];
315
316 // Step 6.c.
317 var testResult = callContentFunction(callbackfn, thisArg, kValue, k, O);
318
319 // Step 6.d.
320 if (!testResult) {
321 return false;
322 }
323 }
324
325 // Step 7.
326 return true;
327 }
328 // Inlining this enables inlining of the callback function.
329 SetIsInlinableLargeFunction(TypedArrayEvery);
330
331 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
332 // %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
333 function TypedArrayFilter(callbackfn /*, thisArg*/) {
334 // Step 1.
335 var O = this;
336
337 // Step 2.
338 // This function is not generic.
339 // We want to make sure that we have an attached buffer, per spec prose.
340 EnsureTypedArrayWithArrayBuffer(O);
341
342 // If we got here, `this` is either a typed array or a wrapper for one.
343
344 // Step 3.
345 var len = PossiblyWrappedTypedArrayLength(O);
346
347 // Step 4.
348 if (ArgumentsLength() === 0) {
349 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.filter");
350 }
351 if (!IsCallable(callbackfn)) {
352 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
353 }
354
355 // Step 5.
356 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
357
358 // Step 6.
359 var kept = new_List();
360
361 // Step 8.
362 var captured = 0;
363
364 // Steps 7 and 9.e.
365 for (var k = 0; k < len; k++) {
366 // Steps 9.a-b.
367 var kValue = O[k];
368
369 // Steps 9.c-d.
370 if (callContentFunction(callbackfn, T, kValue, k, O)) {
371 // Steps 9.d.i-ii.
372 kept[captured++] = kValue;
373 }
374 }
375
376 // Step 10.
377 var A = TypedArraySpeciesCreateWithLength(O, captured);
378
379 // Steps 11 and 12.b.
380 for (var n = 0; n < captured; n++) {
381 // Step 12.a.
382 A[n] = kept[n];
383 }
384
385 // Step 13.
386 return A;
387 }
388 // Inlining this enables inlining of the callback function.
389 SetIsInlinableLargeFunction(TypedArrayFilter);
390
391 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
392 // Plus <https://github.com/tc39/ecma262/pull/2221>
393 // 22.2.3.10 %TypedArray%.prototype.find ( predicate [ , thisArg ] )
394 function TypedArrayFind(predicate /*, thisArg*/) {
395 // Step 1.
396 var O = this;
397
398 // Step 2.
399 EnsureTypedArrayWithArrayBuffer(O);
400
401 // If we got here, `this` is either a typed array or a wrapper for one.
402
403 // Step 3.
404 var len = PossiblyWrappedTypedArrayLength(O);
405
406 // Step 4.
407 if (ArgumentsLength() === 0) {
408 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.find");
409 }
410 if (!IsCallable(predicate)) {
411 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
412 }
413
414 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
415
416 // Steps 5-6.
417 for (var k = 0; k < len; k++) {
418 // Steps 6.a-b.
419 var kValue = O[k];
420
421 // Steps 6.c-d.
422 if (callContentFunction(predicate, thisArg, kValue, k, O)) {
423 return kValue;
424 }
425 }
426
427 // Step 7.
428 return undefined;
429 }
430 // Inlining this enables inlining of the callback function.
431 SetIsInlinableLargeFunction(TypedArrayFind);
432
433 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
434 // Plus <https://github.com/tc39/ecma262/pull/2221>
435 // 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
436 function TypedArrayFindIndex(predicate /*, thisArg*/) {
437 // Step 1.
438 var O = this;
439
440 // Step 2.
441 EnsureTypedArrayWithArrayBuffer(O);
442
443 // If we got here, `this` is either a typed array or a wrapper for one.
444
445 // Step 3.
446 var len = PossiblyWrappedTypedArrayLength(O);
447
448 // Step 4.
449 if (ArgumentsLength() === 0) {
450 ThrowTypeError(
451 JSMSG_MISSING_FUN_ARG,
452 0,
453 "%TypedArray%.prototype.findIndex"
454 );
455 }
456 if (!IsCallable(predicate)) {
457 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
458 }
459
460 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
461
462 // Steps 5-6.
463 for (var k = 0; k < len; k++) {
464 // Steps 6.a-f.
465 if (callContentFunction(predicate, thisArg, O[k], k, O)) {
466 return k;
467 }
468 }
469
470 // Step 7.
471 return -1;
472 }
473 // Inlining this enables inlining of the callback function.
474 SetIsInlinableLargeFunction(TypedArrayFindIndex);
475
476 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
477 // Plus <https://github.com/tc39/ecma262/pull/2221>
478 // 22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )
479 function TypedArrayForEach(callbackfn /*, thisArg*/) {
480 // Step 1.
481 var O = this;
482
483 // Step 2.
484 EnsureTypedArrayWithArrayBuffer(O);
485
486 // If we got here, `this` is either a typed array or a wrapper for one.
487
488 // Step 3.
489 var len = PossiblyWrappedTypedArrayLength(O);
490
491 // Step 4.
492 if (ArgumentsLength() === 0) {
493 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "TypedArray.prototype.forEach");
494 }
495 if (!IsCallable(callbackfn)) {
496 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
497 }
498
499 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
500
501 // Steps 5-6.
502 for (var k = 0; k < len; k++) {
503 // Steps 6.a-c.
504 callContentFunction(callbackfn, thisArg, O[k], k, O);
505 }
506
507 // Step 7.
508 return undefined;
509 }
510 // Inlining this enables inlining of the callback function.
511 SetIsInlinableLargeFunction(TypedArrayForEach);
512
513 // ES6 draft (2016/1/11) 22.2.3.15 %TypedArray%.prototype.keys()
514 function TypedArrayKeys() {
515 // Step 1.
516 var O = this;
517
518 // See the big comment in TypedArrayEntries for what we're doing here.
519
520 // Step 2.
521 EnsureTypedArrayWithArrayBuffer(O);
522 PossiblyWrappedTypedArrayLength(O);
523
524 // Step 3.
525 return CreateArrayIterator(O, ITEM_KIND_KEY);
526 }
527
528 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
529 // 22.2.3.19 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )
530 function TypedArrayMap(callbackfn /*, thisArg*/) {
531 // Step 1.
532 var O = this;
533
534 // Step 2.
535 // This function is not generic.
536 // We want to make sure that we have an attached buffer, per spec prose.
537 EnsureTypedArrayWithArrayBuffer(O);
538
539 // If we got here, `this` is either a typed array or a wrapper for one.
540
541 // Step 3.
542 var len = PossiblyWrappedTypedArrayLength(O);
543
544 // Step 4.
545 if (ArgumentsLength() === 0) {
546 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.map");
547 }
548 if (!IsCallable(callbackfn)) {
549 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
550 }
551
552 // Step 5.
553 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
554
555 // Step 6.
556 var A = TypedArraySpeciesCreateWithLength(O, len);
557
558 // Steps 7, 8.a (implicit) and 8.e.
559 for (var k = 0; k < len; k++) {
560 // Steps 8.b-c.
561 var mappedValue = callContentFunction(callbackfn, T, O[k], k, O);
562
563 // Steps 8.d.
564 A[k] = mappedValue;
565 }
566
567 // Step 9.
568 return A;
569 }
570 // Inlining this enables inlining of the callback function.
571 SetIsInlinableLargeFunction(TypedArrayMap);
572
573 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
574 // Plus <https://github.com/tc39/ecma262/pull/2221>
575 // 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] )
576 function TypedArrayReduce(callbackfn /*, initialValue*/) {
577 // Step 1.
578 var O = this;
579
580 // Step 2.
581 EnsureTypedArrayWithArrayBuffer(O);
582
583 // If we got here, `this` is either a typed array or a wrapper for one.
584
585 // Step 3.
586 var len = PossiblyWrappedTypedArrayLength(O);
587
588 // Step 4.
589 if (ArgumentsLength() === 0) {
590 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.reduce");
591 }
592 if (!IsCallable(callbackfn)) {
593 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
594 }
595
596 // Step 5.
597 if (len === 0 && ArgumentsLength() === 1) {
598 ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
599 }
600
601 // Step 6.
602 var k = 0;
603
604 // Steps 7-9.
605 var accumulator = ArgumentsLength() > 1 ? GetArgument(1) : O[k++];
606
607 // Step 10.
608 for (; k < len; k++) {
609 accumulator = callContentFunction(
610 callbackfn,
611 undefined,
612 accumulator,
613 O[k],
614 k,
615 O
616 );
617 }
618
619 // Step 11.
620 return accumulator;
621 }
622
623 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
624 // Plus <https://github.com/tc39/ecma262/pull/2221>
625 // 22.2.3.21 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] )
626 function TypedArrayReduceRight(callbackfn /*, initialValue*/) {
627 // Step 1.
628 var O = this;
629
630 // Step 2.
631 EnsureTypedArrayWithArrayBuffer(O);
632
633 // If we got here, `this` is either a typed array or a wrapper for one.
634
635 // Step 3.
636 var len = PossiblyWrappedTypedArrayLength(O);
637
638 // Step 4.
639 if (ArgumentsLength() === 0) {
640 ThrowTypeError(
641 JSMSG_MISSING_FUN_ARG,
642 0,
643 "%TypedArray%.prototype.reduceRight"
644 );
645 }
646 if (!IsCallable(callbackfn)) {
647 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
648 }
649
650 // Step 5.
651 if (len === 0 && ArgumentsLength() === 1) {
652 ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
653 }
654
655 // Step 6.
656 var k = len - 1;
657
658 // Steps 7-9.
659 var accumulator = ArgumentsLength() > 1 ? GetArgument(1) : O[k--];
660
661 // Step 10.
662 for (; k >= 0; k--) {
663 accumulator = callContentFunction(
664 callbackfn,
665 undefined,
666 accumulator,
667 O[k],
668 k,
669 O
670 );
671 }
672
673 // Step 11.
674 return accumulator;
675 }
676
677 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
678 // 22.2.3.24 %TypedArray%.prototype.slice ( start, end )
679 function TypedArraySlice(start, end) {
680 // Step 1.
681 var O = this;
682
683 // Step 2.
684 if (!IsObject(O) || !IsTypedArray(O)) {
685 return callFunction(
686 CallTypedArrayMethodIfWrapped,
687 O,
688 start,
689 end,
690 "TypedArraySlice"
691 );
692 }
693
694 GetAttachedArrayBuffer(O);
695
696 // Step 3.
697 var len = TypedArrayLength(O);
698
699 // Step 4.
700 var relativeStart = ToInteger(start);
701
702 // Step 5.
703 var k =
704 relativeStart < 0
705 ? std_Math_max(len + relativeStart, 0)
706 : std_Math_min(relativeStart, len);
707
708 // Step 6.
709 var relativeEnd = end === undefined ? len : ToInteger(end);
710
711 // Step 7.
712 var final =
713 relativeEnd < 0
714 ? std_Math_max(len + relativeEnd, 0)
715 : std_Math_min(relativeEnd, len);
716
717 // Step 8.
718 var count = std_Math_max(final - k, 0);
719
720 // Step 9.
721 var A = TypedArraySpeciesCreateWithLength(O, count);
722
723 // Steps 14-15.
724 if (count > 0) {
725 // Steps 10-13, 15.
726 var sliced = TypedArrayBitwiseSlice(O, A, k, count);
727
728 // Step 14.
729 if (!sliced) {
730 // Adjust |final| in case |O| has been resized.
731 //
732 // https://tc39.es/proposal-resizablearraybuffer/#sec-%typedarray%.prototype.slice
733 final = std_Math_min(final, TypedArrayLength(O));
734
735 // Step 14.a.
736 var n = 0;
737
738 // Step 14.b.
739 while (k < final) {
740 // Steps 14.b.i-v.
741 A[n++] = O[k++];
742 }
743 }
744 }
745
746 // Step 16.
747 return A;
748 }
749
750 // ES2021 draft rev 190d474c3d8728653fbf8a5a37db1de34b9c1472
751 // Plus <https://github.com/tc39/ecma262/pull/2221>
752 // 22.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )
753 function TypedArraySome(callbackfn /*, thisArg*/) {
754 // Step 1.
755 var O = this;
756
757 // Step 2.
758 EnsureTypedArrayWithArrayBuffer(O);
759
760 // If we got here, `this` is either a typed array or a wrapper for one.
761
762 // Step 3.
763 var len = PossiblyWrappedTypedArrayLength(O);
764
765 // Step 4.
766 if (ArgumentsLength() === 0) {
767 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.some");
768 }
769 if (!IsCallable(callbackfn)) {
770 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
771 }
772
773 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
774
775 // Steps 5-6.
776 for (var k = 0; k < len; k++) {
777 // Steps 6.a-b.
778 var kValue = O[k];
779
780 // Step 6.c.
781 var testResult = callContentFunction(callbackfn, thisArg, kValue, k, O);
782
783 // Step 6.d.
784 if (testResult) {
785 return true;
786 }
787 }
788
789 // Step 7.
790 return false;
791 }
792 // Inlining this enables inlining of the callback function.
793 SetIsInlinableLargeFunction(TypedArraySome);
794
795 // ES2017 draft rev f8a9be8ea4bd97237d176907a1e3080dce20c68f
796 // 22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
797 // ES2017 Intl draft rev 78bbe7d1095f5ff3760ac4017ed366026e4cb276
798 // 13.4.1 Array.prototype.toLocaleString ([ locales [ , options ]])
799 function TypedArrayToLocaleString(locales = undefined, options = undefined) {
800 // ValidateTypedArray, then step 1.
801 var array = this;
802
803 // This function is not generic.
804 // We want to make sure that we have an attached buffer, per spec prose.
805 EnsureTypedArrayWithArrayBuffer(array);
806
807 // If we got here, `this` is either a typed array or a wrapper for one.
808
809 // Step 2.
810 var len = PossiblyWrappedTypedArrayLength(array);
811
812 // Step 4.
813 if (len === 0) {
814 return "";
815 }
816
817 // Step 5.
818 var firstElement = array[0];
819 assert(
820 typeof firstElement === "number" || typeof firstElement === "bigint",
821 "TypedArray elements are either Numbers or BigInts"
822 );
823
824 // Steps 6-7.
825 // Omit the 'if' clause in step 6, since non-empty typed arrays can't have
826 // undefined or null elements.
827 #if JS_HAS_INTL_API
828 var R = ToString(
829 callContentFunction(
830 firstElement.toLocaleString,
831 firstElement,
832 locales,
833 options
834 )
835 );
836 #else
837 var R = ToString(
838 callContentFunction(firstElement.toLocaleString, firstElement)
839 );
840 #endif
841
842 // Step 3 (reordered).
843 // We don't (yet?) implement locale-dependent separators.
844 var separator = ",";
845
846 // Steps 8-9.
847 for (var k = 1; k < len; k++) {
848 // Step 9.a.
849 R += separator;
850
851 // Step 9.b.
852 var nextElement = array[k];
853
854 // Step 9.c.
855 if (nextElement === undefined) {
856 continue;
857 }
858 assert(
859 typeof nextElement === "number" || typeof nextElement === "bigint",
860 "TypedArray elements are either Numbers or BigInts"
861 );
862
863 // Steps 9.d-e.
864 #if JS_HAS_INTL_API
865 R += ToString(
866 callContentFunction(
867 nextElement.toLocaleString,
868 nextElement,
869 locales,
870 options
871 )
872 );
873 #else
874 R += ToString(callContentFunction(nextElement.toLocaleString, nextElement));
875 #endif
876 }
877
878 // Step 10.
879 return R;
880 }
881
882 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
883 // 22.2.3.27 %TypedArray%.prototype.subarray ( begin, end )
884 function TypedArraySubarray(begin, end) {
885 // Step 1.
886 var obj = this;
887
888 // Steps 2-3.
889 // This function is not generic.
890 if (!IsObject(obj) || !IsTypedArray(obj)) {
891 return callFunction(
892 CallTypedArrayMethodIfWrapped,
893 this,
894 begin,
895 end,
896 "TypedArraySubarray"
897 );
898 }
899
900 // Step 4.
901 var buffer = ViewedArrayBufferIfReified(obj);
902 if (buffer === null) {
903 buffer = TypedArrayBuffer(obj);
904 }
905
906 // Step 5.
907 var srcLength = TypedArrayLengthZeroOnOutOfBounds(obj);
908
909 // Step 13 (Reordered because otherwise it'd be observable that we reset
910 // the byteOffset to zero when the underlying array buffer gets detached).
911 var srcByteOffset = TypedArrayByteOffset(obj);
912
913 // Step 6.
914 var relativeBegin = ToInteger(begin);
915
916 // Step 7.
917 var beginIndex =
918 relativeBegin < 0
919 ? std_Math_max(srcLength + relativeBegin, 0)
920 : std_Math_min(relativeBegin, srcLength);
921
922 // Steps 11-12. (Reordered)
923 var elementSize = TypedArrayElementSize(obj);
924
925 // Step 14. (Reordered)
926 var beginByteOffset = srcByteOffset + beginIndex * elementSize;
927
928 if (end === undefined && TypedArrayIsAutoLength(obj)) {
929 return TypedArraySpeciesCreateWithResizableBuffer(
930 obj,
931 buffer,
932 beginByteOffset
933 );
934 }
935
936 // Step 8.
937 var relativeEnd = end === undefined ? srcLength : ToInteger(end);
938
939 // Step 9.
940 var endIndex =
941 relativeEnd < 0
942 ? std_Math_max(srcLength + relativeEnd, 0)
943 : std_Math_min(relativeEnd, srcLength);
944
945 // Step 10.
946 var newLength = std_Math_max(endIndex - beginIndex, 0);
947
948 // Steps 15-16.
949 return TypedArraySpeciesCreateWithBuffer(
950 obj,
951 buffer,
952 beginByteOffset,
953 newLength
954 );
955 }
956
957 // https://tc39.es/proposal-relative-indexing-method
958 // %TypedArray%.prototype.at ( index )
959 function TypedArrayAt(index) {
960 // Step 1.
961 var obj = this;
962
963 // Step 2.
964 // This function is not generic.
965 if (!IsObject(obj) || !IsTypedArray(obj)) {
966 return callFunction(
967 CallTypedArrayMethodIfWrapped,
968 obj,
969 index,
970 "TypedArrayAt"
971 );
972 }
973 GetAttachedArrayBuffer(obj);
974
975 // Step 3.
976 var len = TypedArrayLength(obj);
977
978 // Step 4.
979 var relativeIndex = ToInteger(index);
980
981 // Steps 5-6.
982 var k;
983 if (relativeIndex >= 0) {
984 k = relativeIndex;
985 } else {
986 k = len + relativeIndex;
987 }
988
989 // Step 7.
990 if (k < 0 || k >= len) {
991 return undefined;
992 }
993
994 // Step 8.
995 return obj[k];
996 }
997 // This function is only barely too long for normal inlining.
998 SetIsInlinableLargeFunction(TypedArrayAt);
999
1000 // https://github.com/tc39/proposal-array-find-from-last
1001 // %TypedArray%.prototype.findLast ( predicate, thisArg )
1002 function TypedArrayFindLast(predicate /*, thisArg*/) {
1003 // Step 1.
1004 var O = this;
1005
1006 // Step 2.
1007 EnsureTypedArrayWithArrayBuffer(O);
1008
1009 // If we got here, `this` is either a typed array or a wrapper for one.
1010
1011 // Step 3.
1012 var len = PossiblyWrappedTypedArrayLength(O);
1013
1014 // Step 4.
1015 if (ArgumentsLength() === 0) {
1016 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.findLast");
1017 }
1018 if (!IsCallable(predicate)) {
1019 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1020 }
1021
1022 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1023
1024 // Steps 5-6.
1025 for (var k = len - 1; k >= 0; k--) {
1026 // Steps 6.a-b.
1027 var kValue = O[k];
1028
1029 // Steps 6.c-d.
1030 if (callContentFunction(predicate, thisArg, kValue, k, O)) {
1031 return kValue;
1032 }
1033 }
1034
1035 // Step 7.
1036 return undefined;
1037 }
1038 // Inlining this enables inlining of the callback function.
1039 SetIsInlinableLargeFunction(TypedArrayFindLast);
1040
1041 // https://github.com/tc39/proposal-array-find-from-last
1042 // %TypedArray%.prototype.findLastIndex ( predicate, thisArg )
1043 function TypedArrayFindLastIndex(predicate /*, thisArg*/) {
1044 // Step 1.
1045 var O = this;
1046
1047 // Step 2.
1048 EnsureTypedArrayWithArrayBuffer(O);
1049
1050 // If we got here, `this` is either a typed array or a wrapper for one.
1051
1052 // Step 3.
1053 var len = PossiblyWrappedTypedArrayLength(O);
1054
1055 // Step 4.
1056 if (ArgumentsLength() === 0) {
1057 ThrowTypeError(
1058 JSMSG_MISSING_FUN_ARG,
1059 0,
1060 "%TypedArray%.prototype.findLastIndex"
1061 );
1062 }
1063 if (!IsCallable(predicate)) {
1064 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1065 }
1066
1067 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1068
1069 // Steps 5-6.
1070 for (var k = len - 1; k >= 0; k--) {
1071 // Steps 6.a-f.
1072 if (callContentFunction(predicate, thisArg, O[k], k, O)) {
1073 return k;
1074 }
1075 }
1076
1077 // Step 7.
1078 return -1;
1079 }
1080 // Inlining this enables inlining of the callback function.
1081 SetIsInlinableLargeFunction(TypedArrayFindLastIndex);
1082
1083 // ES6 draft rev30 (2014/12/24) 22.2.3.30 %TypedArray%.prototype.values()
1084 //
1085 // Uncloned functions with `$` prefix are allocated as extended function
1086 // to store the original name in `SetCanonicalName`.
1087 function $TypedArrayValues() {
1088 // Step 1.
1089 var O = this;
1090
1091 // See the big comment in TypedArrayEntries for what we're doing here.
1092 EnsureTypedArrayWithArrayBuffer(O);
1093 PossiblyWrappedTypedArrayLength(O);
1094
1095 // Step 7.
1096 return CreateArrayIterator(O, ITEM_KIND_VALUE);
1097 }
1098 SetCanonicalName($TypedArrayValues, "values");
1099
1100 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
1101 // 22.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )
1102 function TypedArrayStaticFrom(source, mapfn = undefined, thisArg = undefined) {
1103 // Step 1.
1104 var C = this;
1105
1106 // Step 2.
1107 if (!IsConstructor(C)) {
1108 ThrowTypeError(JSMSG_NOT_CONSTRUCTOR, typeof C);
1109 }
1110
1111 // Step 3.
1112 var mapping;
1113 if (mapfn !== undefined) {
1114 // Step 3.a.
1115 if (!IsCallable(mapfn)) {
1116 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(1, mapfn));
1117 }
1118
1119 // Step 3.b.
1120 mapping = true;
1121 } else {
1122 // Step 4.
1123 mapping = false;
1124 }
1125
1126 // Step 5.
1127 var T = thisArg;
1128
1129 // Step 6.
1130 // Inlined: GetMethod, steps 1-2.
1131 var usingIterator = source[GetBuiltinSymbol("iterator")];
1132
1133 // Step 7.
1134 // Inlined: GetMethod, step 3.
1135 if (usingIterator !== undefined && usingIterator !== null) {
1136 // Inlined: GetMethod, step 4.
1137 if (!IsCallable(usingIterator)) {
1138 ThrowTypeError(JSMSG_NOT_ITERABLE, DecompileArg(0, source));
1139 }
1140
1141 // Try to take a fast path when there's no mapper function and the
1142 // constructor is a built-in TypedArray constructor.
1143 if (!mapping && IsTypedArrayConstructor(C) && IsObject(source)) {
1144 // The source is a TypedArray using the default iterator.
1145 if (
1146 usingIterator === $TypedArrayValues &&
1147 IsTypedArray(source) &&
1148 ArrayIteratorPrototypeOptimizable()
1149 ) {
1150 // Step 7.a.
1151 // Omitted but we still need to throw if |source| was detached.
1152 GetAttachedArrayBuffer(source);
1153
1154 // Step 7.b.
1155 var len = TypedArrayLength(source);
1156
1157 // Step 7.c.
1158 var targetObj = constructContentFunction(C, C, len);
1159
1160 // Steps 7.d-f.
1161 for (var k = 0; k < len; k++) {
1162 targetObj[k] = source[k];
1163 }
1164
1165 // Step 7.g.
1166 return targetObj;
1167 }
1168
1169 // The source is a packed array using the default iterator.
1170 if (
1171 usingIterator === $ArrayValues &&
1172 IsPackedArray(source) &&
1173 ArrayIteratorPrototypeOptimizable()
1174 ) {
1175 // Steps 7.b-c.
1176 var targetObj = constructContentFunction(C, C, source.length);
1177
1178 // Steps 7.a, 7.d-f.
1179 TypedArrayInitFromPackedArray(targetObj, source);
1180
1181 // Step 7.g.
1182 return targetObj;
1183 }
1184 }
1185
1186 // Step 7.a.
1187 var values = IterableToList(source, usingIterator);
1188
1189 // Step 7.b.
1190 var len = values.length;
1191
1192 // Step 7.c.
1193 var targetObj = TypedArrayCreateWithLength(C, len);
1194
1195 // Steps 7.d-e.
1196 for (var k = 0; k < len; k++) {
1197 // Step 7.e.ii.
1198 var kValue = values[k];
1199
1200 // Steps 7.e.iii-iv.
1201 var mappedValue = mapping
1202 ? callContentFunction(mapfn, T, kValue, k)
1203 : kValue;
1204
1205 // Step 7.e.v.
1206 targetObj[k] = mappedValue;
1207 }
1208
1209 // Step 7.f.
1210 // Asserting that `values` is empty here would require removing them one by one from
1211 // the list's start in the loop above. That would introduce unacceptable overhead.
1212 // Additionally, the loop's logic is simple enough not to require the assert.
1213
1214 // Step 7.g.
1215 return targetObj;
1216 }
1217
1218 // Step 8 is an assertion: items is not an Iterator. Testing this is
1219 // literally the very last thing we did, so we don't assert here.
1220
1221 // Step 9.
1222 var arrayLike = ToObject(source);
1223
1224 // Step 10.
1225 var len = ToLength(arrayLike.length);
1226
1227 // Step 11.
1228 var targetObj = TypedArrayCreateWithLength(C, len);
1229
1230 // Steps 12-13.
1231 for (var k = 0; k < len; k++) {
1232 // Steps 13.a-b.
1233 var kValue = arrayLike[k];
1234
1235 // Steps 13.c-d.
1236 var mappedValue = mapping
1237 ? callContentFunction(mapfn, T, kValue, k)
1238 : kValue;
1239
1240 // Step 13.e.
1241 targetObj[k] = mappedValue;
1242 }
1243
1244 // Step 14.
1245 return targetObj;
1246 }
1247
1248 // ES2017 draft rev 6859bb9ccaea9c6ede81d71e5320e3833b92cb3e
1249 // 22.2.2.2 %TypedArray%.of ( ...items )
1250 function TypedArrayStaticOf(/*...items*/) {
1251 // Step 1.
1252 var len = ArgumentsLength();
1253
1254 // Step 2 (implicit).
1255
1256 // Step 3.
1257 var C = this;
1258
1259 // Step 4.
1260 if (!IsConstructor(C)) {
1261 ThrowTypeError(JSMSG_NOT_CONSTRUCTOR, typeof C);
1262 }
1263
1264 // Step 5.
1265 var newObj = TypedArrayCreateWithLength(C, len);
1266
1267 // Steps 6-7.
1268 for (var k = 0; k < len; k++) {
1269 newObj[k] = GetArgument(k);
1270 }
1271
1272 // Step 8.
1273 return newObj;
1274 }
1275
1276 // ES 2016 draft Mar 25, 2016 22.2.2.4.
1277 function $TypedArraySpecies() {
1278 // Step 1.
1279 return this;
1280 }
1281 SetCanonicalName($TypedArraySpecies, "get [Symbol.species]");
1282
1283 // ES2018 draft rev 0525bb33861c7f4e9850f8a222c89642947c4b9c
1284 // 22.2.2.1.1 Runtime Semantics: IterableToList( items, method )
1285 function IterableToList(items, method) {
1286 // Step 1 (Inlined GetIterator).
1287
1288 // 7.4.1 GetIterator, step 1.
1289 assert(IsCallable(method), "method argument is a function");
1290
1291 // 7.4.1 GetIterator, step 2.
1292 var iterator = callContentFunction(method, items);
1293
1294 // 7.4.1 GetIterator, step 3.
1295 if (!IsObject(iterator)) {
1296 ThrowTypeError(JSMSG_GET_ITER_RETURNED_PRIMITIVE);
1297 }
1298
1299 // 7.4.1 GetIterator, step 4.
1300 var nextMethod = iterator.next;
1301
1302 // Step 2.
1303 var values = [];
1304
1305 // Steps 3-4.
1306 var i = 0;
1307 while (true) {
1308 // Step 4.a.
1309 var next = callContentFunction(nextMethod, iterator);
1310 if (!IsObject(next)) {
1311 ThrowTypeError(JSMSG_ITER_METHOD_RETURNED_PRIMITIVE, "next");
1312 }
1313
1314 // Step 4.b.
1315 if (next.done) {
1316 break;
1317 }
1318 DefineDataProperty(values, i++, next.value);
1319 }
1320
1321 // Step 5.
1322 return values;
1323 }
1324
1325 // ES 2016 draft Mar 25, 2016 24.1.3.3.
1326 function $ArrayBufferSpecies() {
1327 // Step 1.
1328 return this;
1329 }
1330 SetCanonicalName($ArrayBufferSpecies, "get [Symbol.species]");
1331
1332 // Shared memory and atomics proposal (30 Oct 2016)
1333 function $SharedArrayBufferSpecies() {
1334 // Step 1.
1335 return this;
1336 }
1337 SetCanonicalName($SharedArrayBufferSpecies, "get [Symbol.species]");
1338
1339 // https://github.com/tc39/proposal-change-array-by-copy
1340 function TypedArrayCreateSameType(exemplar, length) {
1341 // Step 1. Assert: exemplar is an Object that has [[TypedArrayName]] and [[ContentType]] internal slots.
1342 assert(
1343 IsPossiblyWrappedTypedArray(exemplar),
1344 "in TypedArrayCreateSameType, exemplar does not have a [[ContentType]] internal slot"
1345 );
1346
1347 // Step 2. Let constructor be the intrinsic object listed in column one of Table 63 for exemplar.[[TypedArrayName]].
1348 var constructor = ConstructorForTypedArray(exemplar);
1349
1350 // Step 4 omitted. Assert: result has [[TypedArrayName]] and [[ContentType]] internal slots. - guaranteed by the TypedArray implementation
1351 // Step 5 omitted. Assert: result.[[ContentType]] is exemplar.[[ContentType]]. - guaranteed by the typed array implementation
1352
1353 // Step 3. Let result be ? TypedArrayCreate(constructor, argumentList).
1354 // Step 6. Return result.
1355 return TypedArrayCreateWithLength(constructor, length);
1356 }
1357
1358 // https://tc39.es/ecma262/#sec-%typedarray%.prototype.tosorted
1359 // 23.2.3.33 %TypedArray%.prototype.toSorted ( comparefn )
1360 function TypedArrayToSorted(comparefn) {
1361 // Step 1. If comparefn is not undefined and IsCallable(comparefn) is false,
1362 // throw a TypeError exception.
1363 if (comparefn !== undefined) {
1364 if (!IsCallable(comparefn)) {
1365 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, comparefn));
1366 }
1367 }
1368
1369 // Step 2. Let O be the this value.
1370 var O = this;
1371
1372 // Step 3. Perform ? ValidateTypedArray(this).
1373 EnsureTypedArrayWithArrayBuffer(O);
1374
1375 // Step 4. Let len be TypedArrayLength(taRecord).
1376 var len = PossiblyWrappedTypedArrayLength(O);
1377
1378 // Step 5. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
1379 var A = TypedArrayCreateSameType(O, len);
1380
1381 // Steps 6-10 not followed exactly; this implementation copies the list and then
1382 // sorts the copy, rather than calling a sort method that copies the list and then
1383 // copying the result again.
1384
1385 // Equivalent to steps 9-10.
1386 for (var k = 0; k < len; k++) {
1387 A[k] = O[k];
1388 }
1389
1390 // Arrays with less than two elements remain unchanged when sorted.
1391 if (len > 1) {
1392 // Equivalent to steps 6-8.
1393 callFunction(std_TypedArray_sort, A, comparefn);
1394 }
1395
1396 // Step 11.
1397 return A;
1398 }