OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 // You can have a generator in strict mode. |
| 39 function* g() { "use strict"; yield 3; yield 4; } |
| 40 |
| 41 // Generator expression. |
| 42 (function* () { yield 3; }); |
| 43 |
| 44 // Named generator expression. |
| 45 (function* g() { yield 3; }); |
| 46 |
| 47 // A generator without a yield is specified as causing an early error. This |
| 48 // behavior is currently unimplemented. See |
| 49 // https://bugs.ecmascript.org/show_bug.cgi?id=1283. |
| 50 function* g() { } |
| 51 |
| 52 // A YieldExpression in the RHS of a YieldExpression is currently specified as |
| 53 // causing an early error. This behavior is currently unimplemented. See |
| 54 // https://bugs.ecmascript.org/show_bug.cgi?id=1283. |
| 55 function* g() { yield yield 1; } |
| 56 function* g() { yield 3 + (yield 4); } |
| 57 |
| 58 // Generator definitions with a name of "yield" are not specifically ruled out |
| 59 // by the spec, as the `yield' name is outside the generator itself. However, |
| 60 // in strict-mode, "yield" is an invalid identifier. |
| 61 function* yield() { (yield 3) + (yield 4); } |
| 62 assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }", |
| 63 SyntaxError); |
| 64 |
| 65 // In classic mode, yield is a normal identifier, outside of generators. |
| 66 function yield(yield) { yield: yield (yield + yield (0)); } |
| 67 |
| 68 // Yield is always valid as a key in an object literal. |
| 69 ({ yield: 1 }); |
| 70 function* g() { yield ({ yield: 1 }) } |
| 71 function* g() { yield ({ get yield() { return 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 (0)); } } |
| 76 |
| 77 // Yield needs a RHS. |
| 78 assertThrows("function* g() { yield; }", SyntaxError); |
| 79 |
| 80 // Yield in a generator is not an identifier. |
| 81 assertThrows("function* g() { yield = 10; }", SyntaxError); |
| 82 |
| 83 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is |
| 84 // invalid. |
| 85 assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError); |
| 86 |
| 87 // Yield is still a future-reserved-word in strict mode |
| 88 assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError); |
| 89 |
| 90 // The name of the NFE is let-bound in G, so is invalid. |
| 91 assertThrows("function* g() { yield (function yield() {}); }", SyntaxError); |
| 92 |
| 93 // In generators, yield is invalid as a formal argument name. |
| 94 assertThrows("function* g(yield) { yield (10); }", SyntaxError); |
OLD | NEW |