From 11ec03ce930121736655769b9bbccaae0280b64c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 3 Mar 2021 19:53:26 +0000 Subject: [Wasm GC] Parse text field names even of types that end up canonicalized together (#3647) Names of structurally identical types end up "collapsed" together after the types are canonicalized, but with this PR we can properly read content that has structurally identical types with different names. --- src/wasm/wasm-s-parser.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src/wasm') diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 0b0f3f407..3baf19063 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -786,10 +786,6 @@ void SExpressionWasmBuilder::preParseHeapTypes(Element& module) { builder.getTempTupleType(results)); }; - // Maps type indexes to a mapping of field index => name. We store the data - // here while parsing as types have not been created yet. - std::unordered_map> fieldNames; - // Parses a field, and notes the name if one is found. auto parseField = [&](Element* elem, Name& name) { Mutability mutable_ = Immutable; @@ -2610,27 +2606,28 @@ Expression* SExpressionWasmBuilder::makeStructNew(Element& s, bool default_) { return Builder(wasm).makeStructNew(rtt, operands); } -Index SExpressionWasmBuilder::getStructIndex(const HeapType& type, Element& s) { - if (s.dollared()) { - auto name = s.str(); - auto struct_ = type.getStruct(); +Index SExpressionWasmBuilder::getStructIndex(Element& type, Element& field) { + if (field.dollared()) { + auto name = field.str(); + auto index = typeIndices[type.str().str]; + auto struct_ = types[index].getStruct(); auto& fields = struct_.fields; - const auto& fieldNames = wasm.typeNames[type].fieldNames; + const auto& names = fieldNames[index]; for (Index i = 0; i < fields.size(); i++) { - auto it = fieldNames.find(i); - if (it != fieldNames.end() && it->second == name) { + auto it = names.find(i); + if (it != names.end() && it->second == name) { return i; } } - throw ParseException("bad struct field name", s.line, s.col); + throw ParseException("bad struct field name", field.line, field.col); } // this is a numeric index - return atoi(s.c_str()); + return atoi(field.c_str()); } Expression* SExpressionWasmBuilder::makeStructGet(Element& s, bool signed_) { auto heapType = parseHeapType(*s[1]); - auto index = getStructIndex(heapType, *s[2]); + auto index = getStructIndex(*s[1], *s[2]); auto type = heapType.getStruct().fields[index].type; auto ref = parseExpression(*s[3]); validateHeapTypeUsingChild(ref, heapType, s); @@ -2639,7 +2636,7 @@ Expression* SExpressionWasmBuilder::makeStructGet(Element& s, bool signed_) { Expression* SExpressionWasmBuilder::makeStructSet(Element& s) { auto heapType = parseHeapType(*s[1]); - auto index = getStructIndex(heapType, *s[2]); + auto index = getStructIndex(*s[1], *s[2]); auto ref = parseExpression(*s[3]); validateHeapTypeUsingChild(ref, heapType, s); auto value = parseExpression(*s[4]); -- cgit v1.2.3