blob: b4e8f950e1358898d31d16beddd6e36da9228a30 [file] [log] [blame]
mstarzinger@chromium.orge27d6172013-04-17 11:51: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// 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
36function f() { }
37function* g() { yield 1; }
38var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
39var GeneratorFunction = GeneratorFunctionPrototype.constructor;
40var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
41
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);
58 assertEquals(f_desc.writable, g_desc.writable, prop);
59 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* () {}));
75}
76TestGeneratorFunctionPrototype();
77
78
79// Functions that we associate with generator objects are actually defined by
80// a common prototype.
81function TestGeneratorObjectPrototype() {
82 assertSame(Object.prototype,
83 Object.getPrototypeOf(GeneratorObjectPrototype));
84 assertSame(GeneratorObjectPrototype,
85 Object.getPrototypeOf((function*(){yield 1}).prototype));
86
danno@chromium.orgca29dd82013-04-26 11:59:48 +000087 var expected_property_names = ["next", "send", "throw", "constructor"];
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000088 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}
96TestGeneratorObjectPrototype();
97
98
99// This tests the object that would be called "GeneratorFunction", if it were
100// like "Function".
101function TestGeneratorFunction() {
102 assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
103 assertTrue(g instanceof GeneratorFunction);
104
105 assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
106 assertTrue(g instanceof Function);
107
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000108 assertEquals("function* g() { yield 1; }", g.toString());
109
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000110 // Not all functions are generators.
111 assertTrue(f instanceof Function); // Sanity check.
112 assertTrue(!(f instanceof GeneratorFunction));
113}
114TestGeneratorFunction();
115
116
117function TestPerGeneratorPrototype() {
118 assertTrue((function*(){}).prototype !== (function*(){}).prototype);
119 assertTrue((function*(){}).prototype !== g.prototype);
120 assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
121 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
122 assertTrue(!(g.prototype instanceof Function));
123 assertSame(typeof (g.prototype), "object");
124
125 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
126}
127TestPerGeneratorPrototype();