promise-lookup-getter-setter.js 2.97 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
// Copyright 2016 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.

let get = () => {};
let set = () => {};
let target = {};
let handler = {
  getOwnPropertyDescriptor(target, prop) {
    let configurable = true;
    if (prop == "both") {
      return { get, set, configurable };
    } else if (prop == "get") {
      return { get, configurable };
    } else if (prop == "set") {
      return { set, configurable };
    } else if (prop == "data") {
      return { value: 42, configurable };
    } else {
      return Reflect.getOwnPropertyDescriptor(target, prop);
    }
  }
};

// Test behavior on own properties.
let proxy = new Proxy(target, handler);
assertSame(get, proxy.__lookupGetter__("both"));
assertSame(get, proxy.__lookupGetter__("get"));
assertSame(undefined, proxy.__lookupGetter__("set"));
assertSame(undefined, proxy.__lookupGetter__("data"));
assertSame(set, proxy.__lookupSetter__("both"));
assertSame(undefined, proxy.__lookupSetter__("get"));
assertSame(set, proxy.__lookupSetter__("set"));
assertSame(undefined, proxy.__lookupSetter__("data"));

// Test behavior on the prototype chain.
let object = { __proto__: proxy };
assertSame(get, object.__lookupGetter__("both"));
assertSame(get, object.__lookupGetter__("get"));
assertSame(undefined, object.__lookupGetter__("set"));
assertSame(undefined, object.__lookupGetter__("data"));
assertSame(set, object.__lookupSetter__("both"));
assertSame(undefined, object.__lookupSetter__("get"));
assertSame(set, object.__lookupSetter__("set"));
assertSame(undefined, object.__lookupSetter__("data"));

// Test being shadowed while on prototype chain.
let shadower = { __proto__: proxy, both: 1, get: 2, set: 3, data: 4 };
assertSame(undefined, shadower.__lookupGetter__("both"));
assertSame(undefined, shadower.__lookupGetter__("get"));
assertSame(undefined, shadower.__lookupGetter__("set"));
assertSame(undefined, shadower.__lookupGetter__("data"));
assertSame(undefined, shadower.__lookupSetter__("both"));
assertSame(undefined, shadower.__lookupSetter__("get"));
assertSame(undefined, shadower.__lookupSetter__("set"));
assertSame(undefined, shadower.__lookupSetter__("data"));

// Test getPrototypeOf trap.
let getFoo = () => {};
let setFoo = () => {};
let proto = {};
Reflect.defineProperty(proto, "foo", { get: getFoo, set: setFoo });
Reflect.setPrototypeOf(target, proto);
assertSame(getFoo, proxy.__lookupGetter__("foo"));
assertSame(setFoo, proxy.__lookupSetter__("foo"));
handler.getPrototypeOf = () => null;
assertSame(undefined, proxy.__lookupGetter__("foo"));
assertSame(undefined, proxy.__lookupSetter__("foo"));
handler.getPrototypeOf = () => proto;
assertSame(getFoo, proxy.__lookupGetter__("foo"));
assertSame(setFoo, proxy.__lookupSetter__("foo"));

// Test shadowing the prototype.
Reflect.defineProperty(proto, "data", { get: getFoo, set: setFoo });
assertSame(undefined, proxy.__lookupGetter__("data"));
assertSame(undefined, proxy.__lookupSetter__("data"));