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 //eslint-disable-next-line
type AnyFunction = Function; export type AnyFunction = Function;
type ProxyCtx = { export type ProxyCtx = {
fn: AnyFunction; fn: AnyFunction;
this: any; this: any;
args: any[]; args: any[];
newTarget: AnyFunction; newTarget: AnyFunction;
return: (r: any) => void; return: (r: any) => void;
}; };
type Proxy = { export type Proxy = {
construct?(ctx: ProxyCtx): any; construct?(ctx: ProxyCtx): any;
apply?(ctx: ProxyCtx): any; apply?(ctx: ProxyCtx): any;
}; };
type TrapCtx<T> = { export type TrapCtx<T> = {
this: any; this: any;
get: () => T; get: () => T;
set: (v: T) => void; set: (v: T) => void;
}; };
type Trap<T> = { export type Trap<T> = {
writable?: boolean; writable?: boolean;
value?: any; value?: any;
enumerable?: boolean; enumerable?: boolean;

View file

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