From 04c1515acb354714af3d07fe2a3b65b2bb0398ab Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 25 Feb 2021 21:15:41 +0000 Subject: Support Type names in the Names section (#3615) --- src/wasm/wasm-binary.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 0ddc11320..0f5a03117 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -685,6 +685,27 @@ void WasmBinaryWriter::writeNames() { } } + // type names + { + std::vector namedTypes; + for (auto& kv : typeIndices) { + auto type = kv.first; + if (wasm->typeNames.count(type)) { + namedTypes.push_back(type); + } + } + if (!namedTypes.empty()) { + auto substart = + startSubsection(BinaryConsts::UserSections::Subsection::NameType); + o << U32LEB(namedTypes.size()); + for (auto type : namedTypes) { + o << U32LEB(typeIndices[type]); + writeEscapedName(wasm->typeNames[type].name.str); + } + finishSubsection(substart); + } + } + // table names { std::vector> tablesWithNames; @@ -2805,6 +2826,22 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { } } } + } else if (nameType == BinaryConsts::UserSections::Subsection::NameType) { + auto num = getU32LEB(); + NameProcessor processor; + for (size_t i = 0; i < num; i++) { + auto index = getU32LEB(); + auto rawName = getInlineString(); + auto name = processor.process(rawName); + if (index < types.size()) { + wasm.typeNames[types[index]].name = name; + } else { + std::cerr << "warning: type index out of bounds in name section, " + "type subsection: " + << std::string(rawName.str) << " at index " + << std::to_string(index) << std::endl; + } + } } else if (nameType == BinaryConsts::UserSections::Subsection::NameTable) { auto num = getU32LEB(); NameProcessor processor; -- cgit v1.2.3