Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 28 | // Flags: --ignition-generators |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | // Test aspects of the generator runtime. |
| 31 | |
| 32 | // See: |
| 33 | // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects |
| 34 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | function f() { "use strict"; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | function* g() { yield 1; } |
| 37 | var GeneratorFunctionPrototype = Object.getPrototypeOf(g); |
| 38 | var GeneratorFunction = GeneratorFunctionPrototype.constructor; |
| 39 | var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | var IteratorPrototype = Object.getPrototypeOf(GeneratorObjectPrototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | assertEquals(f_desc.writable, g_desc.writable, prop); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | assertEquals(f_desc.enumerable, g_desc.enumerable, prop); |
| 60 | } |
| 61 | } |
| 62 | TestGeneratorFunctionInstance(); |
| 63 | |
| 64 | |
| 65 | // Generators have an additional object interposed in the chain between |
| 66 | // themselves and Function.prototype. |
| 67 | function 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 75 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | } |
| 93 | TestGeneratorFunctionPrototype(); |
| 94 | |
| 95 | |
| 96 | // Functions that we associate with generator objects are actually defined by |
| 97 | // a common prototype. |
| 98 | function TestGeneratorObjectPrototype() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | assertSame(IteratorPrototype, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 100 | Object.getPrototypeOf(GeneratorObjectPrototype)); |
| 101 | assertSame(GeneratorObjectPrototype, |
| 102 | Object.getPrototypeOf((function*(){yield 1}).prototype)); |
| 103 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 104 | var expected_property_names = ["next", "return", "throw", "constructor"]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 105 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | } |
| 134 | TestGeneratorObjectPrototype(); |
| 135 | |
| 136 | |
| 137 | // This tests the object that would be called "GeneratorFunction", if it were |
| 138 | // like "Function". |
| 139 | function 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 154 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | } |
| 162 | TestGeneratorFunction(); |
| 163 | |
| 164 | |
| 165 | function TestPerGeneratorPrototype() { |
| 166 | assertTrue((function*(){}).prototype !== (function*(){}).prototype); |
| 167 | assertTrue((function*(){}).prototype !== g.prototype); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | 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 | } |
| 174 | TestPerGeneratorPrototype(); |