Home | History | Annotate | Download | only in es6
      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.
     31 function* g() { yield 3; yield 4; }
     32 
     33 // Yield expressions.
     34 function* g() { (yield 3) + (yield 4); }
     35 
     36 // Yield without a RHS.
     37 function* g() { yield; }
     38 function* g() { yield }
     39 function* g() {
     40   yield
     41 }
     42 function* g() { (yield) }
     43 function* g() { [yield] }
     44 function* g() { {yield} }
     45 function* g() { yield, yield }
     46 function* g() { yield; yield }
     47 function* g() { (yield) ? yield : yield }
     48 function* 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.
     56 function* g() {
     57   yield *
     58   foo
     59 }
     60 assertThrows("function* g() { yield\n* foo }", SyntaxError);
     61 assertEquals(undefined,
     62              (function*(){
     63                yield
     64                3
     65              })().next().value);
     66 
     67 // A YieldExpression is not a LogicalORExpression.
     68 assertThrows("function* g() { yield ? yield : yield }", SyntaxError);
     69 
     70 // You can have a generator in strict mode.
     71 function* 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.
     75 function* g() { yield 1; return; }
     76 function* g() { yield 1; return 2; }
     77 function* 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.
     86 function* g() { }
     87 
     88 // A YieldExpression is valid as the RHS of a YieldExpression.
     89 function* g() { yield yield 1; }
     90 function* 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.
     95 function* yield() { (yield 3) + (yield 4); }
     96 assertThrows("function* yield() { \"use strict\"; (yield 3) + (yield 4); }",
     97              SyntaxError);
     98 
     99 // In sloppy mode, yield is a normal identifier, outside of generators.
    100 function yield(yield) { yield: yield (yield + yield (0)); }
    101 
    102 // Yield is always valid as a key in an object literal.
    103 ({ yield: 1 });
    104 function* g() { yield ({ yield: 1 }) }
    105 function* 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.
    109 function f() { yield: 1 }
    110 assertThrows("function f() { \"use strict\"; yield: 1 }", SyntaxError)
    111 assertThrows("function* g() { yield: 1 }", SyntaxError)
    112 
    113 // Yield is only a keyword in the body of the generator, not in nested
    114 // functions.
    115 function* g() { function f() { yield (yield + yield (0)); } }
    116 
    117 // Yield in a generator is not an identifier.
    118 assertThrows("function* g() { yield = 10; }", SyntaxError);
    119 
    120 // Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is
    121 // invalid.
    122 assertThrows("function* g() { yield 3 + yield 4; }", SyntaxError);
    123 
    124 // Yield is still a future-reserved-word in strict mode
    125 assertThrows("function f() { \"use strict\"; var yield = 13; }", SyntaxError);
    126 
    127 // The name of the NFE is let-bound in G, so is invalid.
    128 assertThrows("function* g() { yield (function yield() {}); }", SyntaxError);
    129 
    130 // In generators, yield is invalid as a formal argument name.
    131 assertThrows("function* g(yield) { yield (10); }", SyntaxError);
    132