Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: test/mjsunit/harmony/array-iterator.js

Issue 338323003: Add @@iterator to Array.prototype (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix @@iterator property attributes, add tests Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/array-iterator.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 10 matching lines...) Expand all
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --harmony-iteration --allow-natives-syntax 28 // Flags: --harmony-iteration --allow-natives-syntax
29 29
30 30
31 var NONE = 0;
32 var READ_ONLY = 1;
33 var DONT_ENUM = 2;
34 var DONT_DELETE = 4;
35
36
37 function assertHasOwnProperty(object, name, attrs) {
38 assertTrue(object.hasOwnProperty(name));
39 var desc = Object.getOwnPropertyDescriptor(object, name);
40 assertEquals(desc.writable, !(attrs & READ_ONLY));
41 assertEquals(desc.enumerable, !(attrs & DONT_ENUM));
42 assertEquals(desc.configurable, !(attrs & DONT_DELETE));
43 }
44
45
31 function TestArrayPrototype() { 46 function TestArrayPrototype() {
32 assertTrue(Array.prototype.hasOwnProperty('entries')); 47 assertHasOwnProperty(Array.prototype, 'entries', DONT_ENUM);
33 assertTrue(Array.prototype.hasOwnProperty('values')); 48 assertHasOwnProperty(Array.prototype, 'values', DONT_ENUM);
34 assertTrue(Array.prototype.hasOwnProperty('keys')); 49 assertHasOwnProperty(Array.prototype, 'keys', DONT_ENUM);
50 assertHasOwnProperty(Array.prototype, Symbol.iterator, DONT_ENUM);
35 51
36 assertFalse(Array.prototype.propertyIsEnumerable('entries')); 52 assertEquals(Array.prototype.values, Array.prototype[Symbol.iterator]);
37 assertFalse(Array.prototype.propertyIsEnumerable('values'));
38 assertFalse(Array.prototype.propertyIsEnumerable('keys'));
39 } 53 }
40 TestArrayPrototype(); 54 TestArrayPrototype();
41 55
42 56
43 function assertIteratorResult(value, done, result) { 57 function assertIteratorResult(value, done, result) {
44 assertEquals({value: value, done: done}, result); 58 assertEquals({value: value, done: done}, result);
45 } 59 }
46 60
47 61
48 function TestValues() { 62 function TestValues() {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 assertIteratorResult([1, 'b'], false, iterator.next()); 134 assertIteratorResult([1, 'b'], false, iterator.next());
121 assertIteratorResult([2, 'c'], false, iterator.next()); 135 assertIteratorResult([2, 'c'], false, iterator.next());
122 array.push('d'); 136 array.push('d');
123 assertIteratorResult([3, 'd'], false, iterator.next()); 137 assertIteratorResult([3, 'd'], false, iterator.next());
124 assertIteratorResult(void 0, true, iterator.next()); 138 assertIteratorResult(void 0, true, iterator.next());
125 } 139 }
126 TestEntriesMutate(); 140 TestEntriesMutate();
127 141
128 142
129 function TestArrayIteratorPrototype() { 143 function TestArrayIteratorPrototype() {
130 var ArrayIteratorPrototype = [].values().__proto__;
131 assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor'));
132 assertEquals(ArrayIteratorPrototype.__proto__, Object.prototype);
133 assertArrayEquals(['next'],
134 Object.getOwnPropertyNames(ArrayIteratorPrototype));
135 }
136 TestArrayIteratorPrototype();
137
138
139 function TestArrayIteratorPrototype() {
140 var array = []; 144 var array = [];
141 var iterator = array.values(); 145 var iterator = array.values();
142 146
143 var ArrayIteratorPrototype = iterator.__proto__; 147 var ArrayIteratorPrototype = iterator.__proto__;
144 148
145 assertEquals(ArrayIteratorPrototype, array.values().__proto__); 149 assertEquals(ArrayIteratorPrototype, array.values().__proto__);
146 assertEquals(ArrayIteratorPrototype, array.keys().__proto__); 150 assertEquals(ArrayIteratorPrototype, array.keys().__proto__);
147 assertEquals(ArrayIteratorPrototype, array.entries().__proto__); 151 assertEquals(ArrayIteratorPrototype, array.entries().__proto__);
148 152
149 assertEquals(Object.prototype, ArrayIteratorPrototype.__proto__); 153 assertEquals(Object.prototype, ArrayIteratorPrototype.__proto__);
150 154
151 assertEquals('Array Iterator', %_ClassOf(array.values())); 155 assertEquals('Array Iterator', %_ClassOf(array.values()));
152 assertEquals('Array Iterator', %_ClassOf(array.keys())); 156 assertEquals('Array Iterator', %_ClassOf(array.keys()));
153 assertEquals('Array Iterator', %_ClassOf(array.entries())); 157 assertEquals('Array Iterator', %_ClassOf(array.entries()));
154 158
155 assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor')); 159 assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor'));
156 assertArrayEquals(['next'], 160 assertArrayEquals(['next'],
157 Object.getOwnPropertyNames(ArrayIteratorPrototype)); 161 Object.getOwnPropertyNames(ArrayIteratorPrototype));
162 assertHasOwnProperty(ArrayIteratorPrototype, 'next', DONT_ENUM);
163 assertHasOwnProperty(ArrayIteratorPrototype, Symbol.iterator, DONT_ENUM);
158 } 164 }
159 TestArrayIteratorPrototype(); 165 TestArrayIteratorPrototype();
160 166
161 167
162 function TestForArrayValues() { 168 function TestForArrayValues() {
163 var buffer = []; 169 var buffer = [];
164 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; 170 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
165 var i = 0; 171 var i = 0;
166 for (var value of array.values()) { 172 for (var value of array.values()) {
167 buffer[i++] = value; 173 buffer[i++] = value;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 } 215 }
210 assertTrue(isNaN(buffer[buffer.length - 1][1])); 216 assertTrue(isNaN(buffer[buffer.length - 1][1]));
211 217
212 for (var i = 0; i < buffer.length; i++) { 218 for (var i = 0; i < buffer.length; i++) {
213 assertEquals(i, buffer[i][0]); 219 assertEquals(i, buffer[i][0]);
214 } 220 }
215 } 221 }
216 TestForArrayEntries(); 222 TestForArrayEntries();
217 223
218 224
225 function TestForArray() {
226 var buffer = [];
227 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
228 var i = 0;
229 for (var value of array) {
230 buffer[i++] = value;
231 }
232
233 assertEquals(8, buffer.length);
234
235 for (var i = 0; i < buffer.length - 1; i++) {
236 assertEquals(array[i], buffer[i]);
rossberg 2014/06/18 09:25:45 Use assertSame to avoid the special case for NaN.
rossberg 2014/06/25 07:52:39 Just saw that you changed this to assertSame, but
237 }
238 assertTrue(isNaN(buffer[buffer.length - 1]));
239 }
240 TestForArrayValues();
241
242
219 function TestNonOwnSlots() { 243 function TestNonOwnSlots() {
220 var array = [0]; 244 var array = [0];
221 var iterator = array.values(); 245 var iterator = array.values();
222 var object = {__proto__: iterator}; 246 var object = {__proto__: iterator};
223 247
224 assertThrows(function() { 248 assertThrows(function() {
225 object.next(); 249 object.next();
226 }, TypeError); 250 }, TypeError);
227 } 251 }
228 TestNonOwnSlots(); 252 TestNonOwnSlots();
OLDNEW
« no previous file with comments | « src/array-iterator.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698