object-create.js 8.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test ES5 sections 15.2.3.5 Object.create.
// We do not support nonconfigurable properties on objects so that is not
// tested.  We do test getters, setters, writable, enumerable and value.

// Check that no exceptions are thrown.
Object.create(null);
Object.create(null, undefined);

// Check that the right exception is thrown.
try {
  Object.create(4);
  assertTrue(false);
} catch (e) {
  assertTrue(/Object or null/.test(e));
}

try {
  Object.create("foo");
  print(2);
  assertTrue(false);
} catch (e) {
  assertTrue(/Object or null/.test(e));
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
try {
  Object.create(null, this);
  assertTrue(false);
} catch(e) {
  assertTrue(/Property description/.test(e))
}

try {
  Object.create(null, [1, 2, 3]);
  assertTrue(false);
} catch(e) {
  assertTrue(/Property description/.test(e))
}

try {
  Object.create(null, new Proxy([1, 2, 3], {}));
  assertTrue(false);
} catch(e) {
  assertTrue(/Property description/.test(e))
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
var ctr = 0;
var ctr2 = 0;
var ctr3 = 0;
var ctr4 = 0;
var ctr5 = 0;
var ctr6 = 1000;

var protoFoo = { foo: function() { ctr++; }};
var fooValue = { foo: { writable: true, value: function() { ctr2++; }}};
var fooGetter = { foo: { get: function() { return ctr3++; }}};
var fooSetter = { foo: { set: function() { return ctr4++; }}};
var fooAmbiguous = { foo: { get: function() { return ctr3++; },
                            value: 3 }};

function valueGet() { ctr5++; return 3 };
function getterGet() { ctr5++; return function() { return ctr6++; }; };

// Simple object with prototype, no properties added.
Object.create(protoFoo).foo();
assertEquals(1, ctr);

// Simple object with object with prototype, no properties added.
Object.create(Object.create(protoFoo)).foo();
assertEquals(2, ctr);

// Add a property foo that returns a function.
var v = Object.create(protoFoo, fooValue);
v.foo();
assertEquals(2, ctr);
assertEquals(1, ctr2);

// Ensure the property is writable.
v.foo = 42;
assertEquals(42, v.foo);
assertEquals(2, ctr);
assertEquals(1, ctr2);

// Ensure by default properties are not writable.
v = Object.create(null, { foo: {value: 103}});
assertEquals(103, v.foo);
v.foo = 42;
assertEquals(103, v.foo);

// Add a getter foo that returns a counter value.
assertEquals(0, Object.create(protoFoo, fooGetter).foo);
assertEquals(2, ctr);
assertEquals(1, ctr2);
assertEquals(1, ctr3);

// Add a setter foo that runs a function.
assertEquals(1, Object.create(protoFoo, fooSetter).foo = 1);
assertEquals(2, ctr);
assertEquals(1, ctr2);
assertEquals(1, ctr3);
assertEquals(1, ctr4);

// Make sure that trying to add both a value and a getter
// will result in an exception.
try {
  Object.create(protoFoo, fooAmbiguous);
  assertTrue(false);
} catch (e) {
  assertTrue(/Invalid property/.test(e));
}
assertEquals(2, ctr);
assertEquals(1, ctr2);
assertEquals(1, ctr3);
assertEquals(1, ctr4);

var ctr7 = 0;

var metaProps = {
  enumerable: { get: function() {
                       assertEquals(0, ctr7++);
                       return true;
                     }},
  configurable: { get: function() {
                         assertEquals(1, ctr7++);
                         return true;
                       }},
  value: { get: function() {
                  assertEquals(2, ctr7++);
                  return 4;
                }},
  writable: { get: function() {
                     assertEquals(3, ctr7++);
                     return true;
                   }},
  get: { get: function() {
                assertEquals(4, ctr7++);
                return function() { };
              }},
  set: { get: function() {
                assertEquals(5, ctr7++);
                return function() { };
              }}
};


// Instead of a plain props object, let's use getters to return its properties.
var magicValueProps = { foo: Object.create(null, { value: { get: valueGet }})};
var magicGetterProps = { foo: Object.create(null, { get: { get: getterGet }})};
var magicAmbiguousProps = { foo: Object.create(null, metaProps) };

assertEquals(3, Object.create(null, magicValueProps).foo);
assertEquals(1, ctr5);

assertEquals(1000, Object.create(null, magicGetterProps).foo);
assertEquals(2, ctr5);

// See if we do the steps in ToPropertyDescriptor in the right order.
// We shouldn't throw the exception for an ambiguous properties object
// before we got all the values out.
try {
  Object.create(null, magicAmbiguousProps);
  assertTrue(false);
} catch (e) {
  assertTrue(/Invalid property/.test(e));
  assertEquals(6, ctr7);
}

var magicWritableProps = {
  foo: Object.create(null, { value: { value: 4 },
                             writable: { get: function() {
                                                ctr6++;
                                                return false;
                                              }}})};

var fooNotWritable = Object.create(null, magicWritableProps)
assertEquals(1002, ctr6);
assertEquals(4, fooNotWritable.foo);
fooNotWritable.foo = 5;
assertEquals(4, fooNotWritable.foo);


// Test enumerable flag.

var fooNotEnumerable =
    Object.create({fizz: 14}, {foo: {value: 3, enumerable: false},
                               bar: {value: 4, enumerable: true},
                               baz: {value: 5}});
var sum = 0;
for (x in fooNotEnumerable) {
  assertTrue(x === 'bar' || x === 'fizz');
  sum += fooNotEnumerable[x];
}
assertEquals(18, sum);


try {
  Object.create(null, {foo: { get: 0 }});
  assertTrue(false);
} catch (e) {
  assertTrue(/Getter must be a function/.test(e));
}

try {
  Object.create(null, {foo: { set: 0 }});
  assertTrue(false);
} catch (e) {
  assertTrue(/Setter must be a function/.test(e));
}

try {
  Object.create(null, {foo: { set: 0, get: 0 }});
  assertTrue(false);
} catch (e) {
  assertTrue(/Getter must be a function/.test(e));
}


// Ensure that only enumerable own properties on the descriptor are used.
var tricky = Object.create(
  { foo: { value: 1, enumerable: true }},
  { bar: { value: { value: 2, enumerable: true }, enumerable: false },
    baz: { value: { value: 4, enumerable: false }, enumerable: true },
    fizz: { value: { value: 8, enumerable: false }, enumerable: false },
    buzz: { value: { value: 16, enumerable: true }, enumerable: true }});

assertEquals(1, tricky.foo.value);
assertEquals(2, tricky.bar.value);
assertEquals(4, tricky.baz.value);
assertEquals(8, tricky.fizz.value);
assertEquals(16, tricky.buzz.value);

var sonOfTricky = Object.create(null, tricky);

assertFalse("foo" in sonOfTricky);
assertFalse("bar" in sonOfTricky);
assertTrue("baz" in sonOfTricky);
assertFalse("fizz" in sonOfTricky);
assertTrue("buzz" in sonOfTricky);

var sum = 0;
for (x in sonOfTricky) {
  assertTrue(x === 'buzz');
  sum += sonOfTricky[x];
}
assertEquals(16, sum);
272 273 274 275 276 277 278 279 280 281 282


(function createWithEmptyProtoInfoCreateMap() {
  var proto = {a:1};
  var instance = {__proto__: proto };
  // Try force creation of prototype info on proto by loading a proto property.
  assertEquals(instance.a, 1);
  var result = Object.create(proto, {});
  assertEquals(result.a, 1);
  assertEquals(result.__proto__, proto);
})()