Home | History | Annotate | Download | only in es6
      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 --harmony-tostring
     29 
     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 
     46 function TestArrayPrototype() {
     47   assertHasOwnProperty(Array.prototype, 'entries', DONT_ENUM);
     48   assertHasOwnProperty(Array.prototype, 'keys', DONT_ENUM);
     49   assertHasOwnProperty(Array.prototype, Symbol.iterator, DONT_ENUM);
     50   assertEquals('entries', Array.prototype.entries.name);
     51   assertEquals('keys', Array.prototype.keys.name);
     52   assertEquals('values', Array.prototype[Symbol.iterator].name);
     53 }
     54 TestArrayPrototype();
     55 
     56 
     57 function assertIteratorResult(value, done, result) {
     58   assertEquals({value: value, done: done}, result);
     59 }
     60 
     61 
     62 function TestValues() {
     63   var array = ['a', 'b', 'c'];
     64   var iterator = array[Symbol.iterator]();
     65   assertIteratorResult('a', false, iterator.next());
     66   assertIteratorResult('b', false, iterator.next());
     67   assertIteratorResult('c', false, iterator.next());
     68   assertIteratorResult(void 0, true, iterator.next());
     69 
     70   array.push('d');
     71   assertIteratorResult(void 0, true, iterator.next());
     72 }
     73 TestValues();
     74 
     75 
     76 function TestValuesMutate() {
     77   var array = ['a', 'b', 'c'];
     78   var iterator = array[Symbol.iterator]();
     79   assertIteratorResult('a', false, iterator.next());
     80   assertIteratorResult('b', false, iterator.next());
     81   assertIteratorResult('c', false, iterator.next());
     82   array.push('d');
     83   assertIteratorResult('d', false, iterator.next());
     84   assertIteratorResult(void 0, true, iterator.next());
     85 }
     86 TestValuesMutate();
     87 
     88 
     89 function TestKeys() {
     90   var array = ['a', 'b', 'c'];
     91   var iterator = array.keys();
     92   assertIteratorResult(0, false, iterator.next());
     93   assertIteratorResult(1, false, iterator.next());
     94   assertIteratorResult(2, false, iterator.next());
     95   assertIteratorResult(void 0, true, iterator.next());
     96 
     97   array.push('d');
     98   assertIteratorResult(void 0, true, iterator.next());
     99 }
    100 TestKeys();
    101 
    102 
    103 function TestKeysMutate() {
    104   var array = ['a', 'b', 'c'];
    105   var iterator = array.keys();
    106   assertIteratorResult(0, false, iterator.next());
    107   assertIteratorResult(1, false, iterator.next());
    108   assertIteratorResult(2, false, iterator.next());
    109   array.push('d');
    110   assertIteratorResult(3, false, iterator.next());
    111   assertIteratorResult(void 0, true, iterator.next());
    112 }
    113 TestKeysMutate();
    114 
    115 
    116 function TestEntries() {
    117   var array = ['a', 'b', 'c'];
    118   var iterator = array.entries();
    119   assertIteratorResult([0, 'a'], false, iterator.next());
    120   assertIteratorResult([1, 'b'], false, iterator.next());
    121   assertIteratorResult([2, 'c'], false, iterator.next());
    122   assertIteratorResult(void 0, true, iterator.next());
    123 
    124   array.push('d');
    125   assertIteratorResult(void 0, true, iterator.next());
    126 }
    127 TestEntries();
    128 
    129 
    130 function TestEntriesMutate() {
    131   var array = ['a', 'b', 'c'];
    132   var iterator = array.entries();
    133   assertIteratorResult([0, 'a'], false, iterator.next());
    134   assertIteratorResult([1, 'b'], false, iterator.next());
    135   assertIteratorResult([2, 'c'], false, iterator.next());
    136   array.push('d');
    137   assertIteratorResult([3, 'd'], false, iterator.next());
    138   assertIteratorResult(void 0, true, iterator.next());
    139 }
    140 TestEntriesMutate();
    141 
    142 
    143 function TestArrayIteratorPrototype() {
    144   var array = [];
    145   var iterator = array.keys();
    146 
    147   var ArrayIteratorPrototype = iterator.__proto__;
    148 
    149   assertEquals(ArrayIteratorPrototype, array[Symbol.iterator]().__proto__);
    150   assertEquals(ArrayIteratorPrototype, array.keys().__proto__);
    151   assertEquals(ArrayIteratorPrototype, array.entries().__proto__);
    152 
    153   assertEquals(Object.prototype, ArrayIteratorPrototype.__proto__);
    154 
    155   assertEquals('Array Iterator', %_ClassOf(array[Symbol.iterator]()));
    156   assertEquals('Array Iterator', %_ClassOf(array.keys()));
    157   assertEquals('Array Iterator', %_ClassOf(array.entries()));
    158 
    159   assertFalse(ArrayIteratorPrototype.hasOwnProperty('constructor'));
    160   assertArrayEquals(['next'],
    161       Object.getOwnPropertyNames(ArrayIteratorPrototype));
    162   assertHasOwnProperty(ArrayIteratorPrototype, 'next', DONT_ENUM);
    163   assertHasOwnProperty(ArrayIteratorPrototype, Symbol.iterator, DONT_ENUM);
    164 
    165   assertEquals("[object Array Iterator]",
    166       Object.prototype.toString.call(iterator));
    167   assertEquals("Array Iterator", ArrayIteratorPrototype[Symbol.toStringTag]);
    168   var desc = Object.getOwnPropertyDescriptor(
    169       ArrayIteratorPrototype, Symbol.toStringTag);
    170   assertTrue(desc.configurable);
    171   assertFalse(desc.writable);
    172   assertEquals("Array Iterator", desc.value);
    173 }
    174 TestArrayIteratorPrototype();
    175 
    176 
    177 function TestForArrayValues() {
    178   var buffer = [];
    179   var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
    180   var i = 0;
    181   for (var value of array[Symbol.iterator]()) {
    182     buffer[i++] = value;
    183   }
    184 
    185   assertEquals(8, buffer.length);
    186 
    187   for (var i = 0; i < buffer.length; i++) {
    188     assertSame(array[i], buffer[i]);
    189   }
    190 }
    191 TestForArrayValues();
    192 
    193 
    194 function TestForArrayKeys() {
    195   var buffer = [];
    196   var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
    197   var i = 0;
    198   for (var key of array.keys()) {
    199     buffer[i++] = key;
    200   }
    201 
    202   assertEquals(8, buffer.length);
    203 
    204   for (var i = 0; i < buffer.length; i++) {
    205     assertEquals(i, buffer[i]);
    206   }
    207 }
    208 TestForArrayKeys();
    209 
    210 
    211 function TestForArrayEntries() {
    212   var buffer = [];
    213   var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
    214   var i = 0;
    215   for (var entry of array.entries()) {
    216     buffer[i++] = entry;
    217   }
    218 
    219   assertEquals(8, buffer.length);
    220 
    221   for (var i = 0; i < buffer.length; i++) {
    222     assertSame(array[i], buffer[i][1]);
    223   }
    224 
    225   for (var i = 0; i < buffer.length; i++) {
    226     assertEquals(i, buffer[i][0]);
    227   }
    228 }
    229 TestForArrayEntries();
    230 
    231 
    232 function TestForArray() {
    233   var buffer = [];
    234   var array = [0, 'a', true, false, null, /* hole */, undefined, NaN];
    235   var i = 0;
    236   for (var value of array) {
    237     buffer[i++] = value;
    238   }
    239 
    240   assertEquals(8, buffer.length);
    241 
    242   for (var i = 0; i < buffer.length; i++) {
    243     assertSame(array[i], buffer[i]);
    244   }
    245 }
    246 TestForArrayValues();
    247 
    248 
    249 function TestNonOwnSlots() {
    250   var array = [0];
    251   var iterator = array[Symbol.iterator]();
    252   var object = {__proto__: iterator};
    253 
    254   assertThrows(function() {
    255     object.next();
    256   }, TypeError);
    257 }
    258 TestNonOwnSlots();
    259