@@ -1486,12 +1486,12 @@ void WasmBinaryBuilder::readSignatures() {
14861486}
14871487
14881488Name WasmBinaryBuilder::getFunctionIndexName (Index i) {
1489- if (i < functionImportIndexes .size ()) {
1490- auto * import = wasm. getImport (functionImportIndexes [i]) ;
1489+ if (i < functionImports .size ()) {
1490+ auto * import = functionImports [i];
14911491 assert (import ->kind == ExternalKind::Function);
14921492 return import ->name ;
14931493 } else {
1494- i -= functionImportIndexes .size ();
1494+ i -= functionImports .size ();
14951495 if (i >= wasm.functions .size ()) {
14961496 throw ParseException (" bad function index" );
14971497 }
@@ -1529,7 +1529,8 @@ void WasmBinaryBuilder::readImports() {
15291529 }
15301530 curr->functionType = wasm.functionTypes [index]->name ;
15311531 assert (curr->functionType .is ());
1532- functionImportIndexes.push_back (curr->name );
1532+ functionImports.push_back (curr);
1533+ continue ; // don't add the import yet, we add them later after we know their names
15331534 break ;
15341535 }
15351536 case ExternalKind::Table: {
@@ -1962,10 +1963,14 @@ Name WasmBinaryBuilder::getGlobalName(Index index) {
19621963}
19631964
19641965void WasmBinaryBuilder::processFunctions () {
1965- for (auto & func : functions) {
1966+ for (auto * func : functions) {
19661967 wasm.addFunction (func);
19671968 }
19681969
1970+ for (auto * import : functionImports) {
1971+ wasm.addImport (import );
1972+ }
1973+
19691974 // we should have seen all the functions
19701975 // we assume this later down in fact, when we read wasm.functions[index],
19711976 // as index was validated vs functionTypes.size()
@@ -2002,6 +2007,14 @@ void WasmBinaryBuilder::processFunctions() {
20022007 }
20032008 }
20042009
2010+ for (auto & iter : functionImportCalls) {
2011+ size_t index = iter.first ;
2012+ auto & calls = iter.second ;
2013+ for (auto * call : calls) {
2014+ call->target = functionImports[index]->name ;
2015+ }
2016+ }
2017+
20052018 for (auto & pair : functionTable) {
20062019 auto i = pair.first ;
20072020 auto & indexes = pair.second ;
@@ -2076,18 +2089,16 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) {
20762089 continue ;
20772090 }
20782091 auto num = getU32LEB ();
2079- uint32_t importedFunctions = 0 ;
2080- for (auto & import : wasm.imports ) {
2081- if (import ->kind != ExternalKind::Function) continue ;
2082- importedFunctions++;
2083- }
20842092 for (size_t i = 0 ; i < num; i++) {
20852093 auto index = getU32LEB ();
2086- if (index < importedFunctions) {
2087- getInlineString (); // TODO: use this
2088- } else if (index - importedFunctions < functions.size ()) {
2089- auto name = getInlineString ();
2090- functions[index - importedFunctions]->name = name;
2094+ auto name = getInlineString ();
2095+ // note: we silently ignore errors here, as name section errors
2096+ // are not fatal. should we warn?
2097+ auto numFunctionImports = functionImports.size ();
2098+ if (index < numFunctionImports) {
2099+ functionImports[index]->name = name;
2100+ } else if (index - numFunctionImports < functions.size ()) {
2101+ functions[index - numFunctionImports]->name = name;
20912102 }
20922103 }
20932104 // disallow duplicate names
@@ -2359,19 +2370,20 @@ Expression* WasmBinaryBuilder::visitCall() {
23592370 auto index = getU32LEB ();
23602371 FunctionType* type;
23612372 Expression* ret;
2362- if (index < functionImportIndexes .size ()) {
2373+ if (index < functionImports .size ()) {
23632374 // this is a call of an imported function
23642375 auto * call = allocator.alloc <CallImport>();
2365- auto * import = wasm.getImport (functionImportIndexes[index]);
2366- call->target = import ->name ;
2376+ auto * import = functionImports[index];
23672377 type = wasm.getFunctionType (import ->functionType );
2378+ functionImportCalls[index].push_back (call);
2379+ call->target = import ->name ; // name section may modify it
23682380 fillCall (call, type);
23692381 call->finalize ();
23702382 ret = call;
23712383 } else {
23722384 // this is a call of a defined function
23732385 auto * call = allocator.alloc <Call>();
2374- auto adjustedIndex = index - functionImportIndexes .size ();
2386+ auto adjustedIndex = index - functionImports .size ();
23752387 if (adjustedIndex >= functionTypes.size ()) {
23762388 throw ParseException (" bad call index" );
23772389 }
0 commit comments