From 6a35e33f126d80e7583821e584ae9d101ba0ccb5 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 7 Jan 2021 20:01:06 +0000 Subject: [GC] Fix parsing/printing of ref types using i31 (#3469) This lets us parse (ref null i31) and (ref i31) and not just i31ref. It also fixes the parsing of i31ref, making it nullable for now, which we need to do until we support non-nullability. Fix some internal handling of i31 where we had just i31ref (which meant we just handled the non-nullable type). After fixing a bug in printing (where we didn't print out (ref null i31) properly), I found some a simplification, to remove TypeName. --- src/wasm/wasm-s-parser.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/wasm/wasm-s-parser.cpp') diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 20ab4f3d2..d76a3c49a 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -22,6 +22,7 @@ #include "ir/branch-utils.h" #include "shared-constants.h" +#include "support/string.h" #include "wasm-binary.h" #include "wasm-builder.h" @@ -867,7 +868,8 @@ Type SExpressionWasmBuilder::stringToType(const char* str, return Type::eqref; } if (strncmp(str, "i31ref", 6) == 0 && (prefix || str[6] == 0)) { - return Type::i31ref; + // FIXME: for now, force all inputs to be nullable + return Type(HeapType::BasicHeapType::i31, Nullable); } if (allowError) { return Type::none; @@ -2802,12 +2804,17 @@ HeapType SExpressionWasmBuilder::parseHeapType(Element& s) { } return types[it->second]; } else { - // index - size_t offset = atoi(s.str().c_str()); - if (offset >= types.size()) { - throw ParseException("unknown indexed function type", s.line, s.col); + // It may be a numerical index, or it may be a built-in type name like + // "i31". + auto* str = s.str().c_str(); + if (String::isNumber(str)) { + size_t offset = atoi(str); + if (offset >= types.size()) { + throw ParseException("unknown indexed function type", s.line, s.col); + } + return types[offset]; } - return types[offset]; + return stringToHeapType(str, /* prefix = */ false); } } // It's a list. -- cgit v1.2.3