classes-proxy.js 1.72 KB
Newer Older
1 2 3 4
// 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.

5
// Flags: --allow-natives-syntax
6 7

function CreateConstructableProxy(handler) {
8
  return new Proxy(function(){}, handler);
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
}

(function() {
  var prototype = { x: 1 };
  var log = [];

  var proxy = CreateConstructableProxy({
    get(k) {
      log.push("get trap");
      return prototype;
    }});

  var o = Reflect.construct(Number, [100], proxy);
  assertEquals(["get trap"], log);
  assertTrue(Object.getPrototypeOf(o) === prototype);
  assertEquals(100, Number.prototype.valueOf.call(o));
})();

(function() {
  var prototype = { x: 1 };
  var log = [];

  var proxy = CreateConstructableProxy({
    get(k) {
      log.push("get trap");
      return 10;
    }});

  var o = Reflect.construct(Number, [100], proxy);
  assertEquals(["get trap"], log);
39
  assertTrue(Object.getPrototypeOf(o) === Number.prototype);
40 41
  assertEquals(100, Number.prototype.valueOf.call(o));
})();
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

(function() {
  var prototype = { x: 1 };
  var log = [];

  var proxy = CreateConstructableProxy({
    get(k) {
      log.push("get trap");
      return prototype;
    }});

  var o = Reflect.construct(Function, ["return 1000"], proxy);
  assertEquals(["get trap"], log);
  assertTrue(Object.getPrototypeOf(o) === prototype);
  assertEquals(1000, o());
})();

(function() {
  var prototype = { x: 1 };
  var log = [];

  var proxy = CreateConstructableProxy({
    get(k) {
      log.push("get trap");
      return prototype;
    }});

  var o = Reflect.construct(Array, [1, 2, 3], proxy);
  assertEquals(["get trap"], log);
  assertTrue(Object.getPrototypeOf(o) === prototype);
  assertEquals([1, 2, 3], o);
})();