object-freeze.js revision 3bec4d28b1f388dbc06a9c4276e1a03e86c52b04
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  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
73try {
74   obj.foo = 42;
75   assertUnreachable();
76} catch(e) {
77  assertTrue(/object is not extensible/.test(e));
78}
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
121try {
122  obj2.foo = 42;
123  assertUnreachable();
124} catch(e) {
125  assertTrue(/object is not extensible/.test(e));
126}
127
128
129// Test freeze on arrays.
130var arr = new Array(42,43);
131
132desc = Object.getOwnPropertyDescriptor(arr, '0');
133assertTrue(desc.configurable);
134assertTrue(desc.writable);
135assertEquals(42, desc.value);
136
137desc = Object.getOwnPropertyDescriptor(arr, '1');
138assertTrue(desc.configurable);
139assertTrue(desc.writable);
140assertEquals(43, desc.value);
141
142assertTrue(Object.isExtensible(arr));
143assertFalse(Object.isFrozen(arr));
144Object.freeze(arr);
145assertTrue(Object.isFrozen(arr));
146assertFalse(Object.isExtensible(arr));
147
148desc = Object.getOwnPropertyDescriptor(arr, '0');
149assertFalse(desc.configurable);
150assertFalse(desc.writable);
151assertEquals(42, desc.value);
152
153desc = Object.getOwnPropertyDescriptor(arr, '1');
154assertFalse(desc.configurable);
155assertFalse(desc.writable);
156assertEquals(43, desc.value);
157
158arr[0] = 'foo';
159
160assertEquals(arr[0], 42);
161
162
163// Test that isFrozen return the correct value even if configurable has been set
164// to false on all properties manually and the extensible flag has also been set
165// to false manually.
166var obj3 = { x: 42, y: 'foo' };
167
168assertFalse(Object.isFrozen(obj3));
169
170Object.defineProperty(obj3, 'x', {configurable: false, writable: false});
171Object.defineProperty(obj3, 'y', {configurable: false, writable: false});
172Object.preventExtensions(obj3);
173
174assertTrue(Object.isFrozen(obj3));
175
176
177// Make sure that an object that has only non-configurable, but one
178// writable property, is not classified as frozen.
179var obj4 = {};
180Object.defineProperty(obj4, 'x', {configurable: false, writable: true});
181Object.defineProperty(obj4, 'y', {configurable: false, writable: false});
182Object.preventExtensions(obj4);
183
184assertFalse(Object.isFrozen(obj4));
185
186// Make sure that an object that has only non-writable, but one
187// configurable property, is not classified as frozen.
188var obj5 = {};
189Object.defineProperty(obj5, 'x', {configurable: true, writable: false});
190Object.defineProperty(obj5, 'y', {configurable: false, writable: false});
191Object.preventExtensions(obj5);
192
193assertFalse(Object.isFrozen(obj5));
194