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: --allow-natives-syntax
29 // Test functionality of block scopes.
33 // Hoisting of var declarations.
40 assertEquals(undefined, y)
42 for (var j = 0; j < 5; ++j) f1();
43 %OptimizeFunctionOnNextCall(f1);
45 assertTrue(%GetOptimizationStatus(f1) != 2);
47 // Dynamic lookup in and through block contexts.
55 assertEquals(1, eval('one'));
56 assertEquals(2, eval('x'));
57 assertEquals(3, eval('y'));
58 assertEquals(4, eval('z'));
59 assertEquals(5, eval('u'));
60 assertEquals(6, eval('v'));
66 // Lookup in and through block contexts.
82 for (var j = 0; j < 5; ++j) f3(1);
83 %OptimizeFunctionOnNextCall(f3);
85 assertTrue(%GetOptimizationStatus(f3) != 2);
89 // Dynamic lookup from closure.
98 assertEquals(1, eval('one'));
99 assertEquals(2, eval('x'));
100 assertEquals(3, eval('y'));
101 assertEquals(4, eval('z'));
102 assertEquals(5, eval('u'));
103 assertEquals(6, eval('v'));
111 // Lookup from closure.
120 assertEquals(1, one);
133 // Return from block.
143 assertEquals(3, f6(6));
146 // Variable shadowing and lookup.
152 { // let variables shadowing argument, let, const and var variables
162 { // const variables shadowing argument, let, const and var variables
175 assertEquals('stuff1',a);
176 // catch variable shadowing argument
180 // let variable shadowing catch variable
186 assertEquals('stuff2',a);
187 // catch variable shadowing let variable
198 // catch variable shadowing var variable
199 assertEquals('stuff3',c);
201 // const variable shadowing catch variable
205 assertEquals('stuff3',c);
209 assertEquals('stuff4',c);
210 // catch variable shadowing catch variable
215 // argument shadowing catch variable
219 assertEquals('stuff3', c);
221 // var variable shadowing catch variable
224 assertEquals('stuff3', c);
229 // arguments shadowing argument, let, const and var variable
238 // var variable shadowing var variable
250 // Ensure let and const variables are block local
251 // and var variables function local.
253 var let_accessors = [];
254 var var_accessors = [];
255 var const_accessors = [];
256 for (var i = 0; i < 10; i++) {
260 let_accessors[i] = function() { return x; }
261 var_accessors[i] = function() { return y; }
262 const_accessors[i] = function() { return z; }
264 for (var j = 0; j < 10; j++) {
266 assertEquals(j, let_accessors[j]());
267 assertEquals(y, var_accessors[j]());
268 assertEquals(j, const_accessors[j]());