Debian Codegraph @

js/src/builtin/Array.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 /* ES5 15.4.4.16. */
6 function ArrayEvery(callbackfn /*, thisArg*/) {
7 /* Step 1. */
8 var O = ToObject(this);
9
10 /* Steps 2-3. */
11 var len = ToLength(O.length);
12
13 /* Step 4. */
14 if (ArgumentsLength() === 0) {
15 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.every");
16 }
17 if (!IsCallable(callbackfn)) {
18 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
19 }
20
21 /* Step 5. */
22 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
23
24 /* Steps 6-7. */
25 /* Steps a (implicit), and d. */
26 for (var k = 0; k < len; k++) {
27 /* Step b */
28 if (k in O) {
29 /* Step c. */
30 if (!callContentFunction(callbackfn, T, O[k], k, O)) {
31 return false;
32 }
33 }
34 }
35
36 /* Step 8. */
37 return true;
38 }
39 // Inlining this enables inlining of the callback function.
40 SetIsInlinableLargeFunction(ArrayEvery);
41
42 /* ES5 15.4.4.17. */
43 function ArraySome(callbackfn /*, thisArg*/) {
44 /* Step 1. */
45 var O = ToObject(this);
46
47 /* Steps 2-3. */
48 var len = ToLength(O.length);
49
50 /* Step 4. */
51 if (ArgumentsLength() === 0) {
52 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.some");
53 }
54 if (!IsCallable(callbackfn)) {
55 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
56 }
57
58 /* Step 5. */
59 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
60
61 /* Steps 6-7. */
62 /* Steps a (implicit), and d. */
63 for (var k = 0; k < len; k++) {
64 /* Step b */
65 if (k in O) {
66 /* Step c. */
67 if (callContentFunction(callbackfn, T, O[k], k, O)) {
68 return true;
69 }
70 }
71 }
72
73 /* Step 8. */
74 return false;
75 }
76 // Inlining this enables inlining of the callback function.
77 SetIsInlinableLargeFunction(ArraySome);
78
79 /* ES5 15.4.4.18. */
80 function ArrayForEach(callbackfn /*, thisArg*/) {
81 /* Step 1. */
82 var O = ToObject(this);
83
84 /* Steps 2-3. */
85 var len = ToLength(O.length);
86
87 /* Step 4. */
88 if (ArgumentsLength() === 0) {
89 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.forEach");
90 }
91 if (!IsCallable(callbackfn)) {
92 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
93 }
94
95 /* Step 5. */
96 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
97
98 /* Steps 6-7. */
99 /* Steps a (implicit), and d. */
100 for (var k = 0; k < len; k++) {
101 /* Step b */
102 if (k in O) {
103 /* Step c. */
104 callContentFunction(callbackfn, T, O[k], k, O);
105 }
106 }
107
108 /* Step 8. */
109 return undefined;
110 }
111 // Inlining this enables inlining of the callback function.
112 SetIsInlinableLargeFunction(ArrayForEach);
113
114 /* ES 2016 draft Mar 25, 2016 22.1.3.15. */
115 function ArrayMap(callbackfn /*, thisArg*/) {
116 /* Step 1. */
117 var O = ToObject(this);
118
119 /* Step 2. */
120 var len = ToLength(O.length);
121
122 /* Step 3. */
123 if (ArgumentsLength() === 0) {
124 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.map");
125 }
126 if (!IsCallable(callbackfn)) {
127 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
128 }
129
130 /* Step 4. */
131 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
132
133 /* Steps 5. */
134 var A = CanOptimizeArraySpecies(O) ? std_Array(len) : ArraySpeciesCreate(O, len);
135
136 /* Steps 6-7. */
137 /* Steps 7.a (implicit), and 7.d. */
138 for (var k = 0; k < len; k++) {
139 /* Steps 7.b-c. */
140 if (k in O) {
141 /* Steps 7.c.i-iii. */
142 var mappedValue = callContentFunction(callbackfn, T, O[k], k, O);
143 DefineDataProperty(A, k, mappedValue);
144 }
145 }
146
147 /* Step 8. */
148 return A;
149 }
150 // Inlining this enables inlining of the callback function.
151 SetIsInlinableLargeFunction(ArrayMap);
152
153 /* ES 2016 draft Mar 25, 2016 22.1.3.7 Array.prototype.filter. */
154 function ArrayFilter(callbackfn /*, thisArg*/) {
155 /* Step 1. */
156 var O = ToObject(this);
157
158 /* Step 2. */
159 var len = ToLength(O.length);
160
161 /* Step 3. */
162 if (ArgumentsLength() === 0) {
163 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.filter");
164 }
165 if (!IsCallable(callbackfn)) {
166 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
167 }
168
169 /* Step 4. */
170 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
171
172 /* Step 5. */
173 var A = CanOptimizeArraySpecies(O) ? [] : ArraySpeciesCreate(O, 0);
174
175 /* Steps 6-8. */
176 /* Steps 8.a (implicit), and 8.d. */
177 for (var k = 0, to = 0; k < len; k++) {
178 /* Steps 8.b-c. */
179 if (k in O) {
180 /* Step 8.c.i. */
181 var kValue = O[k];
182 /* Steps 8.c.ii-iii. */
183 if (callContentFunction(callbackfn, T, kValue, k, O)) {
184 DefineDataProperty(A, to++, kValue);
185 }
186 }
187 }
188
189 /* Step 9. */
190 return A;
191 }
192 // Inlining this enables inlining of the callback function.
193 SetIsInlinableLargeFunction(ArrayFilter);
194
195 /* ES5 15.4.4.21. */
196 function ArrayReduce(callbackfn /*, initialValue*/) {
197 /* Step 1. */
198 var O = ToObject(this);
199
200 /* Steps 2-3. */
201 var len = ToLength(O.length);
202
203 /* Step 4. */
204 if (ArgumentsLength() === 0) {
205 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.reduce");
206 }
207 if (!IsCallable(callbackfn)) {
208 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
209 }
210
211 /* Step 6. */
212 var k = 0;
213
214 /* Steps 5, 7-8. */
215 var accumulator;
216 if (ArgumentsLength() > 1) {
217 accumulator = GetArgument(1);
218 } else {
219 /* Step 5. */
220 // Add an explicit |throw| here and below to inform Ion that the
221 // ThrowTypeError calls exit this function.
222 if (len === 0) {
223 throw ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
224 }
225
226 // Use a |do-while| loop to let Ion know that the loop will definitely
227 // be entered at least once. When Ion is then also able to inline the
228 // |in| operator, it can optimize away the whole loop.
229 var kPresent = false;
230 do {
231 if (k in O) {
232 kPresent = true;
233 break;
234 }
235 } while (++k < len);
236 if (!kPresent) {
237 throw ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
238 }
239
240 // Moved outside of the loop to ensure the assignment is non-conditional.
241 accumulator = O[k++];
242 }
243
244 /* Step 9. */
245 /* Steps a (implicit), and d. */
246 for (; k < len; k++) {
247 /* Step b */
248 if (k in O) {
249 /* Step c. */
250 accumulator = callContentFunction(
251 callbackfn,
252 undefined,
253 accumulator,
254 O[k],
255 k,
256 O
257 );
258 }
259 }
260
261 /* Step 10. */
262 return accumulator;
263 }
264
265 /* ES5 15.4.4.22. */
266 function ArrayReduceRight(callbackfn /*, initialValue*/) {
267 /* Step 1. */
268 var O = ToObject(this);
269
270 /* Steps 2-3. */
271 var len = ToLength(O.length);
272
273 /* Step 4. */
274 if (ArgumentsLength() === 0) {
275 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.reduce");
276 }
277 if (!IsCallable(callbackfn)) {
278 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, callbackfn));
279 }
280
281 /* Step 6. */
282 var k = len - 1;
283
284 /* Steps 5, 7-8. */
285 var accumulator;
286 if (ArgumentsLength() > 1) {
287 accumulator = GetArgument(1);
288 } else {
289 /* Step 5. */
290 // Add an explicit |throw| here and below to inform Ion that the
291 // ThrowTypeError calls exit this function.
292 if (len === 0) {
293 throw ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
294 }
295
296 // Use a |do-while| loop to let Ion know that the loop will definitely
297 // be entered at least once. When Ion is then also able to inline the
298 // |in| operator, it can optimize away the whole loop.
299 var kPresent = false;
300 do {
301 if (k in O) {
302 kPresent = true;
303 break;
304 }
305 } while (--k >= 0);
306 if (!kPresent) {
307 throw ThrowTypeError(JSMSG_EMPTY_ARRAY_REDUCE);
308 }
309
310 // Moved outside of the loop to ensure the assignment is non-conditional.
311 accumulator = O[k--];
312 }
313
314 /* Step 9. */
315 /* Steps a (implicit), and d. */
316 for (; k >= 0; k--) {
317 /* Step b */
318 if (k in O) {
319 /* Step c. */
320 accumulator = callContentFunction(
321 callbackfn,
322 undefined,
323 accumulator,
324 O[k],
325 k,
326 O
327 );
328 }
329 }
330
331 /* Step 10. */
332 return accumulator;
333 }
334
335 /* ES6 draft 2013-05-14 15.4.3.23. */
336 function ArrayFind(predicate /*, thisArg*/) {
337 /* Steps 1-2. */
338 var O = ToObject(this);
339
340 /* Steps 3-5. */
341 var len = ToLength(O.length);
342
343 /* Step 6. */
344 if (ArgumentsLength() === 0) {
345 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.find");
346 }
347 if (!IsCallable(predicate)) {
348 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
349 }
350
351 /* Step 7. */
352 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
353
354 /* Steps 8-9. */
355 /* Steps a (implicit), and g. */
356 for (var k = 0; k < len; k++) {
357 /* Steps a-c. */
358 var kValue = O[k];
359 /* Steps d-f. */
360 if (callContentFunction(predicate, T, kValue, k, O)) {
361 return kValue;
362 }
363 }
364
365 /* Step 10. */
366 return undefined;
367 }
368 // Inlining this enables inlining of the callback function.
369 SetIsInlinableLargeFunction(ArrayFind);
370
371 /* ES6 draft 2013-05-14 15.4.3.23. */
372 function ArrayFindIndex(predicate /*, thisArg*/) {
373 /* Steps 1-2. */
374 var O = ToObject(this);
375
376 /* Steps 3-5. */
377 var len = ToLength(O.length);
378
379 /* Step 6. */
380 if (ArgumentsLength() === 0) {
381 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.find");
382 }
383 if (!IsCallable(predicate)) {
384 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
385 }
386
387 /* Step 7. */
388 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
389
390 /* Steps 8-9. */
391 /* Steps a (implicit), and g. */
392 for (var k = 0; k < len; k++) {
393 /* Steps a-f. */
394 if (callContentFunction(predicate, T, O[k], k, O)) {
395 return k;
396 }
397 }
398
399 /* Step 10. */
400 return -1;
401 }
402 // Inlining this enables inlining of the callback function.
403 SetIsInlinableLargeFunction(ArrayFindIndex);
404
405 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
406 // 22.1.3.3 Array.prototype.copyWithin ( target, start [ , end ] )
407 function ArrayCopyWithin(target, start, end = undefined) {
408 // Step 1.
409 var O = ToObject(this);
410
411 // Step 2.
412 var len = ToLength(O.length);
413
414 // Step 3.
415 var relativeTarget = ToInteger(target);
416
417 // Step 4.
418 var to =
419 relativeTarget < 0
420 ? std_Math_max(len + relativeTarget, 0)
421 : std_Math_min(relativeTarget, len);
422
423 // Step 5.
424 var relativeStart = ToInteger(start);
425
426 // Step 6.
427 var from =
428 relativeStart < 0
429 ? std_Math_max(len + relativeStart, 0)
430 : std_Math_min(relativeStart, len);
431
432 // Step 7.
433 var relativeEnd = end === undefined ? len : ToInteger(end);
434
435 // Step 8.
436 var final =
437 relativeEnd < 0
438 ? std_Math_max(len + relativeEnd, 0)
439 : std_Math_min(relativeEnd, len);
440
441 // Step 9.
442 var count = std_Math_min(final - from, len - to);
443
444 // Steps 10-12.
445 if (from < to && to < from + count) {
446 // Steps 10.b-c.
447 from = from + count - 1;
448 to = to + count - 1;
449
450 // Step 12.
451 while (count > 0) {
452 if (from in O) {
453 O[to] = O[from];
454 } else {
455 delete O[to];
456 }
457
458 from--;
459 to--;
460 count--;
461 }
462 } else {
463 // Step 12.
464 while (count > 0) {
465 if (from in O) {
466 O[to] = O[from];
467 } else {
468 delete O[to];
469 }
470
471 from++;
472 to++;
473 count--;
474 }
475 }
476
477 // Step 13.
478 return O;
479 }
480
481 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
482 // 22.1.3.6 Array.prototype.fill ( value [ , start [ , end ] ] )
483 function ArrayFill(value, start = 0, end = undefined) {
484 // Step 1.
485 var O = ToObject(this);
486
487 // Step 2.
488 var len = ToLength(O.length);
489
490 // Step 3.
491 var relativeStart = ToInteger(start);
492
493 // Step 4.
494 var k =
495 relativeStart < 0
496 ? std_Math_max(len + relativeStart, 0)
497 : std_Math_min(relativeStart, len);
498
499 // Step 5.
500 var relativeEnd = end === undefined ? len : ToInteger(end);
501
502 // Step 6.
503 var final =
504 relativeEnd < 0
505 ? std_Math_max(len + relativeEnd, 0)
506 : std_Math_min(relativeEnd, len);
507
508 // Step 7.
509 for (; k < final; k++) {
510 O[k] = value;
511 }
512
513 // Step 8.
514 return O;
515 }
516
517 // ES6 draft specification, section 22.1.5.1, version 2013-09-05.
518 function CreateArrayIterator(obj, kind) {
519 var iteratedObject = ToObject(obj);
520 var iterator = NewArrayIterator();
521 UnsafeSetReservedSlot(iterator, ITERATOR_SLOT_TARGET, iteratedObject);
522 UnsafeSetReservedSlot(iterator, ITERATOR_SLOT_NEXT_INDEX, 0);
523 UnsafeSetReservedSlot(iterator, ARRAY_ITERATOR_SLOT_ITEM_KIND, kind);
524 return iterator;
525 }
526
527 // ES6, 22.1.5.2.1
528 // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%.next
529 function ArrayIteratorNext() {
530 // Step 1-3.
531 var obj = this;
532 if (!IsObject(obj) || (obj = GuardToArrayIterator(obj)) === null) {
533 return callFunction(
534 CallArrayIteratorMethodIfWrapped,
535 this,
536 "ArrayIteratorNext"
537 );
538 }
539
540 // Step 4.
541 var a = UnsafeGetReservedSlot(obj, ITERATOR_SLOT_TARGET);
542 var result = { value: undefined, done: false };
543
544 // Step 5.
545 if (a === null) {
546 result.done = true;
547 return result;
548 }
549
550 // Step 6.
551 // The index might not be an integer, so we have to do a generic get here.
552 var index = UnsafeGetReservedSlot(obj, ITERATOR_SLOT_NEXT_INDEX);
553
554 // Step 7.
555 var itemKind = UnsafeGetInt32FromReservedSlot(obj, ARRAY_ITERATOR_SLOT_ITEM_KIND);
556
557 // Step 8-9.
558 var len;
559 if (IsPossiblyWrappedTypedArray(a)) {
560 len = PossiblyWrappedTypedArrayLength(a);
561
562 // If the length is non-zero, the buffer can't be detached.
563 if (len === 0) {
564 if (PossiblyWrappedTypedArrayHasDetachedBuffer(a)) {
565 ThrowTypeError(JSMSG_TYPED_ARRAY_DETACHED);
566 }
567 }
568 } else {
569 len = ToLength(a.length);
570 }
571
572 // Step 10.
573 if (index >= len) {
574 UnsafeSetReservedSlot(obj, ITERATOR_SLOT_TARGET, null);
575 result.done = true;
576 return result;
577 }
578
579 // Step 11.
580 UnsafeSetReservedSlot(obj, ITERATOR_SLOT_NEXT_INDEX, index + 1);
581
582 // Step 16.
583 if (itemKind === ITEM_KIND_VALUE) {
584 result.value = a[index];
585 return result;
586 }
587
588 // Step 13.
589 if (itemKind === ITEM_KIND_KEY_AND_VALUE) {
590 var pair = [index, a[index]];
591 result.value = pair;
592 return result;
593 }
594
595 // Step 12.
596 assert(itemKind === ITEM_KIND_KEY, itemKind);
597 result.value = index;
598 return result;
599 }
600 // We want to inline this to do scalar replacement of the result object.
601 SetIsInlinableLargeFunction(ArrayIteratorNext);
602
603 // Uncloned functions with `$` prefix are allocated as extended function
604 // to store the original name in `SetCanonicalName`.
605 function $ArrayValues() {
606 return CreateArrayIterator(this, ITEM_KIND_VALUE);
607 }
608 SetCanonicalName($ArrayValues, "values");
609
610 function ArrayEntries() {
611 return CreateArrayIterator(this, ITEM_KIND_KEY_AND_VALUE);
612 }
613
614 function ArrayKeys() {
615 return CreateArrayIterator(this, ITEM_KIND_KEY);
616 }
617
618 // https://tc39.es/proposal-array-from-async/
619 // TODO: Bug 1834560 The step numbers in this will need updating when this is merged
620 // into the main spec.
621 function ArrayFromAsync(asyncItems, mapfn = undefined, thisArg = undefined) {
622 // Step 1. Let C be the this value.
623 var C = this;
624
625 // Step 2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
626 // Step 3. Let fromAsyncClosure be a new Abstract Closure with no parameters that captures C, mapfn, and thisArg and performs the following steps when called:
627 var fromAsyncClosure = async () => {
628 // Step 3.a. If mapfn is undefined, let mapping be false.
629 // Step 3.b. Else,
630 // Step 3.b.i. If IsCallable(mapfn) is false, throw a TypeError exception.
631 // Step 3.b.ii. Let mapping be true.
632 var mapping = mapfn !== undefined;
633 if (mapping && !IsCallable(mapfn)) {
634 ThrowTypeError(JSMSG_NOT_FUNCTION, ToSource(mapfn));
635 }
636
637 // Step 3.c. Let usingAsyncIterator be ? GetMethod(asyncItems, @@asyncIterator).
638 var usingAsyncIterator = asyncItems[GetBuiltinSymbol("asyncIterator")];
639 if (usingAsyncIterator === null) {
640 usingAsyncIterator = undefined;
641 }
642
643 var usingSyncIterator = undefined;
644 if (usingAsyncIterator !== undefined) {
645 if (!IsCallable(usingAsyncIterator)) {
646 ThrowTypeError(JSMSG_NOT_ITERABLE, ToSource(asyncItems));
647 }
648 } else {
649 // Step 3.d. If usingAsyncIterator is undefined, then
650
651 // Step 3.d.i. Let usingSyncIterator be ? GetMethod(asyncItems, @@iterator).
652 usingSyncIterator = asyncItems[GetBuiltinSymbol("iterator")];
653 if (usingSyncIterator === null) {
654 usingSyncIterator = undefined;
655 }
656
657 if (usingSyncIterator !== undefined) {
658 if (!IsCallable(usingSyncIterator)) {
659 ThrowTypeError(JSMSG_NOT_ITERABLE, ToSource(asyncItems));
660 }
661 }
662 }
663
664 // Step 3.g. Let iteratorRecord be undefined.
665 // Step 3.j. If iteratorRecord is not undefined, then ...
666 if (usingAsyncIterator !== undefined || usingSyncIterator !== undefined) {
667 // Note: The published spec as of f6acfc4f0277e625f13fd22068138aec61a12df3
668 // is incorrect. See https://github.com/tc39/proposal-array-from-async/issues/33
669 // Here we use the implementation provided by @bakkot in that bug
670 // in lieu for now; This allows to use a for-await loop below.
671
672 // Steps 3.h-i are implicit through the for-await loop.
673
674 // Step 3.h. If usingAsyncIterator is not undefined, then
675 // Step 3.h.i. Set iteratorRecord to ? GetIterator(asyncItems, async, usingAsyncIterator).
676 // Step 3.i. Else if usingSyncIterator is not undefined, then
677 // Set iteratorRecord to ? CreateAsyncFromSyncIterator(GetIterator(asyncItems, sync, usingSyncIterator)).
678
679 // https://github.com/tc39/proposal-array-from-async/pull/41
680 // Step 3.e. If IsConstructor(C) is true, then
681 // Step 3.e.i. Let A be ? Construct(C).
682 // Step 3.f. Else,
683 // Step 3.f.i. Let A be ! ArrayCreate(0).
684 var A = IsConstructor(C) ? constructContentFunction(C, C) : [];
685
686 // Step 3.j.i. Let k be 0.
687 var k = 0;
688
689 // Step 3.j.ii. Repeat,
690 for await (var nextValue of allowContentIterWith(
691 asyncItems,
692 usingAsyncIterator,
693 usingSyncIterator
694 )) {
695 // Following in the steps of Array.from, we don't actually implement 3.j.ii.1.
696 // The comment in Array.from also applies here; we should only encounter this
697 // after a huge loop around a proxy
698 // Step 3.j.ii.1. If k ≥ 2**53 - 1, then
699 // Step 3.j.ii.1.a. Let error be ThrowCompletion(a newly created TypeError object).
700 // Step 3.j.ii.1.b. Return ? AsyncIteratorClose(iteratorRecord, error).
701 // Step 3.j.ii.2. Let Pk be ! ToString(𝔽(k)).
702
703 // Step 3.j.ii.3. Let next be ? Await(IteratorStep(iteratorRecord)).
704
705 // Step 3.j.ii.5. Let nextValue be ? IteratorValue(next). (Implicit through the for-await loop).
706
707 // Step 3.j.ii.7. Else, let mappedValue be nextValue. (Reordered)
708 var mappedValue = nextValue;
709
710 // Step 3.j.ii.6. If mapping is true, then
711 if (mapping) {
712 // Step 3.j.ii.6.a. Let mappedValue be Call(mapfn, thisArg, « nextValue, 𝔽(k) »).
713 // Step 3.j.ii.6.b. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
714 // Abrupt completion will be handled by the for-await loop.
715 mappedValue = callContentFunction(mapfn, thisArg, nextValue, k);
716
717 // Step 3.j.ii.6.c. Set mappedValue to Await(mappedValue).
718 // Step 3.j.ii.6.d. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord).
719 mappedValue = await mappedValue;
720 }
721
722 // Step 3.j.ii.8. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
723 // Step 3.j.ii.9. If defineStatus is an abrupt completion, return ? AsyncIteratorClose(iteratorRecord, defineStatus).
724 DefineDataProperty(A, k, mappedValue);
725
726 // Step 3.j.ii.10. Set k to k + 1.
727 k = k + 1;
728 }
729
730 // Step 3.j.ii.4. If next is false, then (Reordered)
731
732 // Step 3.j.ii.4.a. Perform ? Set(A, "length", 𝔽(k), true).
733 A.length = k;
734
735 // Step 3.j.ii.4.b. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
736 return A;
737 }
738
739 // Step 3.k. Else,
740
741 // Step 3.k.i. NOTE: asyncItems is neither an AsyncIterable nor an Iterable so assume it is an array-like object.
742 // Step 3.k.ii. Let arrayLike be ! ToObject(asyncItems).
743 var arrayLike = ToObject(asyncItems);
744
745 // Step 3.k.iii. Let len be ? LengthOfArrayLike(arrayLike).
746 var len = ToLength(arrayLike.length);
747
748 // Step 3.k.iv. If IsConstructor(C) is true, then
749 // Step 3.k.iv.1. Let A be ? Construct(C, « 𝔽(len) »).
750 // Step 3.k.v. Else,
751 // Step 3.k.v.1. Let A be ? ArrayCreate(len).
752 var A = IsConstructor(C) ? constructContentFunction(C, C, len) : std_Array(len);
753
754 // Step 3.k.vi. Let k be 0.
755 var k = 0;
756
757 // Step 3.k.vii. Repeat, while k < len,
758 while (k < len) {
759 // Step 3.k.vii.1. Let Pk be ! ToString(𝔽(k)).
760 // Step 3.k.vii.2. Let kValue be ? Get(arrayLike, Pk).
761 // Step 3.k.vii.3. Let kValue be ? Await(kValue).
762 var kValue = await arrayLike[k];
763
764 // Step 3.k.vii.4. If mapping is true, then
765 // Step 3.k.vii.4.a. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
766 // Step 3.k.vii.4.b. Let mappedValue be ? Await(mappedValue).
767 // Step 3.k.vii.5. Else, let mappedValue be kValue.
768 var mappedValue = mapping
769 ? await callContentFunction(mapfn, thisArg, kValue, k)
770 : kValue;
771
772 // Step 3.k.vii.6. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
773 DefineDataProperty(A, k, mappedValue);
774
775 // Step 3.k.vii.7. Set k to k + 1.
776 k = k + 1;
777 }
778
779 // Step 3.k.viii. Perform ? Set(A, "length", 𝔽(len), true).
780 A.length = len;
781
782 // Step 3.k.ix. Return Completion Record { [[Type]]: return, [[Value]]: A, [[Target]]: empty }.
783 return A;
784 };
785
786 // Step 4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
787 // Step 5. Return promiseCapability.[[Promise]].
788 return fromAsyncClosure();
789 }
790
791 // ES 2017 draft 0f10dba4ad18de92d47d421f378233a2eae8f077 22.1.2.1
792 function ArrayFrom(items, mapfn = undefined, thisArg = undefined) {
793 // Step 1.
794 var C = this;
795
796 // Steps 2-3.
797 var mapping = mapfn !== undefined;
798 if (mapping && !IsCallable(mapfn)) {
799 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(1, mapfn));
800 }
801 var T = thisArg;
802
803 // Step 4.
804 // Inlined: GetMethod, steps 1-2.
805 var usingIterator = items[GetBuiltinSymbol("iterator")];
806
807 // Step 5.
808 // Inlined: GetMethod, step 3.
809 if (!IsNullOrUndefined(usingIterator)) {
810 // Inlined: GetMethod, step 4.
811 if (!IsCallable(usingIterator)) {
812 ThrowTypeError(JSMSG_NOT_ITERABLE, DecompileArg(0, items));
813 }
814
815 // Steps 5.a-b.
816 var A = IsConstructor(C) ? constructContentFunction(C, C) : [];
817
818 // Step 5.d.
819 var k = 0;
820
821 // Steps 5.c, 5.e
822 for (var nextValue of allowContentIterWith(items, usingIterator)) {
823 // Step 5.e.i.
824 // Disabled for performance reason. We won't hit this case on
825 // normal array, since DefineDataProperty will throw before it.
826 // We could hit this when |A| is a proxy and it ignores
827 // |DefineDataProperty|, but it happens only after too long loop.
828 /*
829 if (k >= 0x1fffffffffffff) {
830 ThrowTypeError(JSMSG_TOO_LONG_ARRAY);
831 }
832 */
833
834 // Steps 5.e.vi-vii.
835 var mappedValue = mapping
836 ? callContentFunction(mapfn, T, nextValue, k)
837 : nextValue;
838
839 // Steps 5.e.ii (reordered), 5.e.viii.
840 DefineDataProperty(A, k++, mappedValue);
841 }
842
843 // Step 5.e.iv.
844 A.length = k;
845 return A;
846 }
847
848 // Step 7 is an assertion: items is not an Iterator. Testing this is
849 // literally the very last thing we did, so we don't assert here.
850
851 // Steps 8-9.
852 var arrayLike = ToObject(items);
853
854 // Steps 10-11.
855 var len = ToLength(arrayLike.length);
856
857 // Steps 12-14.
858 var A = IsConstructor(C)
859 ? constructContentFunction(C, C, len)
860 : std_Array(len);
861
862 // Steps 15-16.
863 for (var k = 0; k < len; k++) {
864 // Steps 16.a-c.
865 var kValue = items[k];
866
867 // Steps 16.d-e.
868 var mappedValue = mapping
869 ? callContentFunction(mapfn, T, kValue, k)
870 : kValue;
871
872 // Steps 16.f-g.
873 DefineDataProperty(A, k, mappedValue);
874 }
875
876 // Steps 17-18.
877 A.length = len;
878
879 // Step 19.
880 return A;
881 }
882
883 // ES2015 22.1.3.27 Array.prototype.toString.
884 function ArrayToString() {
885 // Steps 1-2.
886 var array = ToObject(this);
887
888 // Steps 3-4.
889 var func = array.join;
890
891 // Steps 5-6.
892 if (!IsCallable(func)) {
893 return callFunction(std_Object_toString, array);
894 }
895 return callContentFunction(func, array);
896 }
897
898 // ES2017 draft rev f8a9be8ea4bd97237d176907a1e3080dce20c68f
899 // 22.1.3.27 Array.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
900 // ES2017 Intl draft rev 78bbe7d1095f5ff3760ac4017ed366026e4cb276
901 // 13.4.1 Array.prototype.toLocaleString ([ locales [ , options ]])
902 function ArrayToLocaleString(locales, options) {
903 // Step 1 (ToObject already performed in native code).
904 assert(IsObject(this), "|this| should be an object");
905 var array = this;
906
907 // Step 2.
908 var len = ToLength(array.length);
909
910 // Step 4.
911 if (len === 0) {
912 return "";
913 }
914
915 // Step 5.
916 var firstElement = array[0];
917
918 // Steps 6-7.
919 var R;
920 if (IsNullOrUndefined(firstElement)) {
921 R = "";
922 } else {
923 #if JS_HAS_INTL_API
924 R = ToString(
925 callContentFunction(
926 firstElement.toLocaleString,
927 firstElement,
928 locales,
929 options
930 )
931 );
932 #else
933 R = ToString(
934 callContentFunction(firstElement.toLocaleString, firstElement)
935 );
936 #endif
937 }
938
939 // Step 3 (reordered).
940 // We don't (yet?) implement locale-dependent separators.
941 var separator = ",";
942
943 // Steps 8-9.
944 for (var k = 1; k < len; k++) {
945 // Step 9.b.
946 var nextElement = array[k];
947
948 // Steps 9.a, 9.c-e.
949 R += separator;
950 if (!IsNullOrUndefined(nextElement)) {
951 #if JS_HAS_INTL_API
952 R += ToString(
953 callContentFunction(
954 nextElement.toLocaleString,
955 nextElement,
956 locales,
957 options
958 )
959 );
960 #else
961 R += ToString(
962 callContentFunction(nextElement.toLocaleString, nextElement)
963 );
964 #endif
965 }
966 }
967
968 // Step 10.
969 return R;
970 }
971
972 // ES 2016 draft Mar 25, 2016 22.1.2.5.
973 function $ArraySpecies() {
974 // Step 1.
975 return this;
976 }
977 SetCanonicalName($ArraySpecies, "get [Symbol.species]");
978
979 // ES 2016 draft Mar 25, 2016 9.4.2.3.
980 function ArraySpeciesCreate(originalArray, length) {
981 // Step 1.
982 assert(typeof length === "number", "length should be a number");
983 assert(length >= 0, "length should be a non-negative number");
984
985 // Step 2.
986 // eslint-disable-next-line no-compare-neg-zero
987 if (length === -0) {
988 length = 0;
989 }
990
991 // Step 4, 6.
992 if (!IsArray(originalArray)) {
993 return std_Array(length);
994 }
995
996 // Step 5.a.
997 var originalConstructor = originalArray.constructor;
998 var C = originalConstructor;
999
1000 // Step 5.b.
1001 if (IsConstructor(C) && IsCrossRealmArrayConstructor(C)) {
1002 return std_Array(length);
1003 }
1004
1005 // Step 5.c.
1006 if (IsObject(C)) {
1007 // Step 5.c.i.
1008 C = C[GetBuiltinSymbol("species")];
1009
1010 // Optimized path for an ordinary Array.
1011 if (C === GetBuiltinConstructor("Array")) {
1012 return std_Array(length);
1013 }
1014
1015 // Step 5.c.ii.
1016 if (C === null) {
1017 return std_Array(length);
1018 }
1019
1020 }
1021
1022 // Step 6.
1023 if (C === undefined) {
1024 return std_Array(length);
1025 }
1026
1027 // Step 7.
1028 if (!IsConstructor(C)) {
1029 ThrowTypeError(JSMSG_NOT_CONSTRUCTOR, "constructor property");
1030 }
1031
1032 // Step 8.
1033 return constructContentFunction(C, C, length);
1034 }
1035
1036 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1037 // 22.1.3.11 Array.prototype.flatMap ( mapperFunction [ , thisArg ] )
1038 function ArrayFlatMap(mapperFunction /*, thisArg*/) {
1039 // Step 1.
1040 var O = ToObject(this);
1041
1042 // Step 2.
1043 var sourceLen = ToLength(O.length);
1044
1045 // Step 3.
1046 if (!IsCallable(mapperFunction)) {
1047 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, mapperFunction));
1048 }
1049
1050 // Step 4.
1051 var T = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1052
1053 // Step 5.
1054 var A = CanOptimizeArraySpecies(O) ? [] : ArraySpeciesCreate(O, 0);
1055
1056 // Step 6.
1057 FlattenIntoArray(A, O, sourceLen, 0, 1, mapperFunction, T);
1058
1059 // Step 7.
1060 return A;
1061 }
1062
1063 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1064 // 22.1.3.10 Array.prototype.flat ( [ depth ] )
1065 function ArrayFlat(/* depth */) {
1066 // Step 1.
1067 var O = ToObject(this);
1068
1069 // Step 2.
1070 var sourceLen = ToLength(O.length);
1071
1072 // Step 3.
1073 var depthNum = 1;
1074
1075 // Step 4.
1076 if (ArgumentsLength() && GetArgument(0) !== undefined) {
1077 depthNum = ToInteger(GetArgument(0));
1078 }
1079
1080 // Step 5.
1081 var A = CanOptimizeArraySpecies(O) ? [] : ArraySpeciesCreate(O, 0);
1082
1083 // Step 6.
1084 FlattenIntoArray(A, O, sourceLen, 0, depthNum);
1085
1086 // Step 7.
1087 return A;
1088 }
1089
1090 // ES2020 draft rev dc1e21c454bd316810be1c0e7af0131a2d7f38e9
1091 // 22.1.3.10.1 FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction, thisArg ] )
1092 function FlattenIntoArray(
1093 target,
1094 source,
1095 sourceLen,
1096 start,
1097 depth,
1098 mapperFunction,
1099 thisArg
1100 ) {
1101 // Step 1.
1102 var targetIndex = start;
1103
1104 // Steps 2-3.
1105 for (var sourceIndex = 0; sourceIndex < sourceLen; sourceIndex++) {
1106 // Steps 3.a-c.
1107 if (sourceIndex in source) {
1108 // Step 3.c.i.
1109 var element = source[sourceIndex];
1110
1111 if (mapperFunction) {
1112 // Step 3.c.ii.1.
1113 assert(ArgumentsLength() === 7, "thisArg is present");
1114
1115 // Step 3.c.ii.2.
1116 element = callContentFunction(
1117 mapperFunction,
1118 thisArg,
1119 element,
1120 sourceIndex,
1121 source
1122 );
1123 }
1124
1125 // Step 3.c.iii.
1126 var shouldFlatten = false;
1127
1128 // Step 3.c.iv.
1129 if (depth > 0) {
1130 // Step 3.c.iv.1.
1131 shouldFlatten = IsArray(element);
1132 }
1133
1134 // Step 3.c.v.
1135 if (shouldFlatten) {
1136 // Step 3.c.v.1.
1137 var elementLen = ToLength(element.length);
1138
1139 // Step 3.c.v.2.
1140 targetIndex = FlattenIntoArray(
1141 target,
1142 element,
1143 elementLen,
1144 targetIndex,
1145 depth - 1
1146 );
1147 } else {
1148 // Step 3.c.vi.1.
1149 if (targetIndex >= MAX_NUMERIC_INDEX) {
1150 ThrowTypeError(JSMSG_TOO_LONG_ARRAY);
1151 }
1152
1153 // Step 3.c.vi.2.
1154 DefineDataProperty(target, targetIndex, element);
1155
1156 // Step 3.c.vi.3.
1157 targetIndex++;
1158 }
1159 }
1160 }
1161
1162 // Step 4.
1163 return targetIndex;
1164 }
1165
1166 // https://github.com/tc39/proposal-relative-indexing-method
1167 // Array.prototype.at ( index )
1168 function ArrayAt(index) {
1169 // Step 1.
1170 var O = ToObject(this);
1171
1172 // Step 2.
1173 var len = ToLength(O.length);
1174
1175 // Step 3.
1176 var relativeIndex = ToInteger(index);
1177
1178 // Steps 4-5.
1179 var k;
1180 if (relativeIndex >= 0) {
1181 k = relativeIndex;
1182 } else {
1183 k = len + relativeIndex;
1184 }
1185
1186 // Step 6.
1187 if (k < 0 || k >= len) {
1188 return undefined;
1189 }
1190
1191 // Step 7.
1192 return O[k];
1193 }
1194 // This function is only barely too long for normal inlining.
1195 SetIsInlinableLargeFunction(ArrayAt);
1196
1197 // https://github.com/tc39/proposal-change-array-by-copy
1198 // Array.prototype.toReversed()
1199 function ArrayToReversed() {
1200 // Step 1. Let O be ? ToObject(this value).
1201 var O = ToObject(this);
1202
1203 // Step 2. Let len be ? LengthOfArrayLike(O).
1204 var len = ToLength(O.length);
1205
1206 // Step 3. Let A be ArrayCreate(𝔽(len)).
1207 var A = std_Array(len);
1208
1209 // Step 4. Let k be 0.
1210 // Step 5. Repeat, while k < len,
1211 for (var k = 0; k < len; k++) {
1212 // Step 5.a. Let from be ! ToString(𝔽(len - k - 1)).
1213 var from = len - k - 1;
1214
1215 // Skip Step 5.b. Let Pk be ToString(𝔽(k)).
1216 // k is coerced into a string through the property access.
1217
1218 // Step 5.c. Let fromValue be ? Get(O, from).
1219 var fromValue = O[from];
1220
1221 // Step 5.d. Perform ! CreateDataPropertyOrThrow(A, 𝔽(k), fromValue).
1222 DefineDataProperty(A, k, fromValue);
1223 }
1224
1225 // Step 6. Return A.
1226 return A;
1227 }
1228
1229 // https://github.com/tc39/proposal-change-array-by-copy
1230 // Array.prototype.toSorted()
1231 function ArrayToSorted(comparefn) {
1232 // Step 1. If comparefn is not undefined and IsCallable(comparefn) is
1233 // false, throw a TypeError exception.
1234 if (comparefn !== undefined && !IsCallable(comparefn)) {
1235 ThrowTypeError(JSMSG_BAD_TOSORTED_ARG);
1236 }
1237
1238 // Step 2. Let O be ? ToObject(this value).
1239 var O = ToObject(this);
1240
1241 // Step 3. Let len be ? LengthOfArrayLike(O).
1242 var len = ToLength(O.length);
1243
1244 // Step 4. Let A be ? ArrayCreate(𝔽(len)).
1245 var items = std_Array(len);
1246
1247 // We depart from steps 5-8 of the spec for performance reasons, as
1248 // following the spec would require copying the input array twice.
1249 // Instead, we create a new array that replaces holes with undefined,
1250 // and sort this array.
1251 for (var k = 0; k < len; k++) {
1252 DefineDataProperty(items, k, O[k]);
1253 }
1254
1255 // Arrays with less than two elements remain unchanged when sorted.
1256 if (len <= 1) {
1257 return items;
1258 }
1259
1260 // Steps 5-9.
1261 return callFunction(std_Array_sort, items, comparefn);
1262 }
1263
1264 // https://github.com/tc39/proposal-array-find-from-last
1265 // Array.prototype.findLast ( predicate, thisArg )
1266 function ArrayFindLast(predicate /*, thisArg*/) {
1267 // Step 1.
1268 var O = ToObject(this);
1269
1270 // Step 2.
1271 var len = ToLength(O.length);
1272
1273 // Step 3.
1274 if (ArgumentsLength() === 0) {
1275 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.findLast");
1276 }
1277 if (!IsCallable(predicate)) {
1278 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1279 }
1280
1281 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1282
1283 // Steps 4-5.
1284 for (var k = len - 1; k >= 0; k--) {
1285 // Steps 5.a-b.
1286 var kValue = O[k];
1287
1288 // Steps 5.c-d.
1289 if (callContentFunction(predicate, thisArg, kValue, k, O)) {
1290 return kValue;
1291 }
1292 }
1293
1294 // Step 6.
1295 return undefined;
1296 }
1297 // Inlining this enables inlining of the callback function.
1298 SetIsInlinableLargeFunction(ArrayFindLast);
1299
1300 // https://github.com/tc39/proposal-array-find-from-last
1301 // Array.prototype.findLastIndex ( predicate, thisArg )
1302 function ArrayFindLastIndex(predicate /*, thisArg*/) {
1303 // Step 1.
1304 var O = ToObject(this);
1305
1306 // Steps 2.
1307 var len = ToLength(O.length);
1308
1309 // Step 3.
1310 if (ArgumentsLength() === 0) {
1311 ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.findLastIndex");
1312 }
1313 if (!IsCallable(predicate)) {
1314 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
1315 }
1316
1317 var thisArg = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
1318
1319 // Steps 4-5.
1320 for (var k = len - 1; k >= 0; k--) {
1321 // Steps 5.a-d.
1322 if (callContentFunction(predicate, thisArg, O[k], k, O)) {
1323 return k;
1324 }
1325 }
1326
1327 // Step 6.
1328 return -1;
1329 }
1330 // Inlining this enables inlining of the callback function.
1331 SetIsInlinableLargeFunction(ArrayFindLastIndex);