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 17 matching lines...) Expand all Loading... |
28 // Tests the Object.preventExtensions method - ES 15.2.3.10 | 28 // Tests the Object.preventExtensions method - ES 15.2.3.10 |
29 | 29 |
30 | 30 |
31 var obj1 = {}; | 31 var obj1 = {}; |
32 // Extensible defaults to true. | 32 // Extensible defaults to true. |
33 assertTrue(Object.isExtensible(obj1)); | 33 assertTrue(Object.isExtensible(obj1)); |
34 Object.preventExtensions(obj1); | 34 Object.preventExtensions(obj1); |
35 | 35 |
36 // Make sure the is_extensible flag is set. | 36 // Make sure the is_extensible flag is set. |
37 assertFalse(Object.isExtensible(obj1)); | 37 assertFalse(Object.isExtensible(obj1)); |
38 // Try adding a new property. | 38 obj1.x = 42; |
39 try { | |
40 obj1.x = 42; | |
41 assertUnreachable(); | |
42 } catch (e) { | |
43 assertTrue(/object is not extensible/.test(e)); | |
44 } | |
45 assertEquals(undefined, obj1.x); | 39 assertEquals(undefined, obj1.x); |
46 | 40 |
47 // Try adding a new element. | 41 // Try adding a new element. |
48 try { | 42 obj1[1] = 42; |
49 obj1[1] = 42; | |
50 assertUnreachable(); | |
51 } catch (e) { | |
52 assertTrue(/object is not extensible/.test(e)); | |
53 } | |
54 assertEquals(undefined, obj1[1]); | 43 assertEquals(undefined, obj1[1]); |
55 | 44 |
56 | 45 |
57 // Try when the object has an existing property. | 46 // Try when the object has an existing property. |
58 var obj2 = {}; | 47 var obj2 = {}; |
59 assertTrue(Object.isExtensible(obj2)); | 48 assertTrue(Object.isExtensible(obj2)); |
60 obj2.x = 42; | 49 obj2.x = 42; |
61 assertEquals(42, obj2.x); | 50 assertEquals(42, obj2.x); |
62 assertTrue(Object.isExtensible(obj2)); | 51 assertTrue(Object.isExtensible(obj2)); |
63 | 52 |
64 Object.preventExtensions(obj2); | 53 Object.preventExtensions(obj2); |
65 assertEquals(42, obj2.x); | 54 assertEquals(42, obj2.x); |
66 | 55 |
67 try { | 56 obj2.y = 42; |
68 obj2.y = 42; | |
69 assertUnreachable(); | |
70 } catch (e) { | |
71 assertTrue(/object is not extensible/.test(e)); | |
72 } | |
73 | |
74 // obj2.y should still be undefined. | 57 // obj2.y should still be undefined. |
75 assertEquals(undefined, obj2.y); | 58 assertEquals(undefined, obj2.y); |
76 // Make sure we can still write values to obj.x. | 59 // Make sure we can still write values to obj.x. |
77 obj2.x = 43; | 60 obj2.x = 43; |
78 assertEquals(43, obj2.x) | 61 assertEquals(43, obj2.x) |
79 | 62 |
80 try { | 63 obj2.y = new function() { return 42; }; |
81 obj2.y = new function() { return 42; }; | |
82 assertUnreachable(); | |
83 } catch (e) { | |
84 assertTrue(/object is not extensible/.test(e)); | |
85 } | |
86 // obj2.y should still be undefined. | 64 // obj2.y should still be undefined. |
87 assertEquals(undefined, obj2.y); | 65 assertEquals(undefined, obj2.y); |
88 assertEquals(43, obj2.x) | 66 assertEquals(43, obj2.x) |
89 | 67 |
90 try { | 68 try { |
91 Object.defineProperty(obj2, "y", {value: 42}); | 69 Object.defineProperty(obj2, "y", {value: 42}); |
92 } catch (e) { | 70 } catch (e) { |
93 assertTrue(/object is not extensible/.test(e)); | 71 assertTrue(/object is not extensible/.test(e)); |
94 } | 72 } |
95 | 73 |
96 // obj2.y should still be undefined. | 74 // obj2.y should still be undefined. |
97 assertEquals(undefined, obj2.y); | 75 assertEquals(undefined, obj2.y); |
98 assertEquals(43, obj2.x); | 76 assertEquals(43, obj2.x); |
99 | 77 |
100 try { | 78 obj2[1] = 42; |
101 obj2[1] = 42; | |
102 } catch (e) { | |
103 assertTrue(/object is not extensible/.test(e)); | |
104 } | |
105 | |
106 assertEquals(undefined, obj2[1]); | 79 assertEquals(undefined, obj2[1]); |
107 | 80 |
108 var arr = new Array(); | 81 var arr = new Array(); |
109 arr[1] = 10; | 82 arr[1] = 10; |
110 | 83 |
111 Object.preventExtensions(arr); | 84 Object.preventExtensions(arr); |
112 | 85 |
113 try { | 86 arr[2] = 42; |
114 arr[2] = 42; | |
115 assertUnreachable(); | |
116 } catch (e) { | |
117 assertTrue(/object is not extensible/.test(e)); | |
118 } | |
119 assertEquals(10, arr[1]); | 87 assertEquals(10, arr[1]); |
120 | 88 |
121 // We should still be able to change exiting elements. | 89 // We should still be able to change exiting elements. |
122 arr[1]= 42; | 90 arr[1]= 42; |
123 assertEquals(42, arr[1]); | 91 assertEquals(42, arr[1]); |
124 | 92 |
125 | 93 |
126 // Test the the extensible flag is not inherited. | 94 // Test the the extensible flag is not inherited. |
127 var parent = {}; | 95 var parent = {}; |
128 parent.x = 42; | 96 parent.x = 42; |
129 Object.preventExtensions(parent); | 97 Object.preventExtensions(parent); |
130 | 98 |
131 var child = Object.create(parent); | 99 var child = Object.create(parent); |
132 | 100 |
133 // We should be able to add new properties to the child object. | 101 // We should be able to add new properties to the child object. |
134 child.y = 42; | 102 child.y = 42; |
135 | 103 |
136 // This should have no influence on the parent class. | 104 // This should have no influence on the parent class. |
137 try { | 105 parent.y = 29; |
138 parent.y = 29; | |
139 assertUnreachable(); | |
140 } catch (e) { | |
141 assertTrue(/object is not extensible/.test(e)); | |
142 } | |
143 | 106 |
144 | 107 |
145 // Test that attributes on functions are also handled correctly. | 108 // Test that attributes on functions are also handled correctly. |
146 function foo() { | 109 function foo() { |
147 return 42; | 110 return 42; |
148 } | 111 } |
149 | 112 |
150 Object.preventExtensions(foo); | 113 Object.preventExtensions(foo); |
151 | 114 |
152 try { | 115 foo.x = 29; |
153 foo.x = 29; | 116 assertEquals(undefined, foo.x); |
154 assertUnreachable(); | |
155 } catch (e) { | |
156 assertTrue(/object is not extensible/.test(e)); | |
157 } | |
OLD | NEW |