1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/Generators/syntax.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +// This file was written by Andy Wingo <wingo@igalia.com> and originally 1.5 +// contributed to V8 as generators-parsing.js, available here: 1.6 +// 1.7 +// http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-parsing.js 1.8 + 1.9 +function assertSyntaxError(str) { 1.10 + var msg; 1.11 + var evil = eval; 1.12 + try { 1.13 + // Non-direct eval. 1.14 + evil(str); 1.15 + } catch (exc) { 1.16 + if (exc instanceof SyntaxError) 1.17 + return; 1.18 + msg = "Assertion failed: expected SyntaxError, got " + exc; 1.19 + } 1.20 + if (msg === undefined) 1.21 + msg = "Assertion failed: expected SyntaxError, but no exception thrown"; 1.22 + throw new Error(msg + " - " + str); 1.23 +} 1.24 + 1.25 +// Yield statements. 1.26 +function* g() { yield 3; yield 4; } 1.27 + 1.28 +// Yield expressions. 1.29 +function* g() { (yield 3) + (yield 4); } 1.30 + 1.31 +// You can have a generator in strict mode. 1.32 +function* g() { "use strict"; yield 3; yield 4; } 1.33 + 1.34 +// Generators can have return statements also, which internally parse to a kind 1.35 +// of yield expression. 1.36 +function* g() { yield 1; return; } 1.37 +function* g() { yield 1; return 2; } 1.38 +function* g() { yield 1; return 2; yield "dead"; } 1.39 + 1.40 +// Generator expression. 1.41 +(function* () { yield 3; }); 1.42 + 1.43 +// Named generator expression. 1.44 +(function* g() { yield 3; }); 1.45 + 1.46 +// Generators do not have to contain yield expressions. 1.47 +function* g() { } 1.48 + 1.49 +// YieldExpressions can occur in the RHS of a YieldExpression. 1.50 +function* g() { yield yield 1; } 1.51 +function* g() { yield 3 + (yield 4); } 1.52 + 1.53 +// Generator definitions with a name of "yield" are not specifically ruled out 1.54 +// by the spec, as the `yield' name is outside the generator itself. However, 1.55 +// in strict-mode, "yield" is an invalid identifier. 1.56 +function* yield() { (yield 3) + (yield 4); } 1.57 +assertSyntaxError("function* yield() { 'use strict'; (yield 3) + (yield 4); }"); 1.58 + 1.59 +// In classic mode, yield is a normal identifier, outside of generators. 1.60 +function yield(yield) { yield: yield (yield + yield (0)); } 1.61 + 1.62 +// Yield is always valid as a key in an object literal. 1.63 +({ yield: 1 }); 1.64 +function* g() { yield ({ yield: 1 }) } 1.65 +function* g() { yield ({ get yield() { return 1; }}) } 1.66 + 1.67 +// Yield is a valid property name. 1.68 +function* g(obj) { yield obj.yield; } 1.69 + 1.70 +// Checks that yield is a valid label in classic mode, but not valid in a strict 1.71 +// mode or in generators. 1.72 +function f() { yield: 1 } 1.73 +assertSyntaxError("function f() { 'use strict'; yield: 1 }") 1.74 +assertSyntaxError("function* g() { yield: 1 }") 1.75 + 1.76 +// Yield is only a keyword in the body of the generator, not in nested 1.77 +// functions. 1.78 +function* g() { function f(yield) { yield (yield + yield (0)); } } 1.79 + 1.80 +// Yield needs a RHS. 1.81 +assertSyntaxError("function* g() { yield; }"); 1.82 + 1.83 +// Yield in a generator is not an identifier. 1.84 +assertSyntaxError("function* g() { yield = 10; }"); 1.85 + 1.86 +// Yield binds very loosely, so this parses as "yield (3 + yield 4)", which is 1.87 +// invalid. 1.88 +assertSyntaxError("function* g() { yield 3 + yield 4; }"); 1.89 + 1.90 +// Yield is still a future-reserved-word in strict mode 1.91 +assertSyntaxError("function f() { 'use strict'; var yield = 13; }"); 1.92 + 1.93 +// The name of the NFE is let-bound in G, so is invalid. 1.94 +assertSyntaxError("function* g() { yield (function yield() {}); }"); 1.95 + 1.96 +// In generators, yield is invalid as a formal argument name. 1.97 +assertSyntaxError("function* g(yield) { yield (10); }"); 1.98 + 1.99 +if (typeof reportCompare == "function") 1.100 + reportCompare(true, true);