OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 | |
5 // Flags: --harmony-arrays | |
6 (function() { | |
7 | |
8 assertEquals(1, Array.from.length); | |
9 | |
10 function assertArrayLikeEquals(value, expected, type) { | |
11 assertInstanceof(value, type); | |
12 assertEquals(expected.length, value.length); | |
13 for (var i=0; i<value.length; ++i) { | |
14 assertEquals(expected[i], value[i]); | |
15 } | |
16 } | |
17 | |
18 // Assert that constructor is called with "length" for array-like objects | |
19 var myCollectionCalled = false; | |
20 function MyCollection(length) { | |
21 myCollectionCalled = true; | |
22 assertEquals(1, arguments.length); | |
23 assertEquals(5, length); | |
24 } | |
25 | |
26 Array.from.call(MyCollection, {length: 5}); | |
27 assertTrue(myCollectionCalled); | |
28 | |
29 // Assert that calling mapfn with / without thisArg in sloppy and strict modes | |
30 // works as expected. | |
31 var global = this; | |
32 function non_strict(){ assertEquals(global, this); } | |
33 function strict(){ "use strict"; assertEquals(void 0, this); } | |
34 function strict_null(){ "use strict"; assertEquals(null, this); } | |
35 Array.from([1], non_strict); | |
36 Array.from([1], non_strict, void 0); | |
37 Array.from([1], non_strict, null); | |
38 Array.from([1], strict); | |
39 Array.from([1], strict, void 0); | |
40 Array.from([1], strict_null, null); | |
41 | |
42 function testArrayFrom(thisArg, constructor) { | |
43 assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], | |
44 constructor); | |
45 assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor); | |
46 assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor); | |
47 assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor); | |
48 assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], | |
49 constructor); | |
50 | |
51 assertArrayLikeEquals(Array.from.call(thisArg, | |
52 { length: 1, '0': { 'foo': 'bar' } }), [{'foo': 'bar'}], constructor); | |
53 | |
54 assertArrayLikeEquals(Array.from.call(thisArg, | |
55 { length: -1, '0': { 'foo': 'bar' } }), [], constructor); | |
56 | |
57 assertArrayLikeEquals(Array.from.call(thisArg, | |
58 [ 'foo', 'bar', 'baz' ]), ['foo', 'bar', 'baz'], constructor); | |
59 | |
60 var kSet = new Set(['foo', 'bar', 'baz']); | |
61 assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'], | |
62 constructor); | |
63 | |
64 var kMap = new Map(['foo', 'bar', 'baz'].entries()); | |
65 assertArrayLikeEquals(Array.from.call(thisArg, kMap), | |
66 [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor); | |
67 | |
68 | |
69 function* generator() { | |
70 yield 'a'; | |
71 yield 'b'; | |
72 yield 'c'; | |
73 } | |
74 | |
75 assertArrayLikeEquals(Array.from.call(thisArg, generator()), | |
76 ['a', 'b', 'c'], constructor); | |
77 | |
78 // Mozilla: | |
79 // Array.from on a string handles surrogate pairs correctly. | |
80 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF | |
81 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor); | |
82 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), | |
83 [gclef, " ", "G"], constructor); | |
84 | |
85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
86 return this.filter(x); | |
87 }, { | |
88 filter: function(x) { return x.toUpperCase(); } | |
89 }), ['T', 'E', 'S', 'T'], constructor); | |
90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
91 return x.toUpperCase(); | |
92 }), ['T', 'E', 'S', 'T'], constructor); | |
93 | |
94 assertThrows(function() { Array.from.call(thisArg, null); }, TypeError); | |
95 assertThrows(function() { Array.from.call(thisArg, undefined); }, TypeError); | |
96 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); | |
97 assertThrows(function() { Array.from.call(thisArg, [], "noncallable"); }, | |
98 TypeError); | |
99 | |
100 var nullIterator = {}; | |
101 nullIterator[Symbol.iterator] = null; | |
102 assertArrayLikeEquals(Array.from.call(thisArg, nullIterator), [], | |
103 constructor); | |
104 | |
105 var nonObjIterator = {}; | |
106 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | |
107 assertThrows(function() { Array.from.call(thisArg, nonObjIterator); }, | |
108 TypeError); | |
109 | |
110 assertThrows(function() { Array.from.call(thisArg, [], null); }, TypeError); | |
111 | |
112 // Ensure iterator is only accessed once, and only invoked once | |
113 var called = false; | |
114 var arr = [1, 2, 3]; | |
115 var obj = {}; | |
116 | |
117 // Test order --- only get iterator method once | |
118 function testIterator() { | |
119 assertFalse(called, "@@iterator should be called only once"); | |
120 called = true; | |
121 assertEquals(obj, this); | |
122 return arr[Symbol.iterator](); | |
123 } | |
124 var getCalled = false; | |
125 Object.defineProperty(obj, Symbol.iterator, { | |
126 get: function() { | |
127 assertFalse(getCalled, "@@iterator should be accessed only once"); | |
128 getCalled = true; | |
129 return testIterator; | |
130 }, | |
131 set: function() { | |
132 assertUnreachable("@@iterator should not be set"); | |
133 } | |
134 }); | |
135 assertArrayLikeEquals(Array.from.call(thisArg, obj), [1, 2, 3], constructor); | |
136 } | |
137 | |
138 function Other() {} | |
139 | |
140 var boundFn = (function() {}).bind(Array, 27); | |
141 | |
142 testArrayFrom(Array, Array); | |
143 testArrayFrom(null, Array); | |
144 testArrayFrom({}, Array); | |
145 testArrayFrom(Object, Object); | |
146 testArrayFrom(Other, Other); | |
147 testArrayFrom(Math.cos, Array); | |
148 testArrayFrom(Math.cos.bind(Math), Array); | |
149 testArrayFrom(boundFn, boundFn); | |
150 | |
151 // Assert that [[DefineOwnProperty]] is used in ArrayFrom, meaning a | |
152 // setter isn't called, and a failed [[DefineOwnProperty]] will throw. | |
153 var setterCalled = 0; | |
154 function exotic() { | |
155 Object.defineProperty(this, '0', { | |
156 get: function() { return 2; }, | |
157 set: function() { setterCalled++; } | |
158 }); | |
159 } | |
160 // Non-configurable properties can't be overwritten with DefineOwnProperty | |
161 assertThrows(function () { Array.from.call(exotic, [1]); }, TypeError); | |
162 // The setter wasn't called | |
163 assertEquals(0, setterCalled); | |
164 | |
165 // Check that array properties defined are writable, enumerable, configurable | |
166 function ordinary() { } | |
167 var x = Array.from.call(ordinary, [2]); | |
168 var xlength = Object.getOwnPropertyDescriptor(x, 'length'); | |
169 assertEquals(1, xlength.value); | |
170 assertEquals(true, xlength.writable); | |
171 assertEquals(true, xlength.enumerable); | |
172 assertEquals(true, xlength.configurable); | |
173 var x0 = Object.getOwnPropertyDescriptor(x, 0); | |
174 assertEquals(2, x0.value); | |
175 assertEquals(true, xlength.writable); | |
176 assertEquals(true, xlength.enumerable); | |
177 assertEquals(true, xlength.configurable); | |
178 | |
179 })(); | |
OLD | NEW |