Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function findLastSection(
ast: Program,
forSection: SectionName
): ?SectionMetadata {
const targetSectionId = constants.sections[forSection];
// $FlowIgnore: metadata can not be empty
const moduleSections = ast.body[0].metadata.sections;
let lastSection;
let lastId = 0;
for (let i = 0, len = moduleSections.length; i < len; i++) {
const section = moduleSections[i];
// Ignore custom section since they can actually occur everywhere
if (section.section === "custom") {
continue;
}
const sectionId = constants.sections[section.section];
nodes.sort(function (a, b) {
var sectionA = getSectionForNode(a);
var sectionB = getSectionForNode(b);
var aId = constants.sections[sectionA];
var bId = constants.sections[sectionB];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
if (aId === bId) {
// $FlowIgnore originalOrder is filled for all nodes
return originalOrder.get(a) - originalOrder.get(b);
}
return aId - bId;
});
}
nodes.sort(function (a, b) {
var sectionA = getSectionForNode(a);
var sectionB = getSectionForNode(b);
var aId = constants.sections[sectionA];
var bId = constants.sections[sectionB];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
if (aId === bId) {
// $FlowIgnore originalOrder is filled for all nodes
return originalOrder.get(a) - originalOrder.get(b);
}
return aId - bId;
});
}
function findLastSection(ast, forSection) {
var targetSectionId = constants.sections[forSection]; // $FlowIgnore: metadata can not be empty
var moduleSections = ast.body[0].metadata.sections;
var lastSection;
var lastId = 0;
for (var i = 0, len = moduleSections.length; i < len; i++) {
var section = moduleSections[i]; // Ignore custom section since they can actually occur everywhere
if (section.section === "custom") {
continue;
}
var sectionId = constants.sections[section.section];
if (targetSectionId > lastId && targetSectionId < sectionId) {
return lastSection;
}
lastId = sectionId;
lastSection = section;
}
return lastSection;
}
m.metadata.sections.sort(function (a, b) {
var aId = constants.sections[a.section];
var bId = constants.sections[b.section];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
return aId - bId;
});
}
m.metadata.sections.sort((a, b) => {
const aId = constants.sections[a.section];
const bId = constants.sections[b.section];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
return aId > bId;
});
}
function parseSection(sectionIndex) {
var sectionId = readByte();
eatBytes(1);
if (sectionId >= sectionIndex || sectionIndex === constants.sections.custom) {
sectionIndex = sectionId + 1;
} else {
if (sectionId !== constants.sections.custom) throw new CompileError("Unexpected section: " + toHex(sectionId));
}
var nextSectionIndex = sectionIndex;
var startOffset = offset;
var startLoc = getPosition();
var u32 = readU32();
var sectionSizeInBytes = u32.value;
eatBytes(u32.nextIndex);
var sectionSizeInBytesNode = function () {
var endLoc = getPosition();
return t.withLoc(t.numberLiteralFromRaw(sectionSizeInBytes), endLoc, startLoc);
}();
const numberOfElements = numberOfElementsu32.value;
eatBytes(numberOfElementsu32.nextIndex);
const metadata = t.sectionMetadata(
"element",
startOffset,
sectionSizeInBytesNode,
WITH_LOC(t.numberLiteralFromRaw(numberOfElements), startLoc)
);
const nodes = parseElemSection(numberOfElements);
return { nodes, metadata, nextSectionIndex };
}
case constants.sections.global: {
dumpSep("section Global");
dump([sectionId], "section code");
dump([sectionSizeInBytes], "section size");
const startLoc = getPosition();
const numberOfGlobalsu32 = readU32();
const numberOfGlobals = numberOfGlobalsu32.value;
eatBytes(numberOfGlobalsu32.nextIndex);
const metadata = t.sectionMetadata(
"global",
startOffset,
sectionSizeInBytesNode,
WITH_LOC(t.numberLiteralFromRaw(numberOfGlobals), startLoc)
);
export function encodeSectionMetadata(n) {
var out = [];
var sectionId = constants.sections[n.section];
if (typeof sectionId === "undefined") {
throw new Error("Unknown section: " + n.section);
}
if (n.section === "start") {
/**
* This is not implemented yet because it's a special case which
* doesn't have a vector in its section.
*/
throw new Error("Unsupported section encoding of type start");
}
out.push(sectionId);
out.push.apply(out, _toConsumableArray(encodeU32(n.size.value)));
out.push.apply(out, _toConsumableArray(encodeU32(n.vectorOfSize.value)));
export function encodeSectionMetadata(n) {
var out = [];
var sectionId = constants.sections[n.section];
if (typeof sectionId === "undefined") {
throw new Error("Unknown section: " + n.section);
}
if (n.section === "start") {
/**
* This is not implemented yet because it's a special case which
* doesn't have a vector in its section.
*/
throw new Error("Unsupported section encoding of type start");
}
out.push(sectionId);
out.push.apply(out, _toConsumableArray(encodeU32(n.size.value)));
out.push.apply(out, _toConsumableArray(encodeU32(n.vectorOfSize.value)));