1// 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// Flags: --harmony-scoping 29 30// Test let declarations in various settings. 31 32"use strict"; 33 34// Global 35let x; 36let y = 2; 37const z = 4; 38 39// Block local 40{ 41 let y; 42 let x = 3; 43 const z = 5; 44} 45 46assertEquals(undefined, x); 47assertEquals(2,y); 48assertEquals(4,z); 49 50if (true) { 51 let y; 52 assertEquals(undefined, y); 53} 54 55// 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. 58function TestLocalThrows(str, expect) { 59 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect); 60} 61 62function TestLocalDoesNotThrow(str) { 63 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()"); 64} 65 66// Test let declarations in statement positions. 67TestLocalThrows("if (true) let x;", SyntaxError); 68TestLocalThrows("if (true) {} else let x;", SyntaxError); 69TestLocalThrows("do let x; while (false)", SyntaxError); 70TestLocalThrows("while (false) let x;", SyntaxError); 71TestLocalThrows("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); 75 76// 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. 102TestLocalDoesNotThrow("if (true) var x;"); 103TestLocalDoesNotThrow("if (true) {} else var x;"); 104TestLocalDoesNotThrow("do var x; while (false)"); 105TestLocalDoesNotThrow("while (false) var x;"); 106TestLocalDoesNotThrow("label: var x;"); 107TestLocalDoesNotThrow("for (;false;) var x;"); 108TestLocalDoesNotThrow("switch (true) { case true: var x; }"); 109TestLocalDoesNotThrow("switch (true) { default: var x; }"); 110 111// 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 133// Test function declarations in source element and 134// sloppy statement positions. 135function f() { 136 // Sloppy source element positions. 137 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); 160