blob: 344c50a7e4e300f1a9b9f3048984aff59b16000d [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005(function testNonConstructable() {
6 var proxy = new Proxy({},{});
7 assertThrows(function(){ new proxy() }, TypeError);
8
9 var proxy2 = new Proxy(proxy, {});
10 assertThrows(function(){ proxy2() }, TypeError);
11})();
12
13(function testFailingConstructRevoked() {
14 var pair = Proxy.revocable(Array, {});
15 var instance = new pair.proxy();
16 pair.revoke();
17 assertThrows(function(){ new pair.proxy() }, TypeError);
18})();
19
20(function testFailingGetTrap() {
21 var handler = {
22 get() {
23 throw TypeError();
24 }
25 }
26 var proxy = new Proxy({},{});
27 var proxy2 = new Proxy({}, proxy);
28 assertThrows(function(){ new proxy2() }, TypeError);
29})();
30
31(function testConstructFallback() {
32 var called = false;
33 function Target() {
34 called = true;
35 this.property1 = 'value1';
36 };
37 Target.prototype = {};
38 var proxy = new Proxy(Target, {});
39
40 assertFalse(called);
41 var instance = new proxy();
42 assertTrue(called);
43 assertEquals('value1', instance.property1);
44 assertSame(Target.prototype, Reflect.getPrototypeOf(instance));
45
46 var proxy2 = new Proxy(proxy, {});
47 called = false;
48 var instance2 = new proxy2();
49 assertTrue(called);
50 assertEquals('value1', instance2.property1);
51 assertSame(Target.prototype, Reflect.getPrototypeOf(instance));
52})();
53
54(function testConstructTrapDirectReturn() {
55 function Target(a, b) {
56 this.sum = a + b;
57 };
58 var handler = {
59 construct(t, c, args) {
60 return { sum: 42 };
61 }
62 };
63 var proxy = new Proxy(Target, handler);
64 assertEquals(42, (new proxy(1, 2)).sum);
65})();
66
67(function testConstructTrap() {
68 function Target(arg1, arg2) {
69 this.arg1 = arg1;
70 this.arg2 = arg2;
71 }
72 var seen_target, seen_arguments, seen_new_target;
73 var handler = {
74 construct(target, args, new_target) {
75 seen_target = target;
76 seen_arguments = args;
77 seen_new_target = new_target;
78 return Reflect.construct(target, args, new_target);
79 }
80 }
81 var proxy = new Proxy(Target, handler);
82 var instance = new proxy('a', 'b');
83 assertEquals(Target, seen_target);
84 assertEquals(['a','b'], seen_arguments);
85 assertEquals(proxy, seen_new_target);
86 assertEquals('a', instance.arg1);
87 assertEquals('b', instance.arg2);
88
89 var instance2 = Reflect.construct(proxy, ['a1', 'b1'], Array);
90 assertEquals(Target, seen_target);
91 assertEquals(['a1', 'b1'], seen_arguments);
92 assertEquals(Array, seen_new_target);
93 assertEquals('a1', instance2.arg1);
94 assertEquals('b1', instance2.arg2);
95})();
96
97(function testConstructCrossRealm() {
98 var realm1 = Realm.create();
99 var handler = {
100 construct(target, args, new_target) {
101 return args;
102 }
103 };
104 var OtherProxy = Realm.eval(realm1, "Proxy");
105 var otherArrayPrototype = Realm.eval(realm1, 'Array.prototype');
106
107 // Proxy and handler are from this realm.
108 var proxy = new Proxy(Array, handler);
109 var result = new proxy();
110 assertSame(Array.prototype, Reflect.getPrototypeOf(result));
111
112 // Proxy is from this realm, handler is from realm1.
113 var otherProxy = new OtherProxy(Array, handler);
114 var otherResult = new otherProxy();
115 assertSame(Array.prototype, Reflect.getPrototypeOf(otherResult));
116
117 // Proxy and handler are from realm1.
118 var otherProxy2 = Realm.eval(realm1, 'new Proxy('+
119 'Array, { construct(target, args, new_target) { return args }} )');
120 var otherResult2 = new otherProxy2();
121 assertSame(Array.prototype, Reflect.getPrototypeOf(otherResult2));
122})();
123
124(function testReflectConstructCrossReal() {
125 var realm1 = Realm.create();
126 var realm2 = Realm.create();
127 var realm3 = Realm.create();
128 var realm4 = Realm.create();
129
130 var argsRealm1 = Realm.eval(realm1, '[]');
131 var ProxyRealm2 = Realm.eval(realm2, 'Proxy');
132 var constructorRealm3 = Realm.eval(realm3, '(function(){})');
133 var handlerRealm4 = Realm.eval(realm4,
134 '({ construct(target, args, new_target) {return args} })');
135
136 var proxy = new ProxyRealm2(constructorRealm3, handlerRealm4);
137
138 // Check that the arguments array returned by handlerRealm4 is created in the
139 // realm of the Reflect.construct function.
140 var result = Reflect.construct(proxy, argsRealm1);
141 assertSame(Array.prototype, Reflect.getPrototypeOf(result));
142
143 var ReflectConstructRealm1 = Realm.eval(realm1, 'Reflect.construct');
144 var result2 = ReflectConstructRealm1(proxy, argsRealm1);
145 assertSame(Realm.eval(realm1, 'Array.prototype'),
146 Reflect.getPrototypeOf(result2));
147
148 var result3 = ReflectConstructRealm1(proxy, []);
149 assertSame(Realm.eval(realm1, 'Array.prototype'),
150 Reflect.getPrototypeOf(result3));
151
152 var ReflectConstructRealm2 = Realm.eval(realm2, 'Reflect.construct');
153 var result4 = ReflectConstructRealm2(proxy, argsRealm1);
154 assertSame(Realm.eval(realm2, 'Array.prototype'),
155 Reflect.getPrototypeOf(result4));
156})();