Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const oldNodeSize = encodeNode(oldNode).length;
const bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
const newValue = node.metadata.bodySize + bodySizeDeltaBytes;
const newByteArray = encodeU32(newValue);
debug("resize func body newValue=%d", newValue);
// function body size byte
// FIXME(sven): only handles one byte u32
const start = node.loc.start.column;
const end = start + 1;
uint8Buffer = overrideBytesInBuffer(
uint8Buffer,
start,
end,
newByteArray
);
}
}
}
});
* Add nodes
*/
const newByteArray = encodeNode(node);
// The size of the section doesn't include the storage of the size itself
// we need to manually add it here
const start = getEndOfSection(sectionMetadata);
const end = start;
/**
* Update section
*/
const deltaBytes = newByteArray.length;
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
node.loc = {
start: { line: -1, column: start },
end: { line: -1, column: start + deltaBytes }
};
// for func add the additional metadata in the AST
if (node.type === "Func") {
// the size is the first byte
// FIXME(sven): handle LEB128 correctly here
const bodySize = newByteArray[0];
node.metadata = { bodySize };
}
if (node.type !== "IndexInFuncSection") {
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = removeSection(ast, uint8Buffer, "start");
const deltaBytes = -(sectionMetadata.size.value + 1) /* section id */;
return { uint8Buffer, deltaBytes, deltaElements };
}
// replacement is nothing
const replacement = [];
uint8Buffer = overrideBytesInBuffer(
uint8Buffer,
// $FlowIgnore: assertHasLoc ensures that
node.loc.start.column,
// $FlowIgnore: assertHasLoc ensures that
node.loc.end.column,
replacement
);
/**
* Update section
*/
// $FlowIgnore: assertHasLoc ensures that
const deltaBytes = -(node.loc.end.column - node.loc.start.column);
return { uint8Buffer, deltaBytes, deltaElements };
column: start + 1
}; // 1 byte for the empty vector
var size = t.withLoc(t.numberLiteralFromRaw(1), sizeEndLoc, sizeStartLoc);
var vectorOfSizeStartLoc = {
line: -1,
column: sizeEndLoc.column
};
var vectorOfSizeEndLoc = {
line: -1,
column: sizeEndLoc.column + 1
};
var vectorOfSize = t.withLoc(t.numberLiteralFromRaw(0), vectorOfSizeEndLoc, vectorOfSizeStartLoc);
var sectionMetadata = t.sectionMetadata(section, start, size, vectorOfSize);
var sectionBytes = encodeNode(sectionMetadata);
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start - 1, end, sectionBytes); // Add section into the AST for later lookups
if (_typeof(ast.body[0].metadata) === "object") {
// $FlowIgnore: metadata can not be empty
ast.body[0].metadata.sections.push(sectionMetadata);
t.sortSectionMetadata(ast.body[0]);
}
/**
* Update AST
*/
// Once we hit our section every that is after needs to be shifted by the delta
var deltaBytes = +sectionBytes.length;
var encounteredSection = false;
t.traverse(ast, {
SectionMetadata: function SectionMetadata(path) {
}) !== 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
var start = node.loc.start.column;
var end = start + 1;
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
}
}
}
});
// These are the old functions locations informations
assertHasLoc(node);
const oldNodeSize = encodeNode(oldNode).length;
const bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
if (bodySizeDeltaBytes !== 0) {
const newValue = node.metadata.bodySize + bodySizeDeltaBytes;
const newByteArray = encodeU32(newValue);
// function body size byte
// FIXME(sven): only handles one byte u32
const start = node.loc.start.column;
const end = start + 1;
uint8Buffer = overrideBytesInBuffer(
uint8Buffer,
start,
end,
newByteArray
);
}
}
}
});
function applyUpdate(
ast: Program,
uint8Buffer: Uint8Array,
[oldNode, newNode]: [Node, Node]
): State {
const deltaElements = 0;
assertHasLoc(oldNode);
const sectionName = getSectionForNode(newNode);
const 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({ node }) {
const funcHasThisIntr =
function applyUpdate(ast, uint8Buffer, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
oldNode = _ref2[0],
newNode = _ref2[1];
var deltaElements = 0;
assertHasLoc(oldNode);
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) {
SectionMetadata: function SectionMetadata(_ref2) {
var node = _ref2.node;
/**
* Section size
*/
{
var newu32Encoded = encodeU32(node.size.value);
var newu32EncodedLen = newu32Encoded.length;
var start = node.size.loc.start.column;
var end = node.size.loc.end.column;
var oldu32EncodedLen = end - start;
if (newu32EncodedLen !== oldu32EncodedLen) {
var deltaInSizeEncoding = oldu32EncodedLen - newu32EncodedLen;
uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newu32Encoded);
shiftFollowingSections(ast, node, -deltaInSizeEncoding);
}
}
}
});