mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
wasm
This commit is contained in:
parent
a7cbca42d1
commit
6cc293b453
10 changed files with 1050 additions and 91 deletions
|
@ -16,92 +16,106 @@ import * as ESTree from "estree";
|
|||
// top
|
||||
// parent
|
||||
|
||||
import init, { rewrite_js } from "../../../rewriter/out/rewriter.js";
|
||||
init("/rewriter_bg.wasm");
|
||||
|
||||
export function rewriteJs(js: string, origin?: URL) {
|
||||
try {
|
||||
const ast = parseModule(js, {
|
||||
module: true,
|
||||
webcompat: true,
|
||||
});
|
||||
|
||||
const identifierList = [
|
||||
"window",
|
||||
"self",
|
||||
"globalThis",
|
||||
"this",
|
||||
"parent",
|
||||
"top",
|
||||
"location",
|
||||
];
|
||||
|
||||
const customTraveler = makeTraveler({
|
||||
ImportDeclaration: (node: ESTree.ImportDeclaration) => {
|
||||
node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
},
|
||||
|
||||
ImportExpression: (node: ESTree.ImportExpression) => {
|
||||
if (node.source.type === "Literal") {
|
||||
node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
} else if (node.source.type === "Identifier") {
|
||||
// this is for things that import something like
|
||||
// const moduleName = "name";
|
||||
// await import(moduleName);
|
||||
node.source.name = `__wrapImport(${node.source.name})`;
|
||||
}
|
||||
},
|
||||
|
||||
ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
|
||||
node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
},
|
||||
|
||||
ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
|
||||
// strings are Literals in ESTree syntax but these will always be strings
|
||||
if (node.source)
|
||||
node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
},
|
||||
|
||||
MemberExpression: (node: ESTree.MemberExpression) => {
|
||||
if (
|
||||
node.object.type === "Identifier" &&
|
||||
identifierList.includes(node.object.name)
|
||||
) {
|
||||
node.object.name = `globalThis.$s(${node.object.name})`;
|
||||
}
|
||||
},
|
||||
|
||||
AssignmentExpression: (node: ESTree.AssignmentExpression) => {
|
||||
if (
|
||||
node.left.type === "Identifier" &&
|
||||
identifierList.includes(node.left.name)
|
||||
) {
|
||||
node.left.name = `globalThis.$s(${node.left.name})`;
|
||||
}
|
||||
|
||||
if (
|
||||
node.right.type === "Identifier" &&
|
||||
identifierList.includes(node.right.name)
|
||||
) {
|
||||
node.right.name = `globalThis.$s(${node.right.name})`;
|
||||
}
|
||||
},
|
||||
|
||||
VariableDeclarator: (node: ESTree.VariableDeclarator) => {
|
||||
if (
|
||||
node.init &&
|
||||
node.init.type === "Identifier" &&
|
||||
identifierList.includes(node.init.name)
|
||||
) {
|
||||
node.init.name = `globalThis.$s(${node.init.name})`;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
customTraveler.go(ast);
|
||||
|
||||
return generate(ast);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
console.log(js);
|
||||
|
||||
return js;
|
||||
}
|
||||
let f = rewrite_js(js);
|
||||
console.log(f)
|
||||
return f
|
||||
}
|
||||
// try {
|
||||
// const ast = parseModule(js, {
|
||||
// module: true,
|
||||
// webcompat: true,
|
||||
// });
|
||||
//
|
||||
// const identifierList = [
|
||||
// "window",
|
||||
// "self",
|
||||
// "globalThis",
|
||||
// "this",
|
||||
// "parent",
|
||||
// "top",
|
||||
// "location",
|
||||
// ];
|
||||
//
|
||||
// const customTraveler = makeTraveler({
|
||||
// ImportDeclaration: (node: ESTree.ImportDeclaration) => {
|
||||
// node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
// },
|
||||
//
|
||||
// ImportExpression: (node: ESTree.ImportExpression) => {
|
||||
// if (node.source.type === "Literal") {
|
||||
// node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
// } else if (node.source.type === "Identifier") {
|
||||
// // this is for things that import something like
|
||||
// // const moduleName = "name";
|
||||
// // await import(moduleName);
|
||||
// node.source.name = `__wrapImport(${node.source.name})`;
|
||||
// }
|
||||
// },
|
||||
//
|
||||
// ExportAllDeclaration: (node: ESTree.ExportAllDeclaration) => {
|
||||
// node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
// },
|
||||
//
|
||||
// ExportNamedDeclaration: (node: ESTree.ExportNamedDeclaration) => {
|
||||
// // strings are Literals in ESTree syntax but these will always be strings
|
||||
// if (node.source)
|
||||
// node.source.value = encodeUrl(node.source.value as string, origin);
|
||||
// },
|
||||
//
|
||||
// MemberExpression: (node: ESTree.MemberExpression) => {
|
||||
// if (
|
||||
// node.object.type === "Identifier" &&
|
||||
// identifierList.includes(node.object.name)
|
||||
// ) {
|
||||
// node.object.name = `globalThis.$s(${node.object.name})`;
|
||||
// }
|
||||
// },
|
||||
//
|
||||
// AssignmentExpression: (node: ESTree.AssignmentExpression, more) => {
|
||||
// console.log(node, more);
|
||||
// if (
|
||||
// node.left.type === "Identifier" &&
|
||||
// identifierList.includes(node.left.name)
|
||||
// ) {
|
||||
// node.left.name = `globalThis.$s(${node.left.name})`;
|
||||
// }
|
||||
//
|
||||
// if (
|
||||
// node.right.type === "Identifier" &&
|
||||
// identifierList.includes(node.right.name)
|
||||
// ) {
|
||||
// node.right.name = `globalThis.$s(${node.right.name})`;
|
||||
// }
|
||||
// },
|
||||
// ArrayExpression: (node: ESTree.ArrayExpression) => {
|
||||
// node.elements.forEach((element) => {
|
||||
// if (element.type === "Identifier" && identifierList.includes(element.name)) {
|
||||
// element.name = `globalThis.$s(${element.name})`;
|
||||
// }
|
||||
// });
|
||||
// },
|
||||
//
|
||||
// VariableDeclarator: (node: ESTree.VariableDeclarator) => {
|
||||
// if (
|
||||
// node.init &&
|
||||
// node.init.type === "Identifier" &&
|
||||
// identifierList.includes(node.init.name)
|
||||
// ) {
|
||||
// node.init.name = `globalThis.$s(${node.init.name})`;
|
||||
// }
|
||||
// },
|
||||
// });
|
||||
//
|
||||
// customTraveler.go(ast);
|
||||
//
|
||||
// return generate(ast);
|
||||
// } catch (e) {
|
||||
// console.error(e);
|
||||
// console.log(js);
|
||||
//
|
||||
// return js;
|
||||
// }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue