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