Skip to content

Commit

Permalink
API for resolving imported function names into function types
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 18, 2020
1 parent 565bfbf commit fc6787d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,37 @@ execution_result execute(const Module& module, FuncIdx func_idx, std::vector<uin
return execute(*instance, func_idx, std::move(args));
}

std::vector<ExternalFunction> resolve_imported_functions(
Module module, std::vector<ImportedFunction> imported_functions)
{
std::vector<ExternalFunction> 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<FuncIdx> find_exported_function(const Module& module, std::string_view name)
{
for (const auto& export_ : module.exportsec)
Expand Down
10 changes: 10 additions & 0 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ execution_result execute(
// TODO: remove this helper
execution_result execute(const Module& module, FuncIdx func_idx, std::vector<uint64_t> args);

struct ImportedFunction
{
std::string module;
std::string name;
std::function<execution_result(Instance&, std::vector<uint64_t>, int depth)> function;
};

std::vector<ExternalFunction> resolve_imported_functions(
Module module, std::vector<ImportedFunction> function);

// Find exported function index by name.
std::optional<FuncIdx> find_exported_function(const Module& module, std::string_view name);

Expand Down

0 comments on commit fc6787d

Please sign in to comment.