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 basic generator syntax. | |
31 | |
32 // Yield statements. | |
33 function* g() { yield 3; yield 4; } | |
34 | |
35 // Yield expressions. | |
36 function* g() { (yield 3) + (yield 4); } | |
37 | |
38 // Yield without a RHS. | |
39 function* g() { yield; } | |
40 function* g() { yield } | |
41 function* g() { | |
42 yield | |
43 } | |
44 function* g() { (yield) } | |
45 function* g() { [yield] } | |
46 function* g() { {yield} } | |
47 function* g() { yield, yield } | |
48 function* g() { yield; yield } | |
49 function* g() { (yield) ? yield : yield } | |
50 function* g() { | |
51 (yield) | |
52 ? yield | |
53 : yield | |
54 } | |
55 | |
56 // If yield has a RHS, it needs to start on the same line. The * in a | |
57 // yield* counts as starting the RHS. | |
58 function* g() { | |
59 yield * | |
60 foo | |
61 } | |
62 assertThrows("function* g() { yield\n* foo }", SyntaxError); | |
63 assertEquals(undefined, | |
64 (function*(){ | |
65 yield | |
66 3 | |
67 })().next().value); | |
68 | |
69 // A YieldExpression is not a LogicalORExpression. | |
70 assertThrows("function* g() { yield ? yield : yield }", SyntaxError); | |
71 | |
72 // You can have a generator in strict mode. | |
73 function* g() { "use strict"; yield 3; yield 4; } | |
74 | |
75 // Generators can have return statements also, which internally parse to a kind | |
76 // of yield expression. | |
77 function* g() { yield 1; return; } | |
78 function* g() { yield 1; return 2; } | |
79 function* g() { yield 1; return 2; yield "dead"; } | |
80 | |
81 // Generator expression. | |
82 (function* () { yield 3; }); | |
83 | |
84 // Named generator expression. | |
85 (function* g() { yield 3; }); | |
86 | |
87 // You can have a generator without a yield. | |
88 function* g() { } | |
89 | |
90 // A YieldExpression is valid as the RHS of a YieldExpression. | |
91 function* g() { yield yield 1; } | |
92 function* g() { yield 3 + (yield 4); } | |
93 | |
94 // Generator definitions with a name of "yield" are not specifically ruled out | |
95 // by the spec, as the `yield' name is outside the generator itself. However, | |
96 // in strict-mode, "yield" is an invalid identifier. | |
97 function* yield() { (yield 3) + (yield 4); } | |
98 assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }", | |
99 SyntaxError); | |
100 | |
101 // In sloppy mode, yield is a normal identifier, outside of generators. | |
102 function yield(yield) { yield: yield (yield + yield (0)); } | |
103 | |
104 // Yield is always valid as a key in an object literal. | |
105 ({ yield: 1 }); | |
106 function* g() { yield ({ yield: 1 }) } | |
107 function* g() { yield ({ get yield() { return 1; }}) } | |
108 | |
109 // Checks that yield is a valid label in sloppy mode, but not valid in a strict | |
110 // mode or in generators. | |
111 function f() { yield: 1 } | |
112 assertThrows("function f() { \"use strict\"; yield: 1 }", SyntaxError) | |
113 assertThrows("function* g() { yield: 1 }", SyntaxError) | |
114 | |
115 // Yield is only a keyword in the body of the generator, not in nested | |
116 // functions. | |
117 function* g() { function f() { yield (yield + yield (0)); } } | |
118 | |
119 // Yield in a generator is not an identifier. | |
120 assertThrows("function* g() { yield = 10; }", SyntaxError); | |
121 | |
122 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is | |
123 // invalid. | |
124 assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError); | |
125 | |
126 // Yield is still a future-reserved-word in strict mode | |
127 assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError); | |
128 | |
129 // The name of the NFE is let-bound in G, so is invalid. | |
130 assertThrows("function* g() { yield (function yield() {}); }", SyntaxError); | |
131 | |
132 // In generators, yield is invalid as a formal argument name. | |
133 assertThrows("function* g(yield) { yield (10); }", SyntaxError); | |
OLD | NEW |