Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dump([funcLocalNum], "num locals");
var locals = [];
for (var _i = 0; _i < funcLocalNum; _i++) {
var localCountU32 = readU32();
var localCount = localCountU32.value;
eatBytes(localCountU32.nextIndex);
dump([localCount], "num local");
var valtypeByte = readByte();
eatBytes(1);
var type = _helperWasmBytecode.default.valtypes[valtypeByte];
locals.push(type);
dump([valtypeByte], type);
if (typeof type === "undefined") {
throw new _helperApiError.CompileError("Unexpected valtype: " + toHex(valtypeByte));
}
} // Decode instructions until the end
parseInstructionBlock(code);
code.unshift.apply(code, _toConsumableArray(locals.map(function (l) {
return t.instruction("local", [t.valtypeLiteral(l)]);
})));
var endLoc = getPosition();
state.elementsInCodeSection.push({
code: code,
locals: locals,
endLoc: endLoc,
startLoc: startLoc,
bodySize: bodySizeU32.value
});
var valtypeByte = readByte();
eatBytes(1);
var type = _helperWasmBytecode.default.valtypes[valtypeByte];
dump([valtypeByte], type);
if (typeof type === "undefined") {
throw new _helperApiError.CompileError("Unknown valtype: " + toHex(valtypeByte));
}
var globalTypeByte = readByte();
eatBytes(1);
var globalType = _helperWasmBytecode.default.globalTypes[globalTypeByte];
dump([globalTypeByte], "global type (".concat(globalType, ")"));
if (typeof globalType === "undefined") {
throw new _helperApiError.CompileError("Invalid mutability: " + toHex(globalTypeByte));
}
return t.globalType(type, globalType);
} // function parseNameModule() {
// const lenu32 = readVaruint32();
const type = constants.valtypes[valtypeByte];
const args = [];
for (let i = 0; i < localCount; i++) {
args.push(t.valtypeLiteral(type));
}
const localNode = WITH_LOC(t.instruction("local", args), startLoc);
locals.push(localNode);
dump([valtypeByte], type);
if (typeof type === "undefined") {
throw new CompileError("Unexpected valtype: " + toHex(valtypeByte));
}
}
code.push(...locals);
// Decode instructions until the end
parseInstructionBlock(code);
const endLoc = getPosition();
state.elementsInCodeSection.push({
code,
locals,
endLoc,
startLoc,
return t.withLoc(t.callInstruction(t.indexLiteral(index)), endLoc, _startLoc6);
}();
code.push(callNode);
instructionAlreadyCreated = true;
} else if (instruction.name === "call_indirect") {
var _startLoc10 = getPosition();
var indexU32 = readU32();
var typeindex = indexU32.value;
eatBytes(indexU32.nextIndex);
dump([typeindex], "type index");
var signature = state.typesInModule[typeindex];
if (typeof signature === "undefined") {
throw new _helperApiError.CompileError("call_indirect signature not found (".concat(typeindex, ")"));
}
var _callNode = t.callIndirectInstruction(t.signature(signature.params, signature.result), []);
var flagU32 = readU32();
var flag = flagU32.value; // 0x00 - reserved byte
eatBytes(flagU32.nextIndex);
if (flag !== 0) {
throw new _helperApiError.CompileError("zero flag expected");
}
code.push(function () {
var endLoc = getPosition();
return t.withLoc(_callNode, endLoc, _startLoc10);
function parseDataSection(numberOfElements) {
var dataEntries = [];
dump([numberOfElements], "num elements");
for (var i = 0; i < numberOfElements; i++) {
var memoryIndexu32 = readU32();
var memoryIndex = memoryIndexu32.value;
eatBytes(memoryIndexu32.nextIndex);
dump([memoryIndex], "memory index");
var instrs = [];
parseInstructionBlock(instrs);
if (instrs.length !== 1) {
throw new _helperApiError.CompileError("data section offset must be a single instruction");
}
var bytes = parseVec(function (b) {
return b;
});
dump([], "init");
dataEntries.push(t.data(t.memIndexLiteral(memoryIndex), instrs[0], t.byteArray(bytes)));
}
return dataEntries;
} // https://webassembly.github.io/spec/binary/modules.html#binary-section
dump([length], "number");
if (length === 0) {
return [];
}
var elements = [];
for (var i = 0; i < length; i++) {
var byte = readByte();
eatBytes(1);
var value = cast(byte);
dump([byte], value);
if (typeof value === "undefined") {
throw new _helperApiError.CompileError("Internal failure: parseVec could not cast the value");
}
elements.push(value);
}
return elements;
} // Type section
// https://webassembly.github.io/spec/binary/modules.html#binary-typesec
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Mem") {
var memNode = state.memoriesInModule[index];
if (typeof memNode === "undefined") {
throw new _helperApiError.CompileError("entry not found at index ".concat(index, " in memory section"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Global") {
var global = state.globalsInModule[index];
if (typeof global === "undefined") {
throw new _helperApiError.CompileError("entry not found at index ".concat(index, " in global section"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else {
console.warn("Unsupported export type: " + toHex(typeIndex));
return;
}
var endLoc = getPosition();
state.elementsInExportSection.push({
name: name.value,
type: _helperWasmBytecode.default.exportTypes[typeIndex],
signature: signature,
id: id,
index: index,
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Mem") {
var memNode = state.memoriesInModule[index];
if (typeof memNode === "undefined") {
throw new _helperApiError.CompileError("entry not found at index ".concat(index, " in memory section"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Global") {
var global = state.globalsInModule[index];
if (typeof global === "undefined") {
throw new _helperApiError.CompileError("entry not found at index ".concat(index, " in global section"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else {
console.warn("Unsupported export type: " + toHex(typeIndex));
return;
}
var endLoc = getPosition();
state.elementsInExportSection.push({
name: name.value,
type: _helperWasmBytecode.default.exportTypes[typeIndex],
signature: signature,
id: id,
index: index,
if (length === 0) {
return [];
}
const elements = [];
for (let i = 0; i < length; i++) {
const byte = readByte();
eatBytes(1);
const value = cast(byte);
dump([byte], value);
if (typeof value === "undefined") {
throw new CompileError(
"Internal failure: parseVec could not cast the value"
);
}
elements.push(value);
}
return elements;
}
var typeIndex = readByte();
eatBytes(1);
dump([typeIndex], "export kind");
var indexu32 = readU32();
var index = indexu32.value;
eatBytes(indexu32.nextIndex);
dump([index], "export index");
var id = void 0,
signature = void 0;
if (constants.exportTypes[typeIndex] === "Func") {
var func = state.functionsInModule[index];
if (typeof func === "undefined") {
throw new CompileError("unknown function (".concat(index, ")"));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = func.signature;
} else if (constants.exportTypes[typeIndex] === "Table") {
var table = state.tablesInModule[index];
if (typeof table === "undefined") {
throw new CompileError("unknown table ".concat(index));
}
id = t.numberLiteralFromRaw(index, String(index));
signature = null;
} else if (constants.exportTypes[typeIndex] === "Mem") {
var memNode = state.memoriesInModule[index];