Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: test/mjsunit/harmony/generators-runtime.js

Issue 479543003: Stage ES6 generators (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Minor fix: remove harmony -> generators implication (staging is sufficient) Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 // See:
33 // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-ob jects
34
35 function f() { }
36 function* g() { yield 1; }
37 var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
38 var GeneratorFunction = GeneratorFunctionPrototype.constructor;
39 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
40
41 // A generator function should have the same set of properties as any
42 // other function.
43 function TestGeneratorFunctionInstance() {
44 var f_own_property_names = Object.getOwnPropertyNames(f);
45 var g_own_property_names = Object.getOwnPropertyNames(g);
46
47 f_own_property_names.sort();
48 g_own_property_names.sort();
49
50 assertArrayEquals(f_own_property_names, g_own_property_names);
51 var i;
52 for (i = 0; i < f_own_property_names.length; i++) {
53 var prop = f_own_property_names[i];
54 var f_desc = Object.getOwnPropertyDescriptor(f, prop);
55 var g_desc = Object.getOwnPropertyDescriptor(g, prop);
56 assertEquals(f_desc.configurable, g_desc.configurable, prop);
57 if (prop === 'arguments' || prop === 'caller') {
58 // Unlike sloppy functions, which have read-only data arguments and caller
59 // properties, sloppy generators have a poison pill implemented via
60 // accessors
61 assertFalse('writable' in g_desc, prop);
62 assertTrue(g_desc.get instanceof Function, prop);
63 assertEquals(g_desc.get, g_desc.set, prop);
64 } else {
65 assertEquals(f_desc.writable, g_desc.writable, prop);
66 }
67 assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
68 }
69 }
70 TestGeneratorFunctionInstance();
71
72
73 // Generators have an additional object interposed in the chain between
74 // themselves and Function.prototype.
75 function TestGeneratorFunctionPrototype() {
76 // Sanity check.
77 assertSame(Object.getPrototypeOf(f), Function.prototype);
78 assertFalse(GeneratorFunctionPrototype === Function.prototype);
79 assertSame(Function.prototype,
80 Object.getPrototypeOf(GeneratorFunctionPrototype));
81 assertSame(GeneratorFunctionPrototype,
82 Object.getPrototypeOf(function* () {}));
83 }
84 TestGeneratorFunctionPrototype();
85
86
87 // Functions that we associate with generator objects are actually defined by
88 // a common prototype.
89 function TestGeneratorObjectPrototype() {
90 assertSame(Object.prototype,
91 Object.getPrototypeOf(GeneratorObjectPrototype));
92 assertSame(GeneratorObjectPrototype,
93 Object.getPrototypeOf((function*(){yield 1}).prototype));
94
95 var expected_property_names = ["next", "throw", "constructor"];
96 var found_property_names =
97 Object.getOwnPropertyNames(GeneratorObjectPrototype);
98
99 expected_property_names.sort();
100 found_property_names.sort();
101
102 assertArrayEquals(expected_property_names, found_property_names);
103
104 iterator_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
105 Symbol.iterator);
106 assertTrue(iterator_desc !== undefined);
107 assertFalse(iterator_desc.writable);
108 assertFalse(iterator_desc.enumerable);
109 assertFalse(iterator_desc.configurable);
110
111 // The generator object's "iterator" function is just the identity.
112 assertSame(iterator_desc.value.call(42), 42);
113 }
114 TestGeneratorObjectPrototype();
115
116
117 // This tests the object that would be called "GeneratorFunction", if it were
118 // like "Function".
119 function TestGeneratorFunction() {
120 assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
121 assertTrue(g instanceof GeneratorFunction);
122
123 assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
124 assertTrue(g instanceof Function);
125
126 assertEquals("function* g() { yield 1; }", g.toString());
127
128 // Not all functions are generators.
129 assertTrue(f instanceof Function); // Sanity check.
130 assertTrue(!(f instanceof GeneratorFunction));
131
132 assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
133 assertTrue(GeneratorFunction() instanceof GeneratorFunction);
134 }
135 TestGeneratorFunction();
136
137
138 function TestPerGeneratorPrototype() {
139 assertTrue((function*(){}).prototype !== (function*(){}).prototype);
140 assertTrue((function*(){}).prototype !== g.prototype);
141 assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
142 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
143 assertTrue(!(g.prototype instanceof Function));
144 assertSame(typeof (g.prototype), "object");
145
146 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
147 }
148 TestPerGeneratorPrototype();
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/generators-relocation.js ('k') | test/mjsunit/harmony/regress/regress-2681.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698