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