• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.seal and Object.isSealed 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.seal(non_objects[key]);
37    assertUnreachable();
38  } catch(e) {
39    assertTrue(/Object.seal called on non-object/.test(e));
40  }
41}
42
43for (var key in non_objects) {
44  try {
45    Object.isSealed(non_objects[key]);
46    assertUnreachable();
47  } catch(e) {
48    assertTrue(/Object.isSealed 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.isSealed(obj));
66
67Object.seal(obj);
68
69// Make sure we are no longer extensible.
70assertFalse(Object.isExtensible(obj));
71assertTrue(Object.isSealed(obj));
72
73// We should not be frozen, since we are still able to
74// update values.
75assertFalse(Object.isFrozen(obj));
76
77// We should not allow new properties to be added.
78obj.foo = 42;
79assertEquals(obj.foo, undefined);
80
81desc = Object.getOwnPropertyDescriptor(obj, 'x');
82assertTrue(desc.writable);
83assertFalse(desc.configurable);
84assertEquals(42, desc.value);
85
86desc = Object.getOwnPropertyDescriptor(obj, 'z');
87assertTrue(desc.writable);
88assertFalse(desc.configurable);
89assertEquals("foobar", desc.value);
90
91// Since writable is not affected by seal we should still be able to
92// update the values.
93obj.x = "43";
94assertEquals(43, obj.x);
95
96// Test on accessors.
97var obj2 = {};
98function get() { return 43; };
99function set() {};
100Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true });
101
102desc = Object.getOwnPropertyDescriptor(obj2, 'x');
103assertTrue(desc.configurable);
104assertEquals(undefined, desc.value);
105assertEquals(set, desc.set);
106assertEquals(get, desc.get);
107
108assertTrue(Object.isExtensible(obj2));
109assertFalse(Object.isSealed(obj2));
110Object.seal(obj2);
111
112// Since this is an accessor property the object is now effectively both
113// sealed and frozen (accessors has no writable attribute).
114assertTrue(Object.isFrozen(obj2));
115assertFalse(Object.isExtensible(obj2));
116assertTrue(Object.isSealed(obj2));
117
118desc = Object.getOwnPropertyDescriptor(obj2, 'x');
119assertFalse(desc.configurable);
120assertEquals(undefined, desc.value);
121assertEquals(set, desc.set);
122assertEquals(get, desc.get);
123
124obj2.foo = 42;
125assertEquals(obj2.foo, undefined);
126
127// Test seal on arrays.
128var arr = new Array(42,43);
129
130desc = Object.getOwnPropertyDescriptor(arr, '0');
131assertTrue(desc.configurable);
132assertTrue(desc.writable);
133assertEquals(42, desc.value);
134
135desc = Object.getOwnPropertyDescriptor(arr, '1');
136assertTrue(desc.configurable);
137assertTrue(desc.writable);
138assertEquals(43, desc.value);
139
140assertTrue(Object.isExtensible(arr));
141assertFalse(Object.isSealed(arr));
142Object.seal(arr);
143assertTrue(Object.isSealed(arr));
144assertFalse(Object.isExtensible(arr));
145// Since the values in the array is still writable this object
146// is not frozen.
147assertFalse(Object.isFrozen(arr));
148
149desc = Object.getOwnPropertyDescriptor(arr, '0');
150assertFalse(desc.configurable);
151assertTrue(desc.writable);
152assertEquals(42, desc.value);
153
154desc = Object.getOwnPropertyDescriptor(arr, '1');
155assertFalse(desc.configurable);
156assertTrue(desc.writable);
157assertEquals(43, desc.value);
158
159arr[0] = 'foo';
160
161// We should be able to overwrite the existing value.
162assertEquals('foo', arr[0]);
163
164
165// Test that isSealed returns the correct value even if configurable
166// has been set to false on all properties manually and the extensible
167// flag has also been set to false manually.
168var obj3 = { x: 42, y: 'foo' };
169
170assertFalse(Object.isFrozen(obj3));
171
172Object.defineProperty(obj3, 'x', {configurable: false, writable: true});
173Object.defineProperty(obj3, 'y', {configurable: false, writable: false});
174Object.preventExtensions(obj3);
175
176assertTrue(Object.isSealed(obj3));
177
178
179// Make sure that an object that has a configurable property
180// is not classified as sealed.
181var obj4 = {};
182Object.defineProperty(obj4, 'x', {configurable: true, writable: false});
183Object.defineProperty(obj4, 'y', {configurable: false, writable: false});
184Object.preventExtensions(obj4);
185
186assertFalse(Object.isSealed(obj4));
187
188// Make sure that Object.seal returns the sealed object.
189var obj4 = {}
190assertTrue(obj4 === Object.seal(obj4))
191