diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 7b3ee3d35d..00a44c764f 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -1567,6 +1567,37 @@ execution_result execute(const Module& module, FuncIdx func_idx, std::vector resolve_imported_functions( + Module module, std::vector imported_functions) +{ + std::vector external_functions; + for (const auto& import : module.importsec) + { + if (import.kind != ExternalKind::Function) + continue; + + const auto it = std::find_if( + imported_functions.begin(), imported_functions.end(), [&import](const auto& func) { + return import.module == func.module && import.name == func.name; + }); + + if (it == imported_functions.end()) + { + throw instantiate_error( + "imported function " + import.module + "." + import.name + " is required"); + } + + assert(import.desc.function_type_index < module.typesec.size()); + external_functions.emplace_back(ExternalFunction{ + std::move(it->function), module.typesec[import.desc.function_type_index]}); + } + + if (external_functions.size() != imported_functions.size()) + throw instantiate_error("some imported functions are not required by the module"); + + return external_functions; +} + std::optional find_exported_function(const Module& module, std::string_view name) { for (const auto& export_ : module.exportsec) diff --git a/lib/fizzy/execute.hpp b/lib/fizzy/execute.hpp index cb60ce9825..4f71f62f92 100644 --- a/lib/fizzy/execute.hpp +++ b/lib/fizzy/execute.hpp @@ -100,6 +100,16 @@ execution_result execute( // TODO: remove this helper execution_result execute(const Module& module, FuncIdx func_idx, std::vector args); +struct ImportedFunction +{ + std::string module; + std::string name; + std::function, int depth)> function; +}; + +std::vector resolve_imported_functions( + Module module, std::vector function); + // Find exported function index by name. std::optional find_exported_function(const Module& module, std::string_view name);