Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const rewriteImportedGlobals = state => bin => {
const additionalInitCode = state.additionalInitCode;
const newGlobals = [];
bin = editWithAST(state.ast, bin, {
ModuleImport(path) {
if (t.isGlobalType(path.node.descr) === true) {
const globalType = path.node.descr;
globalType.mutability = "var";
const init = createDefaultInitForGlobal(globalType);
newGlobals.push(t.global(globalType, [init]));
path.remove();
}
},
// in order to preserve non-imported global's order we need to re-inject
// those as well
function transformWasm(ast, bin) {
return editWithAST(ast, bin, {
// FIXME(sven): fix https://github.com/webpack/webpack/issues/7454
Elem({node}) {
const offset = t.objectInstruction("const", "i32", [
t.numberLiteralFromRaw(0)
]);
node.offset = [offset];
},
ModuleImport({node}) {
// Webpack only allows memory and table imports from another wasm
if (node.name === "memory" || node.name === "table") {
node.module = "/tmp/hack.wasm";
}
const rewriteImportedGlobals = state => bin => {
const additionalInitCode = state.additionalInitCode;
const newGlobals = [];
bin = editWithAST(state.ast, bin, {
ModuleImport(path) {
if (t.isGlobalType(path.node.descr) === true) {
const globalType = path.node.descr;
globalType.mutability = "var";
const init = createDefaultInitForGlobal(globalType);
newGlobals.push(t.global(globalType, [init]));
path.remove();
}
},
// in order to preserve non-imported global's order we need to re-inject
// those as well
const rewriteExportNames = ({ ast, module, externalExports }) => bin => {
return editWithAST(ast, bin, {
ModuleExport(path) {
const isExternal = externalExports.has(path.node.name);
if (isExternal) {
path.remove();
return;
}
const usedName = module.isUsed(path.node.name);
if (!usedName) {
path.remove();
return;
}
path.node.name = usedName;
}
});
};
const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
return editWithAST(ast, bin, {
ModuleImport(path) {
const result = usedDependencyMap.get(
path.node.module + ":" + path.node.name
);
if (result !== undefined) {
path.node.module = result.module;
path.node.name = result.name;
}
}
});
};
const rewriteImportedGlobals = state => bin => {
const additionalInitCode = state.additionalInitCode;
const newGlobals = [];
bin = editWithAST(state.ast, bin, {
ModuleImport(path) {
if (t.isGlobalType(path.node.descr) === true) {
const globalType = path.node.descr;
globalType.mutability = "var";
const init = [
createDefaultInitForGlobal(globalType),
t.instruction("end")
];
newGlobals.push(t.global(globalType, init));
path.remove();
}
},
const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
return editWithAST(ast, bin, {
ModuleImport(path) {
const result = usedDependencyMap.get(
path.node.module + ":" + path.node.name
);
if (typeof result !== "undefined") {
path.node.module = result.module;
path.node.name = result.name;
}
}
});
};
const rewriteImportedGlobals = state => bin => {
const additionalInitCode = state.additionalInitCode;
const newGlobals = [];
bin = editWithAST(state.ast, bin, {
ModuleImport(path) {
if (t.isGlobalType(path.node.descr)) {
const globalType = path.node.descr;
globalType.mutability = "var";
const init = [
createDefaultInitForGlobal(globalType),
t.instruction("end")
];
newGlobals.push(t.global(globalType, init));
path.remove();
}
},
const removeStartFunc = state => bin => {
return editWithAST(state.ast, bin, {
Start(path) {
path.remove();
}
});
};