OLD | NEW |
---|---|
(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 // 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_ge nerator_object_model_3-29-13.png | |
35 | |
36 function f() { } | |
37 function* g() { yield 1; } | |
38 var GeneratorFunctionPrototype = Object.getPrototypeOf(g); | |
39 var GeneratorFunction = GeneratorFunctionPrototype.constructor; | |
40 var GeneratorIteratorPrototype = GeneratorFunctionPrototype.prototype; | |
41 | |
42 // A generator function should have the same set of properties as any | |
43 // other function. | |
44 function 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 } | |
62 | |
63 | |
64 // Generators have an additional object interposed in the chain between | |
65 // themselves and Function.prototype. | |
66 function TestGeneratorFunctionPrototype() { | |
67 // Sanity check. | |
68 assertSame(Object.getPrototypeOf(f), Function.prototype); | |
69 assertFalse(GeneratorFunctionPrototype === Function.prototype); | |
70 assertSame(Function.prototype, | |
71 Object.getPrototypeOf(GeneratorFunctionPrototype)); | |
72 assertSame(GeneratorFunctionPrototype, | |
73 Object.getPrototypeOf(function* () {})); | |
74 } | |
75 | |
76 | |
77 // Functions that we associate with generator iterators are actually defined by | |
78 // a common prototype. | |
79 function TestGeneratorIteratorPrototype() { | |
80 assertSame(Object.prototype, | |
81 Object.getPrototypeOf(GeneratorIteratorPrototype)); | |
82 assertSame(GeneratorIteratorPrototype, | |
83 Object.getPrototypeOf((function*(){yield 1}).prototype)); | |
84 | |
85 var expected_property_names = ["next", "send", "throw", "close"]; | |
86 var found_property_names = | |
87 Object.getOwnPropertyNames(GeneratorIteratorPrototype); | |
88 | |
89 expected_property_names.sort(); | |
90 found_property_names.sort(); | |
91 | |
92 assertArrayEquals(expected_property_names, found_property_names); | |
93 } | |
94 | |
95 | |
96 // This tests the object that would be called "GeneratorFunction", if it were | |
97 // like "Function". | |
98 function TestGeneratorFunction() { | |
99 assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype); | |
100 assertTrue(g instanceof GeneratorFunction); | |
101 | |
102 assertSame(Function, Object.getPrototypeOf(GeneratorFunction)); | |
103 assertTrue(g instanceof Function); | |
104 | |
105 // Not all functions are generators. | |
106 assertTrue(f instanceof Function); // Sanity check. | |
107 assertTrue(!(f instanceof GeneratorFunction)); | |
108 } | |
109 | |
110 | |
111 function TestPerGeneratorPrototype() { | |
112 assertTrue((function*(){}).prototype !== (function*(){}).prototype); | |
113 assertTrue((function*(){}).prototype !== g.prototype); | |
114 assertTrue(g.prototype instanceof GeneratorFunctionPrototype); | |
115 assertSame(GeneratorIteratorPrototype, Object.getPrototypeOf(g.prototype)); | |
116 assertTrue(!(g.prototype instanceof Function)); | |
117 assertSame(typeof (g.prototype), "object"); | |
118 | |
119 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype)); | |
120 } | |
121 | |
122 | |
123 TestGeneratorFunctionInstance(); | |
rossberg
2013/04/09 16:44:14
Nit: separating the calls from the definitions mak
| |
124 TestGeneratorFunctionPrototype(); | |
125 TestGeneratorIteratorPrototype(); | |
126 TestGeneratorFunction(); | |
127 TestPerGeneratorPrototype(); | |
OLD | NEW |