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