scramjet/src/client/shared/function.ts
2024-09-01 19:01:24 -05:00

40 lines
962 B
TypeScript

import { ScramjetClient, ProxyCtx, Proxy } from "../client";
import { rewriteJs } from "../../shared";
function rewriteFunction(ctx: ProxyCtx, client: ScramjetClient) {
const stringifiedFunction = ctx.call().toString();
const content = rewriteJs(`return ${stringifiedFunction}`, client.meta);
ctx.return(ctx.fn(content)());
}
export default function (client: ScramjetClient, self: Self) {
const handler: Proxy = {
apply(ctx) {
rewriteFunction(ctx, client);
},
construct(ctx) {
rewriteFunction(ctx, client);
},
};
client.Proxy("Function", handler);
// god i love javascript
client.RawProxy(function () {}.constructor.prototype, "constructor", handler);
client.RawProxy(
async function () {}.constructor.prototype,
"constructor",
handler
);
client.RawProxy(
function* () {}.constructor.prototype,
"constructor",
handler
);
client.RawProxy(
async function* () {}.constructor.prototype,
"constructor",
handler
);
}