Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 28 | // Tests the Object.seal and Object.isSealed methods - ES 19.1.2.17 and |
| 29 | // ES 19.1.2.13 |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 30 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | // Flags: --allow-natives-syntax --noalways-opt |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 32 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 33 | // Test that we return obj if non-object is passed as argument |
| 34 | var non_objects = new Array(undefined, null, 1, -1, 0, 42.43, Symbol("test")); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 35 | for (var key in non_objects) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 36 | assertSame(non_objects[key], Object.seal(non_objects[key])); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 39 | // Test that isFrozen always returns true for non-objects |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 40 | for (var key in non_objects) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 41 | assertTrue(Object.isSealed(non_objects[key])); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Test normal data properties. |
| 45 | var obj = { x: 42, z: 'foobar' }; |
| 46 | var desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 47 | assertTrue(desc.writable); |
| 48 | assertTrue(desc.configurable); |
| 49 | assertEquals(42, desc.value); |
| 50 | |
| 51 | desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
| 52 | assertTrue(desc.writable); |
| 53 | assertTrue(desc.configurable); |
| 54 | assertEquals('foobar', desc.value); |
| 55 | |
| 56 | assertTrue(Object.isExtensible(obj)); |
| 57 | assertFalse(Object.isSealed(obj)); |
| 58 | |
| 59 | Object.seal(obj); |
| 60 | |
| 61 | // Make sure we are no longer extensible. |
| 62 | assertFalse(Object.isExtensible(obj)); |
| 63 | assertTrue(Object.isSealed(obj)); |
| 64 | |
| 65 | // We should not be frozen, since we are still able to |
| 66 | // update values. |
| 67 | assertFalse(Object.isFrozen(obj)); |
| 68 | |
| 69 | // We should not allow new properties to be added. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | obj.foo = 42; |
| 71 | assertEquals(obj.foo, undefined); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 72 | |
| 73 | desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 74 | assertTrue(desc.writable); |
| 75 | assertFalse(desc.configurable); |
| 76 | assertEquals(42, desc.value); |
| 77 | |
| 78 | desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
| 79 | assertTrue(desc.writable); |
| 80 | assertFalse(desc.configurable); |
| 81 | assertEquals("foobar", desc.value); |
| 82 | |
| 83 | // Since writable is not affected by seal we should still be able to |
| 84 | // update the values. |
| 85 | obj.x = "43"; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 86 | assertEquals("43", obj.x); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 87 | |
| 88 | // Test on accessors. |
| 89 | var obj2 = {}; |
| 90 | function get() { return 43; }; |
| 91 | function set() {}; |
| 92 | Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true }); |
| 93 | |
| 94 | desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 95 | assertTrue(desc.configurable); |
| 96 | assertEquals(undefined, desc.value); |
| 97 | assertEquals(set, desc.set); |
| 98 | assertEquals(get, desc.get); |
| 99 | |
| 100 | assertTrue(Object.isExtensible(obj2)); |
| 101 | assertFalse(Object.isSealed(obj2)); |
| 102 | Object.seal(obj2); |
| 103 | |
| 104 | // Since this is an accessor property the object is now effectively both |
| 105 | // sealed and frozen (accessors has no writable attribute). |
| 106 | assertTrue(Object.isFrozen(obj2)); |
| 107 | assertFalse(Object.isExtensible(obj2)); |
| 108 | assertTrue(Object.isSealed(obj2)); |
| 109 | |
| 110 | desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 111 | assertFalse(desc.configurable); |
| 112 | assertEquals(undefined, desc.value); |
| 113 | assertEquals(set, desc.set); |
| 114 | assertEquals(get, desc.get); |
| 115 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 116 | obj2.foo = 42; |
| 117 | assertEquals(obj2.foo, undefined); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 118 | |
| 119 | // Test seal on arrays. |
| 120 | var arr = new Array(42,43); |
| 121 | |
| 122 | desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 123 | assertTrue(desc.configurable); |
| 124 | assertTrue(desc.writable); |
| 125 | assertEquals(42, desc.value); |
| 126 | |
| 127 | desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 128 | assertTrue(desc.configurable); |
| 129 | assertTrue(desc.writable); |
| 130 | assertEquals(43, desc.value); |
| 131 | |
| 132 | assertTrue(Object.isExtensible(arr)); |
| 133 | assertFalse(Object.isSealed(arr)); |
| 134 | Object.seal(arr); |
| 135 | assertTrue(Object.isSealed(arr)); |
| 136 | assertFalse(Object.isExtensible(arr)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 137 | // Since the values in the array is still writable this object |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 138 | // is not frozen. |
| 139 | assertFalse(Object.isFrozen(arr)); |
| 140 | |
| 141 | desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 142 | assertFalse(desc.configurable); |
| 143 | assertTrue(desc.writable); |
| 144 | assertEquals(42, desc.value); |
| 145 | |
| 146 | desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 147 | assertFalse(desc.configurable); |
| 148 | assertTrue(desc.writable); |
| 149 | assertEquals(43, desc.value); |
| 150 | |
| 151 | arr[0] = 'foo'; |
| 152 | |
| 153 | // We should be able to overwrite the existing value. |
| 154 | assertEquals('foo', arr[0]); |
| 155 | |
| 156 | |
| 157 | // Test that isSealed returns the correct value even if configurable |
| 158 | // has been set to false on all properties manually and the extensible |
| 159 | // flag has also been set to false manually. |
| 160 | var obj3 = { x: 42, y: 'foo' }; |
| 161 | |
| 162 | assertFalse(Object.isFrozen(obj3)); |
| 163 | |
| 164 | Object.defineProperty(obj3, 'x', {configurable: false, writable: true}); |
| 165 | Object.defineProperty(obj3, 'y', {configurable: false, writable: false}); |
| 166 | Object.preventExtensions(obj3); |
| 167 | |
| 168 | assertTrue(Object.isSealed(obj3)); |
| 169 | |
| 170 | |
| 171 | // Make sure that an object that has a configurable property |
| 172 | // is not classified as sealed. |
| 173 | var obj4 = {}; |
| 174 | Object.defineProperty(obj4, 'x', {configurable: true, writable: false}); |
| 175 | Object.defineProperty(obj4, 'y', {configurable: false, writable: false}); |
| 176 | Object.preventExtensions(obj4); |
| 177 | |
| 178 | assertFalse(Object.isSealed(obj4)); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 179 | |
| 180 | // Make sure that Object.seal returns the sealed object. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 181 | var obj4 = {}; |
| 182 | assertTrue(obj4 === Object.seal(obj4)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 183 | |
| 184 | // |
| 185 | // Test that built-in array functions can't modify a sealed array. |
| 186 | // |
| 187 | obj = [1, 2, 3]; |
| 188 | var objControl = [4, 5, 6]; |
| 189 | |
| 190 | // Allow these functions to set up monomorphic calls, using custom built-ins. |
| 191 | var push_call = function(a) { a.push(10); return a; } |
| 192 | var pop_call = function(a) { return a.pop(); } |
| 193 | for (var i = 0; i < 3; i++) { |
| 194 | push_call(obj); |
| 195 | pop_call(obj); |
| 196 | } |
| 197 | |
| 198 | Object.seal(obj); |
| 199 | assertThrows(function() { push_call(obj); }, TypeError); |
| 200 | assertThrows(function() { pop_call(obj); }, TypeError); |
| 201 | |
| 202 | // But the control object is fine at these sites. |
| 203 | assertDoesNotThrow(function() { push_call(objControl); }); |
| 204 | assertDoesNotThrow(function() { pop_call(objControl); }); |
| 205 | |
| 206 | assertDoesNotThrow(function() { obj.push(); }); |
| 207 | assertThrows(function() { obj.push(3); }, TypeError); |
| 208 | assertThrows(function() { obj.pop(); }, TypeError); |
| 209 | assertThrows(function() { obj.shift(3); }, TypeError); |
| 210 | assertDoesNotThrow(function() { obj.unshift(); }); |
| 211 | assertThrows(function() { obj.unshift(1); }, TypeError); |
| 212 | assertThrows(function() { obj.splice(0, 0, 100, 101, 102); }, TypeError); |
| 213 | assertDoesNotThrow(function() { obj.splice(0,0); }); |
| 214 | |
| 215 | assertDoesNotThrow(function() { objControl.push(3); }); |
| 216 | assertDoesNotThrow(function() { objControl.pop(); }); |
| 217 | assertDoesNotThrow(function() { objControl.shift(3); }); |
| 218 | assertDoesNotThrow(function() { objControl.unshift(); }); |
| 219 | assertDoesNotThrow(function() { objControl.splice(0, 0, 100, 101, 102); }); |
| 220 | |
| 221 | // Verify that crankshaft still does the right thing. |
| 222 | obj = [1, 2, 3]; |
| 223 | |
| 224 | push_call = function(a) { a.push(1000); return a; } |
| 225 | // Include a call site that doesn't have a custom built-in. |
| 226 | var shift_call = function(a) { a.shift(1000); return a; } |
| 227 | for (var i = 0; i < 3; i++) { |
| 228 | push_call(obj); |
| 229 | shift_call(obj); |
| 230 | } |
| 231 | |
| 232 | %OptimizeFunctionOnNextCall(push_call); |
| 233 | %OptimizeFunctionOnNextCall(shift_call); |
| 234 | push_call(obj); |
| 235 | shift_call(obj); |
| 236 | assertOptimized(push_call); |
| 237 | assertOptimized(shift_call); |
| 238 | Object.seal(obj); |
| 239 | assertThrows(function() { push_call(obj); }, TypeError); |
| 240 | assertThrows(function() { shift_call(obj); }, TypeError); |
| 241 | assertUnoptimized(push_call); |
| 242 | assertUnoptimized(shift_call); |
| 243 | assertDoesNotThrow(function() { push_call(objControl); }); |
| 244 | assertDoesNotThrow(function() { shift_call(objControl); }); |
| 245 | |
| 246 | // Verify special behavior of splice on sealed objects. |
| 247 | obj = [1,2,3]; |
| 248 | Object.seal(obj); |
| 249 | assertDoesNotThrow(function() { obj.splice(0,1,100); }); |
| 250 | assertEquals(100, obj[0]); |
| 251 | assertDoesNotThrow(function() { obj.splice(0,2,1,2); }); |
| 252 | assertDoesNotThrow(function() { obj.splice(1,2,1,2); }); |
| 253 | // Count of items to delete is clamped by length. |
| 254 | assertDoesNotThrow(function() { obj.splice(1,2000,1,2); }); |
| 255 | assertThrows(function() { obj.splice(0,0,1); }, TypeError); |
| 256 | assertThrows(function() { obj.splice(1,2000,1,2,3); }, TypeError); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 257 | |
| 258 | // Test that the enumerable attribute is unperturbed by sealing. |
| 259 | obj = { x: 42, y: 'foo' }; |
| 260 | Object.defineProperty(obj, 'y', {enumerable: false}); |
| 261 | Object.seal(obj); |
| 262 | assertTrue(Object.isSealed(obj)); |
| 263 | assertFalse(Object.isFrozen(obj)); |
| 264 | desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 265 | assertTrue(desc.enumerable); |
| 266 | desc = Object.getOwnPropertyDescriptor(obj, 'y'); |
| 267 | assertFalse(desc.enumerable); |
| 268 | |
| 269 | // Fast properties should remain fast |
| 270 | obj = { x: 42, y: 'foo' }; |
| 271 | assertTrue(%HasFastProperties(obj)); |
| 272 | Object.seal(obj); |
| 273 | assertTrue(Object.isSealed(obj)); |
| 274 | assertFalse(Object.isFrozen(obj)); |
| 275 | assertTrue(%HasFastProperties(obj)); |
| 276 | |
| 277 | // Sealed objects should share maps where possible |
| 278 | obj = { prop1: 1, prop2: 2 }; |
| 279 | obj2 = { prop1: 3, prop2: 4 }; |
| 280 | assertTrue(%HaveSameMap(obj, obj2)); |
| 281 | Object.seal(obj); |
| 282 | Object.seal(obj2); |
| 283 | assertTrue(Object.isSealed(obj)); |
| 284 | assertTrue(Object.isSealed(obj2)); |
| 285 | assertFalse(Object.isFrozen(obj)); |
| 286 | assertFalse(Object.isFrozen(obj2)); |
| 287 | assertTrue(%HaveSameMap(obj, obj2)); |
| 288 | |
| 289 | // Sealed objects should share maps even when they have elements |
| 290 | obj = { prop1: 1, prop2: 2, 75: 'foo' }; |
| 291 | obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; |
| 292 | assertTrue(%HaveSameMap(obj, obj2)); |
| 293 | Object.seal(obj); |
| 294 | Object.seal(obj2); |
| 295 | assertTrue(Object.isSealed(obj)); |
| 296 | assertTrue(Object.isSealed(obj2)); |
| 297 | assertFalse(Object.isFrozen(obj)); |
| 298 | assertFalse(Object.isFrozen(obj)); |
| 299 | assertTrue(%HaveSameMap(obj, obj2)); |
| 300 | |
| 301 | // Setting elements after sealing should not be allowed |
| 302 | obj = { prop: 'thing' }; |
| 303 | Object.seal(obj); |
| 304 | assertTrue(Object.isSealed(obj)); |
| 305 | assertFalse(Object.isFrozen(obj)); |
| 306 | obj[0] = 'hello'; |
| 307 | assertFalse(obj.hasOwnProperty(0)); |
| 308 | |
| 309 | // Sealing an object in dictionary mode should work |
| 310 | // Also testing that getter/setter properties work after sealing |
| 311 | obj = { }; |
| 312 | for (var i = 0; i < 100; ++i) { |
| 313 | obj['x' + i] = i; |
| 314 | } |
| 315 | var accessorDidRun = false; |
| 316 | Object.defineProperty(obj, 'accessor', { |
| 317 | get: function() { return 42 }, |
| 318 | set: function() { accessorDidRun = true }, |
| 319 | configurable: true, |
| 320 | enumerable: true |
| 321 | }); |
| 322 | |
| 323 | assertFalse(%HasFastProperties(obj)); |
| 324 | Object.seal(obj); |
| 325 | assertFalse(%HasFastProperties(obj)); |
| 326 | assertTrue(Object.isSealed(obj)); |
| 327 | assertFalse(Object.isFrozen(obj)); |
| 328 | assertFalse(Object.isExtensible(obj)); |
| 329 | for (var i = 0; i < 100; ++i) { |
| 330 | desc = Object.getOwnPropertyDescriptor(obj, 'x' + i); |
| 331 | assertFalse(desc.configurable); |
| 332 | } |
| 333 | assertEquals(42, obj.accessor); |
| 334 | assertFalse(accessorDidRun); |
| 335 | obj.accessor = 'ignored value'; |
| 336 | assertTrue(accessorDidRun); |
| 337 | |
| 338 | // Sealing arguments should work |
| 339 | var func = function(arg) { |
| 340 | Object.seal(arguments); |
| 341 | assertTrue(Object.isSealed(arguments)); |
| 342 | }; |
| 343 | func('hello', 'world'); |
| 344 | func('goodbye', 'world'); |
| 345 | |
| 346 | // Sealing sparse arrays |
| 347 | var sparseArr = [0, 1]; |
| 348 | sparseArr[10000] = 10000; |
| 349 | Object.seal(sparseArr); |
| 350 | assertTrue(Object.isSealed(sparseArr)); |
| 351 | |
| 352 | // Accessors on fast object should behavior properly after sealing |
| 353 | obj = {}; |
| 354 | Object.defineProperty(obj, 'accessor', { |
| 355 | get: function() { return 42 }, |
| 356 | set: function() { accessorDidRun = true }, |
| 357 | configurable: true, |
| 358 | enumerable: true |
| 359 | }); |
| 360 | assertTrue(%HasFastProperties(obj)); |
| 361 | Object.seal(obj); |
| 362 | assertTrue(Object.isSealed(obj)); |
| 363 | assertTrue(%HasFastProperties(obj)); |
| 364 | assertEquals(42, obj.accessor); |
| 365 | accessorDidRun = false; |
| 366 | obj.accessor = 'ignored value'; |
| 367 | assertTrue(accessorDidRun); |
| 368 | |
| 369 | // Test for regression in mixed accessor/data property objects. |
| 370 | // The strict function is one such object. |
| 371 | assertTrue(Object.isSealed(Object.seal(function(){"use strict";}))); |
| 372 | |
| 373 | // Also test a simpler case |
| 374 | obj = {}; |
| 375 | Object.defineProperty(obj, 'accessor2', { |
| 376 | get: function() { return 42 }, |
| 377 | set: function() { accessorDidRun = true }, |
| 378 | configurable: true, |
| 379 | enumerable: true |
| 380 | }); |
| 381 | obj.data = 'foo'; |
| 382 | assertTrue(%HasFastProperties(obj)); |
| 383 | Object.seal(obj); |
| 384 | assertTrue(%HasFastProperties(obj)); |
| 385 | assertTrue(Object.isSealed(obj)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 386 | |
| 387 | function Sealed() {} |
| 388 | Object.seal(Sealed); |
| 389 | assertDoesNotThrow(function() { return new Sealed(); }); |
| 390 | Sealed.prototype.prototypeExists = true; |
| 391 | assertTrue((new Sealed()).prototypeExists); |