blob: a94541c01a4bfb7229e8cd46eb37e874deb622a1 [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 testNonCallable() {
6 var proxy = new Proxy({},{});
7 assertThrows(function(){ proxy() }, TypeError);
8
9 var proxy2 = new Proxy(proxy, {});
10 assertThrows(function(){ proxy2() }, TypeError);
11})();
12
13(function testCallProxyFallbackNoArguments() {
14 var called = false;
15 var target = function() {
16 called = true;
17 }
18 var proxy = new Proxy(target, {});
19 assertFalse(called);
20 proxy();
21 assertTrue(called);
22
23 called = false;
24 var proxy2 = new Proxy(proxy, {});
25 assertFalse(called);
26 proxy2();
27 assertTrue(called);
28})();
29
30(function testCallProxyFallback1Argument() {
31 var called = false;
32 var target = function(a) {
33 called = true;
34 assertEquals('1', a);
35 }
36 var proxy = new Proxy(target, {});
37 assertFalse(called);
38 proxy('1');
39 assertTrue(called);
40})();
41
42(function testCallProxyFallback2Arguments() {
43 var called = false;
44 var target = function(a, b) {
45 called = true;
46 assertEquals('1', a);
47 assertEquals('2', b);
48 }
49 var proxy = new Proxy(target, {});
50 assertFalse(called);
51 proxy('1', '2');
52 assertTrue(called);
53})();
54
55(function testCallProxyFallbackChangedReceiver() {
56 var apply_receiver = {receiver:true};
57 var seen_receiver = undefined;
58 var target = function() {
59 seen_receiver = this;
60 }
61 var proxy = new Proxy(target, {});
62 assertEquals(undefined, seen_receiver);
63 Reflect.apply(proxy, apply_receiver, [1,2,3,4]);
64 assertSame(apply_receiver, seen_receiver);
65})();
66
67(function testCallProxyTrap() {
68 var called_target = false;
69 var called_handler = false;
70 var target = function(a, b) {
71 called_target = true;
72 assertEquals(1, a);
73 assertEquals(2, b);
74 }
75 var handler = {
76 apply: function(target, this_arg, args) {
77 target.apply(this_arg, args);
78 called_handler = true;
79 }
80 }
81 var proxy = new Proxy(target, handler);
82 assertFalse(called_target);
83 assertFalse(called_handler);
84 Reflect.apply(proxy, {rec:1}, [1,2]);
85 assertTrue(called_target);
86 assertTrue(called_handler);
87})();
Ben Murdoch097c5b22016-05-18 11:27:45 +010088
89
90(function testCallProxyNonCallableTarget() {
91 var values = [NaN, 1.5, 100, /RegExp/, "string", {}, [], Symbol(),
92 new Map(), new Set(), new WeakMap(), new WeakSet()];
93 values.forEach(target => {
94 target = Object(target);
95 var proxy = new Proxy(target, { apply() { assertUnreachable(); } });
96 assertThrows(() => { proxy(); }, TypeError);
97 assertThrows(() => { ({ proxy }).proxy(); }, TypeError);
98 assertThrows(() => { Reflect.apply(proxy, null, []); }, TypeError);
99 assertThrows(() => { Reflect.apply(proxy, { proxy }, []); }, TypeError);
100 assertThrows(() => {
101 Function.prototype.call.apply(proxy, [null]);
102 }, TypeError);
103 assertThrows(() => {
104 Function.prototype.apply.apply(proxy, [null, []]);
105 }, TypeError);
106
107 var proxy_to_proxy = new Proxy(proxy, { apply() { assertUnreachable(); } });
108 assertThrows(() => { proxy_to_proxy(); }, TypeError);
109 assertThrows(() => { ({ proxy_to_proxy }).proxy_to_proxy(); }, TypeError);
110 assertThrows(() => { Reflect.apply(proxy_to_proxy, null, []); }, TypeError);
111 assertThrows(() => { Reflect.apply(proxy_to_proxy, { proxy }, []); },
112 TypeError);
113 assertThrows(() => {
114 Function.prototype.call.apply(proxy_to_proxy, [null]);
115 }, TypeError);
116 assertThrows(() => {
117 Function.prototype.apply.apply(proxy_to_proxy, [null, []]);
118 }, TypeError);
119 });
120})();