Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
lookaheadAndCheck(tokens.name) === true ||
lookaheadAndCheck(tokens.valtype) === true ||
token.type === "keyword" // is any keyword
) {
// Instruction
fnBody.push(parseFuncInstr());
} else {
throw createUnexpectedToken("Unexpected token in func body");
}
eatTokenOfType(tokens.closeParen);
}
return t.func(
fnName,
typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult),
fnBody
);
}
eatTokenOfType(tokens.closeParen);
}
if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
eatToken(); // (
eatToken(); // result
result = parseFuncResult();
eatTokenOfType(tokens.closeParen);
}
eatTokenOfType(tokens.closeParen);
}
return t.typeInstruction(id, t.signature(params, result));
}
/**
eatTokenOfType(tokens.closeParen);
}
if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
eatToken(); // (
eatToken(); // result
result = parseFuncResult();
eatTokenOfType(tokens.closeParen);
}
eatTokenOfType(tokens.closeParen);
}
return t.typeInstruction(id, t.signature(params, result));
}
/**
eatToken();
fnResult.push.apply(fnResult, _toConsumableArray(parseFuncResult()));
} else {
throw function () {
return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in import of type" + ", given " + tokenToString(token));
}();
}
eatTokenOfType(tokens.closeParen);
}
if (typeof fnName === "undefined") {
throw new Error("Imported function must have a name");
}
descr = t.funcImportDescr(fnName, typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult));
} else if (isKeyword(token, keywords.global)) {
eatToken(); // keyword
if (token.type === tokens.openParen) {
eatToken(); // (
eatTokenOfType(tokens.keyword); // mut keyword
var valtype = token.value;
eatToken();
descr = t.globalType(valtype, "var");
eatTokenOfType(tokens.closeParen);
} else {
var _valtype = token.value;
eatTokenOfType(tokens.valtype);
descr = t.globalType(_valtype, "const");
let params = [];
let result = [];
if (token.type === tokens.identifier) {
id = identifierFromToken(token);
eatToken();
}
if (lookaheadAndCheck(tokens.openParen, keywords.func)) {
eatToken(); // (
eatToken(); // func
if (token.type === tokens.closeParen) {
eatToken();
// function with an empty signature, we can abort here
return t.typeInstruction(id, t.signature([], []));
}
if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
eatToken(); // (
eatToken(); // param
params = parseFuncParam();
eatTokenOfType(tokens.closeParen);
}
if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
eatToken(); // (
eatToken(); // result
result = parseFuncResult();
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 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 CompileError("zero flag expected");
}
code.push(function () {
var endLoc = getPosition();
return t.withLoc(_callNode, endLoc, _startLoc10);
}());
instructionAlreadyCreated = true;
} else if (instruction.name === "br_table") {
var result = [];
if (token.type === tokens.identifier) {
id = identifierFromToken(token);
eatToken();
}
if (lookaheadAndCheck(tokens.openParen, keywords.func)) {
eatToken(); // (
eatToken(); // func
if (token.type === tokens.closeParen) {
eatToken(); // function with an empty signature, we can abort here
return t.typeInstruction(id, t.signature([], []));
}
if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
eatToken(); // (
eatToken(); // param
params = parseFuncParam();
eatTokenOfType(tokens.closeParen);
}
if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
eatToken(); // (
eatToken(); // result
eatTokenOfType(tokens.closeParen);
}
if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
eatToken(); // (
eatToken(); // result
result = parseFuncResult();
eatTokenOfType(tokens.closeParen);
}
eatTokenOfType(tokens.closeParen);
}
return t.typeInstruction(id, t.signature(params, result));
}
* (result)`
*/
if (token.type !== tokens.closeParen) {
results.push(...parseFuncResult());
}
} else {
eatTokenOfType(tokens.openParen);
instrs.push(parseFuncInstr());
}
eatTokenOfType(tokens.closeParen);
}
return t.callIndirectInstruction(
typeRef !== undefined ? typeRef : t.signature(params, results),
instrs
);
}
var importDescr = void 0;
if (descrType === "func") {
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 CompileError("function signature not found (".concat(typeindex, ")"));
}
var id = getUniqueName("func");
importDescr = t.funcImportDescr(id, t.signature(signature.params, signature.result));
state.functionsInModule.push({
id: t.identifier(name.value),
signature: signature,
isExternal: true
});
} else if (descrType === "global") {
importDescr = parseGlobalType();
var globalNode = t.global(importDescr, []);
state.globalsInModule.push(globalNode);
} else if (descrType === "table") {
importDescr = parseTableType(i);
} else if (descrType === "mem") {
var memoryNode = parseMemoryType(0);
state.memoriesInModule.push(memoryNode);
importDescr = memoryNode;
} else {