OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 | 707 |
708 // Make sure we can redefine with +0. | 708 // Make sure we can redefine with +0. |
709 Object.defineProperty(obj5, 'pluszero', descPlusZero); | 709 Object.defineProperty(obj5, 'pluszero', descPlusZero); |
710 | 710 |
711 try { | 711 try { |
712 Object.defineProperty(obj5, 'pluszero', descMinusZero); | 712 Object.defineProperty(obj5, 'pluszero', descMinusZero); |
713 assertUnreachable(); | 713 assertUnreachable(); |
714 } catch (e) { | 714 } catch (e) { |
715 assertTrue(/Cannot redefine property/.test(e)); | 715 assertTrue(/Cannot redefine property/.test(e)); |
716 } | 716 } |
| 717 |
| 718 |
| 719 var obj6 = {}; |
| 720 obj6[1] = 'foo'; |
| 721 obj6[2] = 'bar'; |
| 722 obj6[3] = '42'; |
| 723 obj6[4] = '43'; |
| 724 obj6[5] = '44'; |
| 725 |
| 726 var descElement = { value: 'foobar' }; |
| 727 var descElementNonConfigurable = { value: 'barfoo', configurable: false }; |
| 728 var descElementNonWritable = { value: 'foofoo', writable: false }; |
| 729 var descElementNonEnumerable = { value: 'barbar', enumerable: false }; |
| 730 var descElementAllFalse = { value: 'foofalse', |
| 731 configurable: false, |
| 732 writable: false, |
| 733 enumerable: false }; |
| 734 |
| 735 |
| 736 // Redefine existing property. |
| 737 Object.defineProperty(obj6, '1', descElement); |
| 738 desc = Object.getOwnPropertyDescriptor(obj6, '1'); |
| 739 assertEquals(desc.value, 'foobar'); |
| 740 assertTrue(desc.writable); |
| 741 assertTrue(desc.enumerable); |
| 742 assertTrue(desc.configurable); |
| 743 |
| 744 // Redefine existing property with configurable: false. |
| 745 Object.defineProperty(obj6, '2', descElementNonConfigurable); |
| 746 desc = Object.getOwnPropertyDescriptor(obj6, '2'); |
| 747 assertEquals(desc.value, 'barfoo'); |
| 748 assertTrue(desc.writable); |
| 749 assertTrue(desc.enumerable); |
| 750 assertFalse(desc.configurable); |
| 751 |
| 752 // Ensure that we can't overwrite the non configurable element. |
| 753 try { |
| 754 Object.defineProperty(obj6, '2', descElement); |
| 755 assertUnreachable(); |
| 756 } catch (e) { |
| 757 assertTrue(/Cannot redefine property/.test(e)); |
| 758 } |
| 759 |
| 760 Object.defineProperty(obj6, '3', descElementNonWritable); |
| 761 desc = Object.getOwnPropertyDescriptor(obj6, '3'); |
| 762 assertEquals(desc.value, 'foofoo'); |
| 763 assertFalse(desc.writable); |
| 764 assertTrue(desc.enumerable); |
| 765 assertTrue(desc.configurable); |
| 766 |
| 767 // Redefine existing property with configurable: false. |
| 768 Object.defineProperty(obj6, '4', descElementNonEnumerable); |
| 769 desc = Object.getOwnPropertyDescriptor(obj6, '4'); |
| 770 assertEquals(desc.value, 'barbar'); |
| 771 assertTrue(desc.writable); |
| 772 assertFalse(desc.enumerable); |
| 773 assertTrue(desc.configurable); |
| 774 |
| 775 // Redefine existing property with configurable: false. |
| 776 Object.defineProperty(obj6, '5', descElementAllFalse); |
| 777 desc = Object.getOwnPropertyDescriptor(obj6, '5'); |
| 778 assertEquals(desc.value, 'foofalse'); |
| 779 assertFalse(desc.writable); |
| 780 assertFalse(desc.enumerable); |
| 781 assertFalse(desc.configurable); |
| 782 |
| 783 // Define non existing property - all attributes should default to false. |
| 784 Object.defineProperty(obj6, '15', descElement); |
| 785 desc = Object.getOwnPropertyDescriptor(obj6, '15'); |
| 786 assertEquals(desc.value, 'foobar'); |
| 787 assertFalse(desc.writable); |
| 788 assertFalse(desc.enumerable); |
| 789 assertFalse(desc.configurable); |
| 790 |
| 791 // Make sure that we can't redefine using direct access. |
| 792 obj6[15] ='overwrite'; |
| 793 assertEquals(obj6[15],'foobar'); |
| 794 |
| 795 |
| 796 // Repeat the above tests on an array. |
| 797 var arr = new Array(); |
| 798 arr[1] = 'foo'; |
| 799 arr[2] = 'bar'; |
| 800 arr[3] = '42'; |
| 801 arr[4] = '43'; |
| 802 arr[5] = '44'; |
| 803 |
| 804 var descElement = { value: 'foobar' }; |
| 805 var descElementNonConfigurable = { value: 'barfoo', configurable: false }; |
| 806 var descElementNonWritable = { value: 'foofoo', writable: false }; |
| 807 var descElementNonEnumerable = { value: 'barbar', enumerable: false }; |
| 808 var descElementAllFalse = { value: 'foofalse', |
| 809 configurable: false, |
| 810 writable: false, |
| 811 enumerable: false }; |
| 812 |
| 813 |
| 814 // Redefine existing property. |
| 815 Object.defineProperty(arr, '1', descElement); |
| 816 desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 817 assertEquals(desc.value, 'foobar'); |
| 818 assertTrue(desc.writable); |
| 819 assertTrue(desc.enumerable); |
| 820 assertTrue(desc.configurable); |
| 821 |
| 822 // Redefine existing property with configurable: false. |
| 823 Object.defineProperty(arr, '2', descElementNonConfigurable); |
| 824 desc = Object.getOwnPropertyDescriptor(arr, '2'); |
| 825 assertEquals(desc.value, 'barfoo'); |
| 826 assertTrue(desc.writable); |
| 827 assertTrue(desc.enumerable); |
| 828 assertFalse(desc.configurable); |
| 829 |
| 830 // Ensure that we can't overwrite the non configurable element. |
| 831 try { |
| 832 Object.defineProperty(arr, '2', descElement); |
| 833 assertUnreachable(); |
| 834 } catch (e) { |
| 835 assertTrue(/Cannot redefine property/.test(e)); |
| 836 } |
| 837 |
| 838 Object.defineProperty(arr, '3', descElementNonWritable); |
| 839 desc = Object.getOwnPropertyDescriptor(arr, '3'); |
| 840 assertEquals(desc.value, 'foofoo'); |
| 841 assertFalse(desc.writable); |
| 842 assertTrue(desc.enumerable); |
| 843 assertTrue(desc.configurable); |
| 844 |
| 845 // Redefine existing property with configurable: false. |
| 846 Object.defineProperty(arr, '4', descElementNonEnumerable); |
| 847 desc = Object.getOwnPropertyDescriptor(arr, '4'); |
| 848 assertEquals(desc.value, 'barbar'); |
| 849 assertTrue(desc.writable); |
| 850 assertFalse(desc.enumerable); |
| 851 assertTrue(desc.configurable); |
| 852 |
| 853 // Redefine existing property with configurable: false. |
| 854 Object.defineProperty(arr, '5', descElementAllFalse); |
| 855 desc = Object.getOwnPropertyDescriptor(arr, '5'); |
| 856 assertEquals(desc.value, 'foofalse'); |
| 857 assertFalse(desc.writable); |
| 858 assertFalse(desc.enumerable); |
| 859 assertFalse(desc.configurable); |
| 860 |
| 861 // Define non existing property - all attributes should default to false. |
| 862 Object.defineProperty(arr, '15', descElement); |
| 863 desc = Object.getOwnPropertyDescriptor(arr, '15'); |
| 864 assertEquals(desc.value, 'foobar'); |
| 865 assertFalse(desc.writable); |
| 866 assertFalse(desc.enumerable); |
| 867 assertFalse(desc.configurable); |
| 868 |
| 869 |
OLD | NEW |