Home | History | Annotate | Download | only in harmony
      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: --harmony-generators
     29 
     30 // Test aspects of the generator runtime.
     31 
     32 // FIXME(wingo): Replace this reference with a more official link.
     33 // See:
     34 // http://wiki.ecmascript.org/lib/exe/fetch.php?cache=cache&media=harmony:es6_generator_object_model_3-29-13.png
     35 
     36 function f() { }
     37 function* g() { yield 1; }
     38 var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
     39 var GeneratorFunction = GeneratorFunctionPrototype.constructor;
     40 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
     41 
     42 // A generator function should have the same set of properties as any
     43 // other function.
     44 function TestGeneratorFunctionInstance() {
     45   var f_own_property_names = Object.getOwnPropertyNames(f);
     46   var g_own_property_names = Object.getOwnPropertyNames(g);
     47 
     48   f_own_property_names.sort();
     49   g_own_property_names.sort();
     50 
     51   assertArrayEquals(f_own_property_names, g_own_property_names);
     52   var i;
     53   for (i = 0; i < f_own_property_names.length; i++) {
     54     var prop = f_own_property_names[i];
     55     var f_desc = Object.getOwnPropertyDescriptor(f, prop);
     56     var g_desc = Object.getOwnPropertyDescriptor(g, prop);
     57     assertEquals(f_desc.configurable, g_desc.configurable, prop);
     58     assertEquals(f_desc.writable, g_desc.writable, prop);
     59     assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
     60   }
     61 }
     62 TestGeneratorFunctionInstance();
     63 
     64 
     65 // Generators have an additional object interposed in the chain between
     66 // themselves and Function.prototype.
     67 function TestGeneratorFunctionPrototype() {
     68   // Sanity check.
     69   assertSame(Object.getPrototypeOf(f), Function.prototype);
     70   assertFalse(GeneratorFunctionPrototype === Function.prototype);
     71   assertSame(Function.prototype,
     72              Object.getPrototypeOf(GeneratorFunctionPrototype));
     73   assertSame(GeneratorFunctionPrototype,
     74              Object.getPrototypeOf(function* () {}));
     75 }
     76 TestGeneratorFunctionPrototype();
     77 
     78 
     79 // Functions that we associate with generator objects are actually defined by
     80 // a common prototype.
     81 function TestGeneratorObjectPrototype() {
     82   assertSame(Object.prototype,
     83              Object.getPrototypeOf(GeneratorObjectPrototype));
     84   assertSame(GeneratorObjectPrototype,
     85              Object.getPrototypeOf((function*(){yield 1}).prototype));
     86 
     87   var expected_property_names = ["next", "throw", "constructor"];
     88   var found_property_names =
     89       Object.getOwnPropertyNames(GeneratorObjectPrototype);
     90 
     91   expected_property_names.sort();
     92   found_property_names.sort();
     93 
     94   assertArrayEquals(expected_property_names, found_property_names);
     95 }
     96 TestGeneratorObjectPrototype();
     97 
     98 
     99 // This tests the object that would be called "GeneratorFunction", if it were
    100 // like "Function".
    101 function TestGeneratorFunction() {
    102   assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
    103   assertTrue(g instanceof GeneratorFunction);
    104 
    105   assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
    106   assertTrue(g instanceof Function);
    107 
    108   assertEquals("function* g() { yield 1; }", g.toString());
    109 
    110   // Not all functions are generators.
    111   assertTrue(f instanceof Function);  // Sanity check.
    112   assertTrue(!(f instanceof GeneratorFunction));
    113 
    114   assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
    115   assertTrue(GeneratorFunction() instanceof GeneratorFunction);
    116 }
    117 TestGeneratorFunction();
    118 
    119 
    120 function TestPerGeneratorPrototype() {
    121   assertTrue((function*(){}).prototype !== (function*(){}).prototype);
    122   assertTrue((function*(){}).prototype !== g.prototype);
    123   assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
    124   assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
    125   assertTrue(!(g.prototype instanceof Function));
    126   assertSame(typeof (g.prototype), "object");
    127 
    128   assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
    129 }
    130 TestPerGeneratorPrototype();
    131