1 // Copyright 2009 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 // Test ES5 sections 15.2.3.5 Object.create.
29 // We do not support nonconfigurable properties on objects so that is not
30 // tested. We do test getters, setters, writable, enumerable and value.
32 // Check that no exceptions are thrown.
34 Object.create(null, undefined);
36 // Check that the right exception is thrown.
41 assertTrue(/Object or null/.test(e));
49 assertTrue(/Object or null/.test(e));
59 var protoFoo = { foo: function() { ctr++; }};
60 var fooValue = { foo: { writable: true, value: function() { ctr2++; }}};
61 var fooGetter = { foo: { get: function() { return ctr3++; }}};
62 var fooSetter = { foo: { set: function() { return ctr4++; }}};
63 var fooAmbiguous = { foo: { get: function() { return ctr3++; },
66 function valueGet() { ctr5++; return 3 };
67 function getterGet() { ctr5++; return function() { return ctr6++; }; };
69 // Simple object with prototype, no properties added.
70 Object.create(protoFoo).foo();
73 // Simple object with object with prototype, no properties added.
74 Object.create(Object.create(protoFoo)).foo();
77 // Add a property foo that returns a function.
78 var v = Object.create(protoFoo, fooValue);
81 assertEquals(1, ctr2);
83 // Ensure the property is writable.
85 assertEquals(42, v.foo);
87 assertEquals(1, ctr2);
89 // Ensure by default properties are not writable.
90 v = Object.create(null, { foo: {value: 103}});
91 assertEquals(103, v.foo);
93 assertEquals(103, v.foo);
95 // Add a getter foo that returns a counter value.
96 assertEquals(0, Object.create(protoFoo, fooGetter).foo);
98 assertEquals(1, ctr2);
99 assertEquals(1, ctr3);
101 // Add a setter foo that runs a function.
102 assertEquals(1, Object.create(protoFoo, fooSetter).foo = 1);
103 assertEquals(2, ctr);
104 assertEquals(1, ctr2);
105 assertEquals(1, ctr3);
106 assertEquals(1, ctr4);
108 // Make sure that trying to add both a value and a getter
109 // will result in an exception.
111 Object.create(protoFoo, fooAmbiguous);
114 assertTrue(/Invalid property/.test(e));
116 assertEquals(2, ctr);
117 assertEquals(1, ctr2);
118 assertEquals(1, ctr3);
119 assertEquals(1, ctr4);
124 enumerable: { get: function() {
125 assertEquals(0, ctr7++);
128 configurable: { get: function() {
129 assertEquals(1, ctr7++);
132 value: { get: function() {
133 assertEquals(2, ctr7++);
136 writable: { get: function() {
137 assertEquals(3, ctr7++);
140 get: { get: function() {
141 assertEquals(4, ctr7++);
142 return function() { };
144 set: { get: function() {
145 assertEquals(5, ctr7++);
146 return function() { };
151 // Instead of a plain props object, let's use getters to return its properties.
152 var magicValueProps = { foo: Object.create(null, { value: { get: valueGet }})};
153 var magicGetterProps = { foo: Object.create(null, { get: { get: getterGet }})};
154 var magicAmbiguousProps = { foo: Object.create(null, metaProps) };
156 assertEquals(3, Object.create(null, magicValueProps).foo);
157 assertEquals(1, ctr5);
159 assertEquals(1000, Object.create(null, magicGetterProps).foo);
160 assertEquals(2, ctr5);
162 // See if we do the steps in ToPropertyDescriptor in the right order.
163 // We shouldn't throw the exception for an ambiguous properties object
164 // before we got all the values out.
166 Object.create(null, magicAmbiguousProps);
169 assertTrue(/Invalid property/.test(e));
170 assertEquals(6, ctr7);
173 var magicWritableProps = {
174 foo: Object.create(null, { value: { value: 4 },
175 writable: { get: function() {
180 var fooNotWritable = Object.create(null, magicWritableProps)
181 assertEquals(1002, ctr6);
182 assertEquals(4, fooNotWritable.foo);
183 fooNotWritable.foo = 5;
184 assertEquals(4, fooNotWritable.foo);
187 // Test enumerable flag.
189 var fooNotEnumerable =
190 Object.create({fizz: 14}, {foo: {value: 3, enumerable: false},
191 bar: {value: 4, enumerable: true},
194 for (x in fooNotEnumerable) {
195 assertTrue(x === 'bar' || x === 'fizz');
196 sum += fooNotEnumerable[x];
198 assertEquals(18, sum);
202 Object.create(null, {foo: { get: 0 }});
205 assertTrue(/Getter must be a function/.test(e));
209 Object.create(null, {foo: { set: 0 }});
212 assertTrue(/Setter must be a function/.test(e));
216 Object.create(null, {foo: { set: 0, get: 0 }});
219 assertTrue(/Getter must be a function/.test(e));
223 // Ensure that only enumerable own properties on the descriptor are used.
224 var tricky = Object.create(
225 { foo: { value: 1, enumerable: true }},
226 { bar: { value: { value: 2, enumerable: true }, enumerable: false },
227 baz: { value: { value: 4, enumerable: false }, enumerable: true },
228 fizz: { value: { value: 8, enumerable: false }, enumerable: false },
229 buzz: { value: { value: 16, enumerable: true }, enumerable: true }});
231 assertEquals(1, tricky.foo.value);
232 assertEquals(2, tricky.bar.value);
233 assertEquals(4, tricky.baz.value);
234 assertEquals(8, tricky.fizz.value);
235 assertEquals(16, tricky.buzz.value);
237 var sonOfTricky = Object.create(null, tricky);
239 assertFalse("foo" in sonOfTricky);
240 assertFalse("bar" in sonOfTricky);
241 assertTrue("baz" in sonOfTricky);
242 assertFalse("fizz" in sonOfTricky);
243 assertTrue("buzz" in sonOfTricky);
246 for (x in sonOfTricky) {
247 assertTrue(x === 'buzz');
248 sum += sonOfTricky[x];
250 assertEquals(16, sum);