OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 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 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 context.Check("var x = 5; x", // assignment ignored | 637 context.Check("var x = 5; x", // assignment ignored |
638 EXPECT_RESULT, Number::New(isolate, 1)); | 638 EXPECT_RESULT, Number::New(isolate, 1)); |
639 context.Check("this.x", | 639 context.Check("this.x", |
640 EXPECT_RESULT, Number::New(isolate, 1)); | 640 EXPECT_RESULT, Number::New(isolate, 1)); |
641 context.Check("function x() { return 7 }; x", | 641 context.Check("function x() { return 7 }; x", |
642 EXPECT_EXCEPTION); | 642 EXPECT_EXCEPTION); |
643 } | 643 } |
644 } | 644 } |
645 | 645 |
646 | 646 |
| 647 TEST(CrossScriptReferences_Simple) { |
| 648 i::FLAG_harmony_scoping = true; |
| 649 i::FLAG_use_strict = true; |
| 650 |
| 651 v8::Isolate* isolate = CcTest::isolate(); |
| 652 HandleScope scope(isolate); |
| 653 |
| 654 { |
| 655 SimpleContext context; |
| 656 context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1)); |
| 657 context.Check("let x = 5; x", EXPECT_EXCEPTION); |
| 658 } |
| 659 } |
| 660 |
| 661 |
| 662 TEST(CrossScriptReferences_Simple2) { |
| 663 i::FLAG_harmony_scoping = true; |
| 664 i::FLAG_use_strict = true; |
| 665 |
| 666 v8::Isolate* isolate = CcTest::isolate(); |
| 667 HandleScope scope(isolate); |
| 668 |
| 669 for (int k = 0; k < 100; k++) { |
| 670 SimpleContext context; |
| 671 bool cond = (k % 2) == 0; |
| 672 if (cond) { |
| 673 context.Check("let x = 1; x", EXPECT_RESULT, Number::New(isolate, 1)); |
| 674 context.Check("let z = 4; z", EXPECT_RESULT, Number::New(isolate, 4)); |
| 675 } else { |
| 676 context.Check("let z = 1; z", EXPECT_RESULT, Number::New(isolate, 1)); |
| 677 context.Check("let x = 4; x", EXPECT_RESULT, Number::New(isolate, 4)); |
| 678 } |
| 679 context.Check("let y = 2; x", EXPECT_RESULT, |
| 680 Number::New(isolate, cond ? 1 : 4)); |
| 681 } |
| 682 } |
| 683 |
| 684 |
647 TEST(CrossScriptReferencesHarmony) { | 685 TEST(CrossScriptReferencesHarmony) { |
648 i::FLAG_use_strict = true; | 686 i::FLAG_use_strict = true; |
649 i::FLAG_harmony_scoping = true; | 687 i::FLAG_harmony_scoping = true; |
650 i::FLAG_harmony_modules = true; | 688 i::FLAG_harmony_modules = true; |
651 | 689 |
652 v8::Isolate* isolate = CcTest::isolate(); | 690 v8::Isolate* isolate = CcTest::isolate(); |
653 HandleScope scope(isolate); | 691 HandleScope scope(isolate); |
654 | 692 |
655 const char* decs[] = { | 693 const char* decs[] = { |
656 "var x = 1; x", "x", "this.x", | 694 "var x = 1; x", "x", "this.x", |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 "const x = 2; x", | 735 "const x = 2; x", |
698 "module x { export let a = 2 }; x.a", | 736 "module x { export let a = 2 }; x.a", |
699 NULL | 737 NULL |
700 }; | 738 }; |
701 | 739 |
702 for (int i = 0; firsts[i] != NULL; ++i) { | 740 for (int i = 0; firsts[i] != NULL; ++i) { |
703 for (int j = 0; seconds[j] != NULL; ++j) { | 741 for (int j = 0; seconds[j] != NULL; ++j) { |
704 SimpleContext context; | 742 SimpleContext context; |
705 context.Check(firsts[i], EXPECT_RESULT, | 743 context.Check(firsts[i], EXPECT_RESULT, |
706 Number::New(CcTest::isolate(), 1)); | 744 Number::New(CcTest::isolate(), 1)); |
707 // TODO(rossberg): All tests should actually be errors in Harmony, | 745 bool success_case = i < 2 && j < 2; |
708 // but we currently do not detect the cases where the first declaration | 746 Local<Value> success_result; |
709 // is not lexical. | 747 if (success_case) success_result = Number::New(CcTest::isolate(), 2); |
710 context.Check(seconds[j], | 748 |
711 i < 2 ? EXPECT_RESULT : EXPECT_ERROR, | 749 context.Check(seconds[j], success_case ? EXPECT_RESULT : EXPECT_EXCEPTION, |
712 Number::New(CcTest::isolate(), 2)); | 750 success_result); |
713 } | 751 } |
714 } | 752 } |
715 } | 753 } |
716 | 754 |
717 | 755 |
718 TEST(CrossScriptDynamicLookup) { | 756 TEST(CrossScriptDynamicLookup) { |
719 i::FLAG_harmony_scoping = true; | 757 i::FLAG_harmony_scoping = true; |
720 | 758 |
721 HandleScope handle_scope(CcTest::isolate()); | 759 HandleScope handle_scope(CcTest::isolate()); |
722 | 760 |
(...skipping 10 matching lines...) Expand all Loading... |
733 "function h(o) { with(o) { return typeof x; } }", | 771 "function h(o) { with(o) { return typeof x; } }", |
734 EXPECT_RESULT, Undefined(CcTest::isolate())); | 772 EXPECT_RESULT, Undefined(CcTest::isolate())); |
735 context.Check("h({})", EXPECT_RESULT, undefined_string); | 773 context.Check("h({})", EXPECT_RESULT, undefined_string); |
736 context.Check( | 774 context.Check( |
737 "'use strict';" | 775 "'use strict';" |
738 "let x = 1;" | 776 "let x = 1;" |
739 "f({})", | 777 "f({})", |
740 EXPECT_RESULT, Number::New(CcTest::isolate(), 1)); | 778 EXPECT_RESULT, Number::New(CcTest::isolate(), 1)); |
741 context.Check( | 779 context.Check( |
742 "'use strict';" | 780 "'use strict';" |
743 "g({});" | 781 "g({});0", |
744 "x", | 782 EXPECT_RESULT, Number::New(CcTest::isolate(), 0)); |
745 EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); | |
746 context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); | 783 context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); |
747 context.Check("h({})", EXPECT_RESULT, number_string); | 784 context.Check("h({})", EXPECT_RESULT, number_string); |
748 } | 785 } |
| 786 } |
| 787 |
| 788 |
| 789 TEST(CrossScriptGlobal) { |
| 790 i::FLAG_harmony_scoping = true; |
| 791 |
| 792 HandleScope handle_scope(CcTest::isolate()); |
| 793 { |
| 794 SimpleContext context; |
| 795 |
| 796 context.Check( |
| 797 "var global = this;" |
| 798 "global.x = 255;" |
| 799 "x", |
| 800 EXPECT_RESULT, Number::New(CcTest::isolate(), 255)); |
| 801 context.Check( |
| 802 "'use strict';" |
| 803 "let x = 1;" |
| 804 "global.x", |
| 805 EXPECT_RESULT, Number::New(CcTest::isolate(), 255)); |
| 806 context.Check("global.x = 15; x", EXPECT_RESULT, |
| 807 Number::New(CcTest::isolate(), 1)); |
| 808 context.Check("x = 221; global.x", EXPECT_RESULT, |
| 809 Number::New(CcTest::isolate(), 15)); |
| 810 context.Check( |
| 811 "z = 15;" |
| 812 "function f() { return z; };" |
| 813 "for (var k = 0; k < 3; k++) { f(); }" |
| 814 "f()", |
| 815 EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); |
| 816 context.Check( |
| 817 "'use strict';" |
| 818 "let z = 5; f()", |
| 819 EXPECT_RESULT, Number::New(CcTest::isolate(), 5)); |
| 820 context.Check( |
| 821 "function f() { konst = 10; return konst; };" |
| 822 "f()", |
| 823 EXPECT_RESULT, Number::New(CcTest::isolate(), 10)); |
| 824 context.Check( |
| 825 "'use strict';" |
| 826 "const konst = 255;" |
| 827 "f()", |
| 828 EXPECT_EXCEPTION); |
| 829 } |
| 830 } |
| 831 |
| 832 |
| 833 TEST(CrossScriptStaticLookupUndeclared) { |
| 834 i::FLAG_harmony_scoping = true; |
| 835 |
| 836 HandleScope handle_scope(CcTest::isolate()); |
| 837 |
| 838 { |
| 839 SimpleContext context; |
| 840 Local<String> undefined_string = String::NewFromUtf8( |
| 841 CcTest::isolate(), "undefined", String::kInternalizedString); |
| 842 Local<String> number_string = String::NewFromUtf8( |
| 843 CcTest::isolate(), "number", String::kInternalizedString); |
| 844 |
| 845 context.Check( |
| 846 "function f(o) { return x; }" |
| 847 "function g(o) { x = 15; }" |
| 848 "function h(o) { return typeof x; }", |
| 849 EXPECT_RESULT, Undefined(CcTest::isolate())); |
| 850 context.Check("h({})", EXPECT_RESULT, undefined_string); |
| 851 context.Check( |
| 852 "'use strict';" |
| 853 "let x = 1;" |
| 854 "f({})", |
| 855 EXPECT_RESULT, Number::New(CcTest::isolate(), 1)); |
| 856 context.Check( |
| 857 "'use strict';" |
| 858 "g({});x", |
| 859 EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); |
| 860 context.Check("h({})", EXPECT_RESULT, number_string); |
| 861 context.Check("f({})", EXPECT_RESULT, Number::New(CcTest::isolate(), 15)); |
| 862 context.Check("h({})", EXPECT_RESULT, number_string); |
| 863 } |
749 } | 864 } |
OLD | NEW |