deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / es6 / block-scoping.js
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: --allow-natives-syntax
29 // Test functionality of block scopes.
30
31 "use strict";
32
33 // Hoisting of var declarations.
34 function f1() {
35   {
36     var x = 1;
37     var y;
38   }
39   assertEquals(1, x)
40   assertEquals(undefined, y)
41 }
42 for (var j = 0; j < 5; ++j) f1();
43 %OptimizeFunctionOnNextCall(f1);
44 f1();
45 assertTrue(%GetOptimizationStatus(f1) != 2);
46
47 // Dynamic lookup in and through block contexts.
48 function f2(one) {
49   var x = one + 1;
50   let y = one + 2;
51   const u = one + 4;
52   {
53     let z = one + 3;
54     const v = one + 5;
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'));
61   }
62 }
63
64 f2(1);
65
66 // Lookup in and through block contexts.
67 function f3(one) {
68   var x = one + 1;
69   let y = one + 2;
70   const u = one + 4;
71   {
72     let z = one + 3;
73     const v = one + 5;
74     assertEquals(1, one);
75     assertEquals(2, x);
76     assertEquals(3, y);
77     assertEquals(4, z);
78     assertEquals(5, u);
79     assertEquals(6, v);
80   }
81 }
82 for (var j = 0; j < 5; ++j) f3(1);
83 %OptimizeFunctionOnNextCall(f3);
84 f3(1);
85 assertTrue(%GetOptimizationStatus(f3) != 2);
86
87
88
89 // Dynamic lookup from closure.
90 function f4(one) {
91   var x = one + 1;
92   let y = one + 2;
93   const u = one + 4;
94   {
95     let z = one + 3;
96     const v = one + 5;
97     function f() {
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'));
104     }
105     f();
106   }
107 }
108 f4(1);
109
110
111 // Lookup from closure.
112 function f5(one) {
113   var x = one + 1;
114   let y = one + 2;
115   const u = one + 4;
116   {
117     let z = one + 3;
118     const v = one + 5;
119     function f() {
120       assertEquals(1, one);
121       assertEquals(2, x);
122       assertEquals(3, y);
123       assertEquals(4, z);
124       assertEquals(5, u);
125       assertEquals(6, v);
126     }
127     f();
128   }
129 }
130 f5(1);
131
132
133 // Return from block.
134 function f6() {
135   let x = 1;
136   const u = 3;
137   {
138     let y = 2;
139     const v = 4;
140     return x + y;
141   }
142 }
143 assertEquals(3, f6(6));
144
145
146 // Variable shadowing and lookup.
147 function f7(a) {
148   let b = 1;
149   var c = 1;
150   var d = 1;
151   const e = 1;
152   { // let variables shadowing argument, let, const and var variables
153     let a = 2;
154     let b = 2;
155     let c = 2;
156     let e = 2;
157     assertEquals(2,a);
158     assertEquals(2,b);
159     assertEquals(2,c);
160     assertEquals(2,e);
161   }
162   { // const variables shadowing argument, let, const and var variables
163     const a = 2;
164     const b = 2;
165     const c = 2;
166     const e = 2;
167     assertEquals(2,a);
168     assertEquals(2,b);
169     assertEquals(2,c);
170     assertEquals(2,e);
171   }
172   try {
173     throw 'stuff1';
174   } catch (a) {
175     assertEquals('stuff1',a);
176     // catch variable shadowing argument
177     a = 2;
178     assertEquals(2,a);
179     {
180       // let variable shadowing catch variable
181       let a = 3;
182       assertEquals(3,a);
183       try {
184         throw 'stuff2';
185       } catch (a) {
186         assertEquals('stuff2',a);
187         // catch variable shadowing let variable
188         a = 4;
189         assertEquals(4,a);
190       }
191       assertEquals(3,a);
192     }
193     assertEquals(2,a);
194   }
195   try {
196     throw 'stuff3';
197   } catch (c) {
198     // catch variable shadowing var variable
199     assertEquals('stuff3',c);
200     {
201       // const variable shadowing catch variable
202       const c = 3;
203       assertEquals(3,c);
204     }
205     assertEquals('stuff3',c);
206     try {
207       throw 'stuff4';
208     } catch(c) {
209       assertEquals('stuff4',c);
210       // catch variable shadowing catch variable
211       c = 3;
212       assertEquals(3,c);
213     }
214     (function(c) {
215       // argument shadowing catch variable
216       c = 3;
217       assertEquals(3,c);
218     })();
219     assertEquals('stuff3', c);
220     (function() {
221       // var variable shadowing catch variable
222       var c = 3;
223     })();
224     assertEquals('stuff3', c);
225     c = 2;
226   }
227   assertEquals(1,c);
228   (function(a,b,c,e) {
229     // arguments shadowing argument, let, const and var variable
230     a = 2;
231     b = 2;
232     c = 2;
233     e = 2;
234     assertEquals(2,a);
235     assertEquals(2,b);
236     assertEquals(2,c);
237     assertEquals(2,e);
238     // var variable shadowing var variable
239     var d = 2;
240   })(1,1);
241   assertEquals(1,a);
242   assertEquals(1,b);
243   assertEquals(1,c);
244   assertEquals(1,d);
245   assertEquals(1,e);
246 }
247 f7(1);
248
249
250 // Ensure let and const variables are block local
251 // and var variables function local.
252 function f8() {
253   var let_accessors = [];
254   var var_accessors = [];
255   var const_accessors = [];
256   for (var i = 0; i < 10; i++) {
257     let x = i;
258     var y = i;
259     const z = i;
260     let_accessors[i] = function() { return x; }
261     var_accessors[i] = function() { return y; }
262     const_accessors[i] = function() { return z; }
263   }
264   for (var j = 0; j < 10; j++) {
265     y = j + 10;
266     assertEquals(j, let_accessors[j]());
267     assertEquals(y, var_accessors[j]());
268     assertEquals(j, const_accessors[j]());
269   }
270 }
271 f8();