From 69229061afefd8209d4a456adbfdae7935806e24 Mon Sep 17 00:00:00 2001 From: dcode Date: Wed, 2 Sep 2020 15:32:09 +0200 Subject: [PATCH] Fix LegalizeJSInterface leaking duplicate stub Functions --- src/passes/LegalizeJSInterface.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp index 7991d7eb30a..2f3e484ad79 100644 --- a/src/passes/LegalizeJSInterface.cpp +++ b/src/passes/LegalizeJSInterface.cpp @@ -214,9 +214,16 @@ struct LegalizeJSInterface : public Pass { // JS calls the export, so it must call a legal stub that calls the actual // wasm function Name makeLegalStub(Function* func, Module* module) { + Name legalName(std::string("legalstub$") + func->name.str); + + // a method may be exported multiple times + if (module->getFunctionOrNull(legalName)) { + return legalName; + } + Builder builder(*module); auto* legal = new Function(); - legal->name = Name(std::string("legalstub$") + func->name.str); + legal->name = legalName; auto* call = module->allocator.alloc(); call->target = func->name; @@ -254,11 +261,7 @@ struct LegalizeJSInterface : public Pass { legal->body = call; } - // a method may be exported multiple times - if (!module->getFunctionOrNull(legal->name)) { - module->addFunction(legal); - } - return legal->name; + return module->addFunction(legal)->name; } // wasm calls the import, so it must call a stub that calls the actual legal