• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Copyright 2015 the V8 project authors. All rights reserved.
16// Use of this source code is governed by a BSD-style license that can be
17// found in the LICENSE file.
18
19var target = function () {};
20var handler = { apply (target) {
21  throw 42;
22}};
23
24var proxy = new Proxy (target, handler);
25
26try {
27  // opfunc_call
28  proxy (5)
29  assert (false);
30} catch (e) {
31  assert (e == 42);
32}
33
34try {
35  var revocable = Proxy.revokable (function () {}, {});
36  proxy = new Proxy(revocable.proxy, {})
37  revocable.revoke();
38  proxy (5)
39  assert (false);
40} catch (e) {
41  assert (e instanceof TypeError);
42}
43
44function sum (a, b) {
45  return a + b;
46}
47
48var handler = {
49  apply: function (target, thisArg, argumentsList) {
50    return target (argumentsList[0], argumentsList[1]) * 10;
51  }
52};
53
54var proxy1 = new Proxy(sum, handler);
55
56assert (sum (1, 2) === 3);
57assert (proxy1 (1, 2) === 30);
58
59// Non Callable tests
60var proxy = new Proxy ({},{});
61try {
62  proxy()
63  assert (false)
64} catch (e) {
65  assert (e instanceof TypeError)
66}
67
68var proxy2 = new Proxy(proxy, {});
69try {
70  proxy2()
71  assert (false)
72} catch (e) {
73  assert (e instanceof TypeError)
74}
75
76// No arguments
77var called = false;
78var target = function () {
79  called = true;
80}
81var proxy = new Proxy (target, {});
82assert (!called);
83proxy();
84assert (called);
85
86called = false;
87var proxy2 = new Proxy (proxy, {});
88assert (!called);
89proxy2();
90assert (called);
91
92//1 Argument
93var called = false;
94var target = function (a) {
95  called = true;
96  assert ('1' === a);
97}
98var proxy = new Proxy (target, {});
99assert (!called);
100proxy ('1');
101assert (called);
102
103// 2 Arguments
104var called = false;
105var target = function (a, b) {
106  called = true;
107  assert ('1' === a);
108  assert ('2' === b);
109}
110var proxy = new Proxy (target, {});
111assert (!called);
112proxy ('1', '2');
113assert (called);
114
115// Changed receiver
116var apply_receiver = {receiver:true};
117var seen_receiver = undefined;
118var target = function () {
119  seen_receiver = this;
120}
121var proxy = new Proxy (target, {});
122assert (undefined === seen_receiver);
123Reflect.apply (proxy, apply_receiver, [1,2,3,4]);
124assert (apply_receiver === seen_receiver);
125
126// Trap
127var called_target = false;
128var called_handler = false;
129var target = function (a, b) {
130  called_target = true;
131  assert (1 === a);
132  assert (2 === b);
133}
134var handler = {
135  apply: function (target, this_arg, args) {
136    target.apply (this_arg, args);
137    called_handler = true;
138  }
139}
140var proxy = new Proxy (target, handler);
141assert (!called_target);
142assert (!called_handler);
143Reflect.apply (proxy, {rec:1}, [1,2]);
144assert (called_target);
145assert (called_handler);
146
147// Trap array arg
148var called_target = false;
149var called_handler = false;
150var target = function (a, b) {
151  called_target = true;
152  var arg = [1, 2];
153  assert (arg[0] === a[0]);
154  assert (arg[1] === a[1]);
155  assert (3 === b);
156}
157var handler = {
158  apply: function (target, this_arg, args) {
159    target.apply (this_arg, args);
160    called_handler = true;
161  }
162}
163var proxy = new Proxy (target, handler);
164assert (!called_target);
165assert (!called_handler);
166proxy ([1,2], 3);
167assert (called_target);
168assert (called_handler);
169
170// Trap object arg
171var called_target = false;
172var called_handler = false;
173var target = function (o) {
174  called_target = true;
175  var obj = {a: 1, b: 2}
176  assert (obj.a === o.a);
177  assert (obj.b === o.b)
178}
179var handler = {
180  apply: function (target, this_arg, args) {
181    target.apply (this_arg, args);
182    called_handler = true;
183  }
184}
185var proxy = new Proxy (target, handler);
186assert (!called_target);
187assert (!called_handler);
188proxy ({a: 1, b: 2});
189assert (called_target);
190assert (called_handler);
191
192// Trap generator arg
193function* gen () {
194  yield 1;
195  yield 2;
196  yield 3;
197}
198var called_target = false;
199var called_handler = false;
200var target = function (g) {
201  called_target = true;
202  var arr = [1, 2, 3];
203  var arr2 = [...g];
204  assert (arr[0] === arr2[0]);
205  assert (arr[1] === arr2[1]);
206  assert (arr[2] === arr2[2]);
207}
208var handler = {
209  apply: function (target, this_arg, args) {
210    target.apply (this_arg, args);
211    called_handler = true;
212  }
213}
214var proxy = new Proxy (target, handler);
215assert (!called_target);
216assert (!called_handler);
217proxy (gen());
218assert (called_target);
219assert (called_handler);
220
221// Noncallable Trap
222var called_target = false;
223var target = function () {
224  called_target = true;
225};
226var handler = {
227  apply: 'non callable trap'
228};
229
230var proxy = new Proxy(target, handler);
231try {
232  proxy ();
233  assert (false);
234} catch (e) {
235  assert (e instanceof TypeError);
236}
237
238assert (!called_target);
239
240// Null trap
241var _args;
242var target = function (a, b) {
243  _args = [a, b];
244  return a + b;
245};
246var handler = {
247  apply: null
248};
249
250var proxy = new Proxy (target, handler);
251var result = proxy (1, 2);
252
253assert (result === 3);
254assert (_args.length === 2);
255assert (_args[0] === 1);
256assert (_args[1] === 2);
257
258var values = [NaN, 1.5, 100, /RegExp/, "string", {}, [], Symbol(),
259                new Map(), new Set(), new WeakMap(), new WeakSet()];
260values.forEach(target => {
261  target = Object (target);
262  var proxy = new Proxy(target, { apply() { assert (false) } });
263  try {
264    proxy();
265    assert (false);
266  } catch (e) {
267    assert (e instanceof TypeError);
268  }
269
270  try {
271    ({ proxy }).proxy();
272    assert (false);
273  } catch (e) {
274    assert (e instanceof TypeError);
275  }
276
277  try {
278    Reflect.apply(proxy, null, []);
279    assert (false);
280  } catch (e) {
281    assert (e instanceof TypeError);
282  }
283
284  try {
285    Reflect.apply(proxy, { proxy }, []);
286    assert (false);
287  } catch (e) {
288    assert (e instanceof TypeError);
289  }
290
291  try {
292    Reflect.apply(proxy, { proxy }, []);
293    assert (false);
294  } catch (e) {
295    assert (e instanceof TypeError);
296  }
297
298  try {
299    Function.prototype.call.apply (proxy, [null]);
300    assert (false);
301  } catch (e) {
302    assert (e instanceof TypeError);
303  }
304
305  try {
306    Function.prototype.apply.apply (proxy, [null, []]);
307    assert (false);
308  } catch (e) {
309    assert (e instanceof TypeError);
310  }
311
312  var proxy_to_proxy = new Proxy (proxy, { apply() { assert (false); } });
313
314  try {
315    proxy_to_proxy ();
316    assert (false);
317  } catch (e) {
318    assert (e instanceof TypeError);
319  }
320
321  try {
322    ({ proxy_to_proxy }).proxy_to_proxy();
323    assert (false);
324  } catch (e) {
325    assert (e instanceof TypeError);
326  }
327
328  try {
329    Reflect.apply (proxy_to_proxy, null, []);
330    assert (false);
331  } catch (e) {
332    assert (e instanceof TypeError);
333  }
334
335  try {
336    Reflect.apply (proxy_to_proxy, { proxy }, []);
337    assert (false);
338  } catch (e) {
339    assert (e instanceof TypeError);
340  }
341
342  try {
343    Function.prototype.call.apply (proxy_to_proxy, [null]);
344    assert (false);
345  } catch (e) {
346    assert (e instanceof TypeError);
347  }
348
349  try {
350    Function.prototype.apply.apply (proxy_to_proxy, [null, []]);
351    assert (false);
352  } catch (e) {
353    assert (e instanceof TypeError);
354  }
355});
356