block-scoping.js 7.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax --opt
29 30
// Test functionality of block scopes.

31 32
"use strict";

33 34 35 36 37 38 39 40 41
// Hoisting of var declarations.
function f1() {
  {
    var x = 1;
    var y;
  }
  assertEquals(1, x)
  assertEquals(undefined, y)
}
42 43
for (var j = 0; j < 5; ++j) f1();
%OptimizeFunctionOnNextCall(f1);
44
f1();
45
assertOptimized(f1);
46 47

// Dynamic lookup in and through block contexts.
48 49
function f2(one) {
  var x = one + 1;
50
  let y = one + 2;
51
  const u = one + 4;
52
  class a { static foo() { return one + 6; } }
53 54
  {
    let z = one + 3;
55
    const v = one + 5;
56
    class b { static foo() { return one + 7; } }
57 58
    assertEquals(1, eval('one'));
    assertEquals(2, eval('x'));
59 60
    assertEquals(3, eval('y'));
    assertEquals(4, eval('z'));
61 62
    assertEquals(5, eval('u'));
    assertEquals(6, eval('v'));
63 64
    assertEquals(7, eval('a.foo()'));
    assertEquals(8, eval('b.foo()'));
65 66 67
  }
}

68
f2(1);
69 70 71 72 73

// Lookup in and through block contexts.
function f3(one) {
  var x = one + 1;
  let y = one + 2;
74
  const u = one + 4;
75
  class a { static foo() { return one + 6; } }
76 77
  {
    let z = one + 3;
78
    const v = one + 5;
79
    class b { static foo() { return one + 7; } }
80 81 82 83
    assertEquals(1, one);
    assertEquals(2, x);
    assertEquals(3, y);
    assertEquals(4, z);
84 85
    assertEquals(5, u);
    assertEquals(6, v);
86 87
    assertEquals(7, a.foo());
    assertEquals(8, b.foo());
88 89
  }
}
90 91
for (var j = 0; j < 5; ++j) f3(1);
%OptimizeFunctionOnNextCall(f3);
92
f3(1);
93

94 95 96 97 98 99


// Dynamic lookup from closure.
function f4(one) {
  var x = one + 1;
  let y = one + 2;
100
  const u = one + 4;
101
  class a { static foo() { return one + 6; } }
102 103
  {
    let z = one + 3;
104
    const v = one + 5;
105
    class b { static foo() { return one + 7; } }
106 107 108 109 110
    function f() {
      assertEquals(1, eval('one'));
      assertEquals(2, eval('x'));
      assertEquals(3, eval('y'));
      assertEquals(4, eval('z'));
111 112
      assertEquals(5, eval('u'));
      assertEquals(6, eval('v'));
113 114
      assertEquals(7, eval('a.foo()'));
      assertEquals(8, eval('b.foo()'));
arv's avatar
arv committed
115 116
    }
    f();
117 118 119 120 121 122 123 124 125
  }
}
f4(1);


// Lookup from closure.
function f5(one) {
  var x = one + 1;
  let y = one + 2;
126
  const u = one + 4;
127
  class a { static foo() { return one + 6; } }
128 129
  {
    let z = one + 3;
130
    const v = one + 5;
131
    class b { static foo() { return one + 7; } }
132 133 134 135 136
    function f() {
      assertEquals(1, one);
      assertEquals(2, x);
      assertEquals(3, y);
      assertEquals(4, z);
137 138
      assertEquals(5, u);
      assertEquals(6, v);
139 140
      assertEquals(7, a.foo());
      assertEquals(8, b.foo());
arv's avatar
arv committed
141 142
    }
    f();
143 144 145 146 147 148 149 150
  }
}
f5(1);


// Return from block.
function f6() {
  let x = 1;
151
  const u = 3;
152 153
  {
    let y = 2;
154
    const v = 4;
155 156 157 158 159 160 161 162 163 164 165
    return x + y;
  }
}
assertEquals(3, f6(6));


