proxies-enumerate.js 1.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2015 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: --harmony-proxies

var target = {
  "target_one": 1
};
target.__proto__ = {
  "target_two": 2
};
var handler = {
  enumerate: function(target) {
    function* keys() {
      yield "foo";
      yield "bar";
    }
    return keys();
20 21 22 23 24
  },
  // For-in calls "has" on every iteration, so for TestForIn() below to
  // detect all results of the "enumerate" trap, "has" must return true.
  has: function(target, name) {
    return true;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  }
}

var proxy = new Proxy(target, handler);

function TestForIn(receiver, expected) {
  var result = [];
  for (var k in receiver) {
    result.push(k);
  }
  assertEquals(expected, result);
}

TestForIn(proxy, ["foo", "bar"]);

// Properly call traps on proxies on the prototype chain.
var receiver = {
  "receiver_one": 1
};
receiver.__proto__ = proxy;
45
TestForIn(receiver, ["receiver_one", "foo", "bar"]);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

// Fall through to default behavior when trap is undefined.
handler.enumerate = undefined;
TestForIn(proxy, ["target_one", "target_two"]);
delete handler.enumerate;
TestForIn(proxy, ["target_one", "target_two"]);

// Non-string keys must be filtered.
function TestNonStringKey(key) {
  handler.enumerate = function(target) {
    function* keys() { yield key; }
    return keys();
  }
  assertThrows("for (var k in proxy) {}", TypeError);
}

TestNonStringKey(1);
TestNonStringKey(3.14);
TestNonStringKey(Symbol("foo"));
TestNonStringKey({bad: "value"});
TestNonStringKey(null);
TestNonStringKey(undefined);
TestNonStringKey(true);