mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
implement direct and indirect eval
This commit is contained in:
parent
e6b237c525
commit
ec8421be8f
12 changed files with 171 additions and 101 deletions
|
@ -1,24 +1,47 @@
|
|||
import { ScramjetClient, ProxyCtx } from "../client";
|
||||
import { rewriteJs } from "../shared";
|
||||
import { config, rewriteJs } from "../shared";
|
||||
|
||||
function rewriteFunction(ctx: ProxyCtx) {
|
||||
for (const i in ctx.args) {
|
||||
ctx.args[i] = rewriteJs(ctx.args[i]);
|
||||
}
|
||||
for (const i in ctx.args) {
|
||||
ctx.args[i] = rewriteJs(ctx.args[i]);
|
||||
}
|
||||
|
||||
ctx.return(ctx.fn(...ctx.args));
|
||||
ctx.return(ctx.fn(...ctx.args));
|
||||
}
|
||||
|
||||
export default function (client: ScramjetClient, self: Self) {
|
||||
client.Proxy("Function", {
|
||||
apply(ctx) {
|
||||
rewriteFunction(ctx);
|
||||
},
|
||||
client.Proxy("Function", {
|
||||
apply(ctx) {
|
||||
rewriteFunction(ctx);
|
||||
},
|
||||
|
||||
construct(ctx) {
|
||||
rewriteFunction(ctx);
|
||||
},
|
||||
});
|
||||
construct(ctx) {
|
||||
rewriteFunction(ctx);
|
||||
},
|
||||
});
|
||||
|
||||
Function.prototype.constructor = Function;
|
||||
}
|
||||
Function.prototype.constructor = Function;
|
||||
|
||||
// used for proxying *direct eval*
|
||||
// eval("...") -> eval($scramjet$rewrite("..."))
|
||||
Object.defineProperty(self, config.rewritefn, {
|
||||
value: function (js: any) {
|
||||
if (typeof js !== "string") return js;
|
||||
|
||||
const rewritten = rewriteJs(js, client.url);
|
||||
|
||||
return rewritten;
|
||||
},
|
||||
writable: false,
|
||||
configurable: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function indirectEval(this: ScramjetClient, js: any) {
|
||||
// > If the argument of eval() is not a string, eval() returns the argument unchanged
|
||||
if (typeof js !== "string") return js;
|
||||
|
||||
const indirection = this.global.eval;
|
||||
|
||||
return indirection(rewriteJs(js, this.url) as string);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue