/*!# @license webfan (C) Till Wehowski, Webfan.de - All rights reserved. !*/
define(
//'webfan/overload',
function(require, exports, module) {
Symbol.frdl = 'fn.__call';
var debug = true; //@toDo: get debumode
/*
// 11 - Proxy as a proxy handler
const logHandler = {
get (obj, trap) {
return function (...args) {
console.log(`${trap}`, args.slice(1));
const result = Reflect[trap](...args);
console.log(' result:', JSON.stringify(result));
console.log(' value:', JSON.stringify(args[0]));
};
}
},
logProxy = new Proxy({}, logHandler);
// try it out
const myObj = new Proxy({a: 1, b: 2}, logProxy);
myObj.a = 3; // set(a,3), getOwnPropertyDescriptor(a), defineProperty(a,3)
delete myObj.b; // deleteProperty(b)
*/
function logProxy(from){
var logHandler = {
/*
constructor: function(target, args) {
var obj = Object.create(target.prototype);
this.apply(target, obj, args);
return obj;
},
*/
constructor: Function.prototype.constructor,
get : function(obj, trap, receiver) {
if(!!debug) console.log('magic:', arguments);
if(!!debug) console.log('magic args:', obj, trap, receiver);
var nArg = arguments[1];
// alert(nArg);
var _check_result = logHandler.__get.apply(this, [nArg]);
if('undefined'!==typeof _check_result)return _check_result;
return function () {
var args = Array.prototype.slice(arguments);
var noMethod = false;
// var TTrap2 = args.slice(1);
var TTrap2 = trap;
if(!!debug) console.log('TTrap2', TTrap2);
if(!!debug) console.log('Reflect:', Reflect);
if('function'!==typeof Reflect[trap] ){
if(!!debug) console.log(' noMethod:', trap, Reflect);
var fetch = logHandler.__call;
var a = [trap, arguments];
noMethod = true;
}else{
// var fetch = Reflect[trap];
console.log('getTrap?', trap, args);
var fetch = logHandler.__get;
var a = arguments;
}
var result = fetch.apply(this, a);
// if('function'===typeof result && false===noMethod){
// if(!!debug)console.log(' resultIsAFunction:', args);
// result = logHandler.__get.apply(this, [nArg]);
// }else
if('undefined'===typeof result){
if(!!debug)console.log(' noResult:', args);
}else {
if(!!debug) console.log(' result:',result);
}
if(!!debug) console.log(' value:', args[0]);
return result;
};
},
has: function(target, name) {
if(!!debug) console.log('has:', arguments);
// return true;
return 'undefined'!==typeof target[name];
},
apply: function(target, THIS, args) {
if(!!debug) console.log('apply:', arguments);
},
call: function(name, args) {
//target.apply(THIS, args);
// this.apply(THIS, args);
if(!!debug) console.log('call:', arguments);
},
__call : function(name, args) {
if(!!debug) console.log('call:', arguments);
// target.apply(THIS, args);
// this.apply(THIS, args);
var prop = Object.getOwnPropertyDescriptor(this, name);
if(prop){
if('undefined'!==typeof prop.value){
if('function'===typeof prop.value){
return prop.value.apply(this, args);
}
return prop.value;
}
}else{
if('function'===typeof frdl.CONST && 'function'===typeof frdl.CONST('__MAGIC_METHODS__').__call){
return frdl.CONST('__MAGIC_METHODS__').__call.apply(this, [name, args]);
}
}//no prop
if(!!debug) console.log('call not resolved:', arguments);
},
__get : function(name) {
if(!!debug) console.log('get:', arguments);
if('function'===typeof frdl.CONST && 'function'===typeof frdl.CONST('__MAGIC_METHODS__').__get){
return frdl.CONST('__MAGIC_METHODS__').__get.apply(this, [name]);
}
}
};
var logProxy = new Proxy(logHandler, logHandler);
if('undefined'!==typeof from){
var k;
for( k in from){
logProxy[k] = from[k];
}
}
logProxy.prototype.constructor = Function.prototype.constructor;
return Object.create(logProxy);
}
logProxy.prototype.constructor = Function.prototype.constructor;
/*
// try it out
const myObj = new Proxy(logProxy, logProxy);
myObj.a = 3; // set(a,3), getOwnPropertyDescriptor(a), defineProperty(a,3)
delete myObj.b; // deleteProperty(b)
console.log(myObj.undef);
console.log(myObj.nometh({a: 1, b: 2}));
*/
exports = module.exports = {
IOverloadableProxy : logProxy,
defaultProxy : logProxy(logProxy),
make : function(o){
return logProxy(o, new logProxy );
}
};
return exports;
});
|