diff --git a/src/client/client.ts b/src/client/client.ts index 60225e5..56ee77a 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -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 = { +export type TrapCtx = { this: any; get: () => T; set: (v: T) => void; }; -type Trap = { +export type Trap = { writable?: boolean; value?: any; enumerable?: boolean; diff --git a/src/client/shared/eval.ts b/src/client/shared/eval.ts index 556e71c..553e5e8 100644 --- a/src/client/shared/eval.ts +++ b/src/client/shared/eval.ts @@ -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]); } - }, - 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", { + apply(ctx) { + rewriteFunction(ctx); + }, -window.eval = new Proxy(window.eval, { - apply(target, thisArg, argArray) { - return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]); - }, -}); -*/ + construct(ctx) { + rewriteFunction(ctx); + }, + }); + + Function.prototype.constructor = Function; +} \ No newline at end of file