From 9c5df069cc466d904c8faa1888095e84ae74d404 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 14 Oct 2020 21:10:26 -0500 Subject: Assign import names consistently between text and binaryn reader (#3238) The s-parser was assigning numbers names per-type where as the binaryn reader was using the global import count as the number to append. This change switches to use per-element count which I think it preferable as it increases the stability of the auto-generated names. e.g. memory is now always named `$mimport0`. --- src/wasm-s-parser.h | 2 ++ src/wasm/wasm-binary.cpp | 17 ++++++++++++----- src/wasm/wasm-s-parser.cpp | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/wasm-s-parser.h b/src/wasm-s-parser.h index 0f9a27abc..b6dcc058c 100644 --- a/src/wasm-s-parser.h +++ b/src/wasm-s-parser.h @@ -120,6 +120,8 @@ class SExpressionWasmBuilder { int functionCounter = 0; int globalCounter = 0; int eventCounter = 0; + int tableCounter = 0; + int memoryCounter = 0; // we need to know function return types before we parse their contents std::map functionTypes; std::unordered_map debugInfoFileIndices; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 8ebddc8e8..9376c88ad 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1450,6 +1450,11 @@ void WasmBinaryBuilder::readImports() { size_t num = getU32LEB(); BYN_TRACE("num: " << num << std::endl); Builder builder(wasm); + size_t tableCounter = 0; + size_t memoryCounter = 0; + size_t functionCounter = 0; + size_t globalCounter = 0; + size_t eventCounter = 0; for (size_t i = 0; i < num; i++) { BYN_TRACE("read one\n"); auto module = getInlineString(); @@ -1460,7 +1465,7 @@ void WasmBinaryBuilder::readImports() { // could occur later due to the names section. switch (kind) { case ExternalKind::Function: { - auto name = Name(std::string("fimport$") + std::to_string(i)); + Name name(std::string("fimport$") + std::to_string(functionCounter++)); auto index = getU32LEB(); if (index >= signatures.size()) { throwError("invalid function index " + std::to_string(index) + " / " + @@ -1474,9 +1479,10 @@ void WasmBinaryBuilder::readImports() { break; } case ExternalKind::Table: { + Name name(std::string("timport$") + std::to_string(tableCounter++)); wasm.table.module = module; wasm.table.base = base; - wasm.table.name = Name(std::string("timport$") + std::to_string(i)); + wasm.table.name = name; auto elementType = getS32LEB(); WASM_UNUSED(elementType); if (elementType != BinaryConsts::EncodedType::funcref) { @@ -1499,9 +1505,10 @@ void WasmBinaryBuilder::readImports() { break; } case ExternalKind::Memory: { + Name name(std::string("mimport$") + std::to_string(memoryCounter++)); wasm.memory.module = module; wasm.memory.base = base; - wasm.memory.name = Name(std::string("mimport$") + std::to_string(i)); + wasm.memory.name = name; wasm.memory.exists = true; getResizableLimits(wasm.memory.initial, wasm.memory.max, @@ -1511,7 +1518,7 @@ void WasmBinaryBuilder::readImports() { break; } case ExternalKind::Global: { - auto name = Name(std::string("gimport$") + std::to_string(i)); + Name name(std::string("gimport$") + std::to_string(globalCounter++)); auto type = getConcreteType(); auto mutable_ = getU32LEB(); auto* curr = @@ -1526,7 +1533,7 @@ void WasmBinaryBuilder::readImports() { break; } case ExternalKind::Event: { - auto name = Name(std::string("eimport$") + std::to_string(i)); + Name name(std::string("eimport$") + std::to_string(eventCounter++)); auto attribute = getU32LEB(); auto index = getU32LEB(); if (index >= signatures.size()) { diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 462d440d8..d3367d3f6 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2366,9 +2366,9 @@ void SExpressionWasmBuilder::parseImport(Element& s) { name = Name("gimport$" + std::to_string(globalCounter++)); globalNames.push_back(name); } else if (kind == ExternalKind::Memory) { - name = Name("mimport$" + std::to_string(0)); + name = Name("mimport$" + std::to_string(memoryCounter++)); } else if (kind == ExternalKind::Table) { - name = Name("timport$" + std::to_string(0)); + name = Name("timport$" + std::to_string(tableCounter++)); } else if (kind == ExternalKind::Event) { name = Name("eimport$" + std::to_string(eventCounter++)); eventNames.push_back(name); -- cgit v1.2.3