Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: test/mjsunit/harmony/block-scoping.js

Issue 7992005: Block scoped const variables. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Comments, function variable mode, preparser. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/mjsunit/harmony/block-let-semantics.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 26 matching lines...) Expand all
37 assertEquals(1, x) 37 assertEquals(1, x)
38 assertEquals(undefined, y) 38 assertEquals(undefined, y)
39 } 39 }
40 f1(); 40 f1();
41 41
42 42
43 // Dynamic lookup in and through block contexts. 43 // Dynamic lookup in and through block contexts.
44 function f2(one) { 44 function f2(one) {
45 var x = one + 1; 45 var x = one + 1;
46 let y = one + 2; 46 let y = one + 2;
47 const u = one + 4;
47 { 48 {
48 let z = one + 3; 49 let z = one + 3;
50 const v = one + 5;
49 assertEquals(1, eval('one')); 51 assertEquals(1, eval('one'));
50 assertEquals(2, eval('x')); 52 assertEquals(2, eval('x'));
51 assertEquals(3, eval('y')); 53 assertEquals(3, eval('y'));
52 assertEquals(4, eval('z')); 54 assertEquals(4, eval('z'));
55 assertEquals(5, eval('u'));
56 assertEquals(6, eval('v'));
53 } 57 }
54 } 58 }
55 f2(1); 59 f2(1);
56 60
57 61
58 // Lookup in and through block contexts. 62 // Lookup in and through block contexts.
59 function f3(one) { 63 function f3(one) {
60 var x = one + 1; 64 var x = one + 1;
61 let y = one + 2; 65 let y = one + 2;
66 const u = one + 4;
62 { 67 {
63 let z = one + 3; 68 let z = one + 3;
69 const v = one + 5;
64 assertEquals(1, one); 70 assertEquals(1, one);
65 assertEquals(2, x); 71 assertEquals(2, x);
66 assertEquals(3, y); 72 assertEquals(3, y);
67 assertEquals(4, z); 73 assertEquals(4, z);
74 assertEquals(5, u);
75 assertEquals(6, v);
76
68 } 77 }
69 } 78 }
70 f3(1); 79 f3(1);
71 80
72 81
73 // Dynamic lookup from closure. 82 // Dynamic lookup from closure.
74 function f4(one) { 83 function f4(one) {
75 var x = one + 1; 84 var x = one + 1;
76 let y = one + 2; 85 let y = one + 2;
86 const u = one + 4;
77 { 87 {
78 let z = one + 3; 88 let z = one + 3;
89 const v = one + 5;
79 function f() { 90 function f() {
80 assertEquals(1, eval('one')); 91 assertEquals(1, eval('one'));
81 assertEquals(2, eval('x')); 92 assertEquals(2, eval('x'));
82 assertEquals(3, eval('y')); 93 assertEquals(3, eval('y'));
83 assertEquals(4, eval('z')); 94 assertEquals(4, eval('z'));
95 assertEquals(5, eval('u'));
96 assertEquals(6, eval('v'));
84 }; 97 };
85 } 98 }
86 } 99 }
87 f4(1); 100 f4(1);
88 101
89 102
90 // Lookup from closure. 103 // Lookup from closure.
91 function f5(one) { 104 function f5(one) {
92 var x = one + 1; 105 var x = one + 1;
93 let y = one + 2; 106 let y = one + 2;
107 const u = one + 4;
94 { 108 {
95 let z = one + 3; 109 let z = one + 3;
110 const v = one + 5;
96 function f() { 111 function f() {
97 assertEquals(1, one); 112 assertEquals(1, one);
98 assertEquals(2, x); 113 assertEquals(2, x);
99 assertEquals(3, y); 114 assertEquals(3, y);
100 assertEquals(4, z); 115 assertEquals(4, z);
116 assertEquals(5, u);
117 assertEquals(6, v);
101 }; 118 };
102 } 119 }
103 } 120 }
104 f5(1); 121 f5(1);
105 122
106 123
107 // Return from block. 124 // Return from block.
108 function f6() { 125 function f6() {
109 let x = 1; 126 let x = 1;
127 const u = 3;
110 { 128 {
111 let y = 2; 129 let y = 2;
130 const v = 4;
112 return x + y; 131 return x + y;
113 } 132 }
114 } 133 }
115 assertEquals(3, f6(6)); 134 assertEquals(3, f6(6));
116 135
117 136
118 // Variable shadowing and lookup. 137 // Variable shadowing and lookup.
119 function f7(a) { 138 function f7(a) {
120 let b = 1; 139 let b = 1;
121 var c = 1; 140 var c = 1;
122 var d = 1; 141 var d = 1;
123 { // let variables shadowing argument, let and var variables 142 const e = 1;
143 { // let variables shadowing argument, let, const and var variables
124 let a = 2; 144 let a = 2;
125 let b = 2; 145 let b = 2;
126 let c = 2; 146 let c = 2;
147 let e = 2;
127 assertEquals(2,a); 148 assertEquals(2,a);
128 assertEquals(2,b); 149 assertEquals(2,b);
129 assertEquals(2,c); 150 assertEquals(2,c);
151 assertEquals(2,e);
152 }
153 { // const variables shadowing argument, let, const and var variables
154 const a = 2;
155 const b = 2;
156 const c = 2;
157 const e = 2;
158 assertEquals(2,a);
159 assertEquals(2,b);
160 assertEquals(2,c);
161 assertEquals(2,e);
130 } 162 }
131 try { 163 try {
132 throw 'stuff1'; 164 throw 'stuff1';
133 } catch (a) { 165 } catch (a) {
134 assertEquals('stuff1',a); 166 assertEquals('stuff1',a);
135 // catch variable shadowing argument 167 // catch variable shadowing argument
136 a = 2; 168 a = 2;
137 assertEquals(2,a); 169 assertEquals(2,a);
138 { 170 {
139 // let variable shadowing catch variable 171 // let variable shadowing catch variable
140 let a = 3; 172 let a = 3;
141 assertEquals(3,a); 173 assertEquals(3,a);
142 try { 174 try {
143 throw 'stuff2'; 175 throw 'stuff2';
144 } catch (a) { 176 } catch (a) {
145 assertEquals('stuff2',a); 177 assertEquals('stuff2',a);
146 // catch variable shadowing let variable 178 // catch variable shadowing let variable
147 a = 4; 179 a = 4;
148 assertEquals(4,a); 180 assertEquals(4,a);
149 } 181 }
150 assertEquals(3,a); 182 assertEquals(3,a);
151 } 183 }
152 assertEquals(2,a); 184 assertEquals(2,a);
153 } 185 }
154 try { 186 try {
155 throw 'stuff3'; 187 throw 'stuff3';
156 } catch (c) { 188 } catch (c) {
157 // catch variable shadowing var variable 189 // catch variable shadowing var variable
158 assertEquals('stuff3',c); 190 assertEquals('stuff3',c);
191 {
192 // const variable shadowing catch variable
193 const c = 3;
194 assertEquals(3,c);
195 }
196 assertEquals('stuff3',c);
159 try { 197 try {
160 throw 'stuff4'; 198 throw 'stuff4';
161 } catch(c) { 199 } catch(c) {
162 assertEquals('stuff4',c); 200 assertEquals('stuff4',c);
163 // catch variable shadowing catch variable 201 // catch variable shadowing catch variable
164 c = 3; 202 c = 3;
165 assertEquals(3,c); 203 assertEquals(3,c);
166 } 204 }
167 (function(c) { 205 (function(c) {
168 // argument shadowing catch variable 206 // argument shadowing catch variable
169 c = 3; 207 c = 3;
170 assertEquals(3,c); 208 assertEquals(3,c);
171 })(); 209 })();
172 assertEquals('stuff3', c); 210 assertEquals('stuff3', c);
173 (function() { 211 (function() {
174 // var variable shadowing catch variable 212 // var variable shadowing catch variable
175 var c = 3; 213 var c = 3;
176 })(); 214 })();
177 assertEquals('stuff3', c); 215 assertEquals('stuff3', c);
178 c = 2; 216 c = 2;
179 } 217 }
180 assertEquals(1,c); 218 assertEquals(1,c);
181 (function(a,b,c) { 219 (function(a,b,c,e) {
182 // arguments shadowing argument, let and var variable 220 // arguments shadowing argument, let, const and var variable
183 a = 2; 221 a = 2;
184 b = 2; 222 b = 2;
185 c = 2; 223 c = 2;
224 e = 2;
186 assertEquals(2,a); 225 assertEquals(2,a);
187 assertEquals(2,b); 226 assertEquals(2,b);
188 assertEquals(2,c); 227 assertEquals(2,c);
228 assertEquals(2,e);
189 // var variable shadowing var variable 229 // var variable shadowing var variable
190 var d = 2; 230 var d = 2;
191 })(1,1); 231 })(1,1);
192 assertEquals(1,a); 232 assertEquals(1,a);
193 assertEquals(1,b); 233 assertEquals(1,b);
194 assertEquals(1,c); 234 assertEquals(1,c);
195 assertEquals(1,d); 235 assertEquals(1,d);
236 assertEquals(1,e);
196 } 237 }
197 f7(1); 238 f7(1);
198 239
199 240
200 // Ensure let variables are block local and var variables function local. 241 // Ensure let and const variables are block local
242 // and var variables function local.
201 function f8() { 243 function f8() {
202 var let_accessors = []; 244 var let_accessors = [];
203 var var_accessors = []; 245 var var_accessors = [];
246 var const_accessors = [];
204 for (var i = 0; i < 10; i++) { 247 for (var i = 0; i < 10; i++) {
205 let x = i; 248 let x = i;
206 var y = i; 249 var y = i;
250 const z = i;
207 let_accessors[i] = function() { return x; } 251 let_accessors[i] = function() { return x; }
208 var_accessors[i] = function() { return y; } 252 var_accessors[i] = function() { return y; }
253 const_accessors[i] = function() { return z; }
209 } 254 }
210 for (var j = 0; j < 10; j++) { 255 for (var j = 0; j < 10; j++) {
211 y = j + 10; 256 y = j + 10;
212 assertEquals(j, let_accessors[j]()); 257 assertEquals(j, let_accessors[j]());
213 assertEquals(y, var_accessors[j]()); 258 assertEquals(y, var_accessors[j]());
259 assertEquals(j, const_accessors[j]());
214 } 260 }
215 } 261 }
216 f8(); 262 f8();
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/block-let-semantics.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698