OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 18 matching lines...) Expand all Loading... | |
29 | 29 |
30 // See: | 30 // See: |
31 // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-ob jects | 31 // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-ob jects |
32 | 32 |
33 function f() { } | 33 function f() { } |
34 function* g() { yield 1; } | 34 function* g() { yield 1; } |
35 var GeneratorFunctionPrototype = Object.getPrototypeOf(g); | 35 var GeneratorFunctionPrototype = Object.getPrototypeOf(g); |
36 var GeneratorFunction = GeneratorFunctionPrototype.constructor; | 36 var GeneratorFunction = GeneratorFunctionPrototype.constructor; |
37 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; | 37 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; |
38 | 38 |
39 | |
40 function removePoisoned(a) { | |
41 return a.filter(function(k) { | |
42 return k !== "arguments" && k !== "caller"; | |
43 }); | |
44 } | |
45 | |
46 | |
39 // A generator function should have the same set of properties as any | 47 // A generator function should have the same set of properties as any |
40 // other function. | 48 // other function. |
41 function TestGeneratorFunctionInstance() { | 49 function TestGeneratorFunctionInstance() { |
42 var f_own_property_names = Object.getOwnPropertyNames(f); | 50 var f_own_property_names = removePoisoned(Object.getOwnPropertyNames(f)); |
arv (Not doing code reviews)
2015/03/25 13:36:47
Would this test be cleaner if f was strict?
caitp (gmail)
2015/03/25 13:58:09
a strict ordinary function still has the propertie
| |
43 var g_own_property_names = Object.getOwnPropertyNames(g); | 51 var g_own_property_names = Object.getOwnPropertyNames(g); |
44 | 52 |
45 f_own_property_names.sort(); | 53 f_own_property_names.sort(); |
46 g_own_property_names.sort(); | 54 g_own_property_names.sort(); |
47 | 55 |
48 assertArrayEquals(f_own_property_names, g_own_property_names); | 56 assertArrayEquals(f_own_property_names, g_own_property_names); |
49 var i; | 57 var i; |
50 for (i = 0; i < f_own_property_names.length; i++) { | 58 for (i = 0; i < f_own_property_names.length; i++) { |
51 var prop = f_own_property_names[i]; | 59 var prop = f_own_property_names[i]; |
52 var f_desc = Object.getOwnPropertyDescriptor(f, prop); | 60 var f_desc = Object.getOwnPropertyDescriptor(f, prop); |
53 var g_desc = Object.getOwnPropertyDescriptor(g, prop); | 61 var g_desc = Object.getOwnPropertyDescriptor(g, prop); |
54 assertEquals(f_desc.configurable, g_desc.configurable, prop); | 62 assertEquals(f_desc.configurable, g_desc.configurable, prop); |
55 if (prop === 'arguments' || prop === 'caller') { | 63 assertEquals(f_desc.writable, g_desc.writable, prop); |
56 // Unlike sloppy functions, which have read-only data arguments and caller | |
57 // properties, sloppy generators have a poison pill implemented via | |
58 // accessors | |
59 assertFalse('writable' in g_desc, prop); | |
60 assertTrue(g_desc.get instanceof Function, prop); | |
61 assertEquals(g_desc.get, g_desc.set, prop); | |
62 } else { | |
63 assertEquals(f_desc.writable, g_desc.writable, prop); | |
64 } | |
65 assertEquals(f_desc.enumerable, g_desc.enumerable, prop); | 64 assertEquals(f_desc.enumerable, g_desc.enumerable, prop); |
66 } | 65 } |
67 } | 66 } |
68 TestGeneratorFunctionInstance(); | 67 TestGeneratorFunctionInstance(); |
69 | 68 |
70 | 69 |
71 // Generators have an additional object interposed in the chain between | 70 // Generators have an additional object interposed in the chain between |
72 // themselves and Function.prototype. | 71 // themselves and Function.prototype. |
73 function TestGeneratorFunctionPrototype() { | 72 function TestGeneratorFunctionPrototype() { |
74 // Sanity check. | 73 // Sanity check. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 assertTrue((function*(){}).prototype !== (function*(){}).prototype); | 136 assertTrue((function*(){}).prototype !== (function*(){}).prototype); |
138 assertTrue((function*(){}).prototype !== g.prototype); | 137 assertTrue((function*(){}).prototype !== g.prototype); |
139 assertTrue(g.prototype instanceof GeneratorFunctionPrototype); | 138 assertTrue(g.prototype instanceof GeneratorFunctionPrototype); |
140 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype)); | 139 assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype)); |
141 assertTrue(!(g.prototype instanceof Function)); | 140 assertTrue(!(g.prototype instanceof Function)); |
142 assertSame(typeof (g.prototype), "object"); | 141 assertSame(typeof (g.prototype), "object"); |
143 | 142 |
144 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype)); | 143 assertArrayEquals([], Object.getOwnPropertyNames(g.prototype)); |
145 } | 144 } |
146 TestPerGeneratorPrototype(); | 145 TestPerGeneratorPrototype(); |
OLD | NEW |