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.freeze and Object.isFrozen methods - ES 15.2.3.9 and 29// ES 15.2.3.12 30 31 32// Test that we throw an error if an object is not passed as argument. 33var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); 34for (var key in non_objects) { 35 var exception = false; 36 try { 37 Object.freeze(non_objects[key]); 38 } catch(e) { 39 exception = true; 40 assertTrue(/Object.freeze called on non-object/.test(e)); 41 } 42 assertTrue(exception); 43} 44 45for (var key in non_objects) { 46 exception = false; 47 try { 48 Object.isFrozen(non_objects[key]); 49 } catch(e) { 50 exception = true; 51 assertTrue(/Object.isFrozen called on non-object/.test(e)); 52 } 53 assertTrue(exception); 54} 55 56// Test normal data properties. 57var obj = { x: 42, z: 'foobar' }; 58var desc = Object.getOwnPropertyDescriptor(obj, 'x'); 59assertTrue(desc.writable); 60assertTrue(desc.configurable); 61assertEquals(42, desc.value); 62 63desc = Object.getOwnPropertyDescriptor(obj, 'z'); 64assertTrue(desc.writable); 65assertTrue(desc.configurable); 66assertEquals('foobar', desc.value); 67 68assertTrue(Object.isExtensible(obj)); 69assertFalse(Object.isFrozen(obj)); 70 71Object.freeze(obj); 72 73// Make sure we are no longer extensible. 74assertFalse(Object.isExtensible(obj)); 75assertTrue(Object.isFrozen(obj)); 76 77obj.foo = 42; 78assertEquals(obj.foo, undefined); 79 80desc = Object.getOwnPropertyDescriptor(obj, 'x'); 81assertFalse(desc.writable); 82assertFalse(desc.configurable); 83assertEquals(42, desc.value); 84 85desc = Object.getOwnPropertyDescriptor(obj, 'z'); 86assertFalse(desc.writable); 87assertFalse(desc.configurable); 88assertEquals("foobar", desc.value); 89 90// Make sure that even if we try overwrite a value that is not writable, it is 91// not changed. 92obj.x = "tete"; 93assertEquals(42, obj.x); 94obj.x = { get: function() {return 43}, set: function() {} }; 95assertEquals(42, obj.x); 96 97// Test on accessors. 98var obj2 = {}; 99function get() { return 43; }; 100function set() {}; 101Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true }); 102 103desc = Object.getOwnPropertyDescriptor(obj2, 'x'); 104assertTrue(desc.configurable); 105assertEquals(undefined, desc.value); 106assertEquals(set, desc.set); 107assertEquals(get, desc.get); 108 109assertTrue(Object.isExtensible(obj2)); 110assertFalse(Object.isFrozen(obj2)); 111Object.freeze(obj2); 112assertTrue(Object.isFrozen(obj2)); 113assertFalse(Object.isExtensible(obj2)); 114 115desc = Object.getOwnPropertyDescriptor(obj2, 'x'); 116assertFalse(desc.configurable); 117assertEquals(undefined, desc.value); 118assertEquals(set, desc.set); 119assertEquals(get, desc.get); 120 121obj2.foo = 42; 122assertEquals(obj2.foo, undefined); 123 124 125// Test freeze on arrays. 126var arr = new Array(42,43); 127 128desc = Object.getOwnPropertyDescriptor(arr, '0'); 129assertTrue(desc.configurable); 130assertTrue(desc.writable); 131assertEquals(42, desc.value); 132 133desc = Object.getOwnPropertyDescriptor(arr, '1'); 134assertTrue(desc.configurable); 135assertTrue(desc.writable); 136assertEquals(43, desc.value); 137 138assertTrue(Object.isExtensible(arr)); 139assertFalse(Object.isFrozen(arr)); 140Object.freeze(arr); 141assertTrue(Object.isFrozen(arr)); 142assertFalse(Object.isExtensible(arr)); 143 144desc = Object.getOwnPropertyDescriptor(arr, '0'); 145assertFalse(desc.configurable); 146assertFalse(desc.writable); 147assertEquals(42, desc.value); 148 149desc = Object.getOwnPropertyDescriptor(arr, '1'); 150assertFalse(desc.configurable); 151assertFalse(desc.writable); 152assertEquals(43, desc.value); 153 154arr[0] = 'foo'; 155 156assertEquals(arr[0], 42); 157 158 159// Test that isFrozen return the correct value even if configurable has been set 160// to false on all properties manually and the extensible flag has also been set 161// to false manually. 162var obj3 = { x: 42, y: 'foo' }; 163 164assertFalse(Object.isFrozen(obj3)); 165 166Object.defineProperty(obj3, 'x', {configurable: false, writable: false}); 167Object.defineProperty(obj3, 'y', {configurable: false, writable: false}); 168Object.preventExtensions(obj3); 169 170assertTrue(Object.isFrozen(obj3)); 171 172 173// Make sure that an object that has only non-configurable, but one 174// writable property, is not classified as frozen. 175var obj4 = {}; 176Object.defineProperty(obj4, 'x', {configurable: false, writable: true}); 177Object.defineProperty(obj4, 'y', {configurable: false, writable: false}); 178Object.preventExtensions(obj4); 179 180assertFalse(Object.isFrozen(obj4)); 181 182// Make sure that an object that has only non-writable, but one 183// configurable property, is not classified as frozen. 184var obj5 = {}; 185Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); 186Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); 187Object.preventExtensions(obj5); 188 189assertFalse(Object.isFrozen(obj5)); 190 191// Make sure that Object.freeze returns the frozen object. 192var obj6 = {} 193assertTrue(obj6 === Object.freeze(obj6)) 194