| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Test Annex B 3.3 semantics for functions declared in blocks in sloppy mode. | 5 // Test Annex B 3.3 semantics for functions declared in blocks in sloppy mode. |
| 6 // http://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-decl
arations-web-legacy-compatibility-semantics | 6 // http://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-decl
arations-web-legacy-compatibility-semantics |
| 7 | 7 |
| 8 (function overridingLocalFunction() { | 8 (function overridingLocalFunction() { |
| 9 var x = []; | 9 var x = []; |
| 10 assertEquals('function', typeof f); | 10 assertEquals('function', typeof f); |
| (...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 | 454 |
| 455 assertEquals(4, f()); | 455 assertEquals(4, f()); |
| 456 })(); | 456 })(); |
| 457 | 457 |
| 458 // B.3.5 interacts with B.3.3 to allow this. | 458 // B.3.5 interacts with B.3.3 to allow this. |
| 459 (function hoistingThroughSimpleCatch() { | 459 (function hoistingThroughSimpleCatch() { |
| 460 assertEquals(undefined, f); | 460 assertEquals(undefined, f); |
| 461 | 461 |
| 462 try { | 462 try { |
| 463 throw 0; | 463 throw 0; |
| 464 } catch(f) { | 464 } catch (f) { |
| 465 { | 465 { |
| 466 assertEquals(4, f()); | 466 assertEquals(4, f()); |
| 467 | 467 |
| 468 function f() { | 468 function f() { |
| 469 return 4; | 469 return 4; |
| 470 } | 470 } |
| 471 | 471 |
| 472 assertEquals(4, f()); | 472 assertEquals(4, f()); |
| 473 } | 473 } |
| 474 |
| 475 assertEquals(0, f); |
| 474 } | 476 } |
| 475 | 477 |
| 476 assertEquals(4, f()); | 478 assertEquals(4, f()); |
| 477 })(); | 479 })(); |
| 478 | 480 |
| 479 (function noHoistingThroughComplexCatch() { | 481 (function noHoistingThroughComplexCatch() { |
| 480 try { | 482 try { |
| 481 throw 0; | 483 throw 0; |
| 482 } catch({f}) { | 484 } catch ({f}) { |
| 483 { | 485 { |
| 484 assertEquals(4, f()); | 486 assertEquals(4, f()); |
| 485 | 487 |
| 486 function f() { | 488 function f() { |
| 487 return 4; | 489 return 4; |
| 488 } | 490 } |
| 489 | 491 |
| 490 assertEquals(4, f()); | 492 assertEquals(4, f()); |
| 491 } | 493 } |
| 492 } | 494 } |
| 493 | 495 |
| 494 assertThrows(()=>f, ReferenceError); | 496 assertThrows(()=>f, ReferenceError); |
| 495 })(); | 497 })(); |
| 496 | 498 |
| 499 (function hoistingThroughWith() { |
| 500 with ({f: 0}) { |
| 501 assertEquals(0, f); |
| 502 |
| 503 { |
| 504 assertEquals(4, f()); |
| 505 |
| 506 function f() { |
| 507 return 4; |
| 508 } |
| 509 |
| 510 assertEquals(4, f()); |
| 511 } |
| 512 |
| 513 assertEquals(0, f); |
| 514 } |
| 515 |
| 516 assertEquals(4, f()); |
| 517 })(); |
| 518 |
| 497 // Test that hoisting from blocks does happen in global scope | 519 // Test that hoisting from blocks does happen in global scope |
| 498 function globalHoisted() { return 0; } | 520 function globalHoisted() { return 0; } |
| 499 { | 521 { |
| 500 function globalHoisted() { return 1; } | 522 function globalHoisted() { return 1; } |
| 501 } | 523 } |
| 502 assertEquals(1, globalHoisted()); | 524 assertEquals(1, globalHoisted()); |
| 503 | 525 |
| 504 // Also happens when not previously defined | 526 // Also happens when not previously defined |
| 505 assertEquals(undefined, globalUndefinedHoisted); | 527 assertEquals(undefined, globalUndefinedHoisted); |
| 506 { | 528 { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 !function() { | 587 !function() { |
| 566 eval(` | 588 eval(` |
| 567 function evalInFunctionHoisted() { return 0; } | 589 function evalInFunctionHoisted() { return 0; } |
| 568 { | 590 { |
| 569 function evalInFunctionHoisted() { return 1; } | 591 function evalInFunctionHoisted() { return 1; } |
| 570 } | 592 } |
| 571 assertEquals(1, evalInFunctionHoisted()); | 593 assertEquals(1, evalInFunctionHoisted()); |
| 572 `); | 594 `); |
| 573 }(); | 595 }(); |
| 574 | 596 |
| 597 // This test is incorrect BUG(v8:5168). The commented assertions are correct. |
| 598 (function evalHoistingThroughSimpleCatch() { |
| 599 try { |
| 600 throw 0; |
| 601 } catch (f) { |
| 602 eval(`{ function f() { |
| 603 return 4; |
| 604 } }`); |
| 605 |
| 606 // assertEquals(0, f); |
| 607 assertEquals(4, f()); |
| 608 } |
| 609 |
| 610 // assertEquals(4, f()); |
| 611 assertEquals(undefined, f); |
| 612 })(); |
| 613 |
| 614 // This test is incorrect BUG(v8:5168). The commented assertions are correct. |
| 615 (function evalHoistingThroughWith() { |
| 616 with ({f: 0}) { |
| 617 eval(`{ function f() { |
| 618 return 4; |
| 619 } }`); |
| 620 |
| 621 // assertEquals(0, f); |
| 622 assertEquals(4, f()); |
| 623 } |
| 624 |
| 625 // assertEquals(4, f()); |
| 626 assertEquals(undefined, f); |
| 627 })(); |
| 628 |
| 575 let dontHoistGlobal; | 629 let dontHoistGlobal; |
| 576 { function dontHoistGlobal() {} } | 630 { function dontHoistGlobal() {} } |
| 577 assertEquals(undefined, dontHoistGlobal); | 631 assertEquals(undefined, dontHoistGlobal); |
| 578 | 632 |
| 579 let dontHoistEval; | 633 let dontHoistEval; |
| 580 // BUG(v8:) This shouldn't hoist and shouldn't throw | |
| 581 var throws = false; | 634 var throws = false; |
| 582 try { | 635 try { |
| 583 eval("{ function dontHoistEval() {} }"); | 636 eval("{ function dontHoistEval() {} }"); |
| 584 } catch (e) { | 637 } catch (e) { |
| 585 throws = true; | 638 throws = true; |
| 586 } | 639 } |
| 587 assertTrue(throws); | 640 assertFalse(throws); |
| 588 | 641 |
| 589 // When the global object is frozen, silently don't hoist | 642 // When the global object is frozen, silently don't hoist |
| 590 // Currently this actually throws BUG(v8:4452) | 643 // Currently this actually throws BUG(v8:4452) |
| 591 Object.freeze(this); | 644 Object.freeze(this); |
| 592 throws = false; | 645 { |
| 593 try { | 646 let throws = false; |
| 594 eval('{ function hoistWhenFrozen() {} }'); | 647 try { |
| 595 } catch (e) { | 648 eval('{ function hoistWhenFrozen() {} }'); |
| 596 throws = true; | 649 } catch (e) { |
| 650 throws = true; |
| 651 } |
| 652 assertFalse(this.hasOwnProperty("hoistWhenFrozen")); |
| 653 assertThrows(() => hoistWhenFrozen, ReferenceError); |
| 654 // Should be assertFalse BUG(v8:4452) |
| 655 assertTrue(throws); |
| 597 } | 656 } |
| 598 assertFalse(this.hasOwnProperty("hoistWhenFrozen")); | |
| 599 assertThrows(() => hoistWhenFrozen, ReferenceError); | |
| 600 // Should be assertFalse BUG(v8:4452) | |
| 601 assertTrue(throws); | |
| OLD | NEW |