js/src/tests/ecma_6/Generators/syntax.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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
     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 }
    22 // Yield statements.
    23 function* g() { yield 3; yield 4; }
    25 // Yield expressions.
    26 function* g() { (yield 3) + (yield 4); }
    28 // You can have a generator in strict mode.
    29 function* g() { "use strict"; yield 3; yield 4; }
    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"; }
    37 // Generator expression.
    38 (function* () { yield 3; });
    40 // Named generator expression.
    41 (function* g() { yield 3; });
    43 // Generators do not have to contain yield expressions.
    44 function* g() { }
    46 // YieldExpressions can occur in the RHS of a YieldExpression.
    47 function* g() { yield yield 1; }
    48 function* g() { yield 3 + (yield 4); }
    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); }");
    56 // In classic mode, yield is a normal identifier, outside of generators.
    57 function yield(yield) { yield: yield (yield + yield (0)); }
    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; }}) }
    64 // Yield is a valid property name.
    65 function* g(obj) { yield obj.yield; }
    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 }")
    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)); } }
    77 // Yield needs a RHS.
    78 assertSyntaxError("function* g() { yield; }");
    80 // Yield in a generator is not an identifier.
    81 assertSyntaxError("function* g() { yield = 10; }");
    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; }");
    87 // Yield is still a future-reserved-word in strict mode
    88 assertSyntaxError("function f() { 'use strict'; var yield = 13; }");
    90 // The name of the NFE is let-bound in G, so is invalid.
    91 assertSyntaxError("function* g() { yield (function yield() {}); }");
    93 // In generators, yield is invalid as a formal argument name.
    94 assertSyntaxError("function* g(yield) { yield (10); }");
    96 if (typeof reportCompare == "function")
    97     reportCompare(true, true);

mercurial