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-sloppy --no-legacy-const --harmony-sloppy-let --harmony-sloppy-function
29
30// Test temporal dead zone semantics of let bound variables in
31// function and block scopes.
32
33function TestFunctionLocal(s) {
34  try {
35    eval("(function(){" + s + "; })")();
36  } catch (e) {
37    assertInstanceof(e, ReferenceError);
38    return;
39  }
40  assertUnreachable();
41}
42
43function TestBlockLocal(s,e) {
44  try {
45    eval("(function(){ {" + s + ";} })")();
46  } catch (e) {
47    assertInstanceof(e, ReferenceError);
48    return;
49  }
50  assertUnreachable();
51}
52
53
54function TestAll(s) {
55  TestBlockLocal(s);
56  TestFunctionLocal(s);
57}
58
59// Use before initialization in declaration statement.
60TestAll('let x = x + 1');
61TestAll('let x = x += 1');
62TestAll('let x = x++');
63TestAll('let x = ++x');
64TestAll('const x = x + 1');
65
66// Use before initialization in prior statement.
67TestAll('x + 1; let x;');
68TestAll('x = 1; let x;');
69TestAll('x += 1; let x;');
70TestAll('++x; let x;');
71TestAll('x++; let x;');
72TestAll('let y = x; const x = 1;');
73TestAll('let y = x; class x {}');
74
75TestAll('f(); let x; function f() { return x + 1; }');
76TestAll('f(); let x; function f() { x = 1; }');
77TestAll('f(); let x; function f() { x += 1; }');
78TestAll('f(); let x; function f() { ++x; }');
79TestAll('f(); let x; function f() { x++; }');
80TestAll('f(); const x = 1; function f() { return x; }');
81TestAll('f(); class x { }; function f() { return x; }');
82
83TestAll('f()(); let x; function f() { return function() { return x + 1; } }');
84TestAll('f()(); let x; function f() { return function() { x = 1; } }');
85TestAll('f()(); let x; function f() { return function() { x += 1; } }');
86TestAll('f()(); let x; function f() { return function() { ++x; } }');
87TestAll('f()(); let x; function f() { return function() { x++; } }');
88TestAll('f()(); const x = 1; function f() { return function() { return x; } }');
89TestAll('f()(); class x { }; function f() { return function() { return x; } }');
90
91for (var kw of ['let x = 2', 'const x = 2', 'class x { }']) {
92  // Use before initialization with a dynamic lookup.
93  TestAll(`eval("x"); ${kw};`);
94  TestAll(`eval("x + 1;"); ${kw};`);
95  TestAll(`eval("x = 1;"); ${kw};`);
96  TestAll(`eval("x += 1;"); ${kw};`);
97  TestAll(`eval("++x;"); ${kw};`);
98  TestAll(`eval("x++;"); ${kw};`);
99
100  // Use before initialization with check for eval-shadowed bindings.
101  TestAll(`function f() { eval("var y = 2;"); x + 1; }; f(); ${kw};`);
102  TestAll(`function f() { eval("var y = 2;"); x = 1; }; f(); ${kw};`);
103  TestAll(`function f() { eval("var y = 2;"); x += 1; }; f(); ${kw};`);
104  TestAll(`function f() { eval("var y = 2;"); ++x; }; f(); ${kw};`);
105  TestAll(`function f() { eval("var y = 2;"); x++; }; f(); ${kw};`);
106}
107
108// Test that variables introduced by function declarations are created and
109// initialized upon entering a function / block scope.
110function f() {
111  {
112    assertEquals(2, g1());
113    assertEquals(2, eval("g1()"));
114
115    // block scoped function declaration
116    function g1() {
117      return 2;
118    }
119  }
120
121  assertEquals(3, g2());
122  assertEquals(3, eval("g2()"));
123  // function scoped function declaration
124  function g2() {
125    return 3;
126  }
127}
128f();
129
130// Test that a function declaration introduces a block scoped variable
131// and no function hoisting if there is a conflict.
132TestFunctionLocal('{ function k() { return 0; } }; k(); let k;');
133
134// Test that a function declaration sees the scope it resides in.
135function f2() {
136  let m, n, o, p;
137  {
138    m = g;
139    function g() {
140      return a;
141    }
142    let a = 1;
143  }
144  assertEquals(1, m());
145
146  try {
147    throw 2;
148  } catch(b) {
149    n = h;
150    function h() {
151      return b + c;
152    }
153    let c = 3;
154  }
155  assertEquals(5, n());
156
157  {
158    o = i;
159    function i() {
160      return d;
161    }
162    let d = 4;
163  }
164  assertEquals(4, o());
165
166  try {
167    throw 5;
168  } catch(e) {
169    p = j;
170    function j() {
171      return e + f;
172    }
173    let f = 6;
174  }
175  assertEquals(11, p());
176}
177f2();
178
179// Test that resolution of let bound variables works with scopes that call eval.
180function outer() {
181  function middle() {
182    function inner() {
183      return x;
184    }
185    eval("1 + 1");
186    return x + inner();
187  }
188
189  let x = 1;
190  return middle();
191}
192
193assertEquals(2, outer());
194