blob: c00374506db2505a421e207fce272112e04c6e9d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1. Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
Ben Murdoch3b9bc312016-06-02 14:46:10 +010024// Flags: --no-harmony-restrictive-declarations
25
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026description(
27"This test checks that the following expressions or statements are valid ECMASCRIPT code or should throw parse error"
28);
29
30function runTest(_a, errorType)
31{
32 var success;
33 if (typeof _a != "string")
34 testFailed("runTest expects string argument: " + _a);
35 try {
36 eval(_a);
37 success = true;
38 } catch (e) {
39 success = !(e instanceof SyntaxError);
40 }
41 if ((!!errorType) == !success) {
42 if (errorType)
43 testPassed('Invalid: "' + _a + '"');
44 else
45 testPassed('Valid: "' + _a + '"');
46 } else {
47 if (errorType)
48 testFailed('Invalid: "' + _a + '" should throw ' + errorType.name);
49 else
50 testFailed('Valid: "' + _a + '" should NOT throw ');
51 }
52}
53
54function valid(_a)
55{
56 // Test both the grammar and the syntax checker
57 runTest(_a, false);
58 runTest("function f() { " + _a + " }", false);
59}
60
61function invalid(_a, _type)
62{
63 _type = _type || SyntaxError;
64 // Test both the grammar and the syntax checker
65 runTest(_a, true);
66 runTest("function f() { " + _a + " }", true);
67}
68
69// known issue:
70// some statements requires statement as argument, and
71// it seems the End-Of-File terminator is converted to semicolon
72// "a:[EOF]" is not parse error, while "{ a: }" is parse error
73// "if (a)[EOF]" is not parse error, while "{ if (a) }" is parse error
74// known issues of bison parser:
75// accepts: 'function f() { return 6 + }' (only inside a function declaration)
76// some comma expressions: see reparsing-semicolon-insertion.js
77
78debug ("Unary operators and member access");
79
80valid ("");
81invalid("(a");
82invalid("a[5");
83invalid("a[5 + 6");
84invalid("a.");
85invalid("()");
86invalid("a.'l'");
87valid ("a: +~!new a");
88invalid("new -a");
89valid ("new (-1)")
90valid ("a: b: c: new f(x++)++")
91valid ("(a)++");
92valid ("(1--).x");
93invalid("a-- ++");
94invalid("(a:) --b");
95valid ("++ -- ++ a");
96valid ("++ new new a ++");
97valid ("delete void 0");
98invalid("delete the void");
99invalid("(a++");
100valid ("++a--");
101valid ("++((a))--");
102valid ("(a.x++)++");
103invalid("1: null");
104invalid("+-!~");
105invalid("+-!~((");
106invalid("a)");
107invalid("a]");
108invalid(".l");
109invalid("1.l");
110valid ("1 .l");
111
112debug ("Binary and conditional operators");
113
114valid ("a + + typeof this");
115invalid("a + * b");
116invalid("a ? b");
117invalid("a ? b :");
118invalid("%a");
119invalid("a-");
120valid ("a = b ? b = c : d = e");
121valid ("s: a[1].l ? b.l['s'] ? c++ : d : true");
122valid ("a ? b + 1 ? c + 3 * d.l : d[5][6] : e");
123valid ("a in b instanceof delete -c");
124invalid("a in instanceof b.l");
125valid ("- - true % 5");
126invalid("- false = 3");
127valid ("a: b: c: (1 + null) = 3");
128valid ("a[2] = b.l += c /= 4 * 7 ^ !6");
129invalid("a + typeof b += c in d");
130invalid("typeof a &= typeof b");
131valid ("a: ((typeof (a))) >>>= a || b.l && c");
132valid ("a: b: c[a /= f[a %= b]].l[c[x] = 7] -= a ? b <<= f : g");
133valid ("-void+x['y'].l == x.l != 5 - f[7]");
134
135debug ("Function calls (and new with arguments)");
136
137valid ("a()()()");
138valid ("s: l: a[2](4 == 6, 5 = 6)(f[4], 6)");
139valid ("s: eval(a.apply(), b.call(c[5] - f[7]))");
140invalid("a(");
141invalid("a(5");
142invalid("a(5,");
143invalid("a(5,)");
144invalid("a(5,6");
145valid ("a(b[7], c <d> e.l, new a() > b)");
146invalid("a(b[5)");
147invalid("a(b.)");
148valid ("~new new a(1)(i++)(c[l])");
149invalid("a(*a)");
150valid ("((((a))((b)()).l))()");
151valid ("(a)[b + (c) / (d())].l--");
152valid ("new (5)");
153invalid("new a(5");
154valid ("new (f + 5)(6, (g)() - 'l'() - true(false))");
155invalid("a(.length)");
156
157debug ("function declaration and expression");
158
159valid ("function f() {}");
160valid ("function f(a,b) {}");
161invalid("function () {}");
162invalid("function f(a b) {}");
163invalid("function f(a,) {}");
164invalid("function f(a,");
165invalid("function f(a, 1) {}");
166valid ("function g(arguments, eval) {}");
167valid ("function f() {} + function g() {}");
168invalid("(function a{})");
169invalid("(function this(){})");
170valid ("(delete new function f(){} + function(a,b){}(5)(6))");
171valid ("6 - function (m) { function g() {} }");
172invalid("function l() {");
173invalid("function l++(){}");
174
175debug ("Array and object literal, comma operator");
176
177// Note these are tested elsewhere, no need to repeat those tests here
178valid ("[] in [5,6] * [,5,] / [,,5,,] || [a,] && new [,b] % [,,]");
179invalid("[5,");
180invalid("[,");
181invalid("(a,)");
182valid ("1 + {get get(){}, set set(a){}, get1:4, set1:get-set, }");
183invalid("1 + {a");
184invalid("1 + {a:");
185invalid("1 + {get l(");
186invalid(",a");
187valid ("(4,(5,a(3,4))),f[4,a-6]");
188invalid("(,f)");
189invalid("a,,b");
190invalid("a ? b, c : d");
191
192debug ("simple statements");
193
194valid ("{ }");
195invalid("{ { }");
196valid ("{ ; ; ; }");
197valid ("a: { ; }");
198invalid("{ a: }");
199valid ("{} f; { 6 + f() }");
200valid ("{ a[5],6; {} ++b-new (-5)() } c().l++");
201valid ("{ l1: l2: l3: { this } a = 32 ; { i++ ; { { { } } ++i } } }");
202valid ("if (a) ;");
203invalid("{ if (a) }");
204invalid("if a {}");
205invalid("if (a");
206invalid("if (a { }");
207valid ("x: s: if (a) ; else b");
208invalid("else {}");
209valid ("if (a) if (b) y; else {} else ;");
210invalid("if (a) {} else x; else");
211invalid("if (a) { else }");
212valid ("if (a.l + new b()) 4 + 5 - f()");
213valid ("if (a) with (x) ; else with (y) ;");
214invalid("with a.b { }");
215valid ("while (a() - new b) ;");
216invalid("while a {}");
217valid ("do ; while(0) i++"); // Is this REALLY valid? (Firefox also accepts this)
218valid ("do if (a) x; else y; while(z)");
219invalid("do g; while 4");
220invalid("do g; while ((4)");
221valid ("{ { do do do ; while(0) while(0) while(0) } }");
222valid ("do while (0) if (a) {} else y; while(0)");
223valid ("if (a) while (b) if (c) with(d) {} else e; else f");
224invalid("break ; break your_limits ; continue ; continue living ; debugger");
225invalid("debugger X");
226invalid("break 0.2");
227invalid("continue a++");
228invalid("continue (my_friend)");
229valid ("while (1) break");
230valid ("do if (a) with (b) continue; else debugger; while (false)");
231invalid("do if (a) while (false) else debugger");
232invalid("while if (a) ;");
233valid ("if (a) function f() {} else function g() {}");
234valid ("if (a()) while(0) function f() {} else function g() {}");
235invalid("if (a()) function f() { else function g() }");
236invalid("if (a) if (b) ; else function f {}");
237invalid("if (a) if (b) ; else function (){}");
238valid ("throw a");
239valid ("throw a + b in void c");
240invalid("throw");
241
242debug ("var and const statements");
243
244valid ("var a, b = null");
245valid ("const a = 5, b, c");
246invalid("var");
247invalid("var = 7");
248invalid("var c (6)");
249valid ("if (a) var a,b; else const b, c");
250invalid("var 5 = 6");
251valid ("while (0) var a, b, c=6, d, e, f=5*6, g=f*h, h");
252invalid("var a = if (b) { c }");
253invalid("var a = var b");
254valid ("const a = b += c, a, a, a = (b - f())");
255invalid("var a %= b | 5");
256invalid("var (a) = 5");
257invalid("var a = (4, b = 6");
258invalid("const 'l' = 3");
259invalid("var var = 3");
260valid ("var varr = 3 in 1");
261valid ("const a, a, a = void 7 - typeof 8, a = 8");
262valid ("const x_x = 6 /= 7 ? e : f");
263invalid("var a = ?");
264invalid("const a = *7");
265invalid("var a = :)");
266valid ("var a = a in b in c instanceof d");
267invalid("var a = b ? c, b");
268invalid("const a = b : c");
269
270debug ("for statement");
271
272valid ("for ( ; ; ) { break }");
273valid ("for ( a ; ; ) { break }");
274valid ("for ( ; a ; ) { break }");
275valid ("for ( ; ; a ) { break }");
276valid ("for ( a ; a ; ) break");
277valid ("for ( a ; ; a ) break");
278valid ("for ( ; a ; a ) break");
279invalid("for () { }");
280invalid("for ( a ) { }");
281invalid("for ( ; ) ;");
282invalid("for a ; b ; c { }");
283invalid("for (a ; { }");
284invalid("for ( a ; ) ;");
285invalid("for ( ; a ) break");
286valid ("for (var a, b ; ; ) { break } ");
287valid ("for (var a = b, b = a ; ; ) break");
288valid ("for (var a = b, c, d, b = a ; x in b ; ) { break }");
289valid ("for (var a = b, c, d ; ; 1 in a()) break");
290invalid("for ( ; var a ; ) break");
291invalid("for (const a; ; ) break");
292invalid("for ( %a ; ; ) { }");
293valid ("for (a in b) break");
294valid ("for (a() in b) break");
295valid ("for (a().l[4] in b) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000296invalid("for (new a in b in c in d) break");
297invalid("for (new new new a in b) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298invalid("for (delete new a() in b) break");
299invalid("for (a * a in b) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000300invalid("for ((a * a) in b) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301invalid("for (a++ in b) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000302invalid("for ((a++) in b) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303invalid("for (++a in b) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000304invalid("for ((++a) in b) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305invalid("for (a, b in c) break");
306invalid("for (a,b in c ;;) break");
307valid ("for (a,(b in c) ;;) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000308invalid("for ((a, b) in c) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309invalid("for (a ? b : c in c) break");
Ben Murdoch014dc512016-03-22 12:00:34 +0000310invalid("for ((a ? b : c) in c) break");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311valid ("for (var a in b in c) break");
312valid ("for (var a = 5 += 6 in b) break");
313invalid("for (var a += 5 in b) break");
314invalid("for (var a = in b) break");
315invalid("for (var a, b in b) break");
316invalid("for (var a = -6, b in b) break");
317invalid("for (var a, b = 8 in b) break");
318valid ("for (var a = (b in c) in d) break");
319invalid("for (var a = (b in c in d) break");
320invalid("for (var (a) in b) { }");
321valid ("for (var a = 7, b = c < d >= d ; f()[6]++ ; --i()[1]++ ) {}");
322
323debug ("try statement");
324
325invalid("try { break } catch(e) {}");
326valid ("try {} finally { c++ }");
327valid ("try { with (x) { } } catch(e) {} finally { if (a) ; }");
328invalid("try {}");
329invalid("catch(e) {}");
330invalid("finally {}");
331invalid("try a; catch(e) {}");
332invalid("try {} catch(e) a()");
333invalid("try {} finally a()");
334invalid("try {} catch(e)");
335invalid("try {} finally");
336invalid("try {} finally {} catch(e) {}");
337invalid("try {} catch (...) {}");
338invalid("try {} catch {}");
339valid ("if (a) try {} finally {} else b;");
340valid ("if (--a()) do with(1) try {} catch(ke) { f() ; g() } while (a in b) else {}");
341invalid("if (a) try {} else b; catch (e) { }");
342invalid("try { finally {}");
343
344debug ("switch statement");
345
346valid ("switch (a) {}");
347invalid("switch () {}");
348invalid("case 5:");
349invalid("default:");
350invalid("switch (a) b;");
351invalid("switch (a) case 3: b;");
352valid ("switch (f()) { case 5 * f(): default: case '6' - 9: ++i }");
353invalid("switch (true) { default: case 6: default: }");
354invalid("switch (l) { f(); }");
355invalid("switch (l) { case 1: ; a: case 5: }");
356valid ("switch (g() - h[5].l) { case 1 + 6: a: b: c: ++f }");
357invalid("switch (g) { case 1: a: }");
358invalid("switch (g) { case 1: a: default: }");
359invalid("switch g { case 1: l() }");
360invalid("switch (g) { case 1:");
361valid ("switch (l) { case a = b ? c : d : }");
362valid ("switch (sw) { case a ? b - 7[1] ? [c,,] : d = 6 : { } : }");
363invalid("switch (l) { case b ? c : }");
364valid ("switch (l) { case 1: a: with(g) switch (g) { case 2: default: } default: }");
365invalid("switch (4 - ) { }");
366invalid("switch (l) { default case: 5; }");
367
368invalid("L: L: ;");
369invalid("L: L1: L: ;");
370invalid("L: L1: L2: L3: L4: L: ;");
371
372invalid("for(var a,b 'this shouldn\'t be allowed' false ; ) ;");
373invalid("for(var a,b '");
374
375valid("function __proto__(){}")
376valid("(function __proto__(){})")
377valid("'use strict'; function __proto__(){}")
378valid("'use strict'; (function __proto__(){})")
379
380valid("if (0) $foo; ")
381valid("if (0) _foo; ")
382valid("if (0) foo$; ")
383valid("if (0) foo_; ")
384valid("if (0) obj.$foo; ")
385valid("if (0) obj._foo; ")
386valid("if (0) obj.foo$; ")
387valid("if (0) obj.foo_; ")
388valid("if (0) obj.foo\\u03bb; ")
389valid("if (0) new a(b+c).d = 5");
390valid("if (0) new a(b+c) = 5");
391valid("([1 || 1].a = 1)");
392valid("({a: 1 || 1}.a = 1)");
393
394invalid("var a.b = c");
395invalid("var a.b;");
396
397try { eval("a.b.c = {};"); } catch(e1) { e=e1; shouldBe("e.line", "1") }
398foo = 'FAIL';
399bar = 'PASS';
400try {
401 eval("foo = 'PASS'; a.b.c = {}; bar = 'FAIL';");
402} catch(e) {
403 shouldBe("foo", "'PASS'");
404 shouldBe("bar", "'PASS'");
405}