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
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.
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.
28 // Tests the Object.preventExtensions method - ES 15.2.3.10
32 // Extensible defaults to true.
33 assertTrue(Object.isExtensible(obj1));
34 Object.preventExtensions(obj1);
36 // Make sure the is_extensible flag is set.
37 assertFalse(Object.isExtensible(obj1));
39 assertEquals(undefined, obj1.x);
41 // Try adding a new element.
43 assertEquals(undefined, obj1[1]);
46 // Try when the object has an existing property.
48 assertTrue(Object.isExtensible(obj2));
50 assertEquals(42, obj2.x);
51 assertTrue(Object.isExtensible(obj2));
53 Object.preventExtensions(obj2);
54 assertEquals(42, obj2.x);
57 // obj2.y should still be undefined.
58 assertEquals(undefined, obj2.y);
59 // Make sure we can still write values to obj.x.
61 assertEquals(43, obj2.x)
63 obj2.y = new function() { return 42; };
64 // obj2.y should still be undefined.
65 assertEquals(undefined, obj2.y);
66 assertEquals(43, obj2.x)
69 Object.defineProperty(obj2, "y", {value: 42});
71 assertTrue(/object is not extensible/.test(e));
74 // obj2.y should still be undefined.
75 assertEquals(undefined, obj2.y);
76 assertEquals(43, obj2.x);
79 assertEquals(undefined, obj2[1]);
81 var arr = new Array();
84 Object.preventExtensions(arr);
87 assertEquals(10, arr[1]);
89 // We should still be able to change exiting elements.
91 assertEquals(42, arr[1]);
94 // Test the the extensible flag is not inherited.
97 Object.preventExtensions(parent);
99 var child = Object.create(parent);
101 // We should be able to add new properties to the child object.
104 // This should have no influence on the parent class.
108 // Test that attributes on functions are also handled correctly.
113 Object.preventExtensions(foo);
116 assertEquals(undefined, foo.x);
118 // when Object.isExtensible(o) === false
119 // assignment should return right hand side value
120 var o = Object.preventExtensions({});
122 assertEquals(undefined, o.v);
125 // test same behavior as above, but for integer properties
127 assertEquals(undefined, o[0]);
128 assertEquals(100, n);