forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPrototype.js
More file actions
953 lines (760 loc) · 26.6 KB
/
ArrayPrototype.js
File metadata and controls
953 lines (760 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
* Copyright (C) 2014-2017 Apple Inc. All rights reserved.
* Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function reduce(callback /*, initialValue */)
{
"use strict";
var array = @toObject(this, "Array.prototype.reduce requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.reduce callback must be a function");
var argumentCount = @argumentCount();
if (length === 0 && argumentCount < 2)
@throwTypeError("reduce of empty array with no initial value");
var accumulator, k = 0;
if (argumentCount > 1)
accumulator = @argument(1);
else {
while (k < length && !(k in array))
k += 1;
if (k >= length)
@throwTypeError("reduce of empty array with no initial value");
accumulator = array[k++];
}
while (k < length) {
if (k in array)
accumulator = callback.@call(@undefined, accumulator, array[k], k, array);
k += 1;
}
return accumulator;
}
function reduceRight(callback /*, initialValue */)
{
"use strict";
var array = @toObject(this, "Array.prototype.reduceRight requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.reduceRight callback must be a function");
var argumentCount = @argumentCount();
if (length === 0 && argumentCount < 2)
@throwTypeError("reduceRight of empty array with no initial value");
var accumulator, k = length - 1;
if (argumentCount > 1)
accumulator = @argument(1);
else {
while (k >= 0 && !(k in array))
k -= 1;
if (k < 0)
@throwTypeError("reduceRight of empty array with no initial value");
accumulator = array[k--];
}
while (k >= 0) {
if (k in array)
accumulator = callback.@call(@undefined, accumulator, array[k], k, array);
k -= 1;
}
return accumulator;
}
function every(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.every requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.every callback must be a function");
var thisArg = @argument(1);
for (var i = 0; i < length; i++) {
if (!(i in array))
continue;
if (!callback.@call(thisArg, array[i], i, array))
return false;
}
return true;
}
function forEach(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.forEach requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.forEach callback must be a function");
var thisArg = @argument(1);
for (var i = 0; i < length; i++) {
if (i in array)
callback.@call(thisArg, array[i], i, array);
}
}
function filter(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.filter requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.filter callback must be a function");
var thisArg = @argument(1);
var result = @arraySpeciesCreate(array, 0);
var nextIndex = 0;
for (var i = 0; i < length; i++) {
if (!(i in array))
continue;
var current = array[i]
if (callback.@call(thisArg, current, i, array)) {
@putByValDirect(result, nextIndex, current);
++nextIndex;
}
}
return result;
}
function groupBy(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.groupBy requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.groupBy callback must be a function");
var thisArg = @argument(1);
var groups = @Object.@create(null);
for (var i = 0; i < length; ++i) {
var value = array[i];
var key = @toPropertyKey(callback.@call(thisArg, value, i, array));
var group = groups[key];
if (!group) {
group = [];
@putByValDirect(groups, key, group);
}
@putByValDirect(group, group.length, value);
}
return groups;
}
function groupByToMap(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.groupByToMap requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.groupByToMap callback must be a function");
var thisArg = @argument(1);
var groups = new @Map;
for (var i = 0; i < length; ++i) {
var value = array[i];
var key = callback.@call(thisArg, value, i, array);
var group = groups.@get(key);
if (!group) {
group = [];
groups.@set(key, group);
}
@putByValDirect(group, group.length, value);
}
return groups;
}
function map(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.map requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.map callback must be a function");
var thisArg = @argument(1);
var result = @arraySpeciesCreate(array, length);
for (var i = 0; i < length; i++) {
if (!(i in array))
continue;
var mappedValue = callback.@call(thisArg, array[i], i, array);
@putByValDirect(result, i, mappedValue);
}
return result;
}
function some(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.some requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.some callback must be a function");
var thisArg = @argument(1);
for (var i = 0; i < length; i++) {
if (!(i in array))
continue;
if (callback.@call(thisArg, array[i], i, array))
return true;
}
return false;
}
function fill(value /* [, start [, end]] */)
{
"use strict";
var array = @toObject(this, "Array.prototype.fill requires that |this| not be null or undefined");
var length = @toLength(array.length);
var relativeStart = @toIntegerOrInfinity(@argument(1));
var k = 0;
if (relativeStart < 0) {
k = length + relativeStart;
if (k < 0)
k = 0;
} else {
k = relativeStart;
if (k > length)
k = length;
}
var relativeEnd = length;
var end = @argument(2);
if (end !== @undefined)
relativeEnd = @toIntegerOrInfinity(end);
var final = 0;
if (relativeEnd < 0) {
final = length + relativeEnd;
if (final < 0)
final = 0;
} else {
final = relativeEnd;
if (final > length)
final = length;
}
for (; k < final; k++)
array[k] = value;
return array;
}
function find(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.find requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.find callback must be a function");
var thisArg = @argument(1);
for (var i = 0; i < length; i++) {
var kValue = array[i];
if (callback.@call(thisArg, kValue, i, array))
return kValue;
}
return @undefined;
}
function findLast(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.findLast requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.findLast callback must be a function");
var thisArg = @argument(1);
for (var i = length - 1; i >= 0; i--) {
var element = array[i];
if (callback.@call(thisArg, element, i, array))
return element;
}
return @undefined;
}
function findIndex(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.findIndex requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.findIndex callback must be a function");
var thisArg = @argument(1);
for (var i = 0; i < length; i++) {
if (callback.@call(thisArg, array[i], i, array))
return i;
}
return -1;
}
function findLastIndex(callback /*, thisArg */)
{
"use strict";
var array = @toObject(this, "Array.prototype.findLastIndex requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.findLastIndex callback must be a function");
var thisArg = @argument(1);
for (var i = length - 1; i >= 0; i--) {
if (callback.@call(thisArg, array[i], i, array))
return i;
}
return -1;
}
function includes(searchElement /*, fromIndex*/)
{
"use strict";
var array = @toObject(this, "Array.prototype.includes requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (length === 0)
return false;
var fromIndex = 0;
var from = @argument(1);
if (from !== @undefined)
fromIndex = @toIntegerOrInfinity(from);
var index;
if (fromIndex >= 0)
index = fromIndex;
else
index = length + fromIndex;
if (index < 0)
index = 0;
var currentElement;
for (; index < length; ++index) {
currentElement = array[index];
// Use SameValueZero comparison, rather than just StrictEquals.
if (searchElement === currentElement || (searchElement !== searchElement && currentElement !== currentElement))
return true;
}
return false;
}
@linkTimeConstant
function sortStringComparator(a, b)
{
"use strict";
var aString = a.string;
var bString = b.string;
if (aString === bString)
return 0;
return aString > bString ? 1 : -1;
}
@linkTimeConstant
function sortCompact(receiver, receiverLength, compacted, isStringSort)
{
"use strict";
var undefinedCount = 0;
var compactedIndex = 0;
for (var i = 0; i < receiverLength; ++i) {
if (i in receiver) {
var value = receiver[i];
if (value === @undefined)
++undefinedCount;
else {
@putByValDirect(compacted, compactedIndex,
isStringSort ? {string: @toString(value), value} : value);
++compactedIndex;
}
}
}
return undefinedCount;
}
@linkTimeConstant
function sortCommit(receiver, receiverLength, sorted, undefinedCount)
{
"use strict";
// Move undefineds and holes to the end of an array. Result is [values..., undefineds..., holes...].
@assert(@isJSArray(sorted));
var sortedLength = sorted.length;
@assert(sortedLength + undefinedCount <= receiverLength);
var i = 0;
if (@isJSArray(receiver) && sortedLength >= 64 && typeof sorted[0] !== "number") { // heuristic
@appendMemcpy(receiver, sorted, 0);
i = sortedLength;
} else {
for (; i < sortedLength; ++i)
receiver[i] = sorted[i];
}
for (; i < sortedLength + undefinedCount; ++i)
receiver[i] = @undefined;
for (; i < receiverLength; ++i)
delete receiver[i];
}
@linkTimeConstant
function sortMerge(dst, src, srcIndex, srcEnd, width, comparator)
{
"use strict";
var left = srcIndex;
var leftEnd = @min(left + width, srcEnd);
var right = leftEnd;
var rightEnd = @min(right + width, srcEnd);
for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) {
if (right < rightEnd) {
if (left >= leftEnd) {
@putByValDirect(dst, dstIndex, src[right]);
++right;
continue;
}
// See https://bugs.webkit.org/show_bug.cgi?id=47825 on boolean special-casing
var comparisonResult = comparator(src[right], src[left]);
if (comparisonResult === false || comparisonResult < 0) {
@putByValDirect(dst, dstIndex, src[right]);
++right;
continue;
}
}
@putByValDirect(dst, dstIndex, src[left]);
++left;
}
}
@linkTimeConstant
function sortMergeSort(array, comparator)
{
"use strict";
var valueCount = array.length;
var buffer = @newArrayWithSize(valueCount);
var dst = buffer;
var src = array;
for (var width = 1; width < valueCount; width *= 2) {
for (var srcIndex = 0; srcIndex < valueCount; srcIndex += 2 * width)
@sortMerge(dst, src, srcIndex, valueCount, width, comparator);
var tmp = src;
src = dst;
dst = tmp;
}
return src;
}
@linkTimeConstant
function sortBucketSort(array, dst, bucket, depth)
{
"use strict";
if (bucket.length < 32 || depth > 32) {
var sorted = @sortMergeSort(bucket, @sortStringComparator);
for (var i = 0; i < sorted.length; ++i) {
@putByValDirect(array, dst, sorted[i].value);
++dst;
}
return dst;
}
var buckets = [ ];
@setPrototypeDirect.@call(buckets, null);
for (var i = 0; i < bucket.length; ++i) {
var entry = bucket[i];
var string = entry.string;
if (string.length == depth) {
@putByValDirect(array, dst, entry.value);
++dst;
continue;
}
var c = string.@charCodeAt(depth);
var cBucket = buckets[c];
if (cBucket)
@arrayPush(cBucket, entry);
else
@putByValDirect(buckets, c, [ entry ]);
}
for (var i = 0; i < buckets.length; ++i) {
if (!buckets[i])
continue;
dst = @sortBucketSort(array, dst, buckets[i], depth + 1);
}
return dst;
}
function sort(comparator)
{
"use strict";
var isStringSort = false;
if (comparator === @undefined)
isStringSort = true;
else if (!@isCallable(comparator))
@throwTypeError("Array.prototype.sort requires the comparator argument to be a function or undefined");
var receiver = @toObject(this, "Array.prototype.sort requires that |this| not be null or undefined");
var receiverLength = @toLength(receiver.length);
// For compatibility with Firefox and Chrome, do nothing observable
// to the target array if it has 0 or 1 sortable properties.
if (receiverLength < 2)
return receiver;
var compacted = [ ];
var sorted = null;
var undefinedCount = @sortCompact(receiver, receiverLength, compacted, isStringSort);
if (isStringSort) {
sorted = @newArrayWithSize(compacted.length);
@sortBucketSort(sorted, 0, compacted, 0);
} else
sorted = @sortMergeSort(compacted, comparator);
@sortCommit(receiver, receiverLength, sorted, undefinedCount);
return receiver;
}
@linkTimeConstant
function concatSlowPath()
{
"use strict";
var currentElement = @toObject(this, "Array.prototype.concat requires that |this| not be null or undefined");
var argCount = arguments.length;
var result = @arraySpeciesCreate(currentElement, 0);
var resultIsArray = @isJSArray(result);
var resultIndex = 0;
var argIndex = 0;
do {
var spreadable = @isObject(currentElement) && currentElement.@@isConcatSpreadable;
if ((spreadable === @undefined && @isArray(currentElement)) || spreadable) {
var length = @toLength(currentElement.length);
if (length + resultIndex > @MAX_SAFE_INTEGER)
@throwTypeError("Length exceeded the maximum array length");
if (resultIsArray && @isJSArray(currentElement) && length + resultIndex <= @MAX_ARRAY_INDEX) {
@appendMemcpy(result, currentElement, resultIndex);
resultIndex += length;
} else {
for (var i = 0; i < length; i++) {
if (i in currentElement)
@putByValDirect(result, resultIndex, currentElement[i]);
resultIndex++;
}
}
} else {
if (resultIndex >= @MAX_SAFE_INTEGER)
@throwTypeError("Length exceeded the maximum array length");
@putByValDirect(result, resultIndex++, currentElement);
}
currentElement = arguments[argIndex];
} while (argIndex++ < argCount);
result.length = resultIndex;
return result;
}
function concat(first)
{
"use strict";
if (@argumentCount() === 1
&& @isJSArray(this)
&& @tryGetByIdWithWellKnownSymbol(this, "isConcatSpreadable") === @undefined
&& (!@isObject(first) || @tryGetByIdWithWellKnownSymbol(first, "isConcatSpreadable") === @undefined)) {
var result = @concatMemcpy(this, first);
if (result !== null)
return result;
}
return @tailCallForwardArguments(@concatSlowPath, this);
}
@linkTimeConstant
function maxWithPositives(a, b)
{
"use strict";
return (a < b) ? b : a;
}
@linkTimeConstant
function minWithMaybeNegativeZeroAndPositive(maybeNegativeZero, positive)
{
"use strict";
return (maybeNegativeZero < positive) ? maybeNegativeZero : positive;
}
function copyWithin(target, start /*, end */)
{
"use strict";
var array = @toObject(this, "Array.prototype.copyWithin requires that |this| not be null or undefined");
var length = @toLength(array.length);
var relativeTarget = @toIntegerOrInfinity(target);
var to = (relativeTarget < 0) ? @maxWithPositives(length + relativeTarget, 0) : @minWithMaybeNegativeZeroAndPositive(relativeTarget, length);
var relativeStart = @toIntegerOrInfinity(start);
var from = (relativeStart < 0) ? @maxWithPositives(length + relativeStart, 0) : @minWithMaybeNegativeZeroAndPositive(relativeStart, length);
var relativeEnd;
var end = @argument(2);
if (end === @undefined)
relativeEnd = length;
else
relativeEnd = @toIntegerOrInfinity(end);
var finalValue = (relativeEnd < 0) ? @maxWithPositives(length + relativeEnd, 0) : @minWithMaybeNegativeZeroAndPositive(relativeEnd, length);
var count = @minWithMaybeNegativeZeroAndPositive(finalValue - from, length - to);
var direction = 1;
if (from < to && to < from + count) {
direction = -1;
from = from + count - 1;
to = to + count - 1;
}
for (var i = 0; i < count; ++i, from += direction, to += direction) {
if (from in array)
array[to] = array[from];
else
delete array[to];
}
return array;
}
@linkTimeConstant
function flatIntoArray(target, source, sourceLength, targetIndex, depth)
{
"use strict";
for (var sourceIndex = 0; sourceIndex < sourceLength; ++sourceIndex) {
if (sourceIndex in source) {
var element = source[sourceIndex];
if (depth > 0 && @isArray(element))
targetIndex = @flatIntoArray(target, element, @toLength(element.length), targetIndex, depth - 1);
else {
if (targetIndex >= @MAX_SAFE_INTEGER)
@throwTypeError("flatten array exceeds 2**53 - 1");
@putByValDirect(target, targetIndex, element);
++targetIndex;
}
}
}
return targetIndex;
}
function flat()
{
"use strict";
var array = @toObject(this, "Array.prototype.flat requires that |this| not be null or undefined");
var length = @toLength(array.length);
var depthNum = 1;
var depth = @argument(0);
if (depth !== @undefined)
depthNum = @toIntegerOrInfinity(depth);
var result = @arraySpeciesCreate(array, 0);
@flatIntoArray(result, array, length, 0, depthNum);
return result;
}
@linkTimeConstant
function flatIntoArrayWithCallback(target, source, sourceLength, targetIndex, callback, thisArg)
{
"use strict";
for (var sourceIndex = 0; sourceIndex < sourceLength; ++sourceIndex) {
if (sourceIndex in source) {
var element = callback.@call(thisArg, source[sourceIndex], sourceIndex, source);
if (@isArray(element))
targetIndex = @flatIntoArray(target, element, @toLength(element.length), targetIndex, 0);
else {
if (targetIndex >= @MAX_SAFE_INTEGER)
@throwTypeError("flatten array exceeds 2**53 - 1");
@putByValDirect(target, targetIndex, element);
++targetIndex;
}
}
}
return target;
}
function flatMap(callback)
{
"use strict";
var array = @toObject(this, "Array.prototype.flatMap requires that |this| not be null or undefined");
var length = @toLength(array.length);
if (!@isCallable(callback))
@throwTypeError("Array.prototype.flatMap callback must be a function");
var thisArg = @argument(1);
var result = @arraySpeciesCreate(array, 0);
return @flatIntoArrayWithCallback(result, array, length, 0, callback, thisArg);
}
function at(index)
{
"use strict";
var array = @toObject(this, "Array.prototype.at requires that |this| not be null or undefined");
var length = @toLength(array.length);
var k = @toIntegerOrInfinity(index);
if (k < 0)
k += length;
return (k >= 0 && k < length) ? array[k] : @undefined;
}
function toReversed()
{
"use strict";
// Step 1.
var array = @toObject(this, "Array.prototype.toReversed requires that |this| not be null or undefined");
// Step 2.
var length = @toLength(array.length);
// Step 3.
var result = @newArrayWithSize(length);
// Step 4-5.
for (var k = 0; k < length; k++) {
var fromValue = array[length - k - 1];
@putByValDirect(result, k, fromValue);
}
return result;
}
function toSorted(comparefn)
{
"use strict";
// Step 1.
if (comparefn !== @undefined && !@isCallable(comparefn))
@throwTypeError("Array.prototype.toSorted requires the comparator argument to be a function or undefined");
// Step 2.
var array = @toObject(this, "Array.prototype.toSorted requires that |this| not be null or undefined");
// Step 3.
var length = @toLength(array.length);
// Step 4.
var result = @newArrayWithSize(length);
// Step 8.
for (var k = 0; k < length; k++)
@putByValDirect(result, k, array[k]);
// Step 6.
@arraySort.@call(result, comparefn);
return result;
}
function toSpliced(start, deleteCount /*, ...items */)
{
"use strict"
// Step 1.
var array = @toObject(this, "Array.prototype.toSpliced requires that |this| not be null or undefined");
// Step 2.
var length = @toLength(array.length);
// Step 3.
var relativeStart = @toIntegerOrInfinity(start);
var actualStart;
// Step 4-6.
if (relativeStart === -@Infinity)
actualStart = 0;
else if (relativeStart < 0)
actualStart = length + relativeStart > 0 ? length + relativeStart : 0;
else
actualStart = @min(relativeStart, length);
// Step 7.
var insertCount = 0;
var actualDeleteCount;
// Step 8-10.
if (arguments.length === 0)
actualDeleteCount = 0;
else if (arguments.length === 1)
actualDeleteCount = length - actualStart;
else {
insertCount = arguments.length - 2;
var tempDeleteCount = @toIntegerOrInfinity(deleteCount);
tempDeleteCount = tempDeleteCount > 0 ? tempDeleteCount : 0;
actualDeleteCount = @min(tempDeleteCount, length - actualStart);
}
// Step 11.
var newLen = length + insertCount - actualDeleteCount;
// Step 12.
if (newLen >= @MAX_SAFE_INTEGER)
@throwTypeError("Array length exceeds 2**53 - 1");
// Step 13.
var result = @newArrayWithSize(newLen);
// Step 14.
var k = 0;
// Step 16.
for (; k < actualStart; k++)
@putByValDirect(result, k, array[k]);
// Step 17.
for (var i = 0; i < insertCount; i++, k++)
@putByValDirect(result, k, arguments[i + 2]);
// Step 18.
for (; k < newLen; k++) {
var from = k + actualDeleteCount - insertCount;
@putByValDirect(result, k, array[from]);
}
return result;
}
function with(index, value)
{
"use strict";
// Step 1.
var array = @toObject(this, "Array.prototype.with requires that |this| not be null or undefined");
// Step 2.
var length = @toLength(array.length);
// Step 3.
var relativeIndex = @toIntegerOrInfinity(index);
// Step 4-5.
var actualIndex;
if (relativeIndex >= 0)
actualIndex = relativeIndex;
else
actualIndex = length + relativeIndex;
// Step 6.
if (actualIndex >= length || actualIndex < 0)
@throwRangeError("Array index out of Range");
// Step 7.
var result = @newArrayWithSize(length);
// Step 8-9
for (var k = 0; k < length; k++) {
if (k === actualIndex)
@putByValDirect(result, k, value);
else
@putByValDirect(result, k, array[k]);
}
return result;
}