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 28 // Tests the Object.seal and Object.isSealed methods - ES 15.2.3.9 and 29 // ES 15.2.3.12 30 31 // Flags: --allow-natives-syntax --noalways-opt 32 33 // Test that we throw an error if an object is not passed as argument. 34 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); 35 for (var key in non_objects) { 36 var exception = false; 37 try { 38 Object.seal(non_objects[key]); 39 } catch(e) { 40 exception = true; 41 assertTrue(/Object.seal called on non-object/.test(e)); 42 } 43 assertTrue(exception); 44 } 45 46 for (var key in non_objects) { 47 exception = false; 48 try { 49 Object.isSealed(non_objects[key]); 50 } catch(e) { 51 exception = true; 52 assertTrue(/Object.isSealed called on non-object/.test(e)); 53 } 54 assertTrue(exception); 55 } 56 57 // Test normal data properties. 58 var obj = { x: 42, z: 'foobar' }; 59 var desc = Object.getOwnPropertyDescriptor(obj, 'x'); 60 assertTrue(desc.writable); 61 assertTrue(desc.configurable); 62 assertEquals(42, desc.value); 63 64 desc = Object.getOwnPropertyDescriptor(obj, 'z'); 65 assertTrue(desc.writable); 66 assertTrue(desc.configurable); 67 assertEquals('foobar', desc.value); 68 69 assertTrue(Object.isExtensible(obj)); 70 assertFalse(Object.isSealed(obj)); 71 72 Object.seal(obj); 73 74 // Make sure we are no longer extensible. 75 assertFalse(Object.isExtensible(obj)); 76 assertTrue(Object.isSealed(obj)); 77 78 // We should not be frozen, since we are still able to 79 // update values. 80 assertFalse(Object.isFrozen(obj)); 81 82 // We should not allow new properties to be added. 83 obj.foo = 42; 84 assertEquals(obj.foo, undefined); 85 86 desc = Object.getOwnPropertyDescriptor(obj, 'x'); 87 assertTrue(desc.writable); 88 assertFalse(desc.configurable); 89 assertEquals(42, desc.value); 90 91 desc = Object.getOwnPropertyDescriptor(obj, 'z'); 92 assertTrue(desc.writable); 93 assertFalse(desc.configurable); 94 assertEquals("foobar", desc.value); 95 96 // Since writable is not affected by seal we should still be able to 97 // update the values. 98 obj.x = "43"; 99 assertEquals("43", obj.x); 100 101 // Test on accessors. 102 var obj2 = {}; 103 function get() { return 43; }; 104 function set() {}; 105 Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true }); 106 107 desc = Object.getOwnPropertyDescriptor(obj2, 'x'); 108 assertTrue(desc.configurable); 109 assertEquals(undefined, desc.value); 110 assertEquals(set, desc.set); 111 assertEquals(get, desc.get); 112 113 assertTrue(Object.isExtensible(obj2)); 114 assertFalse(Object.isSealed(obj2)); 115 Object.seal(obj2); 116 117 // Since this is an accessor property the object is now effectively both 118 // sealed and frozen (accessors has no writable attribute). 119 assertTrue(Object.isFrozen(obj2)); 120 assertFalse(Object.isExtensible(obj2)); 121 assertTrue(Object.isSealed(obj2)); 122 123 desc = Object.getOwnPropertyDescriptor(obj2, 'x'); 124 assertFalse(desc.configurable); 125 assertEquals(undefined, desc.value); 126 assertEquals(set, desc.set); 127 assertEquals(get, desc.get); 128 129 obj2.foo = 42; 130 assertEquals(obj2.foo, undefined); 131 132 // Test seal on arrays. 133 var arr = new Array(42,43); 134 135 desc = Object.getOwnPropertyDescriptor(arr, '0'); 136 assertTrue(desc.configurable); 137 assertTrue(desc.writable); 138 assertEquals(42, desc.value); 139 140 desc = Object.getOwnPropertyDescriptor(arr, '1'); 141 assertTrue(desc.configurable); 142 assertTrue(desc.writable); 143 assertEquals(43, desc.value); 144 145 assertTrue(Object.isExtensible(arr)); 146 assertFalse(Object.isSealed(arr)); 147 Object.seal(arr); 148 assertTrue(Object.isSealed(arr)); 149 assertFalse(Object.isExtensible(arr)); 150 // Since the values in the array is still writable this object 151 // is not frozen. 152 assertFalse(Object.isFrozen(arr)); 153 154 desc = Object.getOwnPropertyDescriptor(arr, '0'); 155 assertFalse(desc.configurable); 156 assertTrue(desc.writable); 157 assertEquals(42, desc.value); 158 159 desc = Object.getOwnPropertyDescriptor(arr, '1'); 160 assertFalse(desc.configurable); 161 assertTrue(desc.writable); 162 assertEquals(43, desc.value); 163 164 arr[0] = 'foo'; 165 166 // We should be able to overwrite the existing value. 167 assertEquals('foo', arr[0]); 168 169 170 // Test that isSealed returns the correct value even if configurable 171 // has been set to false on all properties manually and the extensible 172 // flag has also been set to false manually. 173 var obj3 = { x: 42, y: 'foo' }; 174 175 assertFalse(Object.isFrozen(obj3)); 176 177 Object.defineProperty(obj3, 'x', {configurable: false, writable: true}); 178 Object.defineProperty(obj3, 'y', {configurable: false, writable: false}); 179 Object.preventExtensions(obj3); 180 181 assertTrue(Object.isSealed(obj3)); 182 183 184 // Make sure that an object that has a configurable property 185 // is not classified as sealed. 186 var obj4 = {}; 187 Object.defineProperty(obj4, 'x', {configurable: true, writable: false}); 188 Object.defineProperty(obj4, 'y', {configurable: false, writable: false}); 189 Object.preventExtensions(obj4); 190 191 assertFalse(Object.isSealed(obj4)); 192 193 // Make sure that Object.seal returns the sealed object. 194 var obj4 = {}; 195 assertTrue(obj4 === Object.seal(obj4)); 196 197 // 198 // Test that built-in array functions can't modify a sealed array. 199 // 200 obj = [1, 2, 3]; 201 var objControl = [4, 5, 6]; 202 203 // Allow these functions to set up monomorphic calls, using custom built-ins. 204 var push_call = function(a) { a.push(10); return a; } 205 var pop_call = function(a) { return a.pop(); } 206 for (var i = 0; i < 3; i++) { 207 push_call(obj); 208 pop_call(obj); 209 } 210 211 Object.seal(obj); 212 assertThrows(function() { push_call(obj); }, TypeError); 213 assertThrows(function() { pop_call(obj); }, TypeError); 214 215 // But the control object is fine at these sites. 216 assertDoesNotThrow(function() { push_call(objControl); }); 217 assertDoesNotThrow(function() { pop_call(objControl); }); 218 219 assertDoesNotThrow(function() { obj.push(); }); 220 assertThrows(function() { obj.push(3); }, TypeError); 221 assertThrows(function() { obj.pop(); }, TypeError); 222 assertThrows(function() { obj.shift(3); }, TypeError); 223 assertDoesNotThrow(function() { obj.unshift(); }); 224 assertThrows(function() { obj.unshift(1); }, TypeError); 225 assertThrows(function() { obj.splice(0, 0, 100, 101, 102); }, TypeError); 226 assertDoesNotThrow(function() { obj.splice(0,0); }); 227 228 assertDoesNotThrow(function() { objControl.push(3); }); 229 assertDoesNotThrow(function() { objControl.pop(); }); 230 assertDoesNotThrow(function() { objControl.shift(3); }); 231 assertDoesNotThrow(function() { objControl.unshift(); }); 232 assertDoesNotThrow(function() { objControl.splice(0, 0, 100, 101, 102); }); 233 234 // Verify that crankshaft still does the right thing. 235 obj = [1, 2, 3]; 236 237 push_call = function(a) { a.push(1000); return a; } 238 // Include a call site that doesn't have a custom built-in. 239 var shift_call = function(a) { a.shift(1000); return a; } 240 for (var i = 0; i < 3; i++) { 241 push_call(obj); 242 shift_call(obj); 243 } 244 245 %OptimizeFunctionOnNextCall(push_call); 246 %OptimizeFunctionOnNextCall(shift_call); 247 push_call(obj); 248 shift_call(obj); 249 assertOptimized(push_call); 250 assertOptimized(shift_call); 251 Object.seal(obj); 252 assertThrows(function() { push_call(obj); }, TypeError); 253 assertThrows(function() { shift_call(obj); }, TypeError); 254 assertOptimized(push_call); 255 // shift() doesn't have a custom call generator, so deopt will occur. 256 assertUnoptimized(shift_call); 257 assertDoesNotThrow(function() { push_call(objControl); }); 258 assertDoesNotThrow(function() { shift_call(objControl); }); 259 260 // Verify special behavior of splice on sealed objects. 261 obj = [1,2,3]; 262 Object.seal(obj); 263 assertDoesNotThrow(function() { obj.splice(0,1,100); }); 264 assertEquals(100, obj[0]); 265 assertDoesNotThrow(function() { obj.splice(0,2,1,2); }); 266 assertDoesNotThrow(function() { obj.splice(1,2,1,2); }); 267 // Count of items to delete is clamped by length. 268 assertDoesNotThrow(function() { obj.splice(1,2000,1,2); }); 269 assertThrows(function() { obj.splice(0,0,1); }, TypeError); 270 assertThrows(function() { obj.splice(1,2000,1,2,3); }, TypeError); 271