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 10 matching lines...) Expand all Loading... |
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.freeze and Object.isFrozen methods - ES 15.2.3.9 and | 28 // Tests the Object.freeze and Object.isFrozen methods - ES 15.2.3.9 and |
29 // ES 15.2.3.12 | 29 // ES 15.2.3.12 |
30 | 30 |
| 31 // Flags: --allow-natives-syntax |
31 | 32 |
32 // Test that we throw an error if an object is not passed as argument. | 33 // Test that we throw an error if an object is not passed as argument. |
33 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); | 34 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); |
34 for (var key in non_objects) { | 35 for (var key in non_objects) { |
35 var exception = false; | 36 var exception = false; |
36 try { | 37 try { |
37 Object.freeze(non_objects[key]); | 38 Object.freeze(non_objects[key]); |
38 } catch(e) { | 39 } catch(e) { |
39 exception = true; | 40 exception = true; |
40 assertTrue(/Object.freeze called on non-object/.test(e)); | 41 assertTrue(/Object.freeze called on non-object/.test(e)); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 var obj5 = {}; | 185 var obj5 = {}; |
185 Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); | 186 Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); |
186 Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); | 187 Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); |
187 Object.preventExtensions(obj5); | 188 Object.preventExtensions(obj5); |
188 | 189 |
189 assertFalse(Object.isFrozen(obj5)); | 190 assertFalse(Object.isFrozen(obj5)); |
190 | 191 |
191 // Make sure that Object.freeze returns the frozen object. | 192 // Make sure that Object.freeze returns the frozen object. |
192 var obj6 = {} | 193 var obj6 = {} |
193 assertTrue(obj6 === Object.freeze(obj6)) | 194 assertTrue(obj6 === Object.freeze(obj6)) |
| 195 |
| 196 // Test that the enumerable attribute is unperturbed by freezing. |
| 197 obj = { x: 42, y: 'foo' }; |
| 198 Object.defineProperty(obj, 'y', {enumerable: false}); |
| 199 Object.freeze(obj); |
| 200 assertTrue(Object.isFrozen(obj)); |
| 201 desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 202 assertTrue(desc.enumerable); |
| 203 desc = Object.getOwnPropertyDescriptor(obj, 'y'); |
| 204 assertFalse(desc.enumerable); |
| 205 |
| 206 // Fast properties should remain fast |
| 207 obj = { x: 42, y: 'foo' }; |
| 208 assertTrue(%HasFastProperties(obj)); |
| 209 Object.freeze(obj); |
| 210 assertTrue(Object.isFrozen(obj)); |
| 211 assertTrue(%HasFastProperties(obj)); |
| 212 |
| 213 // Frozen objects should share maps where possible |
| 214 obj = { prop1: 1, prop2: 2 }; |
| 215 obj2 = { prop1: 3, prop2: 4 }; |
| 216 assertTrue(%HaveSameMap(obj, obj2)); |
| 217 Object.freeze(obj); |
| 218 Object.freeze(obj2); |
| 219 assertTrue(Object.isFrozen(obj)); |
| 220 assertTrue(Object.isFrozen(obj2)); |
| 221 assertTrue(%HaveSameMap(obj, obj2)); |
| 222 |
| 223 // Frozen objects should share maps even when they have elements |
| 224 obj = { prop1: 1, prop2: 2, 75: 'foo' }; |
| 225 obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; |
| 226 assertTrue(%HaveSameMap(obj, obj2)); |
| 227 Object.freeze(obj); |
| 228 Object.freeze(obj2); |
| 229 assertTrue(Object.isFrozen(obj)); |
| 230 assertTrue(Object.isFrozen(obj2)); |
| 231 assertTrue(%HaveSameMap(obj, obj2)); |
| 232 |
| 233 // Setting elements after freezing should not be allowed |
| 234 obj = { prop: 'thing' }; |
| 235 Object.freeze(obj); |
| 236 assertTrue(Object.isFrozen(obj)); |
| 237 obj[0] = 'hello'; |
| 238 assertFalse(obj.hasOwnProperty(0)); |
| 239 |
| 240 // Freezing an object in dictionary mode should work |
| 241 // Also testing that getter/setter properties work after freezing |
| 242 obj = { }; |
| 243 for (var i = 0; i < 100; ++i) { |
| 244 obj['x' + i] = i; |
| 245 } |
| 246 var accessorDidRun = false; |
| 247 Object.defineProperty(obj, 'accessor', { |
| 248 get: function() { return 42 }, |
| 249 set: function() { accessorDidRun = true }, |
| 250 configurable: true, |
| 251 enumerable: true |
| 252 }); |
| 253 |
| 254 assertFalse(%HasFastProperties(obj)); |
| 255 Object.freeze(obj); |
| 256 assertFalse(%HasFastProperties(obj)); |
| 257 assertTrue(Object.isFrozen(obj)); |
| 258 assertFalse(Object.isExtensible(obj)); |
| 259 for (var i = 0; i < 100; ++i) { |
| 260 desc = Object.getOwnPropertyDescriptor(obj, 'x' + i); |
| 261 assertFalse(desc.writable); |
| 262 assertFalse(desc.configurable); |
| 263 } |
| 264 assertEquals(42, obj.accessor); |
| 265 assertFalse(accessorDidRun); |
| 266 obj.accessor = 'ignored value'; |
| 267 assertTrue(accessorDidRun); |
| 268 |
| 269 // Freezing arguments should work |
| 270 var func = function(arg) { |
| 271 Object.freeze(arguments); |
| 272 assertTrue(Object.isFrozen(arguments)); |
| 273 }; |
| 274 func('hello', 'world'); |
| 275 func('goodbye', 'world'); |
| 276 |
| 277 // Freezing sparse arrays |
| 278 var sparseArr = [0, 1]; |
| 279 sparseArr[10000] = 10000; |
| 280 Object.freeze(sparseArr); |
| 281 assertTrue(Object.isFrozen(sparseArr)); |
| 282 |
| 283 // Accessors on fast object should behavior properly after freezing |
| 284 obj = {}; |
| 285 Object.defineProperty(obj, 'accessor', { |
| 286 get: function() { return 42 }, |
| 287 set: function() { accessorDidRun = true }, |
| 288 configurable: true, |
| 289 enumerable: true |
| 290 }); |
| 291 assertTrue(%HasFastProperties(obj)); |
| 292 Object.freeze(obj); |
| 293 assertTrue(Object.isFrozen(obj)); |
| 294 assertTrue(%HasFastProperties(obj)); |
| 295 assertEquals(42, obj.accessor); |
| 296 accessorDidRun = false; |
| 297 obj.accessor = 'ignored value'; |
| 298 assertTrue(accessorDidRun); |
OLD | NEW |