blob: 44a0049a44e3d7dc137e407c6e0f2c45de284600 [file] [log] [blame]
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +000027
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028// Flags: --harmony-scoping
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Ben Murdoch69a99ed2011-11-30 16:03:39 +000030// Test let declarations in various settings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031
Ben Murdoch3ef787d2012-04-12 10:51:47 +010032"use strict";
Steve Blocka7e24c12009-10-30 11:49:00 +000033
Ben Murdoch69a99ed2011-11-30 16:03:39 +000034// Global
35let x;
36let y = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037const z = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +000038
Ben Murdoch69a99ed2011-11-30 16:03:39 +000039// Block local
40{
41 let y;
42 let x = 3;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010043 const z = 5;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000044}
Steve Blocka7e24c12009-10-30 11:49:00 +000045
Ben Murdoch69a99ed2011-11-30 16:03:39 +000046assertEquals(undefined, x);
47assertEquals(2,y);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048assertEquals(4,z);
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Ben Murdoch69a99ed2011-11-30 16:03:39 +000050if (true) {
51 let y;
52 assertEquals(undefined, y);
53}
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Ben Murdoch3ef787d2012-04-12 10:51:47 +010055// Invalid declarations are early errors in harmony mode and thus should trigger
56// an exception in eval code during parsing, before even compiling or executing
57// the code. Thus the generated function is not called here.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000058function TestLocalThrows(str, expect) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000060}
Steve Blocka7e24c12009-10-30 11:49:00 +000061
Ben Murdoch69a99ed2011-11-30 16:03:39 +000062function TestLocalDoesNotThrow(str) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()");
Ben Murdoch69a99ed2011-11-30 16:03:39 +000064}
65
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066// Test let declarations in statement positions.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000067TestLocalThrows("if (true) let x;", SyntaxError);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010068TestLocalThrows("if (true) {} else let x;", SyntaxError);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000069TestLocalThrows("do let x; while (false)", SyntaxError);
70TestLocalThrows("while (false) let x;", SyntaxError);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010071TestLocalThrows("label: let x;", SyntaxError);
72TestLocalThrows("for (;false;) let x;", SyntaxError);
73TestLocalThrows("switch (true) { case true: let x; }", SyntaxError);
74TestLocalThrows("switch (true) { default: let x; }", SyntaxError);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000075
Ben Murdoch3ef787d2012-04-12 10:51:47 +010076// Test const declarations with initialisers in statement positions.
77TestLocalThrows("if (true) const x = 1;", SyntaxError);
78TestLocalThrows("if (true) {} else const x = 1;", SyntaxError);
79TestLocalThrows("do const x = 1; while (false)", SyntaxError);
80TestLocalThrows("while (false) const x = 1;", SyntaxError);
81TestLocalThrows("label: const x = 1;", SyntaxError);
82TestLocalThrows("for (;false;) const x = 1;", SyntaxError);
83TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError);
84TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError);
85
86// Test const declarations without initialisers.
87TestLocalThrows("const x;", SyntaxError);
88TestLocalThrows("const x = 1, y;", SyntaxError);
89TestLocalThrows("const x, y = 1;", SyntaxError);
90
91// Test const declarations without initialisers in statement positions.
92TestLocalThrows("if (true) const x;", SyntaxError);
93TestLocalThrows("if (true) {} else const x;", SyntaxError);
94TestLocalThrows("do const x; while (false)", SyntaxError);
95TestLocalThrows("while (false) const x;", SyntaxError);
96TestLocalThrows("label: const x;", SyntaxError);
97TestLocalThrows("for (;false;) const x;", SyntaxError);
98TestLocalThrows("switch (true) { case true: const x; }", SyntaxError);
99TestLocalThrows("switch (true) { default: const x; }", SyntaxError);
100
101// Test var declarations in statement positions.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000102TestLocalDoesNotThrow("if (true) var x;");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100103TestLocalDoesNotThrow("if (true) {} else var x;");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000104TestLocalDoesNotThrow("do var x; while (false)");
105TestLocalDoesNotThrow("while (false) var x;");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106TestLocalDoesNotThrow("label: var x;");
107TestLocalDoesNotThrow("for (;false;) var x;");
108TestLocalDoesNotThrow("switch (true) { case true: var x; }");
109TestLocalDoesNotThrow("switch (true) { default: var x; }");
110
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111// Test that redeclarations of functions are only allowed in outermost scope.
112TestLocalThrows("{ let f; var f; }");
113TestLocalThrows("{ var f; let f; }");
114TestLocalThrows("{ function f() {} let f; }");
115TestLocalThrows("{ let f; function f() {} }");
116TestLocalThrows("{ function f() {} var f; }");
117TestLocalThrows("{ var f; function f() {} }");
118TestLocalThrows("{ function f() {} function f() {} }");
119TestLocalThrows("function f() {} let f;");
120TestLocalThrows("let f; function f() {}");
121TestLocalDoesNotThrow("function arg() {}");
122TestLocalDoesNotThrow("function f() {} var f;");
123TestLocalDoesNotThrow("var f; function f() {}");
124TestLocalDoesNotThrow("function f() {} function f() {}");
125
126function g(f) {
127 function f() { return 1 }
128 return f()
129}
130assertEquals(1, g(function() { return 2 }))
131
132
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100133// Test function declarations in source element and
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134// sloppy statement positions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100135function f() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 // Sloppy source element positions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100137 function g0() {
138 "use strict";
139 // Strict source element positions.
140 function h() { }
141 {
142 function h1() { }
143 }
144 }
145 {
146 function g1() { }
147 }
148}
149f();
150
151// Test function declarations in statement position in strict mode.
152TestLocalThrows("function f() { if (true) function g() {}", SyntaxError);
153TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError);
154TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError);
155TestLocalThrows("function f() { while (false) function g() {}", SyntaxError);
156TestLocalThrows("function f() { label: function g() {}", SyntaxError);
157TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError);
158TestLocalThrows("function f() { switch (true) { case true: function g() {} }", SyntaxError);
159TestLocalThrows("function f() { switch (true) { default: function g() {} }", SyntaxError);