<commit_before>import * as _ from "lodash";
import * as common from "../Common/_all";
import { ICallContext, ProxyType, CallType } from "./ICallContext";
import { ICallInterceptor } from "./ICallInterceptor";
import * as inv from "./Invocation";
import { IProxyHandler, PropKey } from "./IProxyHandler";

export class ProxyES6Handler<T> implements IProxyHandler<T> {

    constructor(private readonly _interceptor: ICallInterceptor) {
    }

    apply(target: T, thisArg: any, argArray?: any): any {

        let funcName = common.Utils.functionName(target);
        let method = new inv.MethodInfo(target, funcName);
        let invocation: ICallContext = new inv.MethodInvocation(target, method, argArray, ProxyType.DYNAMIC);
        this._interceptor.intercept(invocation);

        return invocation.returnValue;
    }

    get(target: T, p: PropKey, receiver: any): any {

        let propValue = (<any>target)[p];
        let method = new inv.PropertyInfo(target, <string>p);
        let invocation = new inv.DynamicGetInvocation(method, propValue);
        
        this._interceptor.intercept(invocation);

        if (!_.isFunction(propValue) && 
            invocation.callType == CallType.PROPERTY &&
            invocation.property.desc) // value getter invocation at execution time
            
            return invocation.returnValue;
        else
            return (...args: any[]) => {

                this._interceptor.removeInvocation(invocation);

                let method = new inv.MethodInfo(target, <string>p);
                let methodInvocation = new inv.MethodInvocation(target, method, <any>args, ProxyType.DYNAMIC);
                this._interceptor.intercept(methodInvocation);

                return methodInvocation.returnValue;
            }
    }

    set(target: T, p: PropKey, value: any, receiver: any): boolean {

        let method = new inv.PropertyInfo(target, <string>p);
        let invocation: ICallContext = new inv.ValueSetterInvocation(method, <any>[value], ProxyType.DYNAMIC);
        this._interceptor.intercept(invocation);

        return Reflect.set(target, p, value, receiver);
    }

    defineProperty(target: T, p: PropKey, attributes: common.PropDescriptor): boolean {
        
        attributes.configurable = true;
        
        return Reflect.defineProperty(target, p, attributes);
    }

}<commit_msg>refactor: cast target type to object in ES6 proxy handler<commit_after>import * as _ from "lodash";
import * as common from "../Common/_all";
import { ICallContext, ProxyType, CallType } from "./ICallContext";
import { ICallInterceptor } from "./ICallInterceptor";
import * as inv from "./Invocation";
import { IProxyHandler, PropKey } from "./IProxyHandler";

export class ProxyES6Handler<T> implements IProxyHandler<T> {

    constructor(private readonly _interceptor: ICallInterceptor) {
    }

    apply(target: T, thisArg: any, argArray?: any): any {

        let funcName = common.Utils.functionName(target);
        let method = new inv.MethodInfo(target, funcName);
        let invocation: ICallContext = new inv.MethodInvocation(target, method, argArray, ProxyType.DYNAMIC);
        this._interceptor.intercept(invocation);

        return invocation.returnValue;
    }

    get(target: T, p: PropKey, receiver: any): any {

        let propValue = (<any>target)[p];
        let method = new inv.PropertyInfo(target, <string>p);
        let invocation = new inv.DynamicGetInvocation(method, propValue);
        
        this._interceptor.intercept(invocation);

        if (!_.isFunction(propValue) && 
            invocation.callType == CallType.PROPERTY &&
            invocation.property.desc) // value getter invocation at execution time
            
            return invocation.returnValue;
        else
            return (...args: any[]) => {

                this._interceptor.removeInvocation(invocation);

                let method = new inv.MethodInfo(target, <string>p);
                let methodInvocation = new inv.MethodInvocation(target, method, <any>args, ProxyType.DYNAMIC);
                this._interceptor.intercept(methodInvocation);

                return methodInvocation.returnValue;
            }
    }

    set(target: T, p: PropKey, value: any, receiver: any): boolean {

        let method = new inv.PropertyInfo(target, <string>p);
        let invocation: ICallContext = new inv.ValueSetterInvocation(method, <any>[value], ProxyType.DYNAMIC);
        this._interceptor.intercept(invocation);

        return Reflect.set(<Object>target, p, value, receiver);
    }

    defineProperty(target: T, p: PropKey, attributes: common.PropDescriptor): boolean {
        
        attributes.configurable = true;
        
        return Reflect.defineProperty(<Object>target, p, attributes);
    }

}
{'fraction_non_alphanumeric': 0.10970024979184013, 'fraction_numerical': 0.0006244796003330558, 'mean_word_length': 2.831738437001595, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3282092', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1293, 'n_words': 325}