summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/es6/proxies-construct.js
blob: 344c50a7e4e300f1a9b9f3048984aff59b16000d (plain)
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
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
// 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.

(function testNonConstructable() {
  var proxy = new Proxy({},{});
  assertThrows(function(){ new proxy() }, TypeError);

  var proxy2 = new Proxy(proxy, {});
  assertThrows(function(){ proxy2() }, TypeError);
})();

(function testFailingConstructRevoked() {
  var pair = Proxy.revocable(Array, {});
  var instance = new pair.proxy();
  pair.revoke();
  assertThrows(function(){ new pair.proxy() }, TypeError);
})();

(function testFailingGetTrap() {
  var handler = {
    get() {
      throw TypeError();
    }
  }
  var proxy = new Proxy({},{});
  var proxy2 = new Proxy({}, proxy);
  assertThrows(function(){ new proxy2() }, TypeError);
})();

(function testConstructFallback() {
  var called = false;
  function Target() {
    called = true;
    this.property1 = 'value1';
  };
  Target.prototype = {};
  var proxy = new Proxy(Target, {});

  assertFalse(called);
  var instance = new proxy();
  assertTrue(called);
  assertEquals('value1', instance.property1);
  assertSame(Target.prototype, Reflect.getPrototypeOf(instance));

  var proxy2 = new Proxy(proxy, {});
  called = false;
  var instance2 = new proxy2();
  assertTrue(called);
  assertEquals('value1', instance2.property1);
  assertSame(Target.prototype, Reflect.getPrototypeOf(instance));
})();

(function testConstructTrapDirectReturn() {
  function Target(a, b) {
      this.sum = a + b;
  };
  var handler = {
      construct(t, c, args) {
          return { sum: 42 };
      }
  };
  var proxy = new Proxy(Target, handler);
  assertEquals(42, (new proxy(1, 2)).sum);
})();

(function testConstructTrap() {
  function Target(arg1, arg2) {
    this.arg1 = arg1;
    this.arg2 = arg2;
  }
  var seen_target, seen_arguments, seen_new_target;
  var handler = {
    construct(target, args, new_target) {
      seen_target = target;
      seen_arguments = args;
      seen_new_target = new_target;
      return Reflect.construct(target, args, new_target);
    }
  }
  var proxy = new Proxy(Target, handler);
  var instance = new proxy('a', 'b');
  assertEquals(Target, seen_target);
  assertEquals(['a','b'], seen_arguments);
  assertEquals(proxy, seen_new_target);
  assertEquals('a', instance.arg1);
  assertEquals('b', instance.arg2);

  var instance2 = Reflect.construct(proxy, ['a1', 'b1'], Array);
  assertEquals(Target, seen_target);
  assertEquals(['a1', 'b1'], seen_arguments);
  assertEquals(Array, seen_new_target);
  assertEquals('a1', instance2.arg1);
  assertEquals('b1', instance2.arg2);
})();

(function testConstructCrossRealm() {
  var realm1 = Realm.create();
  var handler = {
    construct(target, args, new_target) {
      return args;
    }
  };
  var OtherProxy = Realm.eval(realm1, "Proxy");
  var otherArrayPrototype = Realm.eval(realm1, 'Array.prototype');

  // Proxy and handler are from this realm.
  var proxy = new Proxy(Array, handler);
  var result = new proxy();
  assertSame(Array.prototype, Reflect.getPrototypeOf(result));

  // Proxy is from this realm, handler is from realm1.
  var otherProxy = new OtherProxy(Array, handler);
  var otherResult = new otherProxy();
  assertSame(Array.prototype, Reflect.getPrototypeOf(otherResult));

  // Proxy and handler are from realm1.
  var otherProxy2 = Realm.eval(realm1, 'new Proxy('+
        'Array, { construct(target, args, new_target) { return args }} )');
  var otherResult2 = new otherProxy2();
  assertSame(Array.prototype, Reflect.getPrototypeOf(otherResult2));
})();

(function testReflectConstructCrossReal() {
  var realm1 = Realm.create();
  var realm2 = Realm.create();
  var realm3 = Realm.create();
  var realm4 = Realm.create();

  var argsRealm1 = Realm.eval(realm1, '[]');
  var ProxyRealm2 = Realm.eval(realm2, 'Proxy');
  var constructorRealm3 = Realm.eval(realm3, '(function(){})');
  var handlerRealm4 = Realm.eval(realm4,
      '({ construct(target, args, new_target) {return args} })');

  var proxy = new ProxyRealm2(constructorRealm3, handlerRealm4);

  // Check that the arguments array returned by handlerRealm4 is created in the
  // realm of the Reflect.construct function.
  var result = Reflect.construct(proxy, argsRealm1);
  assertSame(Array.prototype, Reflect.getPrototypeOf(result));

  var ReflectConstructRealm1 = Realm.eval(realm1, 'Reflect.construct');
  var result2 = ReflectConstructRealm1(proxy, argsRealm1);
  assertSame(Realm.eval(realm1, 'Array.prototype'),
    Reflect.getPrototypeOf(result2));

  var result3 = ReflectConstructRealm1(proxy, []);
  assertSame(Realm.eval(realm1, 'Array.prototype'),
    Reflect.getPrototypeOf(result3));

  var ReflectConstructRealm2 = Realm.eval(realm2, 'Reflect.construct');
  var result4 = ReflectConstructRealm2(proxy, argsRealm1);
  assertSame(Realm.eval(realm2, 'Array.prototype'),
    Reflect.getPrototypeOf(result4));
})();