@@ -108,57 +108,52 @@ struct LegalizeJSInterface : public Pass {
108108 // for each illegal import, we must call a legalized stub instead
109109 for (auto * im : originalFunctions) {
110110 if (im->imported () && isIllegal (im)) {
111- auto funcName = makeLegalStubForCalledImport (im, module );
112- illegalImportsToLegal[im->name ] = funcName;
113- // we need to use the legalized version in the tables, as the import
114- // from JS is legal for JS. Our stub makes it look like a native wasm
115- // function.
116- ElementUtils::iterAllElementFunctionNames (module , [&](Name& name) {
117- if (name == im->name ) {
118- name = funcName;
119- }
120- });
111+ auto * func = makeLegalStubForCalledImport (im, module );
112+ illegalImportsToLegal[im->name ] = func;
121113 }
122114 }
123115
124116 if (!illegalImportsToLegal.empty ()) {
125- // fix up imports: call_import of an illegal must be turned to a call of a
126- // legal. the same must be done with ref.funcs.
117+ // fix up imports: call of an illegal import must be turned to a call of a
118+ // legal import . the same must be done with ref.funcs.
127119 struct Fixer : public WalkerPass <PostWalker<Fixer>> {
128120 bool isFunctionParallel () override { return true ; }
129121
130122 std::unique_ptr<Pass> create () override {
131123 return std::make_unique<Fixer>(illegalImportsToLegal);
132124 }
133125
134- std::map <Name, Name>* illegalImportsToLegal;
126+ std::unordered_map <Name, Function*>& illegalImportsToLegal;
135127
136- Fixer (std::map <Name, Name>* illegalImportsToLegal)
128+ Fixer (std::unordered_map <Name, Function*>& illegalImportsToLegal)
137129 : illegalImportsToLegal(illegalImportsToLegal) {}
138130
139131 void visitCall (Call* curr) {
140- auto iter = illegalImportsToLegal-> find (curr->target );
141- if (iter == illegalImportsToLegal-> end ()) {
132+ auto iter = illegalImportsToLegal. find (curr->target );
133+ if (iter == illegalImportsToLegal. end ()) {
142134 return ;
143135 }
144136
145- replaceCurrent (
146- Builder (*getModule ())
147- .makeCall (
148- iter->second , curr->operands , curr->type , curr->isReturn ));
137+ replaceCurrent (Builder (*getModule ())
138+ .makeCall (iter->second ->name ,
139+ curr->operands ,
140+ curr->type ,
141+ curr->isReturn ));
149142 }
150143
151144 void visitRefFunc (RefFunc* curr) {
152- auto iter = illegalImportsToLegal-> find (curr->func );
153- if (iter == illegalImportsToLegal-> end ()) {
145+ auto iter = illegalImportsToLegal. find (curr->func );
146+ if (iter == illegalImportsToLegal. end ()) {
154147 return ;
155148 }
156149
157- curr->func = iter->second ;
150+ curr->func = iter->second ->name ;
151+ // TODO: Make this exact.
152+ curr->type = Type (iter->second ->type , NonNullable);
158153 }
159154 };
160155
161- Fixer fixer (& illegalImportsToLegal);
156+ Fixer fixer (illegalImportsToLegal);
162157 fixer.run (getPassRunner (), module );
163158 fixer.runOnModuleCode (getPassRunner (), module );
164159
@@ -174,7 +169,7 @@ struct LegalizeJSInterface : public Pass {
174169
175170private:
176171 // map of illegal to legal names for imports
177- std::map <Name, Name > illegalImportsToLegal;
172+ std::unordered_map <Name, Function* > illegalImportsToLegal;
178173 bool exportedHelpers = false ;
179174 Function* getTempRet0 = nullptr ;
180175 Function* setTempRet0 = nullptr ;
@@ -274,7 +269,7 @@ struct LegalizeJSInterface : public Pass {
274269
275270 // wasm calls the import, so it must call a stub that calls the actual legal
276271 // JS import
277- Name makeLegalStubForCalledImport (Function* im, Module* module ) {
272+ Function* makeLegalStubForCalledImport (Function* im, Module* module ) {
278273 Builder builder (*module );
279274 auto legalIm = std::make_unique<Function>();
280275 legalIm->name = Name (std::string (" legalimport$" ) + im->name .toString ());
@@ -315,14 +310,14 @@ struct LegalizeJSInterface : public Pass {
315310 }
316311 legalIm->type = Signature (Type (params), call->type );
317312
318- const auto & stubName = stub-> name ;
319- if (!module ->getFunctionOrNull (stubName )) {
313+ auto * stubPtr = stub. get () ;
314+ if (!module ->getFunctionOrNull (stub-> name )) {
320315 module ->addFunction (std::move (stub));
321316 }
322317 if (!module ->getFunctionOrNull (legalIm->name )) {
323318 module ->addFunction (std::move (legalIm));
324319 }
325- return stubName ;
320+ return stubPtr ;
326321 }
327322
328323 static Function*
0 commit comments