blob: ebc2cfa69d6d55f028cf061c31d794a2340a9423 [file] [log] [blame]
Steve Block8defd9f2010-07-08 12:39:36 +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.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));
38// Try adding a new property.
39try {
40 obj1.x = 42;
41 assertUnreachable();
42} catch (e) {
43 assertTrue(/object is not extensible/.test(e));
44}
45assertEquals(undefined, obj1.x);
46
47// Try adding a new element.
48try {
49 obj1[1] = 42;
50 assertUnreachable();
51} catch (e) {
52 assertTrue(/object is not extensible/.test(e));
53}
54assertEquals(undefined, obj1[1]);
55
56
57// Try when the object has an existing property.
58var obj2 = {};
59assertTrue(Object.isExtensible(obj2));
60obj2.x = 42;
61assertEquals(42, obj2.x);
62assertTrue(Object.isExtensible(obj2));
63
64Object.preventExtensions(obj2);
65assertEquals(42, obj2.x);
66
67try {
68 obj2.y = 42;
69 assertUnreachable();
70} catch (e) {
71 assertTrue(/object is not extensible/.test(e));
72}
73
74// obj2.y should still be undefined.
75assertEquals(undefined, obj2.y);
76// Make sure we can still write values to obj.x.
77obj2.x = 43;
78assertEquals(43, obj2.x)
79
80try {
81 obj2.y = new function() { return 42; };
82 assertUnreachable();
83} catch (e) {
84 assertTrue(/object is not extensible/.test(e));
85}
86// obj2.y should still be undefined.
87assertEquals(undefined, obj2.y);
88assertEquals(43, obj2.x)
89
90try {
91 Object.defineProperty(obj2, "y", {value: 42});
92} catch (e) {
93 assertTrue(/object is not extensible/.test(e));
94}
95
96// obj2.y should still be undefined.
97assertEquals(undefined, obj2.y);
98assertEquals(43, obj2.x);
99
100try {
101 obj2[1] = 42;
102} catch (e) {
103 assertTrue(/object is not extensible/.test(e));
104}
105
106assertEquals(undefined, obj2[1]);
107
108var arr = new Array();
109arr[1] = 10;
110
111Object.preventExtensions(arr);
112
113try {
114 arr[2] = 42;
115 assertUnreachable();
116} catch (e) {
117 assertTrue(/object is not extensible/.test(e));
118}
119assertEquals(10, arr[1]);
120
121// We should still be able to change exiting elements.
122arr[1]= 42;
123assertEquals(42, arr[1]);
124
125
126// Test the the extensible flag is not inherited.
127var parent = {};
128parent.x = 42;
129Object.preventExtensions(parent);
130
131var child = Object.create(parent);
132
133// We should be able to add new properties to the child object.
134child.y = 42;
135
136// This should have no influence on the parent class.
137try {
138 parent.y = 29;
139 assertUnreachable();
140} catch (e) {
141 assertTrue(/object is not extensible/.test(e));
142}
143
144
145// Test that attributes on functions are also handled correctly.
146function foo() {
147 return 42;
148}
149
150Object.preventExtensions(foo);
151
152try {
153 foo.x = 29;
154 assertUnreachable();
155} catch (e) {
156 assertTrue(/object is not extensible/.test(e));
157}