123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752 |
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
- function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
- import { codeFrameFromSource } from "@webassemblyjs/helper-code-frame";
- import * as t from "@webassemblyjs/ast";
- import { parse32I } from "./number-literals";
- import { parseString } from "./string-literals";
- import { tokens, keywords } from "./tokenizer";
- function hasPlugin(name) {
- if (name !== "wast") throw new Error("unknow plugin");
- return true;
- }
- function isKeyword(token, id) {
- return token.type === tokens.keyword && token.value === id;
- }
- function tokenToString(token) {
- if (token.type === "keyword") {
- return "keyword (".concat(token.value, ")");
- }
- return token.type;
- }
- function identifierFromToken(token) {
- var _token$loc = token.loc,
- end = _token$loc.end,
- start = _token$loc.start;
- return t.withLoc(t.identifier(token.value), end, start);
- }
- export function parse(tokensList, source) {
- var current = 0;
- var getUniqueName = t.getUniqueNameGenerator();
- var state = {
- registredExportedElements: []
- };
-
- function walk() {
- var token = tokensList[current];
- function eatToken() {
- token = tokensList[++current];
- }
- function getEndLoc() {
- var currentToken = token;
- if (typeof currentToken === "undefined") {
- var lastToken = tokensList[tokensList.length - 1];
- currentToken = lastToken;
- }
- return currentToken.loc.end;
- }
- function getStartLoc() {
- return token.loc.start;
- }
- function eatTokenOfType(type) {
- if (token.type !== type) {
- throw new Error("\n" + codeFrameFromSource(source, token.loc) + "Assertion error: expected token of type " + type + ", given " + tokenToString(token));
- }
- eatToken();
- }
- function parseExportIndex(token) {
- if (token.type === tokens.identifier) {
- var index = identifierFromToken(token);
- eatToken();
- return index;
- } else if (token.type === tokens.number) {
- var _index = t.numberLiteralFromRaw(token.value);
- eatToken();
- return _index;
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "unknown export index" + ", given " + tokenToString(token));
- }();
- }
- }
- function lookaheadAndCheck() {
- var len = arguments.length;
- for (var i = 0; i < len; i++) {
- var tokenAhead = tokensList[current + i];
- var expectedToken = i < 0 || arguments.length <= i ? undefined : arguments[i];
- if (tokenAhead.type === "keyword") {
- if (isKeyword(tokenAhead, expectedToken) === false) {
- return false;
- }
- } else if (expectedToken !== tokenAhead.type) {
- return false;
- }
- }
- return true;
- }
-
- function maybeIgnoreComment() {
- if (typeof token === "undefined") {
-
- return;
- }
- while (token.type === tokens.comment) {
- eatToken();
- if (typeof token === "undefined") {
-
- break;
- }
- }
- }
-
- function parseMemory() {
- var id = t.identifier(getUniqueName("memory"));
- var limits = t.limit(0);
- if (token.type === tokens.string || token.type === tokens.identifier) {
- id = t.identifier(token.value);
- eatToken();
- } else {
- id = t.withRaw(id, "");
- }
-
- if (lookaheadAndCheck(tokens.openParen, keywords.data)) {
- eatToken();
- eatToken();
-
- var stringInitializer = token.value;
- eatTokenOfType(tokens.string);
- limits = t.limit(stringInitializer.length);
- eatTokenOfType(tokens.closeParen);
- }
-
- if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
- eatToken();
- eatToken();
- if (token.type !== tokens.string) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Expected string in export" + ", given " + tokenToString(token));
- }();
- }
- var _name = token.value;
- eatToken();
- state.registredExportedElements.push({
- exportType: "Memory",
- name: _name,
- id: id
- });
- eatTokenOfType(tokens.closeParen);
- }
-
- if (token.type === tokens.number) {
- limits = t.limit(parse32I(token.value));
- eatToken();
- if (token.type === tokens.number) {
- limits.max = parse32I(token.value);
- eatToken();
- }
- }
- return t.memory(limits, id);
- }
-
- function parseData() {
-
- var memidx = 0;
- if (token.type === tokens.number) {
- memidx = token.value;
- eatTokenOfType(tokens.number);
- }
- eatTokenOfType(tokens.openParen);
- var offset;
- if (token.type === tokens.valtype) {
- eatTokenOfType(tokens.valtype);
- eatTokenOfType(tokens.dot);
- if (token.value !== "const") {
- throw new Error("constant expression required");
- }
- eatTokenOfType(tokens.name);
- var numberLiteral = t.numberLiteralFromRaw(token.value, "i32");
- offset = t.objectInstruction("const", "i32", [numberLiteral]);
- eatToken();
- eatTokenOfType(tokens.closeParen);
- } else {
- eatTokenOfType(tokens.name);
- var _numberLiteral = t.numberLiteralFromRaw(token.value, "i32");
- offset = t.instruction("get_global", [_numberLiteral]);
- eatToken();
- eatTokenOfType(tokens.closeParen);
- }
- var byteArray = parseString(token.value);
- eatToken();
- return t.data(t.memIndexLiteral(memidx), offset, t.byteArray(byteArray));
- }
-
- function parseTable() {
- var name = t.identifier(getUniqueName("table"));
- var limit = t.limit(0);
- var elemIndices = [];
- var elemType = "anyfunc";
- if (token.type === tokens.string || token.type === tokens.identifier) {
- name = identifierFromToken(token);
- eatToken();
- } else {
- name = t.withRaw(name, "");
- }
- while (token.type !== tokens.closeParen) {
-
- if (lookaheadAndCheck(tokens.openParen, keywords.elem)) {
- eatToken();
- eatToken();
- while (token.type === tokens.identifier) {
- elemIndices.push(t.identifier(token.value));
- eatToken();
- }
- eatTokenOfType(tokens.closeParen);
- } else if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
- eatToken();
- eatToken();
- if (token.type !== tokens.string) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Expected string in export" + ", given " + tokenToString(token));
- }();
- }
- var exportName = token.value;
- eatToken();
- state.registredExportedElements.push({
- exportType: "Table",
- name: exportName,
- id: name
- });
- eatTokenOfType(tokens.closeParen);
- } else if (isKeyword(token, keywords.anyfunc)) {
-
- eatToken();
- } else if (token.type === tokens.number) {
-
- var min = parseInt(token.value);
- eatToken();
- if (token.type === tokens.number) {
- var max = parseInt(token.value);
- eatToken();
- limit = t.limit(min, max);
- } else {
- limit = t.limit(min);
- }
- eatToken();
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token" + ", given " + tokenToString(token));
- }();
- }
- }
- if (elemIndices.length > 0) {
- return t.table(elemType, limit, name, elemIndices);
- } else {
- return t.table(elemType, limit, name);
- }
- }
-
- function parseImport() {
- if (token.type !== tokens.string) {
- throw new Error("Expected a string, " + token.type + " given.");
- }
- var moduleName = token.value;
- eatToken();
- if (token.type !== tokens.string) {
- throw new Error("Expected a string, " + token.type + " given.");
- }
- var name = token.value;
- eatToken();
- eatTokenOfType(tokens.openParen);
- var descr;
- if (isKeyword(token, keywords.func)) {
- eatToken();
- var fnParams = [];
- var fnResult = [];
- var fnName = t.identifier(getUniqueName("func"));
- if (token.type === tokens.identifier) {
- fnName = identifierFromToken(token);
- eatToken();
- }
- while (token.type === tokens.openParen) {
- eatToken();
- if (lookaheadAndCheck(keywords.param) === true) {
- eatToken();
- fnParams.push.apply(fnParams, _toConsumableArray(parseFuncParam()));
- } else if (lookaheadAndCheck(keywords.result) === true) {
- 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, t.signature(fnParams, fnResult));
- } else if (isKeyword(token, keywords.global)) {
- eatToken();
- if (token.type === tokens.openParen) {
- eatToken();
- eatTokenOfType(tokens.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");
- }
- } else if (isKeyword(token, keywords.memory) === true) {
- eatToken();
- descr = parseMemory();
- } else if (isKeyword(token, keywords.table) === true) {
- eatToken();
- descr = parseTable();
- } else {
- throw new Error("Unsupported import type: " + tokenToString(token));
- }
- eatTokenOfType(tokens.closeParen);
- return t.moduleImport(moduleName, name, descr);
- }
-
- function parseBlock() {
- var label = t.identifier(getUniqueName("block"));
- var blockResult = null;
- var instr = [];
- if (token.type === tokens.identifier) {
- label = identifierFromToken(token);
- eatToken();
- } else {
- label = t.withRaw(label, "");
- }
- while (token.type === tokens.openParen) {
- eatToken();
- if (lookaheadAndCheck(keywords.result) === true) {
- eatToken();
- blockResult = token.value;
- eatToken();
- } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
-
- instr.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in block body of type" + ", given " + tokenToString(token));
- }();
- }
- maybeIgnoreComment();
- eatTokenOfType(tokens.closeParen);
- }
- return t.blockInstruction(label, instr, blockResult);
- }
-
- function parseIf() {
- var blockResult = null;
- var label = t.identifier(getUniqueName("if"));
- var testInstrs = [];
- var consequent = [];
- var alternate = [];
- if (token.type === tokens.identifier) {
- label = identifierFromToken(token);
- eatToken();
- } else {
- label = t.withRaw(label, "");
- }
- while (token.type === tokens.openParen) {
- eatToken();
-
- if (isKeyword(token, keywords.result) === true) {
- eatToken();
- blockResult = token.value;
- eatTokenOfType(tokens.valtype);
- eatTokenOfType(tokens.closeParen);
- continue;
- }
-
- if (isKeyword(token, keywords.then) === true) {
- eatToken();
- while (token.type === tokens.openParen) {
- eatToken();
- if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
- consequent.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in consequent body of type" + ", given " + tokenToString(token));
- }();
- }
- eatTokenOfType(tokens.closeParen);
- }
- eatTokenOfType(tokens.closeParen);
- continue;
- }
-
- if (isKeyword(token, keywords.else)) {
- eatToken();
- while (token.type === tokens.openParen) {
- eatToken();
- if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
- alternate.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in alternate body of type" + ", given " + tokenToString(token));
- }();
- }
- eatTokenOfType(tokens.closeParen);
- }
- eatTokenOfType(tokens.closeParen);
- continue;
- }
-
- if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
- testInstrs.push(parseFuncInstr());
- eatTokenOfType(tokens.closeParen);
- continue;
- }
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in if body" + ", given " + tokenToString(token));
- }();
- }
- return t.ifInstruction(label, testInstrs, blockResult, consequent, alternate);
- }
-
- function parseLoop() {
- var label = t.identifier(getUniqueName("loop"));
- var blockResult;
- var instr = [];
- if (token.type === tokens.identifier) {
- label = identifierFromToken(token);
- eatToken();
- } else {
- label = t.withRaw(label, "");
- }
- while (token.type === tokens.openParen) {
- eatToken();
- if (lookaheadAndCheck(keywords.result) === true) {
- eatToken();
- blockResult = token.value;
- eatToken();
- } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
-
- instr.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in loop body" + ", given " + tokenToString(token));
- }();
- }
- eatTokenOfType(tokens.closeParen);
- }
- return t.loopInstruction(label, blockResult, instr);
- }
- function parseCallIndirect() {
- var typeRef;
- var params = [];
- var results = [];
- var instrs = [];
- while (token.type !== tokens.closeParen) {
- if (lookaheadAndCheck(tokens.openParen, keywords.type)) {
- eatToken();
- eatToken();
- typeRef = parseTypeReference();
- } else if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
- eatToken();
- eatToken();
-
- if (token.type !== tokens.closeParen) {
- params.push.apply(params, _toConsumableArray(parseFuncParam()));
- }
- } else if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
- eatToken();
- eatToken();
-
- if (token.type !== tokens.closeParen) {
- results.push.apply(results, _toConsumableArray(parseFuncResult()));
- }
- } else {
- eatTokenOfType(tokens.openParen);
- instrs.push(parseFuncInstr());
- }
- eatTokenOfType(tokens.closeParen);
- }
- return t.callIndirectInstruction(typeRef !== undefined ? typeRef : t.signature(params, results), instrs);
- }
-
- function parseExport() {
- if (token.type !== tokens.string) {
- throw new Error("Expected string after export, got: " + token.type);
- }
- var name = token.value;
- eatToken();
- var moduleExportDescr = parseModuleExportDescr();
- return t.moduleExport(name, moduleExportDescr);
- }
- function parseModuleExportDescr() {
- var startLoc = getStartLoc();
- var type = "";
- var index;
- eatTokenOfType(tokens.openParen);
- while (token.type !== tokens.closeParen) {
- if (isKeyword(token, keywords.func)) {
- type = "Func";
- eatToken();
- index = parseExportIndex(token);
- } else if (isKeyword(token, keywords.table)) {
- type = "Table";
- eatToken();
- index = parseExportIndex(token);
- } else if (isKeyword(token, keywords.global)) {
- type = "Global";
- eatToken();
- index = parseExportIndex(token);
- } else if (isKeyword(token, keywords.memory)) {
- type = "Memory";
- eatToken();
- index = parseExportIndex(token);
- }
- eatToken();
- }
- if (type === "") {
- throw new Error("Unknown export type");
- }
- if (index === undefined) {
- throw new Error("Exported function must have a name");
- }
- var node = t.moduleExportDescr(type, index);
- var endLoc = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(node, endLoc, startLoc);
- }
- function parseModule() {
- var name = null;
- var isBinary = false;
- var isQuote = false;
- var moduleFields = [];
- if (token.type === tokens.identifier) {
- name = token.value;
- eatToken();
- }
- if (hasPlugin("wast") && token.type === tokens.name && token.value === "binary") {
- eatToken();
- isBinary = true;
- }
- if (hasPlugin("wast") && token.type === tokens.name && token.value === "quote") {
- eatToken();
- isQuote = true;
- }
- if (isBinary === true) {
- var blob = [];
- while (token.type === tokens.string) {
- blob.push(token.value);
- eatToken();
- maybeIgnoreComment();
- }
- eatTokenOfType(tokens.closeParen);
- return t.binaryModule(name, blob);
- }
- if (isQuote === true) {
- var string = [];
- while (token.type === tokens.string) {
- string.push(token.value);
- eatToken();
- }
- eatTokenOfType(tokens.closeParen);
- return t.quoteModule(name, string);
- }
- while (token.type !== tokens.closeParen) {
- moduleFields.push(walk());
- if (state.registredExportedElements.length > 0) {
- state.registredExportedElements.forEach(function (decl) {
- moduleFields.push(t.moduleExport(decl.name, t.moduleExportDescr(decl.exportType, decl.id)));
- });
- state.registredExportedElements = [];
- }
- token = tokensList[current];
- }
- eatTokenOfType(tokens.closeParen);
- return t.module(name, moduleFields);
- }
-
- function parseFuncInstrArguments(signature) {
- var args = [];
- var namedArgs = {};
- var signaturePtr = 0;
- while (token.type === tokens.name || isKeyword(token, keywords.offset)) {
- var key = token.value;
- eatToken();
- eatTokenOfType(tokens.equal);
- var value = void 0;
- if (token.type === tokens.number) {
- value = t.numberLiteralFromRaw(token.value);
- } else {
- throw new Error("Unexpected type for argument: " + token.type);
- }
- namedArgs[key] = value;
- eatToken();
- }
- var signatureLength = signature.vector ? Infinity : signature.length;
- while (token.type !== tokens.closeParen && (
- token.type === tokens.openParen || signaturePtr < signatureLength)) {
- if (token.type === tokens.identifier) {
- args.push(t.identifier(token.value));
- eatToken();
- } else if (token.type === tokens.valtype) {
-
- args.push(t.valtypeLiteral(token.value));
- eatToken();
- } else if (token.type === tokens.string) {
- args.push(t.stringLiteral(token.value));
- eatToken();
- } else if (token.type === tokens.number) {
- args.push(
-
- t.numberLiteralFromRaw(token.value,
- signature[signaturePtr] || "f64"));
- if (!signature.vector) {
- ++signaturePtr;
- }
- eatToken();
- } else if (token.type === tokens.openParen) {
-
- eatToken();
- if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
-
- args.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in nested instruction" + ", given " + tokenToString(token));
- }();
- }
- if (token.type === tokens.closeParen) {
- eatToken();
- }
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in instruction argument" + ", given " + tokenToString(token));
- }();
- }
- }
- return {
- args: args,
- namedArgs: namedArgs
- };
- }
-
- function parseFuncInstr() {
- var startLoc = getStartLoc();
- maybeIgnoreComment();
-
- if (token.type === tokens.name || token.type === tokens.valtype) {
- var _name2 = token.value;
- var object;
- eatToken();
- if (token.type === tokens.dot) {
- object = _name2;
- eatToken();
- if (token.type !== tokens.name) {
- throw new TypeError("Unknown token: " + token.type + ", name expected");
- }
- _name2 = token.value;
- eatToken();
- }
- if (token.type === tokens.closeParen) {
- var _endLoc = token.loc.end;
- if (typeof object === "undefined") {
- return t.withLoc(t.instruction(_name2), _endLoc, startLoc);
- } else {
- return t.withLoc(t.objectInstruction(_name2, object, []), _endLoc, startLoc);
- }
- }
- var signature = t.signatureForOpcode(object || "", _name2);
- var _parseFuncInstrArgume = parseFuncInstrArguments(signature),
- _args = _parseFuncInstrArgume.args,
- _namedArgs = _parseFuncInstrArgume.namedArgs;
- var endLoc = token.loc.end;
- if (typeof object === "undefined") {
- return t.withLoc(t.instruction(_name2, _args, _namedArgs), endLoc, startLoc);
- } else {
- return t.withLoc(t.objectInstruction(_name2, object, _args, _namedArgs), endLoc, startLoc);
- }
- } else if (isKeyword(token, keywords.loop)) {
-
- eatToken();
- return parseLoop();
- } else if (isKeyword(token, keywords.block)) {
- eatToken();
- return parseBlock();
- } else if (isKeyword(token, keywords.call_indirect)) {
- eatToken();
- return parseCallIndirect();
- } else if (isKeyword(token, keywords.call)) {
- eatToken();
- var index;
- if (token.type === tokens.identifier) {
- index = identifierFromToken(token);
- eatToken();
- } else if (token.type === tokens.number) {
- index = t.indexLiteral(token.value);
- eatToken();
- }
- var instrArgs = [];
- while (token.type === tokens.openParen) {
- eatToken();
- instrArgs.push(parseFuncInstr());
- eatTokenOfType(tokens.closeParen);
- }
- if (typeof index === "undefined") {
- throw new Error("Missing argument in call instruciton");
- }
- if (instrArgs.length > 0) {
- return t.callInstruction(index, instrArgs);
- } else {
- return t.callInstruction(index);
- }
- } else if (isKeyword(token, keywords.if)) {
- eatToken();
- return parseIf();
- } else if (isKeyword(token, keywords.module) && hasPlugin("wast")) {
- eatToken();
-
-
- var module = parseModule();
- return module;
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected instruction in function body" + ", given " + tokenToString(token));
- }();
- }
- }
-
- function parseFunc() {
- var fnName = t.identifier(getUniqueName("func"));
- var typeRef;
- var fnBody = [];
- var fnParams = [];
- var fnResult = [];
- if (token.type === tokens.identifier) {
- fnName = identifierFromToken(token);
- eatToken();
- } else {
- fnName = t.withRaw(fnName, "");
- }
- maybeIgnoreComment();
- while (token.type === tokens.openParen || token.type === tokens.name || token.type === tokens.valtype) {
-
- if (token.type === tokens.name || token.type === tokens.valtype) {
- fnBody.push(parseFuncInstr());
- continue;
- }
- eatToken();
- if (lookaheadAndCheck(keywords.param) === true) {
- eatToken();
- fnParams.push.apply(fnParams, _toConsumableArray(parseFuncParam()));
- } else if (lookaheadAndCheck(keywords.result) === true) {
- eatToken();
- fnResult.push.apply(fnResult, _toConsumableArray(parseFuncResult()));
- } else if (lookaheadAndCheck(keywords.export) === true) {
- eatToken();
- parseFuncExport(fnName);
- } else if (lookaheadAndCheck(keywords.type) === true) {
- eatToken();
- typeRef = parseTypeReference();
- } else if (lookaheadAndCheck(tokens.name) === true || lookaheadAndCheck(tokens.valtype) === true || token.type === "keyword"
- ) {
-
- fnBody.push(parseFuncInstr());
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in func body" + ", given " + tokenToString(token));
- }();
- }
- eatTokenOfType(tokens.closeParen);
- }
- return t.func(fnName, typeRef !== undefined ? typeRef : t.signature(fnParams, fnResult), fnBody);
- }
-
- function parseFuncExport(funcId) {
- if (token.type !== tokens.string) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Function export expected a string" + ", given " + tokenToString(token));
- }();
- }
- var name = token.value;
- eatToken();
-
- var id = t.identifier(funcId.value);
- state.registredExportedElements.push({
- exportType: "Func",
- name: name,
- id: id
- });
- }
-
- function parseType() {
- var id;
- var params = [];
- var result = [];
- if (token.type === tokens.identifier) {
- id = identifierFromToken(token);
- eatToken();
- }
- if (lookaheadAndCheck(tokens.openParen, keywords.func)) {
- eatToken();
- eatToken();
- if (token.type === tokens.closeParen) {
- eatToken();
- return t.typeInstruction(id, t.signature([], []));
- }
- if (lookaheadAndCheck(tokens.openParen, keywords.param)) {
- eatToken();
- eatToken();
- params = parseFuncParam();
- eatTokenOfType(tokens.closeParen);
- }
- if (lookaheadAndCheck(tokens.openParen, keywords.result)) {
- eatToken();
- eatToken();
- result = parseFuncResult();
- eatTokenOfType(tokens.closeParen);
- }
- eatTokenOfType(tokens.closeParen);
- }
- return t.typeInstruction(id, t.signature(params, result));
- }
-
- function parseFuncResult() {
- var results = [];
- while (token.type !== tokens.closeParen) {
- if (token.type !== tokens.valtype) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unexpected token in func result" + ", given " + tokenToString(token));
- }();
- }
- var valtype = token.value;
- eatToken();
- results.push(valtype);
- }
- return results;
- }
-
- function parseTypeReference() {
- var ref;
- if (token.type === tokens.identifier) {
- ref = identifierFromToken(token);
- eatToken();
- } else if (token.type === tokens.number) {
- ref = t.numberLiteralFromRaw(token.value);
- eatToken();
- }
- return ref;
- }
-
- function parseGlobal() {
- var name = t.identifier(getUniqueName("global"));
- var type;
- var importing = null;
- maybeIgnoreComment();
- if (token.type === tokens.identifier) {
- name = identifierFromToken(token);
- eatToken();
- } else {
- name = t.withRaw(name, "");
- }
-
- if (lookaheadAndCheck(tokens.openParen, keywords.export)) {
- eatToken();
- eatToken();
- var exportName = token.value;
- eatTokenOfType(tokens.string);
- state.registredExportedElements.push({
- exportType: "Global",
- name: exportName,
- id: name
- });
- eatTokenOfType(tokens.closeParen);
- }
-
- if (lookaheadAndCheck(tokens.openParen, keywords.import)) {
- eatToken();
- eatToken();
- var moduleName = token.value;
- eatTokenOfType(tokens.string);
- var _name3 = token.value;
- eatTokenOfType(tokens.string);
- importing = {
- module: moduleName,
- name: _name3,
- descr: undefined
- };
- eatTokenOfType(tokens.closeParen);
- }
-
- if (token.type === tokens.valtype) {
- type = t.globalType(token.value, "const");
- eatToken();
- } else if (token.type === tokens.openParen) {
- eatToken();
- if (isKeyword(token, keywords.mut) === false) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unsupported global type, expected mut" + ", given " + tokenToString(token));
- }();
- }
- eatToken();
- type = t.globalType(token.value, "var");
- eatToken();
- eatTokenOfType(tokens.closeParen);
- }
- if (type === undefined) {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Could not determine global type" + ", given " + tokenToString(token));
- }();
- }
- maybeIgnoreComment();
- var init = [];
- if (importing != null) {
- importing.descr = type;
- init.push(t.moduleImport(importing.module, importing.name, importing.descr));
- }
-
- while (token.type === tokens.openParen) {
- eatToken();
- init.push(parseFuncInstr());
- eatTokenOfType(tokens.closeParen);
- }
- return t.global(type, init, name);
- }
-
- function parseFuncParam() {
- var params = [];
- var id;
- var valtype;
- if (token.type === tokens.identifier) {
- id = token.value;
- eatToken();
- }
- if (token.type === tokens.valtype) {
- valtype = token.value;
- eatToken();
- params.push({
- id: id,
- valtype: valtype
- });
-
- if (id === undefined) {
- while (token.type === tokens.valtype) {
- valtype = token.value;
- eatToken();
- params.push({
- id: undefined,
- valtype: valtype
- });
- }
- }
- } else {
- }
- return params;
- }
-
- function parseElem() {
- var tableIndex = t.indexLiteral(0);
- var offset = [];
- var funcs = [];
- if (token.type === tokens.identifier) {
- tableIndex = identifierFromToken(token);
- eatToken();
- }
- if (token.type === tokens.number) {
- tableIndex = t.indexLiteral(token.value);
- eatToken();
- }
- while (token.type !== tokens.closeParen) {
- if (lookaheadAndCheck(tokens.openParen, keywords.offset)) {
- eatToken();
- eatToken();
- while (token.type !== tokens.closeParen) {
- eatTokenOfType(tokens.openParen);
- offset.push(parseFuncInstr());
- eatTokenOfType(tokens.closeParen);
- }
- eatTokenOfType(tokens.closeParen);
- } else if (token.type === tokens.identifier) {
- funcs.push(t.identifier(token.value));
- eatToken();
- } else if (token.type === tokens.number) {
- funcs.push(t.indexLiteral(token.value));
- eatToken();
- } else if (token.type === tokens.openParen) {
- eatToken();
- offset.push(parseFuncInstr());
- eatTokenOfType(tokens.closeParen);
- } else {
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unsupported token in elem" + ", given " + tokenToString(token));
- }();
- }
- }
- return t.elem(tableIndex, offset, funcs);
- }
-
- function parseStart() {
- if (token.type === tokens.identifier) {
- var index = identifierFromToken(token);
- eatToken();
- return t.start(index);
- }
- if (token.type === tokens.number) {
- var _index2 = t.indexLiteral(token.value);
- eatToken();
- return t.start(_index2);
- }
- throw new Error("Unknown start, token: " + tokenToString(token));
- }
- if (token.type === tokens.openParen) {
- eatToken();
- var startLoc = getStartLoc();
- if (isKeyword(token, keywords.export)) {
- eatToken();
- var node = parseExport();
- var _endLoc2 = getEndLoc();
- return t.withLoc(node, _endLoc2, startLoc);
- }
- if (isKeyword(token, keywords.loop)) {
- eatToken();
- var _node = parseLoop();
- var _endLoc3 = getEndLoc();
- return t.withLoc(_node, _endLoc3, startLoc);
- }
- if (isKeyword(token, keywords.func)) {
- eatToken();
- var _node2 = parseFunc();
- var _endLoc4 = getEndLoc();
- maybeIgnoreComment();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node2, _endLoc4, startLoc);
- }
- if (isKeyword(token, keywords.module)) {
- eatToken();
- var _node3 = parseModule();
- var _endLoc5 = getEndLoc();
- return t.withLoc(_node3, _endLoc5, startLoc);
- }
- if (isKeyword(token, keywords.import)) {
- eatToken();
- var _node4 = parseImport();
- var _endLoc6 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node4, _endLoc6, startLoc);
- }
- if (isKeyword(token, keywords.block)) {
- eatToken();
- var _node5 = parseBlock();
- var _endLoc7 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node5, _endLoc7, startLoc);
- }
- if (isKeyword(token, keywords.memory)) {
- eatToken();
- var _node6 = parseMemory();
- var _endLoc8 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node6, _endLoc8, startLoc);
- }
- if (isKeyword(token, keywords.data)) {
- eatToken();
- var _node7 = parseData();
- var _endLoc9 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node7, _endLoc9, startLoc);
- }
- if (isKeyword(token, keywords.table)) {
- eatToken();
- var _node8 = parseTable();
- var _endLoc10 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node8, _endLoc10, startLoc);
- }
- if (isKeyword(token, keywords.global)) {
- eatToken();
- var _node9 = parseGlobal();
- var _endLoc11 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node9, _endLoc11, startLoc);
- }
- if (isKeyword(token, keywords.type)) {
- eatToken();
- var _node10 = parseType();
- var _endLoc12 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node10, _endLoc12, startLoc);
- }
- if (isKeyword(token, keywords.start)) {
- eatToken();
- var _node11 = parseStart();
- var _endLoc13 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node11, _endLoc13, startLoc);
- }
- if (isKeyword(token, keywords.elem)) {
- eatToken();
- var _node12 = parseElem();
- var _endLoc14 = getEndLoc();
- eatTokenOfType(tokens.closeParen);
- return t.withLoc(_node12, _endLoc14, startLoc);
- }
- var instruction = parseFuncInstr();
- var endLoc = getEndLoc();
- maybeIgnoreComment();
- if (_typeof(instruction) === "object") {
- if (typeof token !== "undefined") {
- eatTokenOfType(tokens.closeParen);
- }
- return t.withLoc(instruction, endLoc, startLoc);
- }
- }
- if (token.type === tokens.comment) {
- var _startLoc = getStartLoc();
- var builder = token.opts.type === "leading" ? t.leadingComment : t.blockComment;
- var _node13 = builder(token.value);
- eatToken();
- var _endLoc15 = getEndLoc();
- return t.withLoc(_node13, _endLoc15, _startLoc);
- }
- throw function () {
- return new Error("\n" + codeFrameFromSource(source, token.loc) + "\n" + "Unknown token" + ", given " + tokenToString(token));
- }();
- }
- var body = [];
- while (current < tokensList.length) {
- body.push(walk());
- }
- return t.program(body);
- }
|