• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"use strict";
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.
131TestAll('{ function k() { return 0; } }; k(); ');
132
133// Test that a function declaration sees the scope it resides in.
134function f2() {
135  let m, n, o, p;
136  {
137    m = g;
138    function g() {
139      return a;
140    }
141    let a = 1;
142  }
143  assertEquals(1, m());
144
145  try {
146    throw 2;
147  } catch(b) {
148    n = h;
149    function h() {
150      return b + c;
151    }
152    let c = 3;
153  }
154  assertEquals(5, n());
155
156  {
157    o = i;
158    function i() {
159      return d;
160    }
161    let d = 4;
162  }
163  assertEquals(4, o());
164
165  try {
166    throw 5;
167  } catch(e) {
168    p = j;
169    function j() {
170      return e + f;
171    }
172    let f = 6;
173  }
174  assertEquals(11, p());
175}
176f2();
177
178// Test that resolution of let bound variables works with scopes that call eval.
179function outer() {
180  function middle() {
181    function inner() {
182      return x;
183    }
184    eval("1 + 1");
185    return x + inner();
186  }
187
188  let x = 1;
189  return middle();
190}
191
192assertEquals(2, outer());
193