blob: 143c3d75bce2e22e164dd81cab13cce58485ac02 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdoch61f157c2016-09-16 13:49:30 +010028// Flags: --ignition-generators
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030// Test basic generator syntax.
31
32// Yield statements.
33function* g() { yield 3; yield 4; }
34
35// Yield expressions.
36function* g() { (yield 3) + (yield 4); }
37
38// Yield without a RHS.
39function* g() { yield; }
40function* g() { yield }
41function* g() {
42 yield
43}
44function* g() { (yield) }
45function* g() { [yield] }
46function* g() { {yield} }
47function* g() { yield, yield }
48function* g() { yield; yield }
49function* g() { (yield) ? yield : yield }
50function* 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.
58function* g() {
59 yield *
60 foo
61}
62assertThrows("function* g() { yield\n* foo }", SyntaxError);
63assertEquals(undefined,
64 (function*(){
65 yield
66 3
67 })().next().value);
68
69// A YieldExpression is not a LogicalORExpression.
70assertThrows("function* g() { yield ? yield : yield }", SyntaxError);
71
72// You can have a generator in strict mode.
73function* 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.
77function* g() { yield 1; return; }
78function* g() { yield 1; return 2; }
79function* 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.
88function* g() { }
89
90// A YieldExpression is valid as the RHS of a YieldExpression.
91function* g() { yield yield 1; }
92function* 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.
97function* yield() { (yield 3) + (yield 4); }
98assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }",
99 SyntaxError);
100
101// In sloppy mode, yield is a normal identifier, outside of generators.
102function yield(yield) { yield: yield (yield + yield (0)); }
103
104// Yield is always valid as a key in an object literal.
105({ yield: 1 });
106function* g() { yield ({ yield: 1 }) }
107function* 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.
111function f() { yield: 1 }
112assertThrows("function f() { \"use strict\"; yield: 1 }", SyntaxError)
113assertThrows("function* g() { yield: 1 }", SyntaxError)
114
115// Yield is only a keyword in the body of the generator, not in nested
116// functions.
117function* g() { function f() { yield (yield + yield (0)); } }
118
119// Yield in a generator is not an identifier.
120assertThrows("function* g() { yield = 10; }", SyntaxError);
121
122// Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is
123// invalid.
124assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError);
125
126// Yield is still a future-reserved-word in strict mode
127assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError);
128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129// The name of the NFE is bound in the generator expression, so is invalid.
130assertThrows("function f() { (function* yield() {}); }", SyntaxError);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131
132// In generators, yield is invalid as a formal argument name.
133assertThrows("function* g(yield) { yield (10); }", SyntaxError);