blob: 98015b7f7c811591d72fd6b0ae35a7d0300ead00 [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
28// Test aspects of the generator runtime.
29
30// See:
31// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects
32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033function f() { "use strict"; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034function* g() { yield 1; }
35var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
36var GeneratorFunction = GeneratorFunctionPrototype.constructor;
37var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038var IteratorPrototype = Object.getPrototypeOf(GeneratorObjectPrototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039
40// A generator function should have the same set of properties as any
41// other function.
42function TestGeneratorFunctionInstance() {
43 var f_own_property_names = Object.getOwnPropertyNames(f);
44 var g_own_property_names = Object.getOwnPropertyNames(g);
45
46 f_own_property_names.sort();
47 g_own_property_names.sort();
48
49 assertArrayEquals(f_own_property_names, g_own_property_names);
50 var i;
51 for (i = 0; i < f_own_property_names.length; i++) {
52 var prop = f_own_property_names[i];
53 var f_desc = Object.getOwnPropertyDescriptor(f, prop);
54 var g_desc = Object.getOwnPropertyDescriptor(g, prop);
55 assertEquals(f_desc.configurable, g_desc.configurable, prop);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 assertEquals(f_desc.writable, g_desc.writable, prop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
58 }
59}
60TestGeneratorFunctionInstance();
61
62
63// Generators have an additional object interposed in the chain between
64// themselves and Function.prototype.
65function TestGeneratorFunctionPrototype() {
66 // Sanity check.
67 assertSame(Object.getPrototypeOf(f), Function.prototype);
68 assertFalse(GeneratorFunctionPrototype === Function.prototype);
69 assertSame(Function.prototype,
70 Object.getPrototypeOf(GeneratorFunctionPrototype));
71 assertSame(GeneratorFunctionPrototype,
72 Object.getPrototypeOf(function* () {}));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 assertSame("object", typeof GeneratorFunctionPrototype);
74
75 var constructor_desc = Object.getOwnPropertyDescriptor(
76 GeneratorFunctionPrototype, "constructor");
77 assertTrue(constructor_desc !== undefined);
78 assertSame(GeneratorFunction, constructor_desc.value);
79 assertFalse(constructor_desc.writable);
80 assertFalse(constructor_desc.enumerable);
81 assertTrue(constructor_desc.configurable);
82
83 var prototype_desc = Object.getOwnPropertyDescriptor(
84 GeneratorFunctionPrototype, "prototype");
85 assertTrue(prototype_desc !== undefined);
86 assertSame(GeneratorObjectPrototype, prototype_desc.value);
87 assertFalse(prototype_desc.writable);
88 assertFalse(prototype_desc.enumerable);
89 assertTrue(prototype_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090}
91TestGeneratorFunctionPrototype();
92
93
94// Functions that we associate with generator objects are actually defined by
95// a common prototype.
96function TestGeneratorObjectPrototype() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 assertSame(IteratorPrototype,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 Object.getPrototypeOf(GeneratorObjectPrototype));
99 assertSame(GeneratorObjectPrototype,
100 Object.getPrototypeOf((function*(){yield 1}).prototype));
101
102 var expected_property_names = ["next", "throw", "constructor"];
103 var found_property_names =
104 Object.getOwnPropertyNames(GeneratorObjectPrototype);
105
106 expected_property_names.sort();
107 found_property_names.sort();
108
109 assertArrayEquals(expected_property_names, found_property_names);
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 var constructor_desc = Object.getOwnPropertyDescriptor(
112 GeneratorObjectPrototype, "constructor");
113 assertTrue(constructor_desc !== undefined);
114 assertFalse(constructor_desc.writable);
115 assertFalse(constructor_desc.enumerable);
116 assertTrue(constructor_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 var next_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
119 "next");
120 assertTrue(next_desc !== undefined);
121 assertTrue(next_desc.writable);
122 assertFalse(next_desc.enumerable);
123 assertTrue(next_desc.configurable);
124
125 var throw_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
126 "throw");
127 assertTrue(throw_desc !== undefined);
128 assertTrue(throw_desc.writable);
129 assertFalse(throw_desc.enumerable);
130 assertTrue(throw_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131}
132TestGeneratorObjectPrototype();
133
134
135// This tests the object that would be called "GeneratorFunction", if it were
136// like "Function".
137function TestGeneratorFunction() {
138 assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
139 assertTrue(g instanceof GeneratorFunction);
140
141 assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
142 assertTrue(g instanceof Function);
143
144 assertEquals("function* g() { yield 1; }", g.toString());
145
146 // Not all functions are generators.
147 assertTrue(f instanceof Function); // Sanity check.
148 assertTrue(!(f instanceof GeneratorFunction));
149
150 assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
151 assertTrue(GeneratorFunction() instanceof GeneratorFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152
153 // ES6 draft 04-14-15, section 25.2.2.2
154 var prototype_desc = Object.getOwnPropertyDescriptor(GeneratorFunction,
155 "prototype");
156 assertFalse(prototype_desc.writable);
157 assertFalse(prototype_desc.enumerable);
158 assertFalse(prototype_desc.configurable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159}
160TestGeneratorFunction();
161
162
163function TestPerGeneratorPrototype() {
164 assertTrue((function*(){}).prototype !== (function*(){}).prototype);
165 assertTrue((function*(){}).prototype !== g.prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
167 assertTrue(!(g.prototype instanceof Function));
168 assertSame(typeof (g.prototype), "object");
169
170 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
171}
172TestPerGeneratorPrototype();