blob: ea0e39bd07f2e39dded587ef6e44e2aac8dab248 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2011 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 let declarations in various settings.
29
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030// Global
31let x;
32let y = 2;
33const z = 4;
34class c { static foo() { return 1; } }
35
36// Block local
37{
38 let y;
39 let x = 3;
40 const z = 5;
41 class c { static foo() { return 2; } }
42}
43
44assertEquals(undefined, x);
45assertEquals(2,y);
46assertEquals(4,z);
47assertEquals(1, c.foo());
48
49if (true) {
50 let y;
51 assertEquals(undefined, y);
52}
53
54// Invalid declarations are early errors in harmony mode and thus should trigger
55// an exception in eval code during parsing, before even compiling or executing
56// the code. Thus the generated function is not called here.
57function TestLocalThrows(str, expect) {
58 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect);
59}
60
61function TestLocalDoesNotThrow(str) {
62 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()");
63}
64
65// Test let declarations in statement positions.
66TestLocalThrows("if (true) let x;", SyntaxError);
67TestLocalThrows("if (true) {} else let x;", SyntaxError);
68TestLocalThrows("do let x; while (false)", SyntaxError);
69TestLocalThrows("while (false) let x;", SyntaxError);
70TestLocalThrows("label: let x;", SyntaxError);
71TestLocalThrows("for (;false;) let x;", SyntaxError);
72TestLocalDoesNotThrow("switch (true) { case true: let x; }");
73TestLocalDoesNotThrow("switch (true) { default: let x; }");
74
75// Test const declarations with initialisers in statement positions.
76TestLocalThrows("if (true) const x = 1;", SyntaxError);
77TestLocalThrows("if (true) {} else const x = 1;", SyntaxError);
78TestLocalThrows("do const x = 1; while (false)", SyntaxError);
79TestLocalThrows("while (false) const x = 1;", SyntaxError);
80TestLocalThrows("label: const x = 1;", SyntaxError);
81TestLocalThrows("for (;false;) const x = 1;", SyntaxError);
82TestLocalDoesNotThrow("switch (true) { case true: const x = 1; }");
83TestLocalDoesNotThrow("switch (true) { default: const x = 1; }");
84
85// Test const declarations without initialisers.
86TestLocalThrows("const x;", SyntaxError);
87TestLocalThrows("const x = 1, y;", SyntaxError);
88TestLocalThrows("const x, y = 1;", SyntaxError);
89
90// Test const declarations without initialisers in statement positions.
91TestLocalThrows("if (true) const x;", SyntaxError);
92TestLocalThrows("if (true) {} else const x;", SyntaxError);
93TestLocalThrows("do const x; while (false)", SyntaxError);
94TestLocalThrows("while (false) const x;", SyntaxError);
95TestLocalThrows("label: const x;", SyntaxError);
96TestLocalThrows("for (;false;) const x;", SyntaxError);
97TestLocalThrows("switch (true) { case true: const x; }", SyntaxError);
98TestLocalThrows("switch (true) { default: const x; }", SyntaxError);
99
100// Test var declarations in statement positions.
101TestLocalDoesNotThrow("if (true) var x;");
102TestLocalDoesNotThrow("if (true) {} else var x;");
103TestLocalDoesNotThrow("do var x; while (false)");
104TestLocalDoesNotThrow("while (false) var x;");
105TestLocalDoesNotThrow("label: var x;");
106TestLocalDoesNotThrow("for (;false;) var x;");
107TestLocalDoesNotThrow("switch (true) { case true: var x; }");
108TestLocalDoesNotThrow("switch (true) { default: var x; }");
109
110// Test class declarations with initialisers in statement positions.
111TestLocalThrows("if (true) class x { };", SyntaxError);
112TestLocalThrows("if (true) {} else class x { };", SyntaxError);
113TestLocalThrows("do class x { }; while (false)", SyntaxError);
114TestLocalThrows("while (false) class x { };", SyntaxError);
115TestLocalThrows("label: class x { };", SyntaxError);
116TestLocalThrows("for (;false;) class x { };", SyntaxError);
117TestLocalDoesNotThrow("switch (true) { case true: class x { }; }");
118TestLocalDoesNotThrow("switch (true) { default: class x { }; }");
119
120// Test that redeclarations of functions are only allowed in outermost scope.
121TestLocalThrows("{ let f; var f; }");
122TestLocalThrows("{ var f; let f; }");
123TestLocalThrows("{ function f() {} let f; }");
124TestLocalThrows("{ let f; function f() {} }");
125TestLocalThrows("{ function f() {} var f; }");
126TestLocalThrows("{ var f; function f() {} }");
127TestLocalThrows("{ function f() {} class f {} }");
128TestLocalThrows("{ class f {}; function f() {} }");
129TestLocalThrows("{ function f() {} function f() {} }");
130TestLocalThrows("function f() {} let f;");
131TestLocalThrows("let f; function f() {}");
132TestLocalThrows("function f() {} class f {}");
133TestLocalThrows("class f {}; function f() {}");
134TestLocalDoesNotThrow("function arg() {}");
135TestLocalDoesNotThrow("function f() {} var f;");
136TestLocalDoesNotThrow("var f; function f() {}");
137TestLocalDoesNotThrow("function f() {} function f() {}");
138
139function g(f) {
140 function f() { return 1 }
141 return f()
142}
143assertEquals(1, g(function() { return 2 }))
144
145
146// Test function declarations in source element and
147// sloppy statement positions.
148function f() {
149 // Sloppy source element positions.
150 function g0() {
151 "use strict";
152 // Strict source element positions.
153 function h() { }
154 {
155 function h1() { }
156 }
157 }
158 {
159 function g1() { }
160 }
161}
162f();
163
164// Test function declarations in statement position in strict mode.
165TestLocalThrows("function f() { if (true) function g() {} }", SyntaxError);
166TestLocalThrows("function f() { if (true) {} else function g() {} }", SyntaxError);
167TestLocalThrows("function f() { do function g() {} while (false) }", SyntaxError);
168TestLocalThrows("function f() { while (false) function g() {} }", SyntaxError);
169TestLocalThrows("function f() { label: function g() {} }", SyntaxError);
170TestLocalThrows("function f() { for (;false;) function g() {} }", SyntaxError);
171TestLocalDoesNotThrow("function f() { switch (true) { case true: function g() {} } }");
172TestLocalDoesNotThrow("function f() { switch (true) { default: function g() {} } }");