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 17 matching lines...) Expand all Loading... | |
28 // Flags: --harmony-generators | 28 // Flags: --harmony-generators |
29 | 29 |
30 // Test basic generator syntax. | 30 // Test basic generator syntax. |
31 | 31 |
32 // Yield statements. | 32 // Yield statements. |
33 function* g() { yield 3; yield 4; } | 33 function* g() { yield 3; yield 4; } |
34 | 34 |
35 // Yield expressions. | 35 // Yield expressions. |
36 function* g() { (yield 3) + (yield 4); } | 36 function* g() { (yield 3) + (yield 4); } |
37 | 37 |
38 // Yield without a RHS. | |
39 function* g() { yield; } | |
marja
2014/06/26 10:41:52
Could you add a test that calls this kind of funct
wingo
2014/06/26 11:04:27
You can see an example of this on line 63. (The r
| |
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 | |
38 // You can have a generator in strict mode. | 72 // You can have a generator in strict mode. |
39 function* g() { "use strict"; yield 3; yield 4; } | 73 function* g() { "use strict"; yield 3; yield 4; } |
40 | 74 |
41 // Generators can have return statements also, which internally parse to a kind | 75 // Generators can have return statements also, which internally parse to a kind |
42 // of yield expression. | 76 // of yield expression. |
43 function* g() { yield 1; return; } | 77 function* g() { yield 1; return; } |
44 function* g() { yield 1; return 2; } | 78 function* g() { yield 1; return 2; } |
45 function* g() { yield 1; return 2; yield "dead"; } | 79 function* g() { yield 1; return 2; yield "dead"; } |
46 | 80 |
47 // Generator expression. | 81 // Generator expression. |
48 (function* () { yield 3; }); | 82 (function* () { yield 3; }); |
49 | 83 |
50 // Named generator expression. | 84 // Named generator expression. |
51 (function* g() { yield 3; }); | 85 (function* g() { yield 3; }); |
52 | 86 |
53 // A generator without a yield is specified as causing an early error. This | 87 // You can have a generator without a yield. |
54 // behavior is currently unimplemented. See | |
55 // https://bugs.ecmascript.org/show_bug.cgi?id=1283. | |
56 function* g() { } | 88 function* g() { } |
57 | 89 |
58 // A YieldExpression in the RHS of a YieldExpression is currently specified as | 90 // A YieldExpression is valid as the RHS of a YieldExpression. |
59 // causing an early error. This behavior is currently unimplemented. See | |
60 // https://bugs.ecmascript.org/show_bug.cgi?id=1283. | |
61 function* g() { yield yield 1; } | 91 function* g() { yield yield 1; } |
62 function* g() { yield 3 + (yield 4); } | 92 function* g() { yield 3 + (yield 4); } |
63 | 93 |
64 // Generator definitions with a name of "yield" are not specifically ruled out | 94 // Generator definitions with a name of "yield" are not specifically ruled out |
65 // by the spec, as the `yield' name is outside the generator itself. However, | 95 // by the spec, as the `yield' name is outside the generator itself. However, |
66 // in strict-mode, "yield" is an invalid identifier. | 96 // in strict-mode, "yield" is an invalid identifier. |
67 function* yield() { (yield 3) + (yield 4); } | 97 function* yield() { (yield 3) + (yield 4); } |
68 assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }", | 98 assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }", |
69 SyntaxError); | 99 SyntaxError); |
70 | 100 |
71 // In sloppy mode, yield is a normal identifier, outside of generators. | 101 // In sloppy mode, yield is a normal identifier, outside of generators. |
72 function yield(yield) { yield: yield (yield + yield (0)); } | 102 function yield(yield) { yield: yield (yield + yield (0)); } |
73 | 103 |
74 // Yield is always valid as a key in an object literal. | 104 // Yield is always valid as a key in an object literal. |
75 ({ yield: 1 }); | 105 ({ yield: 1 }); |
76 function* g() { yield ({ yield: 1 }) } | 106 function* g() { yield ({ yield: 1 }) } |
77 function* g() { yield ({ get yield() { return 1; }}) } | 107 function* g() { yield ({ get yield() { return 1; }}) } |
78 | 108 |
79 // Checks that yield is a valid label in sloppy mode, but not valid in a strict | 109 // Checks that yield is a valid label in sloppy mode, but not valid in a strict |
80 // mode or in generators. | 110 // mode or in generators. |
81 function f() { yield: 1 } | 111 function f() { yield: 1 } |
82 assertThrows("function f() { \"use strict\"; yield: 1 }", SyntaxError) | 112 assertThrows("function f() { \"use strict\"; yield: 1 }", SyntaxError) |
83 assertThrows("function* g() { yield: 1 }", SyntaxError) | 113 assertThrows("function* g() { yield: 1 }", SyntaxError) |
84 | 114 |
85 // Yield is only a keyword in the body of the generator, not in nested | 115 // Yield is only a keyword in the body of the generator, not in nested |
86 // functions. | 116 // functions. |
87 function* g() { function f() { yield (yield + yield (0)); } } | 117 function* g() { function f() { yield (yield + yield (0)); } } |
88 | 118 |
89 // Yield needs a RHS. | |
90 assertThrows("function* g() { yield; }", SyntaxError); | |
91 | |
92 // Yield in a generator is not an identifier. | 119 // Yield in a generator is not an identifier. |
93 assertThrows("function* g() { yield = 10; }", SyntaxError); | 120 assertThrows("function* g() { yield = 10; }", SyntaxError); |
94 | 121 |
95 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is | 122 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is |
96 // invalid. | 123 // invalid. |
97 assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError); | 124 assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError); |
98 | 125 |
99 // Yield is still a future-reserved-word in strict mode | 126 // Yield is still a future-reserved-word in strict mode |
100 assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError); | 127 assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError); |
101 | 128 |
102 // The name of the NFE is let-bound in G, so is invalid. | 129 // The name of the NFE is let-bound in G, so is invalid. |
103 assertThrows("function* g() { yield (function yield() {}); }", SyntaxError); | 130 assertThrows("function* g() { yield (function yield() {}); }", SyntaxError); |
104 | 131 |
105 // In generators, yield is invalid as a formal argument name. | 132 // In generators, yield is invalid as a formal argument name. |
106 assertThrows("function* g(yield) { yield (10); }", SyntaxError); | 133 assertThrows("function* g(yield) { yield (10); }", SyntaxError); |
OLD | NEW |