From 921644ca65afbafb84fb82d58dacc4a028e2d720 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 17 May 2024 17:49:45 -0700 Subject: Rewrite wasm-shell to use new wast parser (#6601) Use the new wast parser to parse a full script up front, then traverse the parsed script data structure and execute the commands. wasm-shell had previously used the new wat parser for top-level modules, but it now uses the new parser for module assertions as well. Fix various bugs this uncovered. After this change, wasm-shell supports all the assertions used in the upstream spec tests (although not new kinds of assertions introduced in any proposals). Uncomment various `assert_exhaustion` tests that we can now execute. Other kinds of assertions remain commented out in our tests: wasm-shell now supports `assert_unlinkable`, but the interpreter does not eagerly check for the existence of imports, so those tests do not pass. Tests that check for NaNs also remain commented out because they do not yet use the standard syntax that wasm-shell now supports for canonical and arithmetic NaN results, and our interpreter would not pass all of those tests even if they did use the standard syntax. --- src/parser/parsers.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/parser/parsers.h') diff --git a/src/parser/parsers.h b/src/parser/parsers.h index 88600fec3..5900deb27 100644 --- a/src/parser/parsers.h +++ b/src/parser/parsers.h @@ -30,7 +30,8 @@ template Result heaptype(Ctx&); template MaybeResult reftype(Ctx&); template MaybeResult tupletype(Ctx&); template Result valtype(Ctx&); -template MaybeResult params(Ctx&); +template +MaybeResult params(Ctx&, bool allowNames = true); template MaybeResult results(Ctx&); template MaybeResult functype(Ctx&); template Result storagetype(Ctx&); @@ -325,7 +326,8 @@ MaybeResult maybeLabelidx(Ctx&, template Result labelidx(Ctx&, bool inDelegate = false); template Result tagidx(Ctx&); -template Result typeuse(Ctx&); +template +Result typeuse(Ctx&, bool allowNames = true); MaybeResult inlineImport(Lexer&); Result> inlineExports(Lexer&); template Result<> strtype(Ctx&); @@ -561,13 +563,18 @@ template Result valtype(Ctx& ctx) { // param ::= '(' 'param id? t:valtype ')' => [t] // | '(' 'param t*:valtype* ')' => [t*] // params ::= param* -template MaybeResult params(Ctx& ctx) { +template +MaybeResult params(Ctx& ctx, bool allowNames) { bool hasAny = false; auto res = ctx.makeParams(); while (ctx.in.takeSExprStart("param"sv)) { hasAny = true; + auto pos = ctx.in.getPos(); if (auto id = ctx.in.takeID()) { // Single named param + if (!allowNames) { + return ctx.in.err(pos, "unexpected named parameter"); + } auto type = valtype(ctx); CHECK_ERR(type); if (!ctx.in.takeRParen()) { @@ -1065,7 +1072,7 @@ template Result blocktype(Ctx& ctx) { // We either had no results or multiple results. Reset and parse again as a // type use. ctx.in = initialLexer; - auto use = typeuse(ctx); + auto use = typeuse(ctx, false); CHECK_ERR(use); auto type = ctx.getBlockTypeFromTypeUse(pos, *use); @@ -1935,7 +1942,7 @@ Result<> makeCallIndirect(Ctx& ctx, bool isReturn) { auto table = maybeTableidx(ctx); CHECK_ERR(table); - auto type = typeuse(ctx); + auto type = typeuse(ctx, false); CHECK_ERR(type); return ctx.makeCallIndirect( pos, annotations, table.getPtr(), *type, isReturn); @@ -2669,7 +2676,8 @@ template Result tagidx(Ctx& ctx) { // (if typedefs[x] = [t1*] -> [t2*]) // | ((t1,IDs):param)* (t2:result)* => x, IDs // (if x is minimum s.t. typedefs[x] = [t1*] -> [t2*]) -template Result typeuse(Ctx& ctx) { +template +Result typeuse(Ctx& ctx, bool allowNames) { auto pos = ctx.in.getPos(); std::optional type; if (ctx.in.takeSExprStart("type"sv)) { @@ -2683,7 +2691,7 @@ template Result typeuse(Ctx& ctx) { type = *x; } - auto namedParams = params(ctx); + auto namedParams = params(ctx, allowNames); CHECK_ERR(namedParams); auto resultTypes = results(ctx); -- cgit v1.2.3