Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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');
}
},
ModuleExport(path) {
if (path.node.name === exportName) {
path.remove();
// console.log('\t> remove export');
}
}
});
};
var functionNames = [];
t.traverse(ast, {
FunctionNameMetadata: function FunctionNameMetadata(_ref) {
var node = _ref.node;
functionNames.push({
name: node.value,
index: node.index
});
}
});
if (functionNames.length === 0) {
return;
}
t.traverse(ast, {
Func: function (_Func) {
function Func(_x) {
return _Func.apply(this, arguments);
}
Func.toString = function () {
return _Func.toString();
};
return Func;
}(function (_ref2) {
var node = _ref2.node;
// $FlowIgnore
var nodeName = node.name;
var indexBasedFunctionName = nodeName.value;
var index = Number(indexBasedFunctionName.replace("func_", ""));
var functionNames = [];
t.traverse(ast, {
FunctionNameMetadata: function FunctionNameMetadata(_ref) {
var node = _ref.node;
functionNames.push({
name: node.value,
index: node.index
});
}
});
if (functionNames.length === 0) {
return;
}
t.traverse(ast, {
Func: function (_Func) {
function Func(_x) {
return _Func.apply(this, arguments);
}
Func.toString = function () {
return _Func.toString();
};
return Func;
}(function (_ref2) {
var node = _ref2.node;
// $FlowIgnore
var nodeName = node.name;
var indexBasedFunctionName = nodeName.value;
var index = Number(indexBasedFunctionName.replace("func_", ""));
const addr = allocator.malloc(1 /* size of the memoryinstance struct */);
allocator.set(addr, memoryinstance);
moduleInstance.memaddrs.push(addr);
}
function handleTableImport(node: ModuleImport) {
const tableinstance = getExternalElementOrThrow(node.module, node.name);
const addr = allocator.malloc(1 /* size of the tableinstance struct */);
allocator.set(addr, tableinstance);
moduleInstance.tableaddrs.push(addr);
}
traverse(n, {
ModuleImport({ node }: NodePath) {
switch (node.descr.type) {
case "FuncImportDescr":
return handleFuncImport(node, node.descr);
case "GlobalType":
return handleGlobalImport(node, node.descr);
case "Memory":
return handleMemoryImport(node);
case "Table":
return handleTableImport(node);
default:
throw new Error("Unsupported import of type: " + node.descr.type);
}
}
});
}
var sectionName = getSectionForNode(newNode);
var replacementByteArray = encodeNode(newNode);
/**
* Replace new node as bytes
*/
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.end.column, replacementByteArray);
/**
* Update function body size if needed
*/
if (sectionName === "code") {
// Find the parent func
traverse(ast, {
Func: function Func(_ref3) {
var node = _ref3.node;
var funcHasThisIntr = node.body.find(function (n) {
return n === newNode;
}) !== undefined; // Update func's body size if needed
if (funcHasThisIntr === true) {
// These are the old functions locations informations
assertHasLoc(node);
var oldNodeSize = encodeNode(oldNode).length;
var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
var newByteArray = encodeU32(newValue); // function body size byte
// FIXME(sven): only handles one byte u32
}(function (moduleNameMetadataPath) {
// update module
t.traverse(ast, {
Module: function (_Module) {
function Module(_x7) {
return _Module.apply(this, arguments);
}
Module.toString = function () {
return _Module.toString();
};
return Module;
}(function (_ref7) {
var node = _ref7.node;
var name = moduleNameMetadataPath.node.value; // compatiblity with wast-parser
if (name === "") {
name = null;
function shiftFollowingSections(ast, { section }, deltaInSizeEncoding) {
// Once we hit our section every that is after needs to be shifted by the delta
let encounteredSection = false;
traverse(ast, {
SectionMetadata(path) {
if (path.node.section === section) {
encounteredSection = true;
return;
}
if (encounteredSection === true) {
path.shift(deltaInSizeEncoding);
debug(
"shift section section=%s detla=%d",
path.node.section,
deltaInSizeEncoding
);
}
}
var sectionName = getSectionForNode(newNode);
var replacementByteArray = encodeNode(newNode);
/**
* Replace new node as bytes
*/
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
oldNode.loc.end.column, replacementByteArray);
/**
* Update function body size if needed
*/
if (sectionName === "code") {
// Find the parent func
traverse(ast, {
Func: function Func(_ref3) {
var node = _ref3.node;
var funcHasThisIntr = node.body.find(function (n) {
return n === newNode;
}) !== undefined; // Update func's body size if needed
if (funcHasThisIntr === true) {
// These are the old functions locations informations
assertHasLoc(node);
var oldNodeSize = encodeNode(oldNode).length;
var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
var newByteArray = encodeU32(newValue); // function body size byte
// FIXME(sven): only handles one byte u32
function getModuleFromProgram(ast: Program): ?Module {
let module = null;
traverse(ast, {
Module({ node }: NodePath) {
module = node;
}
});
return module;
}
function getExportsFromWasm(file) {
const exports = [];
const binary = readFileSync(file, null);
const ast = decode(binary, decoderOpts);
traverse(ast, {
ModuleExport({ node }) {
exports.push(node.name);
}
});
return exports;
}