diff --git a/src/setup.ts b/src/setup.ts new file mode 100644 index 0000000..8ba27a9 --- /dev/null +++ b/src/setup.ts @@ -0,0 +1,113 @@ +import { type Node } from "@babel/types"; + +export const setupNodes: Node[] = [ + { + type: "ExpressionStatement", + expression: { + type: "AssignmentExpression", + operator: "=", + left: { + type: "MemberExpression", + object: { + type: "Identifier", + name: "globalThis", + }, + computed: false, + property: { + type: "Identifier", + name: "XMLHttpRequest", + }, + }, + right: { + type: "ObjectExpression", + properties: [ + { + type: "ObjectProperty", + method: false, + key: { + type: "Identifier", + name: "prototype", + }, + computed: false, + shorthand: false, + value: { + type: "ObjectExpression", + properties: [], + }, + }, + ], + }, + }, + }, + { + type: "VariableDeclaration", + declarations: [ + { + type: "VariableDeclarator", + id: { + type: "Identifier", + name: "window", + }, + init: { + type: "CallExpression", + callee: { + type: "MemberExpression", + object: { + type: "Identifier", + name: "Object", + }, + computed: false, + property: { + type: "Identifier", + name: "assign", + }, + }, + arguments: [ + { + type: "CallExpression", + callee: { + type: "MemberExpression", + object: { + type: "Identifier", + name: "Object", + }, + computed: false, + property: { + type: "Identifier", + name: "create", + }, + }, + arguments: [ + { + type: "NullLiteral", + }, + ], + }, + { + type: "Identifier", + name: "globalThis", + }, + ], + }, + }, + ], + kind: "const", + }, + { + type: "VariableDeclaration", + declarations: [ + { + type: "VariableDeclarator", + id: { + type: "Identifier", + name: "document", + }, + init: { + type: "ObjectExpression", + properties: [], + }, + }, + ], + kind: "const", + }, +]; diff --git a/src/solvers.ts b/src/solvers.ts index 65867ef..3410ab3 100755 --- a/src/solvers.ts +++ b/src/solvers.ts @@ -4,15 +4,7 @@ import { type ArrowFunctionExpression } from "@babel/types"; import { getFunctionNodes } from "./utils.ts"; import { extract as extractSig } from "./sig.ts"; import { extract as extractNsig } from "./nsig.ts"; - -function setup() { - // @ts-ignore: This is used in the babel generated js - globalThis.XMLHttpRequest = { prototype: {} }; - // deno-lint-ignore no-unused-vars - const window = Object.assign(Object.create(null), globalThis); - // deno-lint-ignore no-unused-vars - const document = {}; -} +import { setupNodes } from "./setup.ts"; export function preprocessPlayer(data: string): string { const ast = parse(data, { @@ -109,7 +101,7 @@ export function preprocessPlayer(data: string): string { }); } - ast.program.body.splice(0, 0, ...getFunctionNodes(setup)); + ast.program.body.splice(0, 0, ...setupNodes); const { code } = generate(ast, { comments: false,