Home | History | Annotate | Download | only in es6
      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: --allow-natives-syntax
      6 
      7 
      8 (function TestBasics() {
      9   var object = {
     10     method() {
     11       return 42;
     12     }
     13   };
     14   assertEquals(42, object.method());
     15 })();
     16 
     17 
     18 (function TestThis() {
     19   var object = {
     20     method() {
     21       assertEquals(object, this);
     22     }
     23   };
     24   object.method();
     25 })();
     26 
     27 
     28 (function TestDescriptor() {
     29   var object = {
     30     method() {
     31       return 42;
     32     }
     33   };
     34 
     35   var desc = Object.getOwnPropertyDescriptor(object, 'method');
     36   assertTrue(desc.enumerable);
     37   assertTrue(desc.configurable);
     38   assertTrue(desc.writable);
     39   assertEquals('function', typeof desc.value);
     40 
     41   assertEquals(42, desc.value());
     42 })();
     43 
     44 
     45 (function TestProto() {
     46   var object = {
     47     method() {}
     48   };
     49 
     50   assertEquals(Function.prototype, Object.getPrototypeOf(object.method));
     51 })();
     52 
     53 
     54 (function TestNotConstructable() {
     55   var object = {
     56     method() {}
     57   };
     58 
     59   assertThrows(function() {
     60     new object.method;
     61   });
     62 })();
     63 
     64 
     65 (function TestFunctionName() {
     66   var object = {
     67     method() {},
     68     1() {},
     69     2.0() {}
     70   };
     71   var f = object.method;
     72   assertEquals('method', f.name);
     73   var g = object[1];
     74   assertEquals('1', g.name);
     75   var h = object[2];
     76   assertEquals('2', h.name);
     77 })();
     78 
     79 
     80 (function TestNoBinding() {
     81   var method = 'local';
     82   var calls = 0;
     83   var object = {
     84     method() {
     85       calls++;
     86       assertEquals('local', method);
     87     }
     88   };
     89   object.method();
     90   assertEquals(1, calls);
     91 })();
     92 
     93 
     94 (function TestNoPrototype() {
     95   var object = {
     96     method() {}
     97   };
     98   var f = object.method;
     99   assertFalse(f.hasOwnProperty('prototype'));
    100   assertEquals(undefined, f.prototype);
    101 
    102   f.prototype = 42;
    103   assertEquals(42, f.prototype);
    104 })();
    105 
    106 
    107 (function TestNoRestrictedPropertiesStrict() {
    108   var obj = {
    109     method() { "use strict"; }
    110   };
    111   assertFalse(obj.method.hasOwnProperty("arguments"));
    112   assertThrows(function() { return obj.method.arguments; }, TypeError);
    113   assertThrows(function() { obj.method.arguments = {}; }, TypeError);
    114 
    115   assertFalse(obj.method.hasOwnProperty("caller"));
    116   assertThrows(function() { return obj.method.caller; }, TypeError);
    117   assertThrows(function() { obj.method.caller = {}; }, TypeError);
    118 })();
    119 
    120 
    121 (function TestNoRestrictedPropertiesSloppy() {
    122   var obj = {
    123     method() {}
    124   };
    125   assertFalse(obj.method.hasOwnProperty("arguments"));
    126   assertThrows(function() { return obj.method.arguments; }, TypeError);
    127   assertThrows(function() { obj.method.arguments = {}; }, TypeError);
    128 
    129   assertFalse(obj.method.hasOwnProperty("caller"));
    130   assertThrows(function() { return obj.method.caller; }, TypeError);
    131   assertThrows(function() { obj.method.caller = {}; }, TypeError);
    132 })();
    133 
    134 
    135 (function TestToString() {
    136   var object = {
    137     method() { 42; }
    138   };
    139   assertEquals('method() { 42; }', object.method.toString());
    140 })();
    141 
    142 
    143 (function TestOptimized() {
    144   var object = {
    145     method() { return 42; }
    146   };
    147   assertEquals(42, object.method());
    148   assertEquals(42, object.method());
    149   %OptimizeFunctionOnNextCall(object.method);
    150   assertEquals(42, object.method());
    151   assertFalse(object.method.hasOwnProperty('prototype'));
    152 })();
    153 
    154 
    155 ///////////////////////////////////////////////////////////////////////////////
    156 
    157 
    158 var GeneratorFunction = function*() {}.__proto__.constructor;
    159 var GeneratorPrototype = Object.getPrototypeOf(function*() {}).prototype;
    160 
    161 
    162 function assertIteratorResult(value, done, result) {
    163   assertEquals({value: value, done: done}, result);
    164 }
    165 
    166 
    167 (function TestGeneratorBasics() {
    168   var object = {
    169     *method() {
    170       yield 1;
    171     }
    172   };
    173   var g = object.method();
    174   assertIteratorResult(1, false, g.next());
    175   assertIteratorResult(undefined, true, g.next());
    176 })();
    177 
    178 
    179 (function TestGeneratorThis() {
    180   var object = {
    181     *method() {
    182       yield this;
    183     }
    184   };
    185   var g = object.method();
    186   assertIteratorResult(object, false, g.next());
    187   assertIteratorResult(undefined, true, g.next());
    188 })();
    189 
    190 
    191 (function TestGeneratorSymbolIterator() {
    192   var object = {
    193     *method() {}
    194   };
    195   var g = object.method();
    196   assertEquals(g, g[Symbol.iterator]());
    197 })();
    198 
    199 
    200 (function TestGeneratorDescriptor() {
    201   var object = {
    202     *method() {
    203       yield 1;
    204     }
    205   };
    206 
    207   var desc = Object.getOwnPropertyDescriptor(object, 'method');
    208   assertTrue(desc.enumerable);
    209   assertTrue(desc.configurable);
    210   assertTrue(desc.writable);
    211   assertEquals('function', typeof desc.value);
    212 
    213   var g = desc.value();
    214   assertIteratorResult(1, false, g.next());
    215   assertIteratorResult(undefined, true, g.next());
    216 })();
    217 
    218 
    219 (function TestGeneratorPrototypeDescriptor() {
    220   var object = {
    221     *method() {}
    222   };
    223 
    224   var desc = Object.getOwnPropertyDescriptor(object.method, 'prototype');
    225   assertFalse(desc.enumerable);
    226   assertFalse(desc.configurable);
    227   assertTrue(desc.writable);
    228   assertEquals(GeneratorPrototype, Object.getPrototypeOf(desc.value));
    229 })();
    230 
    231 
    232 (function TestGeneratorProto() {
    233   var object = {
    234     *method() {}
    235   };
    236 
    237   assertEquals(GeneratorFunction.prototype,
    238                Object.getPrototypeOf(object.method));
    239 })();
    240 
    241 
    242 (function TestGeneratorConstructable() {
    243   var object = {
    244     *method() {
    245       yield 1;
    246     }
    247   };
    248 
    249   var g = new object.method();
    250   assertIteratorResult(1, false, g.next());
    251   assertIteratorResult(undefined, true, g.next());
    252 })();
    253 
    254 
    255 (function TestGeneratorName() {
    256   var object = {
    257     *method() {},
    258     *1() {},
    259     *2.0() {}
    260   };
    261   var f = object.method;
    262   assertEquals('method', f.name);
    263   var g = object[1];
    264   assertEquals('1', g.name);
    265   var h = object[2];
    266   assertEquals('2', h.name);
    267 })();
    268 
    269 
    270 (function TestGeneratorNoBinding() {
    271   var method = 'local';
    272   var calls = 0;
    273   var object = {
    274     *method() {
    275       calls++;
    276       assertEquals('local', method);
    277     }
    278   };
    279   var g = object.method();
    280   assertIteratorResult(undefined, true, g.next());
    281   assertEquals(1, calls);
    282 })();
    283 
    284 
    285 (function TestGeneratorToString() {
    286   var object = {
    287     *method() { yield 1; }
    288   };
    289   assertEquals('*method() { yield 1; }', object.method.toString());
    290 })();
    291 
    292 
    293 (function TestProtoName() {
    294   var object = {
    295     __proto__() {
    296       return 1;
    297     }
    298   };
    299   assertEquals(Object.prototype, Object.getPrototypeOf(object));
    300   assertEquals(1, object.__proto__());
    301 })();
    302 
    303 
    304 (function TestProtoName2() {
    305   var p = {};
    306   var object = {
    307     __proto__() {
    308       return 1;
    309     },
    310     __proto__: p
    311   };
    312   assertEquals(p, Object.getPrototypeOf(object));
    313   assertEquals(1, object.__proto__());
    314 })();
    315