blob: dc32342c0e6f7c58fb5594148a309de4e06957a1 [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));
Steve Block44f0eee2011-05-26 01:26:41 +010038obj1.x = 42;
Steve Block8defd9f2010-07-08 12:39:36 +010039assertEquals(undefined, obj1.x);
40
41// Try adding a new element.
Steve Block44f0eee2011-05-26 01:26:41 +010042obj1[1] = 42;
Steve Block8defd9f2010-07-08 12:39:36 +010043assertEquals(undefined, obj1[1]);
44
45
46// Try when the object has an existing property.
47var obj2 = {};
48assertTrue(Object.isExtensible(obj2));
49obj2.x = 42;
50assertEquals(42, obj2.x);
51assertTrue(Object.isExtensible(obj2));
52
53Object.preventExtensions(obj2);
54assertEquals(42, obj2.x);
55
Steve Block44f0eee2011-05-26 01:26:41 +010056obj2.y = 42;
Steve Block8defd9f2010-07-08 12:39:36 +010057// obj2.y should still be undefined.
58assertEquals(undefined, obj2.y);
59// Make sure we can still write values to obj.x.
60obj2.x = 43;
61assertEquals(43, obj2.x)
62
Steve Block44f0eee2011-05-26 01:26:41 +010063obj2.y = new function() { return 42; };
Steve Block8defd9f2010-07-08 12:39:36 +010064// obj2.y should still be undefined.
65assertEquals(undefined, obj2.y);
66assertEquals(43, obj2.x)
67
68try {
69 Object.defineProperty(obj2, "y", {value: 42});
70} catch (e) {
71 assertTrue(/object is not extensible/.test(e));
72}
73
74// obj2.y should still be undefined.
75assertEquals(undefined, obj2.y);
76assertEquals(43, obj2.x);
77
Steve Block44f0eee2011-05-26 01:26:41 +010078obj2[1] = 42;
Steve Block8defd9f2010-07-08 12:39:36 +010079assertEquals(undefined, obj2[1]);
80
81var arr = new Array();
82arr[1] = 10;
83
84Object.preventExtensions(arr);
85
Steve Block44f0eee2011-05-26 01:26:41 +010086arr[2] = 42;
Steve Block8defd9f2010-07-08 12:39:36 +010087assertEquals(10, arr[1]);
88
89// We should still be able to change exiting elements.
90arr[1]= 42;
91assertEquals(42, arr[1]);
92
93
94// Test the the extensible flag is not inherited.
95var parent = {};
96parent.x = 42;
97Object.preventExtensions(parent);
98
99var child = Object.create(parent);
100
101// We should be able to add new properties to the child object.
102child.y = 42;
103
104// This should have no influence on the parent class.
Steve Block44f0eee2011-05-26 01:26:41 +0100105parent.y = 29;
Steve Block8defd9f2010-07-08 12:39:36 +0100106
107
108// Test that attributes on functions are also handled correctly.
109function foo() {
110 return 42;
111}
112
113Object.preventExtensions(foo);
114
Steve Block44f0eee2011-05-26 01:26:41 +0100115foo.x = 29;
116assertEquals(undefined, foo.x);