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.preventExtensions method - ES 15.2.3.10 29 30// Flags: --allow-natives-syntax 31 32 33var obj1 = {}; 34// Extensible defaults to true. 35assertTrue(Object.isExtensible(obj1)); 36Object.preventExtensions(obj1); 37 38// Make sure the is_extensible flag is set. 39assertFalse(Object.isExtensible(obj1)); 40obj1.x = 42; 41assertEquals(undefined, obj1.x); 42 43// Try adding a new element. 44obj1[1] = 42; 45assertEquals(undefined, obj1[1]); 46 47 48// Try when the object has an existing property. 49var obj2 = {}; 50assertTrue(Object.isExtensible(obj2)); 51obj2.x = 42; 52assertEquals(42, obj2.x); 53assertTrue(Object.isExtensible(obj2)); 54 55Object.preventExtensions(obj2); 56assertEquals(42, obj2.x); 57 58obj2.y = 42; 59// obj2.y should still be undefined. 60assertEquals(undefined, obj2.y); 61// Make sure we can still write values to obj.x. 62obj2.x = 43; 63assertEquals(43, obj2.x) 64 65obj2.y = new function() { return 42; }; 66// obj2.y should still be undefined. 67assertEquals(undefined, obj2.y); 68assertEquals(43, obj2.x) 69 70try { 71 Object.defineProperty(obj2, "y", {value: 42}); 72} catch (e) { 73 assertTrue(/object is not extensible/.test(e)); 74} 75 76// obj2.y should still be undefined. 77assertEquals(undefined, obj2.y); 78assertEquals(43, obj2.x); 79 80obj2[1] = 42; 81assertEquals(undefined, obj2[1]); 82 83var arr = new Array(); 84arr[1] = 10; 85 86Object.preventExtensions(arr); 87 88arr[2] = 42; 89assertEquals(10, arr[1]); 90 91// We should still be able to change existing elements. 92arr[1]= 42; 93assertEquals(42, arr[1]); 94 95 96// Test the the extensible flag is not inherited. 97var parent = {}; 98parent.x = 42; 99Object.preventExtensions(parent); 100 101var child = Object.create(parent); 102 103// We should be able to add new properties to the child object. 104child.y = 42; 105 106// This should have no influence on the parent class. 107parent.y = 29; 108 109 110// Test that attributes on functions are also handled correctly. 111function foo() { 112 return 42; 113} 114 115Object.preventExtensions(foo); 116 117foo.x = 29; 118assertEquals(undefined, foo.x); 119 120// when Object.isExtensible(o) === false 121// assignment should return right hand side value 122var o = Object.preventExtensions({}); 123var v = o.v = 50; 124assertEquals(undefined, o.v); 125assertEquals(50, v); 126 127// test same behavior as above, but for integer properties 128var n = o[0] = 100; 129assertEquals(undefined, o[0]); 130assertEquals(100, n); 131 132// Fast properties should remain fast 133obj = { x: 42, y: 'foo' }; 134assertTrue(%HasFastProperties(obj)); 135Object.preventExtensions(obj); 136assertFalse(Object.isExtensible(obj)); 137assertFalse(Object.isSealed(obj)); 138assertTrue(%HasFastProperties(obj)); 139 140// Non-extensible objects should share maps where possible 141obj = { prop1: 1, prop2: 2 }; 142obj2 = { prop1: 3, prop2: 4 }; 143assertTrue(%HaveSameMap(obj, obj2)); 144Object.preventExtensions(obj); 145Object.preventExtensions(obj2); 146assertFalse(Object.isExtensible(obj)); 147assertFalse(Object.isExtensible(obj2)); 148assertFalse(Object.isSealed(obj)); 149assertFalse(Object.isSealed(obj2)); 150assertTrue(%HaveSameMap(obj, obj2)); 151 152// Non-extensible objects should share maps even when they have elements 153obj = { prop1: 1, prop2: 2, 75: 'foo' }; 154obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; 155assertTrue(%HaveSameMap(obj, obj2)); 156Object.preventExtensions(obj); 157Object.preventExtensions(obj2); 158assertFalse(Object.isExtensible(obj)); 159assertFalse(Object.isExtensible(obj2)); 160assertFalse(Object.isSealed(obj)); 161assertFalse(Object.isSealed(obj2)); 162assertTrue(%HaveSameMap(obj, obj2)); 163