object-keys.js 3.04 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax

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
// Ensure empty keys are handled properly
(function() {
  const a = {};
  let k = Object.keys(a);
  %HeapObjectVerify(k);
  assertEquals(0, k.length);
})();

// Ensure non-enumerable keys are handled properly
(function() {
  const a = {};
  Object.defineProperty(a, 'x', {
    value: 1,
    enumerable: false
  });
  let k = Object.keys(a);
  %HeapObjectVerify(k);
  assertEquals(0, k.length);

  a.y = 2;
  k = Object.keys(a);
  %HeapObjectVerify(k);
  assertEquals(1, k.length);
})();

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Ensure that mutation of the Object.keys result doesn't affect the
// enumeration cache for fast-mode objects.
(function() {
  const a = {x:1, y:2};
  let k = Object.keys(a);
  %HeapObjectVerify(k);
  assertEquals(2, k.length);
  assertEquals("x", k[0]);
  assertEquals("y", k[1]);
  k[0] = "y";
  k[1] = "x";
  k = Object.keys(a);
  assertEquals(2, k.length);
  assertEquals("x", k[0]);
  assertEquals("y", k[1]);
})();

// Ensure that the copy-on-write keys are handled properly, even in
// the presence of Symbols.
(function() {
  const s = Symbol();
  const a = {[s]: 1};
  let k = Object.keys(a);
  %HeapObjectVerify(k);
  assertEquals(0, k.length);
  k.shift();
  assertEquals(0, k.length);
})();
60 61 62 63 64 65 66 67 68 69 70 71 72 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

// Ensure we invoke all steps on proxies.
(function ObjectKeysProxy() {
  let log = [];
  let result = Object.keys(new Proxy({}, {
    ownKeys(target) {
      log.push('ownKeys');
      return ['a', 'b', 'c'];
    },
    getOwnPropertyDescriptor(target, key) {
      log.push('getOwnPropertyDescriptor-' + key);
      if (key === 'b') return {enumerable: false, configurable: true};
      return {enumerable: true, configurable: true};
    }
  }));
  assertEquals(['a', 'c'], result);
  assertEquals(
      [
        'ownKeys', 'getOwnPropertyDescriptor-a', 'getOwnPropertyDescriptor-b',
        'getOwnPropertyDescriptor-c'
      ],
      log);

  // Test normal target.
  log = [];
  let target = {a: 1, b: 1, c: 1};
  let handler = {
    getOwnPropertyDescriptor(target, key) {
      log.push('getOwnPropertyDescriptor-' + key);
      if (key === 'b') return {enumerable: false, configurable: true};
      return {enumerable: true, configurable: true};
    }
  };
  result = Object.keys(new Proxy(target, handler));
  assertEquals(['a', 'c'], result);
  assertEquals(
      [
        'getOwnPropertyDescriptor-a', 'getOwnPropertyDescriptor-b',
        'getOwnPropertyDescriptor-c'
      ],
      log);

  // Test trap invocation with non-enumerable target properties.
  log = [];
  target = Object.create(Object.prototype, {
    a: {enumerable: true, configurable: true},
    b: {enumerable: false, configurable: true},
    c: {enumerable: true, configurable: true}
  });
  result = Object.keys(new Proxy(target, handler));
  assertEquals(['a', 'c'], result);
  assertEquals(
      [
        'getOwnPropertyDescriptor-a', 'getOwnPropertyDescriptor-b',
        'getOwnPropertyDescriptor-c'
      ],
      log);
})();