Function constructor proxy

This commit is contained in:
Avad3 2024-07-28 21:18:27 -04:00
parent c2d147442e
commit d043ad0032
2 changed files with 23 additions and 37 deletions

View file

@ -11,26 +11,26 @@ declare global {
}
//eslint-disable-next-line
type AnyFunction = Function;
export type AnyFunction = Function;
type ProxyCtx = {
export type ProxyCtx = {
fn: AnyFunction;
this: any;
args: any[];
newTarget: AnyFunction;
return: (r: any) => void;
};
type Proxy = {
export type Proxy = {
construct?(ctx: ProxyCtx): any;
apply?(ctx: ProxyCtx): any;
};
type TrapCtx<T> = {
export type TrapCtx<T> = {
this: any;
get: () => T;
set: (v: T) => void;
};
type Trap<T> = {
export type Trap<T> = {
writable?: boolean;
value?: any;
enumerable?: boolean;

View file

@ -1,38 +1,24 @@
export default function (client, self) {}
/*
import { ScramjetClient, ProxyCtx } from "../client";
import { rewriteJs } from "../shared";
const FunctionProxy = new Proxy(Function, {
construct(target, argArray) {
if (argArray.length === 1) {
return Reflect.construct(target, rewriteJs(argArray[0]));
} else {
return Reflect.construct(
target,
rewriteJs(argArray[argArray.length - 1])
);
function rewriteFunction(ctx: ProxyCtx) {
for (const i in ctx.args) {
ctx.args[i] = rewriteJs(ctx.args[i]);
}
ctx.return(ctx.fn(...ctx.args));
}
export default function (client: ScramjetClient, self: Self) {
client.Proxy("Function", {
apply(ctx) {
rewriteFunction(ctx);
},
apply(target, thisArg, argArray) {
if (argArray.length === 1) {
return Reflect.apply(target, undefined, [rewriteJs(argArray[0])]);
} else {
return Reflect.apply(target, undefined, [
...argArray.map((x, index) => index === argArray.length - 1),
rewriteJs(argArray[argArray.length - 1]),
]);
}
construct(ctx) {
rewriteFunction(ctx);
},
});
delete window.Function;
window.Function = FunctionProxy;
window.eval = new Proxy(window.eval, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
},
});
*/
Function.prototype.constructor = Function;
}