blob: 709c4533795d4529600e93c49af1bd224a60223c [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var typedArrayConstructors = [
6 Uint8Array,
7 Int8Array,
8 Uint16Array,
9 Int16Array,
10 Uint32Array,
11 Int32Array,
12 Uint8ClampedArray,
13 Float32Array,
14 Float64Array
15];
16
17for (var constructor of typedArrayConstructors) {
18 assertEquals(1, constructor.from.length);
19
20 // TypedArray.from only callable on this subclassing %TypedArray%
21 assertThrows(function () {constructor.from.call(Array, [])}, TypeError);
22
23 function assertArrayLikeEquals(value, expected, type) {
24 assertEquals(value.__proto__, type.prototype);
25 assertEquals(expected.length, value.length);
26 for (var i = 0; i < value.length; ++i) {
27 assertEquals(expected[i], value[i]);
28 }
29 }
30
31 // Assert that calling mapfn with / without thisArg in sloppy and strict modes
32 // works as expected.
33 var global = this;
34 function non_strict() { assertEquals(global, this); }
35 function strict() { 'use strict'; assertEquals(undefined, this); }
36 function strict_null() { 'use strict'; assertEquals(null, this); }
37 constructor.from([1], non_strict);
38 constructor.from([1], non_strict, void 0);
39 constructor.from([1], non_strict, null);
40 constructor.from([1], strict);
41 constructor.from([1], strict, void 0);
42 constructor.from([1], strict_null, null);
43
44 // TypedArray.from can only be called on TypedArray constructors
45 assertThrows(function() {constructor.from.call({}, [])}, TypeError);
46 assertThrows(function() {constructor.from.call([], [])}, TypeError);
47 assertThrows(function() {constructor.from.call(1, [])}, TypeError);
48 assertThrows(function() {constructor.from.call(undefined, [])}, TypeError);
49
50 // Converting from various other types, demonstrating that it can
51 // operate on array-like objects as well as iterables.
52 // TODO(littledan): constructors should have similar flexibility.
53 assertArrayLikeEquals(constructor.from(
54 { length: 1, 0: 5 }), [5], constructor);
55
56 assertArrayLikeEquals(constructor.from(
57 { length: -1, 0: 5 }), [], constructor);
58
59 assertArrayLikeEquals(constructor.from(
60 [1, 2, 3]), [1, 2, 3], constructor);
61
62 var set = new Set([1, 2, 3]);
63 assertArrayLikeEquals(constructor.from(set), [1, 2, 3],
64 constructor);
65
66 function* generator() {
67 yield 4;
68 yield 5;
69 yield 6;
70 }
71
72 assertArrayLikeEquals(constructor.from(generator()),
73 [4, 5, 6], constructor);
74
75 assertThrows(function() { constructor.from(null); }, TypeError);
76 assertThrows(function() { constructor.from(undefined); }, TypeError);
77 assertThrows(function() { constructor.from([], null); }, TypeError);
78 assertThrows(function() { constructor.from([], 'noncallable'); },
79 TypeError);
80
81 var nullIterator = {};
82 nullIterator[Symbol.iterator] = null;
83 assertArrayLikeEquals(constructor.from(nullIterator), [],
84 constructor);
85
86 var nonObjIterator = {};
87 nonObjIterator[Symbol.iterator] = function() { return 'nonObject'; };
88 assertThrows(function() { constructor.from(nonObjIterator); },
89 TypeError);
90
91 assertThrows(function() { constructor.from([], null); }, TypeError);
92
93 // Ensure iterator is only accessed once, and only invoked once
94 var called = 0;
95 var arr = [1, 2, 3];
96 var obj = {};
97 var counter = 0;
98
99 // Test order --- only get iterator method once
100 function testIterator() {
101 called++;
102 assertEquals(obj, this);
103 return arr[Symbol.iterator]();
104 }
105 var getCalled = 0;
106 Object.defineProperty(obj, Symbol.iterator, {
107 get: function() {
108 getCalled++;
109 return testIterator;
110 },
111 set: function() {
112 assertUnreachable('@@iterator should not be set');
113 }
114 });
115 assertArrayLikeEquals(constructor.from(obj), [1, 2, 3], constructor);
116 assertEquals(getCalled, 1);
117 assertEquals(called, 1);
118
119 assertEquals(constructor, Uint8Array.from.call(constructor, [1]).constructor);
120 assertEquals(Uint8Array, constructor.from.call(Uint8Array, [1]).constructor);
121}