From f35b208a360882570bf97bfa7ff8d1293017dc95 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 29 Aug 2018 13:30:36 -0700 Subject: wasm-emscripten-finalize: Don't allow duplicates in 'declares'/'invok… (#1655) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allowing duplicates here was causes emscripten to generate a JS object with duplicate keys. --- src/wasm/wasm-emscripten.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/wasm/wasm-emscripten.cpp') diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index c70069e76..580510ecb 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -724,6 +724,12 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata( meta << "]"; } + // Avoid adding duplicate imports to `declares' or `invokeFuncs`. Even + // though we might import the same function multiple times (i.e. with + // different sigs) we only need to list is in the metadata once. + std::set declares; + std::set invokeFuncs; + // We use the `base` rather than the `name` of the imports here and below // becasue this is the externally visible name that the embedder (JS) will // see. @@ -735,7 +741,8 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata( !import->base.startsWith(EMSCRIPTEN_ASM_CONST.str) && !import->base.startsWith("invoke_") && !import->base.startsWith("jsCall_")) { - meta << maybeComma() << '"' << import->base.str << '"'; + if (declares.insert(import->base.str).second) + meta << maybeComma() << '"' << import->base.str << '"'; } } meta << "]"; @@ -769,7 +776,8 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata( commaFirst = true; for (const auto& import : wasm.imports) { if (import->base.startsWith("invoke_")) { - meta << maybeComma() << '"' << import->base.str << '"'; + if (invokeFuncs.insert(import->base.str).second) + meta << maybeComma() << '"' << import->base.str << '"'; } } meta << "]"; -- cgit v1.2.3