// Variable shadowing and lookup.
function f7(a) {
  let b = 1;
  var c = 1;
  var d = 1;
166
  const e = 1;
167 168
  class f { static foo() { return 1; } }
  { // let variables shadowing argument, let, const, class and var variables
169 170 171
    let a = 2;
    let b = 2;
    let c = 2;
172
    let e = 2;
173
    let f = 2;
174 175 176 177
    assertEquals(2,a);
    assertEquals(2,b);
    assertEquals(2,c);
    assertEquals(2,e);
178
    assertEquals(2,f);
179 180 181 182 183 184
  }
  { // const variables shadowing argument, let, const and var variables
    const a = 2;
    const b = 2;
    const c = 2;
    const e = 2;
185
    const f = 2;
186 187 188
    assertEquals(2,a);
    assertEquals(2,b);
    assertEquals(2,c);
189
    assertEquals(2,e);
190 191 192 193 194 195 196 197 198 199 200 201 202 203
    assertEquals(2,f);
  }
  { // class variables shadowing argument, let, const and var variables
    class a { static foo() { return 2; } }
    class b { static foo() { return 2; } }
    class c { static foo() { return 2; } }
    class d { static foo() { return 2; } }
    class e { static foo() { return 2; } }
    class f { static foo() { return 2; } }
    assertEquals(2,a.foo());
    assertEquals(2,b.foo());
    assertEquals(2,c.foo());
    assertEquals(2,e.foo());
    assertEquals(2,f.foo());
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  }
  try {
    throw 'stuff1';
  } catch (a) {
    assertEquals('stuff1',a);
    // catch variable shadowing argument
    a = 2;
    assertEquals(2,a);
    {
      // let variable shadowing catch variable
      let a = 3;
      assertEquals(3,a);
      try {
        throw 'stuff2';
      } catch (a) {
        assertEquals('stuff2',a);
        // catch variable shadowing let variable
        a = 4;
        assertEquals(4,a);
      }
      assertEquals(3,a);
    }
    assertEquals(2,a);
  }
  try {
    throw 'stuff3';
  } catch (c) {
    // catch variable shadowing var variable
    assertEquals('stuff3',c);
233 234 235 236 237 238
    {
      // const variable shadowing catch variable
      const c = 3;
      assertEquals(3,c);
    }
    assertEquals('stuff3',c);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    try {
      throw 'stuff4';
    } catch(c) {
      assertEquals('stuff4',c);
      // catch variable shadowing catch variable
      c = 3;
      assertEquals(3,c);
    }
    (function(c) {
      // argument shadowing catch variable
      c = 3;
      assertEquals(3,c);
    })();
    assertEquals('stuff3', c);
    (function() {
      // var variable shadowing catch variable
      var c = 3;
    })();
    assertEquals('stuff3', c);
    c = 2;
  }
  assertEquals(1,c);
261 262
  (function(a,b,c,e,f) {
    // arguments shadowing argument, let, const, class and var variable
263 264 265
    a = 2;
    b = 2;
    c = 2;
266
    e = 2;
267
    f = 2;
268 269 270
    assertEquals(2,a);
    assertEquals(2,b);
    assertEquals(2,c);
271
    assertEquals(2,e);
272
    assertEquals(2,f);
273 274 275 276 277 278 279
    // var variable shadowing var variable
    var d = 2;
  })(1,1);
  assertEquals(1,a);
  assertEquals(1,b);
  assertEquals(1,c);
  assertEquals(1,d);
280
  assertEquals(1,e);
281
  assertEquals(1,f.foo());
282 283 284 285
}
f7(1);


286 287
// Ensure let and const variables are block local
// and var variables function local.
288 289 290
function f8() {
  var let_accessors = [];
  var var_accessors = [];
291
  var const_accessors = [];
292
  var class_accessors = [];
293 294 295
  for (var i = 0; i < 10; i++) {
    let x = i;
    var y = i;
296
    const z = i;
297
    class a { static foo() { return x; } }
298 299
    let_accessors[i] = function() { return x; }
    var_accessors[i] = function() { return y; }
300
    const_accessors[i] = function() { return z; }
301
    class_accessors[i] = function() { return a; }
302 303 304 305 306
  }
  for (var j = 0; j < 10; j++) {
    y = j + 10;
    assertEquals(j, let_accessors[j]());
    assertEquals(y, var_accessors[j]());
307
    assertEquals(j, const_accessors[j]());
308
    assertEquals(j, class_accessors[j]().foo());
309 310 311
  }
}
f8();