-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtestDoubleLifting.js
37 lines (31 loc) · 1.02 KB
/
testDoubleLifting.js
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
// for node.js
if(typeof require === 'function') {
var load = require;
var print = function(msg) {
if(/^fail/.test(msg)) { console.error(msg); }
else { console.log(msg); }
}
}
load('../reflect.js');
// a proxy making use of "double-lifting" (i.e. a proxy whose handler is a proxy)
var metaHandler = {
get: function(dummyTarget, trapName, dummyReceiver) {
return function(/*...trapArgs*/) { // function acting as a generic trap, its this-binding is irrelevant
var trapArgs = Array.prototype.slice.call(arguments);
print("intercepting "+trapName);
//return Reflect[trapName](...trapArgs); // forward manually
return Reflect[trapName].apply(undefined, trapArgs);
}
},
};
var target = {x:1};
var dummy = {};
var doubleLiftedProxy = new Proxy(target, new Proxy(dummy, metaHandler));
// tests
print(doubleLiftedProxy.x) // intercepting get, evals to 1
print('x' in doubleLiftedProxy) // interecepting has, evals to true
// expected output:
// intercepting get
// 1
// intercepting has
// true