OLD | NEW |
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 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-scoping --allow-natives-syntax | 28 // Flags: --harmony-scoping --allow-natives-syntax |
29 | 29 |
30 // TODO(ES6): properly activate extended mode | 30 // TODO(ES6): properly activate extended mode |
31 "use strict"; | 31 "use strict"; |
32 | 32 |
33 // Check that the following functions are optimizable. | 33 // Check that the following functions are optimizable. |
34 var functions = [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, | 34 var functions = [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, |
35 f15, f16, f17, f18, f19, f20, f21, f22, f23 ]; | 35 f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, |
| 36 f27, f28, f29, f30, f31, f32, f33]; |
36 | 37 |
37 for (var i = 0; i < functions.length; ++i) { | 38 for (var i = 0; i < functions.length; ++i) { |
38 var func = functions[i]; | 39 var func = functions[i]; |
39 print("Testing:"); | 40 print("Testing:"); |
40 print(func); | 41 print(func); |
41 for (var j = 0; j < 10; ++j) { | 42 for (var j = 0; j < 10; ++j) { |
42 func(12); | 43 func(12); |
43 } | 44 } |
44 %OptimizeFunctionOnNextCall(func); | 45 %OptimizeFunctionOnNextCall(func); |
45 func(12); | 46 func(12); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 x = 1; | 150 x = 1; |
150 (function() { x; }); | 151 (function() { x; }); |
151 } | 152 } |
152 | 153 |
153 function f23() { | 154 function f23() { |
154 function x() { } | 155 function x() { } |
155 x = 1; | 156 x = 1; |
156 (function() { x; }); | 157 (function() { x; }); |
157 } | 158 } |
158 | 159 |
| 160 function f24() { |
| 161 let x = 1; |
| 162 { |
| 163 let x = 2; |
| 164 { |
| 165 let x = 3; |
| 166 assertEquals(3, x); |
| 167 } |
| 168 assertEquals(2, x); |
| 169 } |
| 170 assertEquals(1, x); |
| 171 } |
| 172 |
| 173 function f25() { |
| 174 { |
| 175 let x = 2; |
| 176 L: { |
| 177 let x = 3; |
| 178 assertEquals(3, x); |
| 179 break L; |
| 180 assertTrue(false); |
| 181 } |
| 182 assertEquals(2, x); |
| 183 } |
| 184 assertTrue(true); |
| 185 } |
| 186 |
| 187 function f26() { |
| 188 { |
| 189 let x = 1; |
| 190 L: { |
| 191 let x = 2; |
| 192 { |
| 193 let x = 3; |
| 194 assertEquals(3, x); |
| 195 break L; |
| 196 assertTrue(false); |
| 197 } |
| 198 assertTrue(false); |
| 199 } |
| 200 assertEquals(1, x); |
| 201 } |
| 202 } |
| 203 |
| 204 |
| 205 function f27() { |
| 206 do { |
| 207 let x = 4; |
| 208 assertEquals(4,x); |
| 209 { |
| 210 let x = 5; |
| 211 assertEquals(5, x); |
| 212 continue; |
| 213 assertTrue(false); |
| 214 } |
| 215 } while (false); |
| 216 } |
| 217 |
| 218 function f28() { |
| 219 label: for (var i = 0; i < 10; ++i) { |
| 220 let x = 'middle' + i; |
| 221 for (var j = 0; j < 10; ++j) { |
| 222 let x = 'inner' + j; |
| 223 continue label; |
| 224 } |
| 225 } |
| 226 } |
| 227 |
| 228 function f29() { |
| 229 // Verify that the context is correctly set in the stack frame after exiting |
| 230 // from with. |
| 231 |
| 232 let x = 'outer'; |
| 233 label: { |
| 234 let x = 'inner'; |
| 235 break label; |
| 236 } |
| 237 f(); // The context could be restored from the stack after the call. |
| 238 assertEquals('outer', x); |
| 239 |
| 240 function f() { |
| 241 assertEquals('outer', x); |
| 242 }; |
| 243 } |
| 244 |
| 245 function f30() { |
| 246 let x = 'outer'; |
| 247 for (var i = 0; i < 10; ++i) { |
| 248 let x = 'inner'; |
| 249 continue; |
| 250 } |
| 251 f(); |
| 252 assertEquals('outer', x); |
| 253 |
| 254 function f() { |
| 255 assertEquals('outer', x); |
| 256 }; |
| 257 } |
| 258 |
| 259 function f31() { |
| 260 { |
| 261 let x = 'outer'; |
| 262 label: for (var i = 0; assertEquals('outer', x), i < 10; ++i) { |
| 263 let x = 'middle' + i; |
| 264 { |
| 265 let x = 'inner' + j; |
| 266 continue label; |
| 267 } |
| 268 } |
| 269 assertEquals('outer', x); |
| 270 } |
| 271 } |
| 272 |
| 273 var c = true; |
| 274 |
| 275 function f32() { |
| 276 { |
| 277 let x = 'outer'; |
| 278 L: { |
| 279 { |
| 280 let x = 'inner'; |
| 281 if (c) { |
| 282 break L; |
| 283 } |
| 284 } |
| 285 foo(); |
| 286 } |
| 287 } |
| 288 |
| 289 function foo() { |
| 290 return 'bar'; |
| 291 } |
| 292 } |
| 293 |
| 294 function f33() { |
| 295 { |
| 296 let x = 'outer'; |
| 297 L: { |
| 298 { |
| 299 let x = 'inner'; |
| 300 if (c) { |
| 301 break L; |
| 302 } |
| 303 foo(); |
| 304 } |
| 305 } |
| 306 } |
| 307 |
| 308 function foo() { |
| 309 return 'bar'; |
| 310 } |
| 311 } |
| 312 |
| 313 function TestThrow() { |
| 314 function f() { |
| 315 let x = 'outer'; |
| 316 { |
| 317 let x = 'inner'; |
| 318 throw x; |
| 319 } |
| 320 } |
| 321 for (var i = 0; i < 5; i++) { |
| 322 try { |
| 323 f(); |
| 324 } catch (e) { |
| 325 assertEquals('inner', e); |
| 326 } |
| 327 } |
| 328 %OptimizeFunctionOnNextCall(f); |
| 329 try { |
| 330 f(); |
| 331 } catch (e) { |
| 332 assertEquals('inner', e); |
| 333 } |
| 334 assertOptimized(f); |
| 335 } |
| 336 |
| 337 TestThrow(); |
159 | 338 |
160 // Test that temporal dead zone semantics for function and block scoped | 339 // Test that temporal dead zone semantics for function and block scoped |
161 // let bindings are handled by the optimizing compiler. | 340 // let bindings are handled by the optimizing compiler. |
162 | 341 |
163 function TestFunctionLocal(s) { | 342 function TestFunctionLocal(s) { |
164 'use strict'; | 343 'use strict'; |
165 var func = eval("(function baz(){" + s + "; })"); | 344 var func = eval("(function baz(){" + s + "; })"); |
166 print("Testing:"); | 345 print("Testing:"); |
167 print(func); | 346 print(func); |
168 for (var i = 0; i < 5; ++i) { | 347 for (var i = 0; i < 5; ++i) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 try { | 380 try { |
202 print("call"); | 381 print("call"); |
203 func(); | 382 func(); |
204 assertUnreachable(); | 383 assertUnreachable(); |
205 } catch (e) { | 384 } catch (e) { |
206 print("catch"); | 385 print("catch"); |
207 assertInstanceof(e, ReferenceError); | 386 assertInstanceof(e, ReferenceError); |
208 } | 387 } |
209 } | 388 } |
210 | 389 |
| 390 function TestBlockLocal(s) { |
| 391 'use strict'; |
| 392 var func = eval("(function baz(){ { " + s + "; } })"); |
| 393 print("Testing:"); |
| 394 print(func); |
| 395 for (var i = 0; i < 5; ++i) { |
| 396 try { |
| 397 func(); |
| 398 assertUnreachable(); |
| 399 } catch (e) { |
| 400 assertInstanceof(e, ReferenceError); |
| 401 } |
| 402 } |
| 403 %OptimizeFunctionOnNextCall(func); |
| 404 try { |
| 405 func(); |
| 406 assertUnreachable(); |
| 407 } catch (e) { |
| 408 assertInstanceof(e, ReferenceError); |
| 409 } |
| 410 } |
| 411 |
| 412 function TestBlockContext(s) { |
| 413 'use strict'; |
| 414 var func = eval("(function baz(){ { " + s + "; (function() { x; }); } })"); |
| 415 print("Testing:"); |
| 416 print(func); |
| 417 for (var i = 0; i < 5; ++i) { |
| 418 print(i); |
| 419 try { |
| 420 func(); |
| 421 assertUnreachable(); |
| 422 } catch (e) { |
| 423 assertInstanceof(e, ReferenceError); |
| 424 } |
| 425 } |
| 426 print("optimize"); |
| 427 %OptimizeFunctionOnNextCall(func); |
| 428 try { |
| 429 print("call"); |
| 430 func(); |
| 431 assertUnreachable(); |
| 432 } catch (e) { |
| 433 print("catch"); |
| 434 assertInstanceof(e, ReferenceError); |
| 435 } |
| 436 } |
| 437 |
211 function TestAll(s) { | 438 function TestAll(s) { |
212 TestFunctionLocal(s); | 439 TestFunctionLocal(s); |
213 TestFunctionContext(s); | 440 TestFunctionContext(s); |
| 441 TestBlockLocal(s); |
| 442 TestBlockContext(s); |
214 } | 443 } |
215 | 444 |
216 // Use before initialization in declaration statement. | 445 // Use before initialization in declaration statement. |
217 TestAll('let x = x + 1'); | 446 TestAll('let x = x + 1'); |
218 TestAll('let x = x += 1'); | 447 TestAll('let x = x += 1'); |
219 TestAll('let x = x++'); | 448 TestAll('let x = x++'); |
220 TestAll('let x = ++x'); | 449 TestAll('let x = ++x'); |
221 TestAll('const x = x + 1'); | 450 TestAll('const x = x + 1'); |
222 | 451 |
223 // Use before initialization in prior statement. | 452 // Use before initialization in prior statement. |
224 TestAll('x + 1; let x;'); | 453 TestAll('x + 1; let x;'); |
225 TestAll('x = 1; let x;'); | 454 TestAll('x = 1; let x;'); |
226 TestAll('x += 1; let x;'); | 455 TestAll('x += 1; let x;'); |
227 TestAll('++x; let x;'); | 456 TestAll('++x; let x;'); |
228 TestAll('x++; let x;'); | 457 TestAll('x++; let x;'); |
229 TestAll('let y = x; const x = 1;'); | 458 TestAll('let y = x; const x = 1;'); |
230 | 459 |
231 | 460 |
232 function f(x, b) { | 461 function f(x) { |
233 let y = (b ? y : x) + 42; | 462 let y = x + 42; |
234 return y; | 463 return y; |
235 } | 464 } |
236 | 465 |
237 function g(x, b) { | 466 function g(x) { |
238 { | 467 { |
239 let y = (b ? y : x) + 42; | 468 let y = x + 42; |
240 return y; | 469 return y; |
241 } | 470 } |
242 } | 471 } |
243 | 472 |
244 for (var i=0; i<10; i++) { | 473 for (var i=0; i<10; i++) { |
245 f(i, false); | 474 f(i); |
246 g(i, false); | 475 g(i); |
247 } | 476 } |
248 | 477 |
249 %OptimizeFunctionOnNextCall(f); | 478 %OptimizeFunctionOnNextCall(f); |
250 %OptimizeFunctionOnNextCall(g); | 479 %OptimizeFunctionOnNextCall(g); |
251 | 480 |
252 try { | 481 f(12); |
253 f(42, true); | 482 g(12); |
254 } catch (e) { | |
255 assertInstanceof(e, ReferenceError); | |
256 } | |
257 | 483 |
258 try { | 484 assertTrue(%GetOptimizationStatus(f) != 2); |
259 g(42, true); | 485 assertTrue(%GetOptimizationStatus(g) != 2); |
260 } catch (e) { | |
261 assertInstanceof(e, ReferenceError); | |
262 } | |
OLD | NEW |