Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function applyAdd(ast: Program, uint8Buffer: Uint8Array, node: Node): State {
const deltaElements = +1; // since we added an element
const sectionName = getSectionForNode(node);
let sectionMetadata = getSectionMetadata(ast, sectionName);
// Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
const res = createEmptySection(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* 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;
function applyAdd(ast: Program, uint8Buffer: Uint8Array, node: Node): State {
const deltaElements = +1; // since we added an element
const sectionName = getSectionForNode(node);
let sectionMetadata = getSectionMetadata(ast, sectionName);
// Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
const res = createEmptySection(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* check that the expressions were ended
*/
if (isFunc(node)) {
// $FlowIgnore
CHECK_END(node.body);
}
if (isGlobal(node)) {
// $FlowIgnore
CHECK_END(node.init);
function applyAdd(ast, uint8Buffer, node) {
var deltaElements = +1; // since we added an element
var sectionName = getSectionForNode(node);
var sectionMetadata = getSectionMetadata(ast, sectionName); // Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
var res = createEmptySection(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* check that the expressions were ended
*/
if (isFunc(node)) {
// $FlowIgnore
var body = node.body;
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
}
function applyAdd(ast, uint8Buffer, node) {
var deltaElements = +1; // since we added an element
var sectionName = getSectionForNode(node);
var sectionMetadata = getSectionMetadata(ast, sectionName); // Section doesn't exists, we create an empty one
if (typeof sectionMetadata === "undefined") {
var res = createEmptySection(ast, uint8Buffer, sectionName);
uint8Buffer = res.uint8Buffer;
sectionMetadata = res.sectionMetadata;
}
/**
* Add nodes
*/
var newByteArray = encodeNode(node); // The size of the section doesn't include the storage of the size itself
// we need to manually add it here
var start = getEndOfSection(sectionMetadata);
var end = start;
/**
* Update section
*/
function applyDelete(ast, uint8Buffer, node) {
var deltaElements = -1; // since we removed an element
assertHasLoc(node);
var sectionName = getSectionForNode(node);
if (sectionName === "start") {
var sectionMetadata = getSectionMetadata(ast, "start");
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = removeSections(ast, uint8Buffer, "start");
var _deltaBytes = -(sectionMetadata.size.value + 1)
/* section id */
;
return {
uint8Buffer: uint8Buffer,
deltaBytes: _deltaBytes,
deltaElements: deltaElements
};
} // replacement is nothing
var replacement = [];
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
function applyDelete(ast, uint8Buffer, node) {
var deltaElements = -1; // since we removed an element
assertHasLoc(node);
var sectionName = getSectionForNode(node);
if (sectionName === "start") {
var sectionMetadata = getSectionMetadata(ast, "start");
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = removeSections(ast, uint8Buffer, "start");
var _deltaBytes = -(sectionMetadata.size.value + 1)
/* section id */
;
return {
uint8Buffer: uint8Buffer,
deltaBytes: _deltaBytes,
deltaElements: deltaElements
};
} // replacement is nothing
var replacement = [];
uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
function applyDelete(ast: Program, uint8Buffer: Uint8Array, node: Node): State {
const deltaElements = -1; // since we removed an element
assertHasLoc(node);
const sectionName = getSectionForNode(node);
if (sectionName === "start") {
const sectionMetadata = getSectionMetadata(ast, "start");
/**
* The start section only contains one element,
* we need to remove the whole section
*/
uint8Buffer = removeSections(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
var oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionVecSize(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Resize section byte size.
* If the length of the LEB-encoded size changes, this can change
* the offset for nodes in the section. So we do this before
* shifting following operations' nodes.
*/
if (state.deltaBytes !== 0 && sectionName !== "start") {
var _oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionByteSize(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
}
/**
* Shift following operation's nodes
*/
if (state.deltaBytes !== 0) {
ops.forEach(function (op) {
// We don't need to handle add ops, they are positioning independent
switch (op.kind) {
case "update":
shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
break;
var oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionVecSize(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Resize section byte size.
* If the length of the LEB-encoded size changes, this can change
* the offset for nodes in the section. So we do this before
* shifting following operations' nodes.
*/
if (state.deltaBytes !== 0 && sectionName !== "start") {
var _oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionByteSize(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
}
/**
* Shift following operation's nodes
*/
if (state.deltaBytes !== 0) {
ops.forEach(function (op) {
// We don't need to handle add ops, they are positioning independent
switch (op.kind) {
case "update":
shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
break;
sectionName,
state.deltaElements
);
// Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Resize section byte size.
* If the length of the LEB-encoded size changes, this can change
* the offset for nodes in the section. So we do this before
* shifting following operations' nodes.
*/
if (state.deltaBytes !== 0 && sectionName !== "start") {
const oldBufferLength = state.uint8Buffer.length;
state.uint8Buffer = resizeSectionByteSize(
ast,
state.uint8Buffer,
sectionName,
state.deltaBytes
);
// Infer bytes added/removed by comparing buffer lengths
state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
}
/**
* Shift following operation's nodes
*/
if (state.deltaBytes !== 0) {
ops.forEach(op => {
// We don't need to handle add ops, they are positioning independent
switch (op.kind) {