diff options
Diffstat (limited to 'src/wasm/wasm-binary.cpp')
-rw-r--r-- | src/wasm/wasm-binary.cpp | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index aec2efe0e..b5bce8649 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1486,12 +1486,12 @@ void WasmBinaryBuilder::readSignatures() { } Name WasmBinaryBuilder::getFunctionIndexName(Index i) { - if (i < functionImportIndexes.size()) { - auto* import = wasm.getImport(functionImportIndexes[i]); + if (i < functionImports.size()) { + auto* import = functionImports[i]; assert(import->kind == ExternalKind::Function); return import->name; } else { - i -= functionImportIndexes.size(); + i -= functionImports.size(); if (i >= wasm.functions.size()) { throw ParseException("bad function index"); } @@ -1529,7 +1529,8 @@ void WasmBinaryBuilder::readImports() { } curr->functionType = wasm.functionTypes[index]->name; assert(curr->functionType.is()); - functionImportIndexes.push_back(curr->name); + functionImports.push_back(curr); + continue; // don't add the import yet, we add them later after we know their names break; } case ExternalKind::Table: { @@ -1962,10 +1963,14 @@ Name WasmBinaryBuilder::getGlobalName(Index index) { } void WasmBinaryBuilder::processFunctions() { - for (auto& func : functions) { + for (auto* func : functions) { wasm.addFunction(func); } + for (auto* import : functionImports) { + wasm.addImport(import); + } + // we should have seen all the functions // we assume this later down in fact, when we read wasm.functions[index], // as index was validated vs functionTypes.size() @@ -2002,6 +2007,14 @@ void WasmBinaryBuilder::processFunctions() { } } + for (auto& iter : functionImportCalls) { + size_t index = iter.first; + auto& calls = iter.second; + for (auto* call : calls) { + call->target = functionImports[index]->name; + } + } + for (auto& pair : functionTable) { auto i = pair.first; auto& indexes = pair.second; @@ -2076,18 +2089,16 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { continue; } auto num = getU32LEB(); - uint32_t importedFunctions = 0; - for (auto& import : wasm.imports) { - if (import->kind != ExternalKind::Function) continue; - importedFunctions++; - } for (size_t i = 0; i < num; i++) { auto index = getU32LEB(); - if (index < importedFunctions) { - getInlineString(); // TODO: use this - } else if (index - importedFunctions < functions.size()) { - auto name = getInlineString(); - functions[index - importedFunctions]->name = name; + auto name = getInlineString(); + // note: we silently ignore errors here, as name section errors + // are not fatal. should we warn? + auto numFunctionImports = functionImports.size(); + if (index < numFunctionImports) { + functionImports[index]->name = name; + } else if (index - numFunctionImports < functions.size()) { + functions[index - numFunctionImports]->name = name; } } // disallow duplicate names @@ -2359,19 +2370,20 @@ Expression* WasmBinaryBuilder::visitCall() { auto index = getU32LEB(); FunctionType* type; Expression* ret; - if (index < functionImportIndexes.size()) { + if (index < functionImports.size()) { // this is a call of an imported function auto* call = allocator.alloc<CallImport>(); - auto* import = wasm.getImport(functionImportIndexes[index]); - call->target = import->name; + auto* import = functionImports[index]; type = wasm.getFunctionType(import->functionType); + functionImportCalls[index].push_back(call); + call->target = import->name; // name section may modify it fillCall(call, type); call->finalize(); ret = call; } else { // this is a call of a defined function auto* call = allocator.alloc<Call>(); - auto adjustedIndex = index - functionImportIndexes.size(); + auto adjustedIndex = index - functionImports.size(); if (adjustedIndex >= functionTypes.size()) { throw ParseException("bad call index"); } |