Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const t = require("@webassemblyjs/ast");
const emptyFunc = t.func(null, t.signature([], []), []);
module.exports = function removeFunc(moduleExport, ast) {
const exportName = moduleExport.name;
// TODO(sven): test if we actually want to delete a func
const funcName = moduleExport.descr.id.value;
// console.log(`Remove unused "${exportName}"`);
t.traverse(ast, {
Func(path) {
if (path.node.name.value === funcName) {
path.replaceWith(emptyFunc);
// console.log('\t> remove func');
}
},
eatToken();
typeRef = parseTypeReference();
} else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword" // is any keyword
) {
// Instruction
fnBody.push(parseFuncInstr());
} else {
throw function () {
return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in func body" + ", given " + tokenToString(token));
}();
}
eatTokenOfType(tokens.closeParen);
}
return t.func(fnName, typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult), fnBody);
}
/**
typeRef = parseTypeReference();
} else if (
lookaheadAndCheck(tokens.name) === true ||
lookaheadAndCheck(tokens.valtype) === true ||
token.type === "keyword" // is any keyword
) {
// Instruction
fnBody.push(parseFuncInstr());
} else {
throw createUnexpectedToken("Unexpected token in func body");
}
eatTokenOfType(tokens.closeParen);
}
return t.func(
fnName,
typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult),
fnBody
);
}
if (func.isExternal === true) {
return;
}
var decodedElementInCodeSection = state.elementsInCodeSection[funcIndex];
if (opts.ignoreCodeSection === false) {
if (typeof decodedElementInCodeSection === "undefined") {
throw new CompileError("func " + toHex(funcIndex) + " code not found");
}
body = decodedElementInCodeSection.code;
}
funcIndex++;
var funcNode = t.func(func.id, t.signature(params, result), body);
if (func.isExternal === true) {
funcNode.isExternal = func.isExternal;
} // Add function position in the binary if possible
if (opts.ignoreCodeSection === false) {
var _startLoc23 = decodedElementInCodeSection.startLoc,
endLoc = decodedElementInCodeSection.endLoc,
bodySize = decodedElementInCodeSection.bodySize;
funcNode = t.withLoc(funcNode, endLoc, _startLoc23);
funcNode.metadata = {
bodySize: bodySize
};
}
if (typeof startAtFuncOffset === "number") {
funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
}
for (const instr of additionalInitCode) {
funcBody.push(instr);
}
funcBody.push(t.instruction("end"));
const funcResults = [];
// Code section
const funcSignature = t.signature(funcParams, funcResults);
const func = t.func(initFuncId, funcSignature, funcBody);
// Type section
const functype = t.typeInstruction(undefined, funcSignature);
// Func section
const funcindex = t.indexInFuncSection(nextTypeIndex);
// Export section
const moduleExport = t.moduleExport(
initFuncId.value,
t.moduleExportDescr("Func", nextFuncIndex)
);
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
};
return [...acc, ...body];
}, []);
if (typeof startAtFuncOffset === "number") {
funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
}
for (const instr of additionalInitCode) {
funcBody.push(instr);
}
const funcResults = [];
// Code section
const funcSignature = t.signature(funcParams, funcResults);
const func = t.func(initFuncId, funcSignature, funcBody);
// Type section
const functype = t.typeInstruction(undefined, funcSignature);
// Func section
const funcindex = t.indexInFuncSection(nextTypeIndex);
// Export section
const moduleExport = t.moduleExport(
initFuncId.value,
t.moduleExportDescr("Func", nextFuncIndex)
);
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
};
return [...acc, ...body];
}, []);
if (typeof startAtFuncOffset === "number") {
funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
}
for (const instr of additionalInitCode) {
funcBody.push(instr);
}
const funcResults = [];
// Code section
const funcSignature = t.signature(funcParams, funcResults);
const func = t.func(initFuncId, funcSignature, funcBody);
// Type section
const functype = t.typeInstruction(undefined, funcSignature);
// Func section
const funcindex = t.indexInFuncSection(nextTypeIndex);
// Export section
const moduleExport = t.moduleExport(
initFuncId.value,
t.moduleExportDescr("Func", nextFuncIndex)
);
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
};
export function listOfInstructionsToIr(instrs: Array): IR {
const program = {};
const funcTable = [];
const module = new Module(tProgram([]));
const fakeFunc = func(identifier("main"), [], instrs);
module.beginFuncBody(fakeFunc);
instrs.forEach(i => module.onFuncInstruction(i));
const { name, instructions, startAt } = module.finalizeFunc(fakeFunc);
funcTable.push({ name, startAt });
instructions.forEach(instruction => {
program[instruction.offset] = instruction.node;
});
return {
// $FlowIgnore
funcTable,
if (typeof startAtFuncOffset === "number") {
funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
}
for (const instr of additionalInitCode) {
funcBody.push(instr);
}
funcBody.push(t.instruction("end"));
const funcResults = [];
// Code section
const funcSignature = t.signature(funcParams, funcResults);
const func = t.func(initFuncId, funcSignature, funcBody);
// Type section
const functype = t.typeInstruction(undefined, funcSignature);
// Func section
const funcindex = t.indexInFuncSection(nextTypeIndex);
// Export section
const moduleExport = t.moduleExport(
initFuncId.value,
t.moduleExportDescr("Func", nextFuncIndex)
);
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
};