forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypedArray.js
More file actions
1407 lines (1259 loc) · 35.1 KB
/
TypedArray.js
File metadata and controls
1407 lines (1259 loc) · 35.1 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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// RUN: %hermes -Xhermes-internal-test-methods -Xes6-proxy -O -gc-sanitize-handles=0 %s | %FileCheck %s
'use strict';
print("START");
//CHECK:START
function isLittleEndian() {
var uint16 = new Uint16Array(1);
uint16[0] = 1;
var uint8 = new Uint8Array(uint16.buffer);
return uint8[0] === 1;
}
function getByteWidth(cons) {
switch (cons) {
case Int8Array:
case Uint8Array:
case Uint8ClampedArray:
return 1;
case Int16Array:
case Uint16Array:
return 2;
case Int32Array:
case Uint32Array:
case Float32Array:
return 4;
case Float64Array:
return 8;
}
}
// This has the same API as the Node.js assert object.
var assert = {
_isEqual: function(a, b) {
// Remember to check for NaN which does not compare as equal with itself.
return a === b || (Number.isNaN(a) && Number.isNaN(b));
},
equal: function(actual, expected, msg) {
if (!assert._isEqual(actual, expected)) {
assert.fail(
(msg ? msg + ' -- ' : '') +
'Not equal: actual <' +
actual +
'>, and expected <' +
expected +
'>',
);
}
},
_isArrayEqual: function(a, b) {
if (a.length !== b.length)
return false;
for (var i = 0; i < a.length; i++) {
if (!assert._isEqual(a[i], b[i]))
return false;
}
return true;
},
arrayEqual: function(actual, expected, msg) {
if (!assert._isArrayEqual(actual, expected)) {
assert.fail(
(msg ? msg + ' -- ' : '') +
'Array not equal: actual <' +
actual +
'>, and expected <' +
expected +
'>',
);
}
},
notEqual: function(actual, expected, msg) {
if (assert._isEqual(actual, expected)) {
assert.fail(
(msg ? msg + ' -- ' : '') +
'Equal: actual <' +
actual +
'>, and expected <' +
expected +
'>',
);
}
},
ok: function(value, msg) {
assert.equal(!!value, true, msg);
},
throws: function(block, error, msg) {
try {
block();
} catch (e) {
assert.equal(e.constructor, error, msg);
return;
}
// Can't put fail inside the try because it will catch the AssertionError.
assert.fail((msg ? msg + ' -- ' : '') + 'Failed to throw');
},
fail: function(msg) {
throw new Error('AssertionError: ' + (msg ? msg : 'Failed'));
},
};
var cons = [
Int8Array,
Int16Array,
Int32Array,
Uint8Array,
Uint8ClampedArray,
Uint16Array,
Uint32Array,
Float32Array,
Float64Array,
];
var LE = isLittleEndian();
// Check length
cons.forEach(function(TypedArray) {
var buf = new ArrayBuffer(32);
var view = new TypedArray(buf);
assert.equal(view.length, buf.byteLength / getByteWidth(TypedArray));
});
// Check byteLength
cons.forEach(function(TypedArray) {
var buf = new ArrayBuffer(32);
var view = new TypedArray(buf);
assert.equal(view.byteLength, buf.byteLength);
});
// Check byteOffset
cons.forEach(function(TypedArray) {
assert.equal(new TypedArray(new ArrayBuffer(32), 8, 1).byteOffset, 8);
});
// Check BYTES_PER_ELEMENT
cons.forEach(function(TypedArray) {
assert.equal(TypedArray.BYTES_PER_ELEMENT, getByteWidth(TypedArray));
assert.equal(new TypedArray().BYTES_PER_ELEMENT, getByteWidth(TypedArray));
// Check flags of the field.
var desc = Object.getOwnPropertyDescriptor(TypedArray, 'BYTES_PER_ELEMENT');
assert.ok(!desc.writable);
assert.ok(!desc.enumerable);
assert.ok(!desc.configurable);
desc = Object.getOwnPropertyDescriptor(
TypedArray.prototype,
'BYTES_PER_ELEMENT',
);
assert.ok(!desc.writable);
assert.ok(!desc.enumerable);
assert.ok(!desc.configurable);
});
// Check the flags for various fields.
cons.forEach(function(TypedArray) {
var proto = Object.getPrototypeOf(TypedArray);
assert.ok(
!Object.getOwnPropertyDescriptor(proto.prototype, 'buffer').enumerable,
);
assert.ok(
!Object.getOwnPropertyDescriptor(proto.prototype, 'byteLength').enumerable,
);
assert.ok(
!Object.getOwnPropertyDescriptor(proto.prototype, 'byteOffset').enumerable,
);
assert.ok(
Object.getOwnPropertyDescriptor(proto.prototype, 'buffer').configurable,
);
assert.ok(
Object.getOwnPropertyDescriptor(proto.prototype, 'byteLength').configurable,
);
assert.ok(
Object.getOwnPropertyDescriptor(proto.prototype, 'byteOffset').configurable,
);
});
// Check length of the constructors.
cons.forEach(function(TypedArray) {
assert.equal(TypedArray.length, 3);
});
// Check initialized to zero
cons.forEach(function(TypedArray) {
var buf = new ArrayBuffer(32);
var view = new TypedArray(buf);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], 0);
}
});
(function checkBufferShared() {
var buf = new ArrayBuffer(32);
var int8view = new Int8Array(buf);
var uint8view = new Uint8Array(buf);
// Check that the buffer is shared
// Ensure they're both zero'd out before writing (in case previous tests left
// data in them)
int8view[0] = 0;
uint8view[0] = 0;
int8view[0] = 5;
assert.equal(uint8view[0], 5);
})();
/// @name Constructors
/// @{
// Check constructor from Array with ints
cons.forEach(function(TypedArray) {
var view = new TypedArray([1, 2, 3]);
assert.equal(view.length, 3);
assert.equal(view[0], 1);
assert.equal(view[1], 2);
assert.equal(view[2], 3);
});
// Check constructor from Array with arbitrary types
cons.forEach(function(TypedArray) {
var view = new TypedArray(['1', {}, false]);
assert.equal(view.length, 3);
assert.equal(view[0], 1);
if (TypedArray == Float32Array || TypedArray == Float64Array) {
assert.equal(view[1], NaN);
} else {
assert.equal(view[1], 0);
}
assert.equal(view[2], 0);
});
// Check constructor from empty Array
cons.forEach(function(TypedArray) {
assert.equal(new TypedArray([]).length, 0);
});
// Constructor from undefined should give an empty TypedArray
cons.forEach(function(TypedArray) {
assert.equal(new TypedArray(undefined).length, 0);
});
// Check constructor from Array-esque object
cons.forEach(function(TypedArray) {
var arrayEsque = {length: 1};
arrayEsque[0] = 1;
var view = new TypedArray(arrayEsque);
assert.equal(view.length, 1);
assert.equal(view[0], 1);
});
// Empty constructor
cons.forEach(function(TypedArray) {
assert.equal(new TypedArray().length, 0);
});
// Constructor with length
cons.forEach(function(TypedArray) {
var view = new TypedArray(16);
assert.equal(view.length, 16);
assert.equal(view[0], 0);
});
// Constructor from buffer but with zero size
cons.forEach(function(TypedArray) {
var buf = new ArrayBuffer(32);
var emptyArrayFromOtherBuffer = new TypedArray(buf, 16, 0);
assert.equal(emptyArrayFromOtherBuffer.length, 0);
});
// Constructor from buffer with length smaller than offset
cons.forEach(function(TypedArray) {
var buf = new ArrayBuffer(32);
var arrayFromBufferWithSmallLength = new TypedArray(buf, 8, 2);
assert.equal(arrayFromBufferWithSmallLength.length, 2);
assert.equal(arrayFromBufferWithSmallLength[0], 0);
assert.equal(arrayFromBufferWithSmallLength[1], 0);
});
// Constructor from another TypedArray of the same type
cons.forEach(function(TypedArray) {
var original = new TypedArray(32);
var sameTypeArray = new TypedArray(original);
assert.equal(sameTypeArray.length, original.length);
// The bytes should have been copied, and reside in a different buffer
assert.notEqual(sameTypeArray.buffer, original.buffer);
});
(function checkLargeValuesInConstructor() {
// Check constructor from Array with huge values
// 256 should wrap around to be 0
var consFromArray = new Uint8Array([256]);
assert.equal(consFromArray.length, 1);
assert.equal(consFromArray[0], 0);
})();
/// @}
// Check for..of loop
cons.forEach(function(TypedArray) {
var arr = [10, 20, 30]
var view = new TypedArray(arr);
var result = [];
for (var i of view)
result.push(i);
assert.arrayEqual(result, arr);
});
// Check for..in loop
cons.forEach(function(TypedArray) {
var view = new TypedArray(5);
var result = [];
for(var i in view)
result.push(i);
assert.arrayEqual(result, ["0", "1", "2", "3", "4"]);
// Detach the buffer. Result should be the same because
// IsDetachedBuffer is not checked in ES6 9.4.5.6 [[OwnPropertyKeys]].
HermesInternal.detachArrayBuffer(view.buffer);
result = [];
for(var i in view)
result.push(i);
assert.arrayEqual(result, ["0", "1", "2", "3", "4"]);
});
// Check basic writability properties
cons.forEach(function(TypedArray) {
var view = new TypedArray(32);
for (var i = 0; i < view.length; i++) {
view[i] = i;
assert.equal(view[i], i);
}
});
// Check uint8clamped is clamped
(function checkClamped() {
var view = new Uint8ClampedArray(1);
// Writing a value > 255 into uint8clampedview should give 255
view[0] = 300;
assert.equal(view[0], 255);
// Also check for round-to-even behavior
view[0] = 1.5;
assert.equal(view[0], 2);
view[0] = 2.5;
assert.equal(view[0], 2);
})();
(function checkFloatMaxValue() {
var view = new Float32Array(1);
view[0] = Number.MAX_VALUE;
assert.equal(view[0], Infinity);
})();
(function checkFloatSanitization() {
// Check float is sanitized
// Basis of this test is that in Hermes, a NaN can have a "payload" which
// could represent a pointer. We must ensure that if such a value is read from,
// the payload is sanitized out
var buf = new ArrayBuffer(8);
var uint8view = new Uint8Array(buf);
var uint32view = new Uint32Array(buf);
var float32view = new Float32Array(buf);
var float64view = new Float64Array(buf);
// all 1's
uint32view[0] = 0xffffffff;
assert.equal(float32view[0], NaN);
// 64-bit version, need to use a uint8 array,
// 0xff ff 00 01 00 e0 00 58
// is an example tagged NaN value to sanitize
if (LE) {
// little-endian order of bytes
uint8view[0] = 0x58;
uint8view[1] = 0x00;
uint8view[2] = 0xe0;
uint8view[3] = 0x00;
uint8view[4] = 0x01;
uint8view[5] = 0x00;
uint8view[6] = 0xff;
uint8view[7] = 0xff;
} else {
// big-endian order of bytes
uint8view[7] = 0xff;
uint8view[6] = 0xff;
uint8view[5] = 0x00;
uint8view[4] = 0x01;
uint8view[3] = 0x00;
uint8view[2] = 0xe0;
uint8view[1] = 0x00;
uint8view[0] = 0x58;
}
assert.equal(float64view[0], NaN);
})();
(function checkConstructorFromDifferentType() {
// Constructor from another TypedArray of a different type
var int32view = new Int32Array([0, 1, 2, 3]);
// This constructor copies the values
var differentTypeArray = new Int16Array(int32view);
assert.equal(differentTypeArray.length, int32view.length);
// The bytes should have been copied
for (var i = 0; i < differentTypeArray.length; i++) {
assert.equal(differentTypeArray[i], i);
}
})();
(function checkSignsInFloat() {
// Signs in float
var a = new Float64Array(4);
a[0] = -0;
assert.equal(1 / a[0], -Infinity);
})();
/// @name %TypedArray% in and delete
/// @{
cons.forEach(function(TA) {
var ta = new TA([1,2,3,4,5]);
assert.equal(false, -1 in ta);
assert.equal(true, 0 in ta);
assert.equal(true, 4 in ta);
assert.equal(false, 5 in ta);
assert.equal(true, delete ta[-1]);
assert.throws(_ => delete ta[0], TypeError);
assert.throws(_ => delete ta[4], TypeError);
assert.equal(true, delete ta[5]);
assert.equal(true, Reflect.deleteProperty(ta, -1));
assert.equal(false, Reflect.deleteProperty(ta, 0));
assert.equal(false, Reflect.deleteProperty(ta, 4));
assert.equal(true, Reflect.deleteProperty(ta, 5));
});
/// @}
/// @name %TypedArray%.prototype.buffer
/// @{
cons.forEach(function(TypedArray) {
assert.throws(function() {
TypedArray.prototype.buffer;
}, TypeError);
});
/// @}
/// @name %TypedArray%.prototype.copyWithin
/// @{
cons.forEach(function(TA) {
assert.equal(new TA([1, 2, 3, 4, 5]).copyWithin(-2).toString(), '1,2,3,1,2');
assert.equal(new TA([1, 2, 3, 4, 5]).copyWithin(0).toString(), '1,2,3,4,5');
assert.equal(
new TA([1, 2, 3, 4, 5]).copyWithin(0, 3, 4).toString(),
'4,2,3,4,5',
);
assert.equal(
new TA([1, 2, 3, 4, 5]).copyWithin(-2, -3, -1).toString(),
'1,2,3,3,4',
);
if (HermesInternal.detachArrayBuffer) {
var ta = new TA(1000);
assert.throws(function() {
ta.copyWithin(
{
valueOf: function() {
HermesInternal.detachArrayBuffer(ta.buffer);
return 1;
},
},
4,
256,
);
}, TypeError);
}
});
(function() {
// Ensure bit-level preservation.
var A = new Float32Array(4);
A[0] = 0 / 0;
A[1] = -(0 / 0);
var bytes1 = new Uint8Array(A.buffer, 0, 8);
A.copyWithin(2, 0); // Copy bytes to the second half of A.
var bytes2 = new Uint8Array(A.buffer, 8, 8);
assert.equal(bytes1.length, bytes2.length);
for (var i = 0; i < bytes1.length; ++i) {
assert.equal(bytes1[i], bytes2[i]);
}
})();
/// @}
/// @name %TypedArray%.from
/// @{
cons.forEach(function(c, i) {
// Function exists.
assert.ok('from' in c);
assert.ok(c.from);
// Works on arrays.
var ta = c.from([1, 2, 3]);
assert.equal(ta.length, 3);
assert.equal(ta[0], 1);
assert.equal(ta[1], 2);
assert.equal(ta[2], 3);
// Works on a different typed array.
var otherTA = new cons[i == 0 ? cons.length - 1 : i - 1]([1, 2, 3]);
ta = c.from(otherTA);
assert.equal(ta.length, 3);
assert.equal(ta[0], 1);
assert.equal(ta[1], 2);
assert.equal(ta[2], 3);
// "array-like" object.
var arrayLike = {length: 3};
arrayLike[0] = 1;
arrayLike[1] = 2;
arrayLike[2] = 3;
ta = c.from(arrayLike);
assert.equal(ta.length, 3);
assert.equal(ta[0], 1);
assert.equal(ta[1], 2);
assert.equal(ta[2], 3);
// Array with holes.
ta = c.from([1, 2, , , 3]);
assert.equal(ta.length, 5);
assert.equal(ta[0], 1);
assert.equal(ta[1], 2);
// undefined is coerced to zero, except for floats which are NaN.
if (c === Float32Array || c === Float64Array) {
assert.equal(ta[2], NaN);
assert.equal(ta[3], NaN);
} else {
assert.equal(ta[2], 0);
assert.equal(ta[3], 0);
}
assert.equal(ta[4], 3);
// Test calling without the this argument.
var from = c.from;
assert.throws(function() {
from([]);
}, TypeError);
// Test a TypedArray whose length is greater than 2 ^ 32 - 1
// NOTE: This behavior differs from v8 and JSC, because we use uint32_t as our
// indexing type. They also disagree with each other.
assert.throws(function() {
var ta = new c();
Object.defineProperty(ta, "length", {
get: function() {
// This number is 2 ^ 32 + 1.
return 4294967297;
}
});
return c.from(ta);
}, RangeError);
});
/// @}
/// @name %TypedArray%.of
/// @{
cons.forEach(function(TypedArray) {
// Function exists.
assert.ok('of' in TypedArray);
assert.ok(TypedArray.of);
assert.equal(TypedArray.of.length, 0);
var ta = TypedArray.of(1, 2, 3);
assert.equal(ta.length, 3);
assert.equal(ta[0], 1);
assert.equal(ta[1], 2);
assert.equal(ta[2], 3);
// Works on no arguments.
ta = TypedArray.of();
assert.equal(ta.length, 0);
// Test calling without the this argument.
var of = TypedArray.of;
assert.throws(function() {
of();
}, TypeError);
});
// Returning a smaller array than requested.
cons.forEach(function(TypedArray) {
var thisArg = function() {
return new TypedArray(1);
};
assert.throws(function() {
TypedArray.of.call(thisArg, 1, 2, 3, 4);
}, TypeError);
});
/// @}
/// @name %TypedArray%.prototype.every && .some
/// @{
cons.forEach(function(TypedArray) {
var view = new TypedArray([0, 1, 2, 3, 4]);
// Check that callback function is passed parameters correctly
assert.ok(
view.every(function(elem, i, v) {
return elem === i && v == view;
}),
);
// Check basic properties
assert.ok(
view.every(function() {
return true;
}),
);
assert.ok(
!view.every(function() {
return false;
}),
);
assert.ok(
!view.every(function(elem, i) {
return i !== 0;
}),
);
// Check that callback function is passed parameters correctly
assert.ok(
!view.some(function(elem, i, v) {
return !(elem === i && v == view);
}),
);
// Check basic properties
assert.ok(
!view.some(function() {
return false;
}),
);
assert.ok(
view.some(function() {
return true;
}),
);
assert.ok(
view.some(function(elem, i) {
if (i == 0) {
return true;
}
}),
);
});
/// @}
/// @name %TypedArray%.prototype.fill
/// @{
cons.forEach(function(TypedArray) {
var view = new TypedArray(4);
view.fill(1);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], 1);
}
view.fill(2, 3);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], i < 3 ? 1 : 2);
}
view.fill(0);
view.fill(1, 4, 6);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], i < 4 || i >= 6 ? 0 : 1);
}
view.fill(0);
view.fill(1, 4, 100);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], i < 4 ? 0 : 1);
}
view.fill(0);
view.fill(1, -100, 4);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], i < 4 ? 1 : 0);
}
view.fill(0);
view.fill(1, -2, -1);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], i < view.length - 2 || i >= view.length - 1 ? 0 : 1);
}
view.fill(1);
// This should not fill any elements.
view.fill(0, Infinity);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], 1);
}
// This should fill all elements, Infinity is past the end.
view.fill(2, 0, Infinity);
for (var i = 0; i < view.length; i++) {
assert.equal(view[i], 2);
}
// Calling fill on a detached TypedArray should throw TypeError.
HermesInternal.detachArrayBuffer(view.buffer);
assert.throws(function() {
view.fill(0);
}, TypeError);
// Converting the first argument to a number can detach the TypedArray.
view = new TypedArray(4);
assert.throws(function() {
view.fill({
valueOf: function() {
HermesInternal.detachArrayBuffer(view.buffer);
return 1;
},
});
}, TypeError);
});
cons.forEach(function(TypedArray) {
// Fill with start >= end should remain the same.
var arr = new TypedArray([0, 0, 0, 0]);
arr.fill(8, 3, 1);
assert.equal(arr.length, 4);
assert.equal(arr[0], 0);
assert.equal(arr[1], 0);
assert.equal(arr[2], 0);
assert.equal(arr[3], 0);
arr.fill(8, -1, -3);
assert.equal(arr.length, 4);
assert.equal(arr[0], 0);
assert.equal(arr[1], 0);
assert.equal(arr[2], 0);
assert.equal(arr[3], 0);
});
/// @}
/// @name TypedArray.prototype.filter
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([0, 1, 2, 3]);
// Result should be the same length and contain the same elements
var filtered = arr.filter(function(elem, i, view) {
return elem === i && view === arr;
});
// It should be a copy, not the same
assert.notEqual(filtered, arr);
assert.equal(filtered.length, arr.length);
for (var i = 0; i < filtered.length; i++) {
assert.equal(filtered[i], i);
}
filtered = arr.filter(function(_, i) {
return i >= arr.length - 2 && i < arr.length;
});
assert.equal(filtered.length, 2);
assert.equal(filtered[0], arr.length - 2);
assert.equal(filtered[1], arr.length - 1);
assert.throws(function() {
arr.filter(function() {
throw new RangeError();
});
}, RangeError);
});
/// @}
/// @name TypedArray.prototype.find/.findIndex
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([0, 1, 2, 3]);
var cb = function(elem, i, view) {
assert.equal(view, arr);
assert.equal(elem, i);
return i === arr.length - 1;
};
assert.equal(arr.find(cb), arr.length - 1);
assert.equal(arr.findIndex(cb), arr.length - 1);
var numcalls = 0;
cb = function() {
++numcalls;
return true;
};
assert.equal(arr.find(cb), 0);
assert.equal(arr.findIndex(cb), 0);
assert.equal(numcalls, 2);
cb = function() {
return false;
};
assert.equal(arr.find(cb), undefined);
assert.equal(arr.findIndex(cb), -1);
});
/// @}
/// @name TypedArray.prototype.forEach
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([0, 1, 2, 3]);
assert.equal(
arr.forEach(function(elem, i, view) {
assert.equal(elem, i);
assert.equal(view, arr);
}),
undefined,
);
var numcalls = 0;
arr.forEach(function() {
numcalls++;
});
assert.equal(numcalls, arr.length);
});
/// @}
/// @name TypedArray.prototype.includes && .indexOf
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([0, 1, 2, 3]);
assert.ok(arr.includes(1));
assert.equal(arr.indexOf(1), 1);
assert.equal(arr.lastIndexOf(1), 1);
assert.ok(!arr.includes(1000));
assert.equal(arr.indexOf(1000), -1);
assert.equal(arr.lastIndexOf(1000), -1);
assert.ok(arr.includes(0, 0));
assert.ok(!arr.includes(0, 1));
assert.ok(!arr.includes(0, 1000));
assert.equal(arr.indexOf(0, 0), 0);
assert.equal(arr.indexOf(0, 1), -1);
assert.equal(arr.indexOf(0, 1000), -1);
assert.equal(arr.lastIndexOf(0, 0), 0);
assert.equal(arr.lastIndexOf(0, 1), 0);
assert.equal(arr.lastIndexOf(0, 1000), 0);
assert.ok(arr.includes(arr.length - 2, -2));
assert.ok(!arr.includes(arr.length - 3, -1));
assert.equal(arr.indexOf(arr.length - 2, -2), arr.length - 2);
assert.equal(arr.indexOf(arr.length - 3, -1), -1);
assert.equal(arr.lastIndexOf(arr.length - 2, -2), arr.length - 2);
assert.equal(arr.lastIndexOf(arr.length - 3, -1), arr.length - 3);
// Set up duplicates to test indexOf vs lastIndexOf
arr[0] = 50;
arr[1] = 50;
assert.equal(arr.indexOf(50), 0);
assert.equal(arr.lastIndexOf(50), 1);
assert.throws(function() {
arr.includes(0, {
valueOf() {
HermesInternal.detachArrayBuffer(arr.buffer);
return 0;
},
});
}, TypeError);
});
/// @}
/// @name TypedArray.prototype.join && .toString (since toString calls join)
/// @{
cons.forEach(function(ta) {
var arr = new ta([1, 2, 3]);
assert.equal(arr.join(), '1,2,3');
assert.equal(arr.join(','), '1,2,3');
assert.equal(arr.join(''), '123');
assert.equal(arr.join('asdf'), '1asdf2asdf3');
// Test empty case.
assert.equal(new ta([]).join(), '');
// Test toString.
assert.equal(ta.prototype.toString, Array.prototype.toString);
assert.equal(arr.toString(), arr.join());
// Test difference between integral and floating toString
if (ta === Float32Array || ta === Float64Array) {
assert.equal(new ta([{}]).toString(), 'NaN');
} else {
assert.equal(new ta([{}]).toString(), '0', ta.toString());
}
});
/// @}
/// @name TypedArray.prototype.reverse
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([0, 1, 2, 3]);
arr.reverse();
for (var i = 0; i < arr.length; i++) {
assert.equal(arr[i], arr.length - 1 - i);
}
var emptyReversed = new (Object.getPrototypeOf(arr)).constructor(
[],
).reverse();
assert.equal(emptyReversed.length, 0);
var oddReversed = new (Object.getPrototypeOf(arr)).constructor([
1,
2,
3,
]).reverse();
assert.equal(oddReversed.length, 3);
assert.equal(oddReversed[0], 3);
assert.equal(oddReversed[1], 2);
assert.equal(oddReversed[2], 1);
});
/// @}
/// @name TypedArray.prototype.sort
/// @{
cons.forEach(function(ta) {
var x = new ta([3, 2, 1]);
x.sort();
assert.equal(x[0], 1);
assert.equal(x[1], 2);
assert.equal(x[2], 3);
// Check empty doesn't crash.
x = new ta([]);
x.sort();
assert.equal(x.length, 0);
// Check with comparefn.
x = new ta([3, 2, 1]);
// Use the reverse sorter, normal definition is a - b.
x.sort(function(a, b) {
return b - a;
});
assert.equal(x[0], 3);
assert.equal(x[1], 2);
assert.equal(x[2], 1);
assert.throws(function() {
x.sort(null);
}, TypeError);
assert.throws(function() {
x.sort(true);
}, TypeError);
assert.throws(function() {
x.sort(12);
}, TypeError);
assert.throws(function() {
x.sort({});
}, TypeError);
// Sorting a TypedArray allocates handles linearly with respect to the size
// of the buffer. Sorting a larger array will ensure that we clean those up
// periodically.
x = new ta(104);
x.sort(function(unused1, unused2) {
return true;
});
for (var i = 0; i < x.length; i++) {
assert.equal(x[i], 0);
}
// Check that detaching in the middle of sorting will cause a TypeError.
x = new ta([1, 2, 3]);
assert.throws(function() {
x.sort(function f(unused1, unused2) {
var a = {};
a.valueOf = function() {
HermesInternal.detachArrayBuffer(x.buffer);
};
return a;
});
}, TypeError);
});
(function negativeZeroSort() {
var x = new Float64Array([+0, -0]);
x.sort();
assert.equal(1 / x[0], -Infinity);
assert.equal(1 / x[1], +Infinity);
})();
/// @}
/// @name TypedArray.prototype.set
/// @{
(function checkPrototypeSet() {
cons.forEach(function(ta, i) {
var dst = new ta([1, 2, 3]);