1// Copyright 2010-2015 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 Reflect.preventExtensions method - ES6 26.1.12.
29// This is adapted from object-prevent-extensions.js.
30
31// Flags: --allow-natives-syntax
32
33
34var obj1 = {};
35// Extensible defaults to true.
36assertTrue(Object.isExtensible(obj1));
37assertTrue(Reflect.preventExtensions(obj1));
38
39// Make sure the is_extensible flag is set.
40assertFalse(Object.isExtensible(obj1));
41obj1.x = 42;
42assertEquals(undefined, obj1.x);
43
44// Try adding a new element.
45obj1[1] = 42;
46assertEquals(undefined, obj1[1]);
47
48
49// Try when the object has an existing property.
50var obj2 = {};
51assertTrue(Object.isExtensible(obj2));
52obj2.x = 42;
53assertEquals(42, obj2.x);
54assertTrue(Object.isExtensible(obj2));
55
56assertTrue(Reflect.preventExtensions(obj2));
57assertEquals(42, obj2.x);
58
59obj2.y = 42;
60// obj2.y should still be undefined.
61assertEquals(undefined, obj2.y);
62// Make sure we can still write values to obj.x.
63obj2.x = 43;
64assertEquals(43, obj2.x)
65
66obj2.y = new function() { return 42; };
67// obj2.y should still be undefined.
68assertEquals(undefined, obj2.y);
69assertEquals(43, obj2.x)
70
71try {
72  Object.defineProperty(obj2, "y", {value: 42});
73} catch (e) {
74  assertTrue(/object is not extensible/.test(e));
75}
76
77// obj2.y should still be undefined.
78assertEquals(undefined, obj2.y);
79assertEquals(43, obj2.x);
80
81obj2[1] = 42;
82assertEquals(undefined, obj2[1]);
83
84var arr = new Array();
85arr[1] = 10;
86
87assertTrue(Reflect.preventExtensions(arr));
88
89arr[2] = 42;
90assertEquals(10, arr[1]);
91
92// We should still be able to change existing elements.
93arr[1]= 42;
94assertEquals(42, arr[1]);
95
96
97// Test the the extensible flag is not inherited.
98var parent = {};
99parent.x = 42;
100assertTrue(Reflect.preventExtensions(parent));
101
102var child = Object.create(parent);
103
104// We should be able to add new properties to the child object.
105child.y = 42;
106
107// This should have no influence on the parent class.
108parent.y = 29;
109
110
111// Test that attributes on functions are also handled correctly.
112function foo() {
113  return 42;
114}
115
116assertTrue(Reflect.preventExtensions(foo));
117
118foo.x = 29;
119assertEquals(undefined, foo.x);
120
121// when Object.isExtensible(o) === false
122// assignment should return right hand side value
123var o = {};
124assertTrue(Reflect.preventExtensions(o));
125var v = o.v = 50;
126assertEquals(undefined, o.v);
127assertEquals(50, v);
128
129// test same behavior as above, but for integer properties
130var n = o[0] = 100;
131assertEquals(undefined, o[0]);
132assertEquals(100, n);
133
134// Fast properties should remain fast
135obj = { x: 42, y: 'foo' };
136assertTrue(%HasFastProperties(obj));
137assertTrue(Reflect.preventExtensions(obj));
138assertFalse(Object.isExtensible(obj));
139assertFalse(Object.isSealed(obj));
140assertTrue(%HasFastProperties(obj));
141
142// Non-extensible objects should share maps where possible
143obj = { prop1: 1, prop2: 2 };
144obj2 = { prop1: 3, prop2: 4 };
145assertTrue(%HaveSameMap(obj, obj2));
146assertTrue(Reflect.preventExtensions(obj));
147assertTrue(Reflect.preventExtensions(obj2));
148assertFalse(Object.isExtensible(obj));
149assertFalse(Object.isExtensible(obj2));
150assertFalse(Object.isSealed(obj));
151assertFalse(Object.isSealed(obj2));
152assertTrue(%HaveSameMap(obj, obj2));
153
154// Non-extensible objects should share maps even when they have elements
155obj = { prop1: 1, prop2: 2, 75: 'foo' };
156obj2 = { prop1: 3, prop2: 4, 150: 'bar' };
157assertTrue(%HaveSameMap(obj, obj2));
158assertTrue(Reflect.preventExtensions(obj));
159assertTrue(Reflect.preventExtensions(obj2));
160assertFalse(Object.isExtensible(obj));
161assertFalse(Object.isExtensible(obj2));
162assertFalse(Object.isSealed(obj));
163assertFalse(Object.isSealed(obj2));
164assertTrue(%HaveSameMap(obj, obj2));
165