|
1 // This file was written by Andy Wingo <wingo@igalia.com> and originally |
|
2 // contributed to V8 as generators-parsing.js, available here: |
|
3 // |
|
4 // http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-parsing.js |
|
5 |
|
6 function assertSyntaxError(str) { |
|
7 var msg; |
|
8 var evil = eval; |
|
9 try { |
|
10 // Non-direct eval. |
|
11 evil(str); |
|
12 } catch (exc) { |
|
13 if (exc instanceof SyntaxError) |
|
14 return; |
|
15 msg = "Assertion failed: expected SyntaxError, got " + exc; |
|
16 } |
|
17 if (msg === undefined) |
|
18 msg = "Assertion failed: expected SyntaxError, but no exception thrown"; |
|
19 throw new Error(msg + " - " + str); |
|
20 } |
|
21 |
|
22 // Yield statements. |
|
23 function* g() { yield 3; yield 4; } |
|
24 |
|
25 // Yield expressions. |
|
26 function* g() { (yield 3) + (yield 4); } |
|
27 |
|
28 // You can have a generator in strict mode. |
|
29 function* g() { "use strict"; yield 3; yield 4; } |
|
30 |
|
31 // Generators can have return statements also, which internally parse to a kind |
|
32 // of yield expression. |
|
33 function* g() { yield 1; return; } |
|
34 function* g() { yield 1; return 2; } |
|
35 function* g() { yield 1; return 2; yield "dead"; } |
|
36 |
|
37 // Generator expression. |
|
38 (function* () { yield 3; }); |
|
39 |
|
40 // Named generator expression. |
|
41 (function* g() { yield 3; }); |
|
42 |
|
43 // Generators do not have to contain yield expressions. |
|
44 function* g() { } |
|
45 |
|
46 // YieldExpressions can occur in the RHS of a YieldExpression. |
|
47 function* g() { yield yield 1; } |
|
48 function* g() { yield 3 + (yield 4); } |
|
49 |
|
50 // Generator definitions with a name of "yield" are not specifically ruled out |
|
51 // by the spec, as the `yield' name is outside the generator itself. However, |
|
52 // in strict-mode, "yield" is an invalid identifier. |
|
53 function* yield() { (yield 3) + (yield 4); } |
|
54 assertSyntaxError("function* yield() { 'use strict'; (yield 3) + (yield 4); }"); |
|
55 |
|
56 // In classic mode, yield is a normal identifier, outside of generators. |
|
57 function yield(yield) { yield: yield (yield + yield (0)); } |
|
58 |
|
59 // Yield is always valid as a key in an object literal. |
|
60 ({ yield: 1 }); |
|
61 function* g() { yield ({ yield: 1 }) } |
|
62 function* g() { yield ({ get yield() { return 1; }}) } |
|
63 |
|
64 // Yield is a valid property name. |
|
65 function* g(obj) { yield obj.yield; } |
|
66 |
|
67 // Checks that yield is a valid label in classic mode, but not valid in a strict |
|
68 // mode or in generators. |
|
69 function f() { yield: 1 } |
|
70 assertSyntaxError("function f() { 'use strict'; yield: 1 }") |
|
71 assertSyntaxError("function* g() { yield: 1 }") |
|
72 |
|
73 // Yield is only a keyword in the body of the generator, not in nested |
|
74 // functions. |
|
75 function* g() { function f(yield) { yield (yield + yield (0)); } } |
|
76 |
|
77 // Yield needs a RHS. |
|
78 assertSyntaxError("function* g() { yield; }"); |
|
79 |
|
80 // Yield in a generator is not an identifier. |
|
81 assertSyntaxError("function* g() { yield = 10; }"); |
|
82 |
|
83 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is |
|
84 // invalid. |
|
85 assertSyntaxError("function* g() { yield 3 + yield 4; }"); |
|
86 |
|
87 // Yield is still a future-reserved-word in strict mode |
|
88 assertSyntaxError("function f() { 'use strict'; var yield = 13; }"); |
|
89 |
|
90 // The name of the NFE is let-bound in G, so is invalid. |
|
91 assertSyntaxError("function* g() { yield (function yield() {}); }"); |
|
92 |
|
93 // In generators, yield is invalid as a formal argument name. |
|
94 assertSyntaxError("function* g(yield) { yield (10); }"); |
|
95 |
|
96 if (typeof reportCompare == "function") |
|
97 reportCompare(true, true); |