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 // Flags: --harmony-scoping
32 // Test temporal dead zone semantics of let bound variables in
33 // function and block scopes.
35 function TestFunctionLocal(s) {
37 eval("(function(){" + s + "; })")();
39 assertInstanceof(e, ReferenceError);
45 function TestBlockLocal(s,e) {
47 eval("(function(){ {" + s + ";} })")();
49 assertInstanceof(e, ReferenceError);
61 // Use before initialization in declaration statement.
62 TestAll('let x = x + 1');
63 TestAll('let x = x += 1');
64 TestAll('let x = x++');
65 TestAll('let x = ++x');
66 TestAll('const x = x + 1');
68 // Use before initialization in prior statement.
69 TestAll('x + 1; let x;');
70 TestAll('x = 1; let x;');
71 TestAll('x += 1; let x;');
72 TestAll('++x; let x;');
73 TestAll('x++; let x;');
74 TestAll('let y = x; const x = 1;');
76 TestAll('f(); let x; function f() { return x + 1; }');
77 TestAll('f(); let x; function f() { x = 1; }');
78 TestAll('f(); let x; function f() { x += 1; }');
79 TestAll('f(); let x; function f() { ++x; }');
80 TestAll('f(); let x; function f() { x++; }');
81 TestAll('f(); const x = 1; function f() { return x; }');
83 TestAll('f()(); let x; function f() { return function() { return x + 1; } }');
84 TestAll('f()(); let x; function f() { return function() { x = 1; } }');
85 TestAll('f()(); let x; function f() { return function() { x += 1; } }');
86 TestAll('f()(); let x; function f() { return function() { ++x; } }');
87 TestAll('f()(); let x; function f() { return function() { x++; } }');
88 TestAll('f()(); const x = 1; function f() { return function() { return x; } }');
90 // Use before initialization with a dynamic lookup.
91 TestAll('eval("x + 1;"); let x;');
92 TestAll('eval("x = 1;"); let x;');
93 TestAll('eval("x += 1;"); let x;');
94 TestAll('eval("++x;"); let x;');
95 TestAll('eval("x++;"); let x;');
96 TestAll('eval("x"); const x = 1;');
98 // Use before initialization with check for eval-shadowed bindings.
99 TestAll('function f() { eval("var y = 2;"); x + 1; }; f(); let x;');
100 TestAll('function f() { eval("var y = 2;"); x = 1; }; f(); let x;');
101 TestAll('function f() { eval("var y = 2;"); x += 1; }; f(); let x;');
102 TestAll('function f() { eval("var y = 2;"); ++x; }; f(); let x;');
103 TestAll('function f() { eval("var y = 2;"); x++; }; f(); let x;');
105 // Test that variables introduced by function declarations are created and
106 // initialized upon entering a function / block scope.
109 assertEquals(2, g1());
110 assertEquals(2, eval("g1()"));
112 // block scoped function declaration
118 assertEquals(3, g2());
119 assertEquals(3, eval("g2()"));
120 // function scoped function declaration
127 // Test that a function declaration introduces a block scoped variable.
128 TestAll('{ function k() { return 0; } }; k(); ');
130 // Test that a function declaration sees the scope it resides in.
140 assertEquals(1, m());
151 assertEquals(5, n());
160 assertEquals(4, o());
171 assertEquals(11, p());
175 // Test that resolution of let bound variables works with scopes that call eval.
189 assertEquals(2, outer());