@@ -190,41 +190,42 @@ void BinaryenModuleDispose(BinaryenModuleRef module) {
190190
191191// Function types
192192
193- BinaryenFunctionTypeRef BinaryenAddFunctionType (BinaryenModuleRef module , const char * name, BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams) {
193+ BinaryenFunctionTypeRef BinaryenAddFunctionType (BinaryenModuleRef module , const char * name,
194+ BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams) {
194195 auto * wasm = (Module*)module ;
195- auto * ret = new FunctionType;
196- if (name) ret->name = name;
197- else ret->name = Name::fromInt (wasm->functionTypes .size ());
196+ std::unique_ptr<FunctionType> ret{new FunctionType};
197+ if (name)
198+ ret->name = name;
199+ else
200+ ret->name = Name::fromInt (wasm->functionTypes .size ());
198201 ret->result = Type (result);
199202 for (BinaryenIndex i = 0 ; i < numParams; i++) {
200203 ret->params .push_back (Type (paramTypes[i]));
201204 }
202205
203- // Lock. This can be called from multiple threads at once, and is a
204- // point where they all access and modify the module.
205- {
206- std::lock_guard<std::mutex> lock (BinaryenFunctionTypeMutex);
207- wasm->addFunctionType (ret);
208- }
209-
210206 if (tracing) {
211207 std::cout << " {\n " ;
212208 std::cout << " BinaryenType paramTypes[] = { " ;
213209 for (BinaryenIndex i = 0 ; i < numParams; i++) {
214- if (i > 0 ) std::cout << " , " ;
210+ if (i > 0 )
211+ std::cout << " , " ;
215212 std::cout << paramTypes[i];
216213 }
217- if (numParams == 0 ) std::cout << " 0" ; // ensure the array is not empty, otherwise a compiler error on VS
214+ if (numParams == 0 )
215+ std::cout << " 0" ; // ensure the array is not empty, otherwise a compiler error on VS
218216 std::cout << " };\n " ;
219217 size_t id = functionTypes.size ();
220218 std::cout << " functionTypes[" << id << " ] = BinaryenAddFunctionType(the_module, " ;
221- functionTypes[ret] = id;
219+ functionTypes[ret. get () ] = id;
222220 traceNameOrNULL (name);
223221 std::cout << " , " << result << " , paramTypes, " << numParams << " );\n " ;
224222 std::cout << " }\n " ;
225223 }
226224
227- return ret;
225+ // Lock. This can be called from multiple threads at once, and is a
226+ // point where they all access and modify the module.
227+ std::lock_guard<std::mutex> lock (BinaryenFunctionTypeMutex);
228+ return wasm->addFunctionType (std::move (ret));
228229}
229230void BinaryenRemoveFunctionType (BinaryenModuleRef module , const char * name) {
230231 if (tracing) {
0 commit comments