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 31var obj1 = {}; 32// Extensible defaults to true. 33assertTrue(Object.isExtensible(obj1)); 34Object.preventExtensions(obj1); 35 36// Make sure the is_extensible flag is set. 37assertFalse(Object.isExtensible(obj1)); 38obj1.x = 42; 39assertEquals(undefined, obj1.x); 40 41// Try adding a new element. 42obj1[1] = 42; 43assertEquals(undefined, obj1[1]); 44 45 46// Try when the object has an existing property. 47var obj2 = {}; 48assertTrue(Object.isExtensible(obj2)); 49obj2.x = 42; 50assertEquals(42, obj2.x); 51assertTrue(Object.isExtensible(obj2)); 52 53Object.preventExtensions(obj2); 54assertEquals(42, obj2.x); 55 56obj2.y = 42; 57// obj2.y should still be undefined. 58assertEquals(undefined, obj2.y); 59// Make sure we can still write values to obj.x. 60obj2.x = 43; 61assertEquals(43, obj2.x) 62 63obj2.y = new function() { return 42; }; 64// obj2.y should still be undefined. 65assertEquals(undefined, obj2.y); 66assertEquals(43, obj2.x) 67 68try { 69 Object.defineProperty(obj2, "y", {value: 42}); 70} catch (e) { 71 assertTrue(/object is not extensible/.test(e)); 72} 73 74// obj2.y should still be undefined. 75assertEquals(undefined, obj2.y); 76assertEquals(43, obj2.x); 77 78obj2[1] = 42; 79assertEquals(undefined, obj2[1]); 80 81var arr = new Array(); 82arr[1] = 10; 83 84Object.preventExtensions(arr); 85 86arr[2] = 42; 87assertEquals(10, arr[1]); 88 89// We should still be able to change exiting elements. 90arr[1]= 42; 91assertEquals(42, arr[1]); 92 93 94// Test the the extensible flag is not inherited. 95var parent = {}; 96parent.x = 42; 97Object.preventExtensions(parent); 98 99var child = Object.create(parent); 100 101// We should be able to add new properties to the child object. 102child.y = 42; 103 104// This should have no influence on the parent class. 105parent.y = 29; 106 107 108// Test that attributes on functions are also handled correctly. 109function foo() { 110 return 42; 111} 112 113Object.preventExtensions(foo); 114 115foo.x = 29; 116assertEquals(undefined, foo.x); 117 118// when Object.isExtensible(o) === false 119// assignment should return right hand side value 120var o = Object.preventExtensions({}); 121var v = o.v = 50; 122assertEquals(undefined, o.v); 123assertEquals(50, v); 124 125// test same behavior as above, but for integer properties 126var n = o[0] = 100; 127assertEquals(undefined, o[0]); 128assertEquals(100, n); 129