blob: 0cdbfca3a812963cb6555e2655c9de4a26221a12 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch61f157c2016-09-16 13:49:30 +010028// Flags: --ignition-generators
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030// Test aspects of the generator runtime.
31
32// See:
33// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects
34
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035function f() { "use strict"; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036function* g() { yield 1; }
37var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
38var GeneratorFunction = GeneratorFunctionPrototype.constructor;
39var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040var IteratorPrototype = Object.getPrototypeOf(GeneratorObjectPrototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041
42// A generator function should have the same set of properties as any
43// other function.
44function 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 assertEquals(f_desc.writable, g_desc.writable, prop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
60 }
61}
62TestGeneratorFunctionInstance();
63
64
65// Generators have an additional object interposed in the chain between
66// themselves and Function.prototype.
67function 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* () {}));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 assertSame("object", typeof GeneratorFunctionPrototype);
76
77 var constructor_desc = Object.getOwnPropertyDescriptor(
78 GeneratorFunctionPrototype, "constructor");
79 assertTrue(constructor_desc !== undefined);
80 assertSame(GeneratorFunction, constructor_desc.value);
81 assertFalse(constructor_desc.writable);
82 assertFalse(constructor_desc.enumerable);
83 assertTrue(constructor_desc.configurable);
84
85 var prototype_desc = Object.getOwnPropertyDescriptor(
86 GeneratorFunctionPrototype, "prototype");
87 assertTrue(prototype_desc !== undefined);
88 assertSame(GeneratorObjectPrototype, prototype_desc.value);
89 assertFalse(prototype_desc.writable);
90 assertFalse(prototype_desc.enumerable);
91 assertTrue(prototype_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092}
93TestGeneratorFunctionPrototype();
94
95
96// Functions that we associate with generator objects are actually defined by
97// a common prototype.
98function TestGeneratorObjectPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 assertSame(IteratorPrototype,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 Object.getPrototypeOf(GeneratorObjectPrototype));
101 assertSame(GeneratorObjectPrototype,
102 Object.getPrototypeOf((function*(){yield 1}).prototype));
103
Ben Murdoch097c5b22016-05-18 11:27:45 +0100104 var expected_property_names = ["next", "return", "throw", "constructor"];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 var found_property_names =
106 Object.getOwnPropertyNames(GeneratorObjectPrototype);
107
108 expected_property_names.sort();
109 found_property_names.sort();
110
111 assertArrayEquals(expected_property_names, found_property_names);
112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 var constructor_desc = Object.getOwnPropertyDescriptor(
114 GeneratorObjectPrototype, "constructor");
115 assertTrue(constructor_desc !== undefined);
116 assertFalse(constructor_desc.writable);
117 assertFalse(constructor_desc.enumerable);
118 assertTrue(constructor_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 var next_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
121 "next");
122 assertTrue(next_desc !== undefined);
123 assertTrue(next_desc.writable);
124 assertFalse(next_desc.enumerable);
125 assertTrue(next_desc.configurable);
126
127 var throw_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
128 "throw");
129 assertTrue(throw_desc !== undefined);
130 assertTrue(throw_desc.writable);
131 assertFalse(throw_desc.enumerable);
132 assertTrue(throw_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133}
134TestGeneratorObjectPrototype();
135
136
137// This tests the object that would be called "GeneratorFunction", if it were
138// like "Function".
139function TestGeneratorFunction() {
140 assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
141 assertTrue(g instanceof GeneratorFunction);
142
143 assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
144 assertTrue(g instanceof Function);
145
146 assertEquals("function* g() { yield 1; }", g.toString());
147
148 // Not all functions are generators.
149 assertTrue(f instanceof Function); // Sanity check.
150 assertTrue(!(f instanceof GeneratorFunction));
151
152 assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
153 assertTrue(GeneratorFunction() instanceof GeneratorFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154
155 // ES6 draft 04-14-15, section 25.2.2.2
156 var prototype_desc = Object.getOwnPropertyDescriptor(GeneratorFunction,
157 "prototype");
158 assertFalse(prototype_desc.writable);
159 assertFalse(prototype_desc.enumerable);
160 assertFalse(prototype_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161}
162TestGeneratorFunction();
163
164
165function TestPerGeneratorPrototype() {
166 assertTrue((function*(){}).prototype !== (function*(){}).prototype);
167 assertTrue((function*(){}).prototype !== g.prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
169 assertTrue(!(g.prototype instanceof Function));
170 assertSame(typeof (g.prototype), "object");
171
172 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
173}
174TestPerGeneratorPrototype();