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// Test aspects of the generator runtime.
29
30// See:
31// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects
32
33function f() { }
34function* g() { yield 1; }
35var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
36var GeneratorFunction = GeneratorFunctionPrototype.constructor;
37var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
38
39// A generator function should have the same set of properties as any
40// other function.
41function TestGeneratorFunctionInstance() {
42  var f_own_property_names = Object.getOwnPropertyNames(f);
43  var g_own_property_names = Object.getOwnPropertyNames(g);
44
45  f_own_property_names.sort();
46  g_own_property_names.sort();
47
48  assertArrayEquals(f_own_property_names, g_own_property_names);
49  var i;
50  for (i = 0; i < f_own_property_names.length; i++) {
51    var prop = f_own_property_names[i];
52    var f_desc = Object.getOwnPropertyDescriptor(f, prop);
53    var g_desc = Object.getOwnPropertyDescriptor(g, prop);
54    assertEquals(f_desc.configurable, g_desc.configurable, prop);
55    if (prop === 'arguments' || prop === 'caller') {
56      // Unlike sloppy functions, which have read-only data arguments and caller
57      // properties, sloppy generators have a poison pill implemented via
58      // accessors
59      assertFalse('writable' in g_desc, prop);
60      assertTrue(g_desc.get instanceof Function, prop);
61      assertEquals(g_desc.get, g_desc.set, prop);
62    } else {
63      assertEquals(f_desc.writable, g_desc.writable, prop);
64    }
65    assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
66  }
67}
68TestGeneratorFunctionInstance();
69
70
71// Generators have an additional object interposed in the chain between
72// themselves and Function.prototype.
73function TestGeneratorFunctionPrototype() {
74  // Sanity check.
75  assertSame(Object.getPrototypeOf(f), Function.prototype);
76  assertFalse(GeneratorFunctionPrototype === Function.prototype);
77  assertSame(Function.prototype,
78             Object.getPrototypeOf(GeneratorFunctionPrototype));
79  assertSame(GeneratorFunctionPrototype,
80             Object.getPrototypeOf(function* () {}));
81}
82TestGeneratorFunctionPrototype();
83
84
85// Functions that we associate with generator objects are actually defined by
86// a common prototype.
87function TestGeneratorObjectPrototype() {
88  assertSame(Object.prototype,
89             Object.getPrototypeOf(GeneratorObjectPrototype));
90  assertSame(GeneratorObjectPrototype,
91             Object.getPrototypeOf((function*(){yield 1}).prototype));
92
93  var expected_property_names = ["next", "throw", "constructor"];
94  var found_property_names =
95      Object.getOwnPropertyNames(GeneratorObjectPrototype);
96
97  expected_property_names.sort();
98  found_property_names.sort();
99
100  assertArrayEquals(expected_property_names, found_property_names);
101
102  iterator_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
103      Symbol.iterator);
104  assertTrue(iterator_desc !== undefined);
105  assertFalse(iterator_desc.writable);
106  assertFalse(iterator_desc.enumerable);
107  assertFalse(iterator_desc.configurable);
108
109  // The generator object's "iterator" function is just the identity.
110  assertSame(iterator_desc.value.call(42), 42);
111}
112TestGeneratorObjectPrototype();
113
114
115// This tests the object that would be called "GeneratorFunction", if it were
116// like "Function".
117function TestGeneratorFunction() {
118  assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
119  assertTrue(g instanceof GeneratorFunction);
120
121  assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
122  assertTrue(g instanceof Function);
123
124  assertEquals("function* g() { yield 1; }", g.toString());
125
126  // Not all functions are generators.
127  assertTrue(f instanceof Function);  // Sanity check.
128  assertTrue(!(f instanceof GeneratorFunction));
129
130  assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
131  assertTrue(GeneratorFunction() instanceof GeneratorFunction);
132}
133TestGeneratorFunction();
134
135
136function TestPerGeneratorPrototype() {
137  assertTrue((function*(){}).prototype !== (function*(){}).prototype);
138  assertTrue((function*(){}).prototype !== g.prototype);
139  assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
140  assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
141  assertTrue(!(g.prototype instanceof Function));
142  assertSame(typeof (g.prototype), "object");
143
144  assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
145}
146TestPerGeneratorPrototype();
147