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 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// 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. 31 32// Check that no exceptions are thrown. 33Object.create(null); 34Object.create(null, undefined); 35 36// Check that the right exception is thrown. 37try { 38 Object.create(4); 39 assertTrue(false); 40} catch (e) { 41 assertTrue(/Object or null/.test(e)); 42} 43 44try { 45 Object.create("foo"); 46 print(2); 47 assertTrue(false); 48} catch (e) { 49 assertTrue(/Object or null/.test(e)); 50} 51 52var ctr = 0; 53var ctr2 = 0; 54var ctr3 = 0; 55var ctr4 = 0; 56var ctr5 = 0; 57var ctr6 = 1000; 58 59var protoFoo = { foo: function() { ctr++; }}; 60var fooValue = { foo: { writable: true, value: function() { ctr2++; }}}; 61var fooGetter = { foo: { get: function() { return ctr3++; }}}; 62var fooSetter = { foo: { set: function() { return ctr4++; }}}; 63var fooAmbiguous = { foo: { get: function() { return ctr3++; }, 64 value: 3 }}; 65 66function valueGet() { ctr5++; return 3 }; 67function getterGet() { ctr5++; return function() { return ctr6++; }; }; 68 69// Simple object with prototype, no properties added. 70Object.create(protoFoo).foo(); 71assertEquals(1, ctr); 72 73// Simple object with object with prototype, no properties added. 74Object.create(Object.create(protoFoo)).foo(); 75assertEquals(2, ctr); 76 77// Add a property foo that returns a function. 78var v = Object.create(protoFoo, fooValue); 79v.foo(); 80assertEquals(2, ctr); 81assertEquals(1, ctr2); 82 83// Ensure the property is writable. 84v.foo = 42; 85assertEquals(42, v.foo); 86assertEquals(2, ctr); 87assertEquals(1, ctr2); 88 89// Ensure by default properties are not writable. 90v = Object.create(null, { foo: {value: 103}}); 91assertEquals(103, v.foo); 92v.foo = 42; 93assertEquals(103, v.foo); 94 95// Add a getter foo that returns a counter value. 96assertEquals(0, Object.create(protoFoo, fooGetter).foo); 97assertEquals(2, ctr); 98assertEquals(1, ctr2); 99assertEquals(1, ctr3); 100 101// Add a setter foo that runs a function. 102assertEquals(1, Object.create(protoFoo, fooSetter).foo = 1); 103assertEquals(2, ctr); 104assertEquals(1, ctr2); 105assertEquals(1, ctr3); 106assertEquals(1, ctr4); 107 108// Make sure that trying to add both a value and a getter 109// will result in an exception. 110try { 111 Object.create(protoFoo, fooAmbiguous); 112 assertTrue(false); 113} catch (e) { 114 assertTrue(/Invalid property/.test(e)); 115} 116assertEquals(2, ctr); 117assertEquals(1, ctr2); 118assertEquals(1, ctr3); 119assertEquals(1, ctr4); 120 121var ctr7 = 0; 122 123var metaProps = { 124 enumerable: { get: function() { 125 assertEquals(0, ctr7++); 126 return true; 127 }}, 128 configurable: { get: function() { 129 assertEquals(1, ctr7++); 130 return true; 131 }}, 132 value: { get: function() { 133 assertEquals(2, ctr7++); 134 return 4; 135 }}, 136 writable: { get: function() { 137 assertEquals(3, ctr7++); 138 return true; 139 }}, 140 get: { get: function() { 141 assertEquals(4, ctr7++); 142 return function() { }; 143 }}, 144 set: { get: function() { 145 assertEquals(5, ctr7++); 146 return function() { }; 147 }} 148}; 149 150 151// Instead of a plain props object, let's use getters to return its properties. 152var magicValueProps = { foo: Object.create(null, { value: { get: valueGet }})}; 153var magicGetterProps = { foo: Object.create(null, { get: { get: getterGet }})}; 154var magicAmbiguousProps = { foo: Object.create(null, metaProps) }; 155 156assertEquals(3, Object.create(null, magicValueProps).foo); 157assertEquals(1, ctr5); 158 159assertEquals(1000, Object.create(null, magicGetterProps).foo); 160assertEquals(2, ctr5); 161 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. 165try { 166 Object.create(null, magicAmbiguousProps); 167 assertTrue(false); 168} catch (e) { 169 assertTrue(/Invalid property/.test(e)); 170 assertEquals(6, ctr7); 171} 172 173var magicWritableProps = { 174 foo: Object.create(null, { value: { value: 4 }, 175 writable: { get: function() { 176 ctr6++; 177 return false; 178 }}})}; 179 180var fooNotWritable = Object.create(null, magicWritableProps) 181assertEquals(1002, ctr6); 182assertEquals(4, fooNotWritable.foo); 183fooNotWritable.foo = 5; 184assertEquals(4, fooNotWritable.foo); 185 186 187// Test enumerable flag. 188 189var fooNotEnumerable = 190 Object.create({fizz: 14}, {foo: {value: 3, enumerable: false}, 191 bar: {value: 4, enumerable: true}, 192 baz: {value: 5}}); 193var sum = 0; 194for (x in fooNotEnumerable) { 195 assertTrue(x === 'bar' || x === 'fizz'); 196 sum += fooNotEnumerable[x]; 197} 198assertEquals(18, sum); 199 200 201try { 202 Object.create(null, {foo: { get: 0 }}); 203 assertTrue(false); 204} catch (e) { 205 assertTrue(/Getter must be a function/.test(e)); 206} 207 208try { 209 Object.create(null, {foo: { set: 0 }}); 210 assertTrue(false); 211} catch (e) { 212 assertTrue(/Setter must be a function/.test(e)); 213} 214 215try { 216 Object.create(null, {foo: { set: 0, get: 0 }}); 217 assertTrue(false); 218} catch (e) { 219 assertTrue(/Getter must be a function/.test(e)); 220} 221 222 223// Ensure that only enumerable own properties on the descriptor are used. 224var 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 }}); 230 231assertEquals(1, tricky.foo.value); 232assertEquals(2, tricky.bar.value); 233assertEquals(4, tricky.baz.value); 234assertEquals(8, tricky.fizz.value); 235assertEquals(16, tricky.buzz.value); 236 237var sonOfTricky = Object.create(null, tricky); 238 239assertFalse("foo" in sonOfTricky); 240assertFalse("bar" in sonOfTricky); 241assertTrue("baz" in sonOfTricky); 242assertFalse("fizz" in sonOfTricky); 243assertTrue("buzz" in sonOfTricky); 244 245var sum = 0; 246for (x in sonOfTricky) { 247 assertTrue(x === 'buzz'); 248 sum += sonOfTricky[x]; 249} 250assertEquals(16, sum); 251