mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
Function constructor proxy
This commit is contained in:
parent
c2d147442e
commit
d043ad0032
2 changed files with 23 additions and 37 deletions
|
@ -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;
|
||||||
|
|
|
@ -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])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
construct(ctx) {
|
||||||
return Reflect.apply(target, undefined, [rewriteJs(argArray[0])]);
|
rewriteFunction(ctx);
|
||||||
} else {
|
|
||||||
return Reflect.apply(target, undefined, [
|
|
||||||
...argArray.map((x, index) => index === argArray.length - 1),
|
|
||||||
rewriteJs(argArray[argArray.length - 1]),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
delete window.Function;
|
Function.prototype.constructor = Function;
|
||||||
|
}
|
||||||
window.Function = FunctionProxy;
|
|
||||||
|
|
||||||
window.eval = new Proxy(window.eval, {
|
|
||||||
apply(target, thisArg, argArray) {
|
|
||||||
return Reflect.apply(target, thisArg, [rewriteJs(argArray[0])]);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
*/
|
|
Loading…
Add table
Add a link
Reference in a new issue