OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010-2015 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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 // Tests the Object.preventExtensions method - ES 15.2.3.10 | 28 // Tests the Reflect.preventExtensions method - ES6 26.1.12. |
| 29 // This is adapted from object-prevent-extensions.js. |
29 | 30 |
30 // Flags: --allow-natives-syntax | 31 // Flags: --allow-natives-syntax --harmony-reflect |
31 | 32 |
32 | 33 |
33 var obj1 = {}; | 34 var obj1 = {}; |
34 // Extensible defaults to true. | 35 // Extensible defaults to true. |
35 assertTrue(Object.isExtensible(obj1)); | 36 assertTrue(Object.isExtensible(obj1)); |
36 Object.preventExtensions(obj1); | 37 assertTrue(Reflect.preventExtensions(obj1)); |
37 | 38 |
38 // Make sure the is_extensible flag is set. | 39 // Make sure the is_extensible flag is set. |
39 assertFalse(Object.isExtensible(obj1)); | 40 assertFalse(Object.isExtensible(obj1)); |
40 obj1.x = 42; | 41 obj1.x = 42; |
41 assertEquals(undefined, obj1.x); | 42 assertEquals(undefined, obj1.x); |
42 | 43 |
43 // Try adding a new element. | 44 // Try adding a new element. |
44 obj1[1] = 42; | 45 obj1[1] = 42; |
45 assertEquals(undefined, obj1[1]); | 46 assertEquals(undefined, obj1[1]); |
46 | 47 |
47 | 48 |
48 // Try when the object has an existing property. | 49 // Try when the object has an existing property. |
49 var obj2 = {}; | 50 var obj2 = {}; |
50 assertTrue(Object.isExtensible(obj2)); | 51 assertTrue(Object.isExtensible(obj2)); |
51 obj2.x = 42; | 52 obj2.x = 42; |
52 assertEquals(42, obj2.x); | 53 assertEquals(42, obj2.x); |
53 assertTrue(Object.isExtensible(obj2)); | 54 assertTrue(Object.isExtensible(obj2)); |
54 | 55 |
55 Object.preventExtensions(obj2); | 56 assertTrue(Reflect.preventExtensions(obj2)); |
56 assertEquals(42, obj2.x); | 57 assertEquals(42, obj2.x); |
57 | 58 |
58 obj2.y = 42; | 59 obj2.y = 42; |
59 // obj2.y should still be undefined. | 60 // obj2.y should still be undefined. |
60 assertEquals(undefined, obj2.y); | 61 assertEquals(undefined, obj2.y); |
61 // Make sure we can still write values to obj.x. | 62 // Make sure we can still write values to obj.x. |
62 obj2.x = 43; | 63 obj2.x = 43; |
63 assertEquals(43, obj2.x) | 64 assertEquals(43, obj2.x) |
64 | 65 |
65 obj2.y = new function() { return 42; }; | 66 obj2.y = new function() { return 42; }; |
(...skipping 10 matching lines...) Loading... |
76 // obj2.y should still be undefined. | 77 // obj2.y should still be undefined. |
77 assertEquals(undefined, obj2.y); | 78 assertEquals(undefined, obj2.y); |
78 assertEquals(43, obj2.x); | 79 assertEquals(43, obj2.x); |
79 | 80 |
80 obj2[1] = 42; | 81 obj2[1] = 42; |
81 assertEquals(undefined, obj2[1]); | 82 assertEquals(undefined, obj2[1]); |
82 | 83 |
83 var arr = new Array(); | 84 var arr = new Array(); |
84 arr[1] = 10; | 85 arr[1] = 10; |
85 | 86 |
86 Object.preventExtensions(arr); | 87 assertTrue(Reflect.preventExtensions(arr)); |
87 | 88 |
88 arr[2] = 42; | 89 arr[2] = 42; |
89 assertEquals(10, arr[1]); | 90 assertEquals(10, arr[1]); |
90 | 91 |
91 // We should still be able to change exiting elements. | 92 // We should still be able to change existing elements. |
92 arr[1]= 42; | 93 arr[1]= 42; |
93 assertEquals(42, arr[1]); | 94 assertEquals(42, arr[1]); |
94 | 95 |
95 | 96 |
96 // Test the the extensible flag is not inherited. | 97 // Test the the extensible flag is not inherited. |
97 var parent = {}; | 98 var parent = {}; |
98 parent.x = 42; | 99 parent.x = 42; |
99 Object.preventExtensions(parent); | 100 assertTrue(Reflect.preventExtensions(parent)); |
100 | 101 |
101 var child = Object.create(parent); | 102 var child = Object.create(parent); |
102 | 103 |
103 // We should be able to add new properties to the child object. | 104 // We should be able to add new properties to the child object. |
104 child.y = 42; | 105 child.y = 42; |
105 | 106 |
106 // This should have no influence on the parent class. | 107 // This should have no influence on the parent class. |
107 parent.y = 29; | 108 parent.y = 29; |
108 | 109 |
109 | 110 |
110 // Test that attributes on functions are also handled correctly. | 111 // Test that attributes on functions are also handled correctly. |
111 function foo() { | 112 function foo() { |
112 return 42; | 113 return 42; |
113 } | 114 } |
114 | 115 |
115 Object.preventExtensions(foo); | 116 assertTrue(Reflect.preventExtensions(foo)); |
116 | 117 |
117 foo.x = 29; | 118 foo.x = 29; |
118 assertEquals(undefined, foo.x); | 119 assertEquals(undefined, foo.x); |
119 | 120 |
120 // when Object.isExtensible(o) === false | 121 // when Object.isExtensible(o) === false |
121 // assignment should return right hand side value | 122 // assignment should return right hand side value |
122 var o = Object.preventExtensions({}); | 123 var o = {}; |
| 124 assertTrue(Reflect.preventExtensions(o)); |
123 var v = o.v = 50; | 125 var v = o.v = 50; |
124 assertEquals(undefined, o.v); | 126 assertEquals(undefined, o.v); |
125 assertEquals(50, v); | 127 assertEquals(50, v); |
126 | 128 |
127 // test same behavior as above, but for integer properties | 129 // test same behavior as above, but for integer properties |
128 var n = o[0] = 100; | 130 var n = o[0] = 100; |
129 assertEquals(undefined, o[0]); | 131 assertEquals(undefined, o[0]); |
130 assertEquals(100, n); | 132 assertEquals(100, n); |
131 | 133 |
132 // Fast properties should remain fast | 134 // Fast properties should remain fast |
133 obj = { x: 42, y: 'foo' }; | 135 obj = { x: 42, y: 'foo' }; |
134 assertTrue(%HasFastProperties(obj)); | 136 assertTrue(%HasFastProperties(obj)); |
135 Object.preventExtensions(obj); | 137 assertTrue(Reflect.preventExtensions(obj)); |
136 assertFalse(Object.isExtensible(obj)); | 138 assertFalse(Object.isExtensible(obj)); |
137 assertFalse(Object.isSealed(obj)); | 139 assertFalse(Object.isSealed(obj)); |
138 assertTrue(%HasFastProperties(obj)); | 140 assertTrue(%HasFastProperties(obj)); |
139 | 141 |
140 // Non-extensible objects should share maps where possible | 142 // Non-extensible objects should share maps where possible |
141 obj = { prop1: 1, prop2: 2 }; | 143 obj = { prop1: 1, prop2: 2 }; |
142 obj2 = { prop1: 3, prop2: 4 }; | 144 obj2 = { prop1: 3, prop2: 4 }; |
143 assertTrue(%HaveSameMap(obj, obj2)); | 145 assertTrue(%HaveSameMap(obj, obj2)); |
144 Object.preventExtensions(obj); | 146 assertTrue(Reflect.preventExtensions(obj)); |
145 Object.preventExtensions(obj2); | 147 assertTrue(Reflect.preventExtensions(obj2)); |
146 assertFalse(Object.isExtensible(obj)); | 148 assertFalse(Object.isExtensible(obj)); |
147 assertFalse(Object.isExtensible(obj2)); | 149 assertFalse(Object.isExtensible(obj2)); |
148 assertFalse(Object.isSealed(obj)); | 150 assertFalse(Object.isSealed(obj)); |
149 assertFalse(Object.isSealed(obj2)); | 151 assertFalse(Object.isSealed(obj2)); |
150 assertTrue(%HaveSameMap(obj, obj2)); | 152 assertTrue(%HaveSameMap(obj, obj2)); |
151 | 153 |
152 // Non-extensible objects should share maps even when they have elements | 154 // Non-extensible objects should share maps even when they have elements |
153 obj = { prop1: 1, prop2: 2, 75: 'foo' }; | 155 obj = { prop1: 1, prop2: 2, 75: 'foo' }; |
154 obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; | 156 obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; |
155 assertTrue(%HaveSameMap(obj, obj2)); | 157 assertTrue(%HaveSameMap(obj, obj2)); |
156 Object.preventExtensions(obj); | 158 assertTrue(Reflect.preventExtensions(obj)); |
157 Object.preventExtensions(obj2); | 159 assertTrue(Reflect.preventExtensions(obj2)); |
158 assertFalse(Object.isExtensible(obj)); | 160 assertFalse(Object.isExtensible(obj)); |
159 assertFalse(Object.isExtensible(obj2)); | 161 assertFalse(Object.isExtensible(obj2)); |
160 assertFalse(Object.isSealed(obj)); | 162 assertFalse(Object.isSealed(obj)); |
161 assertFalse(Object.isSealed(obj2)); | 163 assertFalse(Object.isSealed(obj2)); |
162 assertTrue(%HaveSameMap(obj, obj2)); | 164 assertTrue(%HaveSameMap(obj, obj2)); |
OLD | NEW |