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
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.
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.
28 // Test let declarations in various settings.
44 assertEquals(undefined, x);
50 assertEquals(undefined, y);
53 // Invalid declarations are early errors in harmony mode and thus should trigger
54 // an exception in eval code during parsing, before even compiling or executing
55 // the code. Thus the generated function is not called here.
56 function TestLocalThrows(str, expect) {
57 assertThrows("(function(arg){ 'use strict'; " + str + "})", expect);
60 function TestLocalDoesNotThrow(str) {
61 assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()");
64 // Test let declarations in statement positions.
65 TestLocalThrows("if (true) let x;", SyntaxError);
66 TestLocalThrows("if (true) {} else let x;", SyntaxError);
67 TestLocalThrows("do let x; while (false)", SyntaxError);
68 TestLocalThrows("while (false) let x;", SyntaxError);
69 TestLocalThrows("label: let x;", SyntaxError);
70 TestLocalThrows("for (;false;) let x;", SyntaxError);
71 TestLocalDoesNotThrow("switch (true) { case true: let x; }");
72 TestLocalDoesNotThrow("switch (true) { default: let x; }");
74 // Test const declarations with initialisers in statement positions.
75 TestLocalThrows("if (true) const x = 1;", SyntaxError);
76 TestLocalThrows("if (true) {} else const x = 1;", SyntaxError);
77 TestLocalThrows("do const x = 1; while (false)", SyntaxError);
78 TestLocalThrows("while (false) const x = 1;", SyntaxError);
79 TestLocalThrows("label: const x = 1;", SyntaxError);
80 TestLocalThrows("for (;false;) const x = 1;", SyntaxError);
81 TestLocalDoesNotThrow("switch (true) { case true: const x = 1; }");
82 TestLocalDoesNotThrow("switch (true) { default: const x = 1; }");
84 // Test const declarations without initialisers.
85 TestLocalThrows("const x;", SyntaxError);
86 TestLocalThrows("const x = 1, y;", SyntaxError);
87 TestLocalThrows("const x, y = 1;", SyntaxError);
89 // Test const declarations without initialisers in statement positions.
90 TestLocalThrows("if (true) const x;", SyntaxError);
91 TestLocalThrows("if (true) {} else const x;", SyntaxError);
92 TestLocalThrows("do const x; while (false)", SyntaxError);
93 TestLocalThrows("while (false) const x;", SyntaxError);
94 TestLocalThrows("label: const x;", SyntaxError);
95 TestLocalThrows("for (;false;) const x;", SyntaxError);
96 TestLocalThrows("switch (true) { case true: const x; }", SyntaxError);
97 TestLocalThrows("switch (true) { default: const x; }", SyntaxError);
99 // Test var declarations in statement positions.
100 TestLocalDoesNotThrow("if (true) var x;");
101 TestLocalDoesNotThrow("if (true) {} else var x;");
102 TestLocalDoesNotThrow("do var x; while (false)");
103 TestLocalDoesNotThrow("while (false) var x;");
104 TestLocalDoesNotThrow("label: var x;");
105 TestLocalDoesNotThrow("for (;false;) var x;");
106 TestLocalDoesNotThrow("switch (true) { case true: var x; }");
107 TestLocalDoesNotThrow("switch (true) { default: var x; }");
109 // Test that redeclarations of functions are only allowed in outermost scope.
110 TestLocalThrows("{ let f; var f; }");
111 TestLocalThrows("{ var f; let f; }");
112 TestLocalThrows("{ function f() {} let f; }");
113 TestLocalThrows("{ let f; function f() {} }");
114 TestLocalThrows("{ function f() {} var f; }");
115 TestLocalThrows("{ var f; function f() {} }");
116 TestLocalThrows("{ function f() {} function f() {} }");
117 TestLocalThrows("function f() {} let f;");
118 TestLocalThrows("let f; function f() {}");
119 TestLocalDoesNotThrow("function arg() {}");
120 TestLocalDoesNotThrow("function f() {} var f;");
121 TestLocalDoesNotThrow("var f; function f() {}");
122 TestLocalDoesNotThrow("function f() {} function f() {}");
125 function f() { return 1 }
128 assertEquals(1, g(function() { return 2 }))
131 // Test function declarations in source element and
132 // sloppy statement positions.
134 // Sloppy source element positions.
137 // Strict source element positions.
149 // Test function declarations in statement position in strict mode.
150 TestLocalThrows("function f() { if (true) function g() {} }", SyntaxError);
151 TestLocalThrows("function f() { if (true) {} else function g() {} }", SyntaxError);
152 TestLocalThrows("function f() { do function g() {} while (false) }", SyntaxError);
153 TestLocalThrows("function f() { while (false) function g() {} }", SyntaxError);
154 TestLocalThrows("function f() { label: function g() {} }", SyntaxError);
155 TestLocalThrows("function f() { for (;false;) function g() {} }", SyntaxError);
156 TestLocalDoesNotThrow("function f() { switch (true) { case true: function g() {} } }");
157 TestLocalDoesNotThrow("function f() { switch (true) { default: function g() {} } }");