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. 33 Object.create(null); 34 Object.create(null, undefined); 35 36 // Check that the right exception is thrown. 37 try { 38 Object.create(4); 39 assertTrue(false); 40 } catch (e) { 41 assertTrue(/Object or null/.test(e)); 42 } 43 44 try { 45 Object.create("foo"); 46 print(2); 47 assertTrue(false); 48 } catch (e) { 49 assertTrue(/Object or null/.test(e)); 50 } 51 52 var ctr = 0; 53 var ctr2 = 0; 54 var ctr3 = 0; 55 var ctr4 = 0; 56 var ctr5 = 0; 57 var ctr6 = 1000; 58 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++; }, 64 value: 3 }}; 65 66 function valueGet() { ctr5++; return 3 }; 67 function getterGet() { ctr5++; return function() { return ctr6++; }; }; 68 69 // Simple object with prototype, no properties added. 70 Object.create(protoFoo).foo(); 71 assertEquals(1, ctr); 72 73 // Simple object with object with prototype, no properties added. 74 Object.create(Object.create(protoFoo)).foo(); 75 assertEquals(2, ctr); 76 77 // Add a property foo that returns a function. 78 var v = Object.create(protoFoo, fooValue); 79 v.foo(); 80 assertEquals(2, ctr); 81 assertEquals(1, ctr2); 82 83 // Ensure the property is writable. 84 v.foo = 42; 85 assertEquals(42, v.foo); 86 assertEquals(2, ctr); 87 assertEquals(1, ctr2); 88 89 // Ensure by default properties are not writable. 90 v = Object.create(null, { foo: {value: 103}}); 91 assertEquals(103, v.foo); 92 v.foo = 42; 93 assertEquals(103, v.foo); 94 95 // Add a getter foo that returns a counter value. 96 assertEquals(0, Object.create(protoFoo, fooGetter).foo); 97 assertEquals(2, ctr); 98 assertEquals(1, ctr2); 99 assertEquals(1, ctr3); 100 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); 107 108 // Make sure that trying to add both a value and a getter 109 // will result in an exception. 110 try { 111 Object.create(protoFoo, fooAmbiguous); 112 assertTrue(false); 113 } catch (e) { 114 assertTrue(/Invalid property/.test(e)); 115 } 116 assertEquals(2, ctr); 117 assertEquals(1, ctr2); 118 assertEquals(1, ctr3); 119 assertEquals(1, ctr4); 120 121 var ctr7 = 0; 122 123 var 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. 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) }; 155 156 assertEquals(3, Object.create(null, magicValueProps).foo); 157 assertEquals(1, ctr5); 158 159 assertEquals(1000, Object.create(null, magicGetterProps).foo); 160 assertEquals(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. 165 try { 166 Object.create(null, magicAmbiguousProps); 167 assertTrue(false); 168 } catch (e) { 169 assertTrue(/Invalid property/.test(e)); 170 assertEquals(6, ctr7); 171 } 172 173 var magicWritableProps = { 174 foo: Object.create(null, { value: { value: 4 }, 175 writable: { get: function() { 176 ctr6++; 177 return false; 178 }}})}; 179 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); 185 186 187 // Test enumerable flag. 188 189 var fooNotEnumerable = 190 Object.create({fizz: 14}, {foo: {value: 3, enumerable: false}, 191 bar: {value: 4, enumerable: true}, 192 baz: {value: 5}}); 193 var sum = 0; 194 for (x in fooNotEnumerable) { 195 assertTrue(x === 'bar' || x === 'fizz'); 196 sum += fooNotEnumerable[x]; 197 } 198 assertEquals(18, sum); 199 200 201 try { 202 Object.create(null, {foo: { get: 0 }}); 203 assertTrue(false); 204 } catch (e) { 205 assertTrue(/Getter must be a function/.test(e)); 206 } 207 208 try { 209 Object.create(null, {foo: { set: 0 }}); 210 assertTrue(false); 211 } catch (e) { 212 assertTrue(/Setter must be a function/.test(e)); 213 } 214 215 try { 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. 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 }}); 230 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); 236 237 var sonOfTricky = Object.create(null, tricky); 238 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); 244 245 var sum = 0; 246 for (x in sonOfTricky) { 247 assertTrue(x === 'buzz'); 248 sum += sonOfTricky[x]; 249 } 250 assertEquals(16, sum); 251