1// Copyright 2013 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 27 28// Flags: --allow-natives-syntax 29 30 31var NONE = 0; 32var READ_ONLY = 1; 33var DONT_ENUM = 2; 34var DONT_DELETE = 4; 35 36 37function 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 46function TestArrayPrototype() { 47 assertHasOwnProperty(Array.prototype, 'entries', DONT_ENUM); 48 assertHasOwnProperty(Array.prototype, 'keys', DONT_ENUM); 49 assertHasOwnProperty(Array.prototype, Symbol.iterator, DONT_ENUM); 50} 51TestArrayPrototype(); 52 53 54function assertIteratorResult(value, done, result) { 55 assertEquals({value: value, done: done}, result); 56} 57 58 59function TestValues() { 60 var array = ['a', 'b', 'c']; 61 var iterator = array[Symbol.iterator](); 62 assertIteratorResult('a', false, iterator.next()); 63 assertIteratorResult('b', false, iterator.next()); 64 assertIteratorResult('c', false, iterator.next()); 65 assertIteratorResult(void 0, true, iterator.next()); 66 67 array.push('d'); 68 assertIteratorResult(void 0, true, iterator.next()); 69} 70TestValues(); 71 72 73function TestValuesMutate() { 74 var array = ['a', 'b', 'c']; 75 var iterator = array[Symbol.iterator](); 76 assertIteratorResult('a', false, iterator.next()); 77 assertIteratorResult('b', false, iterator.next()); 78 assertIteratorResult('c', false, iterator.next()); 79 array.push('d'); 80 assertIteratorResult('d', false, iterator.next()); 81 assertIteratorResult(void 0, true, iterator.next()); 82} 83TestValuesMutate(); 84 85 86function TestKeys() { 87 var array = ['a', 'b', 'c']; 88 var iterator = array.keys(); 89 assertIteratorResult(0, false, iterator.next()); 90 assertIteratorResult(1, false, iterator.next()); 91 assertIteratorResult(2, false, iterator.next()); 92 assertIteratorResult(void 0, true, iterator.next()); 93 94 array.push('d'); 95 assertIteratorResult(void 0, true, iterator.next()); 96} 97TestKeys(); 98 99 100function TestKeysMutate() { 101 var array = ['a', 'b', 'c']; 102 var iterator = array.keys(); 103 assertIteratorResult(0, false, iterator.next()); 104 assertIteratorResult(1, false, iterator.next()); 105 assertIteratorResult(2, false, iterator.next()); 106 array.push('d'); 107 assertIteratorResult(3, false, iterator.next()); 108 assertIteratorResult(void 0, true, iterator.next()); 109} 110TestKeysMutate(); 111 112 113function TestEntries() { 114 var array = ['a', 'b', 'c']; 115 var iterator = array.entries(); 116 assertIteratorResult([0, 'a'], false, iterator.next()); 117 assertIteratorResult([1, 'b'], false, iterator.next()); 118 assertIteratorResult([2, 'c'], false, iterator.next()); 119 assertIteratorResult(void 0, true, iterator.next()); 120 121 array.push('d'); 122 assertIteratorResult(void 0, true, iterator.next()); 123} 124TestEntries(); 125 126 127function TestEntriesMutate() { 128 var array = ['a', 'b', 'c']; 129 var iterator = array.entries(); 130 assertIteratorResult([0, 'a'], false, iterator.next()); 131 assertIteratorResult([1, 'b'], false, iterator.next()); 132 assertIteratorResult([2, 'c'], false, iterator.next()); 133 array.push('d'); 134 assertIteratorResult([3, 'd'], false, iterator.next()); 135 assertIteratorResult(void 0, true, iterator.next()); 136} 137TestEntriesMutate(); 138 139 140function TestArrayIteratorPrototype() { 141 var array = []; 142 var iterator = array.keys(); 143 144 var ArrayIteratorPrototype = iterator.__proto__; 145 146 assertEquals(ArrayIteratorPrototype, array[Symbol.iterator]().__proto__); 147 assertEquals(ArrayIteratorPrototype, array.keys().__proto__); 148 assertEquals(ArrayIteratorPrototype, array.entries().__proto__); 149 150 assertEquals(Object.prototype, ArrayIteratorPrototype.__proto__); 151 152 assertEquals('Array Iterator', %_ClassOf(array[Symbol.iterator]())); 153 assertEquals('Array Iterator', %_ClassOf(array.keys())); 154 assertEquals('Array Iterator', %_ClassOf(array.entries())); 155 156 assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor')); 157 assertArrayEquals(['next'], 158 Object.getOwnPropertyNames(ArrayIteratorPrototype)); 159 assertHasOwnProperty(ArrayIteratorPrototype, 'next', DONT_ENUM); 160 assertHasOwnProperty(ArrayIteratorPrototype, Symbol.iterator, DONT_ENUM); 161} 162TestArrayIteratorPrototype(); 163 164 165function TestForArrayValues() { 166 var buffer = []; 167 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; 168 var i = 0; 169 for (var value of array[Symbol.iterator]()) { 170 buffer[i++] = value; 171 } 172 173 assertEquals(8, buffer.length); 174 175 for (var i = 0; i < buffer.length; i++) { 176 assertSame(array[i], buffer[i]); 177 } 178} 179TestForArrayValues(); 180 181 182function TestForArrayKeys() { 183 var buffer = []; 184 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; 185 var i = 0; 186 for (var key of array.keys()) { 187 buffer[i++] = key; 188 } 189 190 assertEquals(8, buffer.length); 191 192 for (var i = 0; i < buffer.length; i++) { 193 assertEquals(i, buffer[i]); 194 } 195} 196TestForArrayKeys(); 197 198 199function TestForArrayEntries() { 200 var buffer = []; 201 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; 202 var i = 0; 203 for (var entry of array.entries()) { 204 buffer[i++] = entry; 205 } 206 207 assertEquals(8, buffer.length); 208 209 for (var i = 0; i < buffer.length; i++) { 210 assertSame(array[i], buffer[i][1]); 211 } 212 213 for (var i = 0; i < buffer.length; i++) { 214 assertEquals(i, buffer[i][0]); 215 } 216} 217TestForArrayEntries(); 218 219 220function TestForArray() { 221 var buffer = []; 222 var array = [0, 'a', true, false, null, /* hole */, undefined, NaN]; 223 var i = 0; 224 for (var value of array) { 225 buffer[i++] = value; 226 } 227 228 assertEquals(8, buffer.length); 229 230 for (var i = 0; i < buffer.length; i++) { 231 assertSame(array[i], buffer[i]); 232 } 233} 234TestForArrayValues(); 235 236 237function TestNonOwnSlots() { 238 var array = [0]; 239 var iterator = array[Symbol.iterator](); 240 var object = {__proto__: iterator}; 241 242 assertThrows(function() { 243 object.next(); 244 }, TypeError); 245} 246TestNonOwnSlots(); 247