Skip to content

Commit 0a80dfc

Browse files
committed
capi: Instantiate with resolving host functions
1 parent cb2bd47 commit 0a80dfc

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

include/fizzy/fizzy.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ bool fizzy_find_exported_function(
112112
struct FizzyInstance* fizzy_instantiate(struct FizzyModule* module,
113113
const struct FizzyExternalFunction* imported_functions, size_t imported_functions_size);
114114

115+
/// Imported function.
116+
typedef struct FizzyImportedFunction
117+
{
118+
const char* module;
119+
const char* name;
120+
struct FizzyExternalFunction external_function;
121+
} FizzyImportedFunction;
122+
123+
struct FizzyInstance* fizzy_resolve_instantiate(struct FizzyModule* module,
124+
const struct FizzyImportedFunction* imported_functions, size_t imported_functions_size);
125+
115126
/// Free resources associated with the instance.
116127
void fizzy_free_instance(struct FizzyInstance* instance);
117128

lib/fizzy/capi.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ inline fizzy::ExternalFunction unwrap(const FizzyExternalFunction& external_func
117117
return fizzy::ExternalFunction{
118118
unwrap(external_func.function, external_func.context), unwrap(external_func.type)};
119119
}
120+
121+
inline fizzy::ImportedFunction unwrap(const FizzyImportedFunction& imported_func)
122+
{
123+
fizzy::ImportedFunction result;
124+
result.module = imported_func.module ? std::string{imported_func.module} : std::string{};
125+
result.name = imported_func.name ? std::string{imported_func.name} : std::string{};
126+
127+
fizzy::FuncType type = unwrap(imported_func.external_function.type);
128+
result.inputs = std::move(type.inputs);
129+
result.output = type.outputs.empty() ? std::nullopt : std::make_optional(type.outputs[0]);
130+
131+
result.function =
132+
unwrap(imported_func.external_function.function, imported_func.external_function.context);
133+
134+
return result;
135+
}
136+
120137
} // namespace
121138

122139
extern "C" {
@@ -188,6 +205,29 @@ FizzyInstance* fizzy_instantiate(FizzyModule* module,
188205
}
189206
}
190207

208+
FizzyInstance* fizzy_resolve_instantiate(FizzyModule* module,
209+
const FizzyImportedFunction* imported_functions, size_t imported_functions_size)
210+
{
211+
try
212+
{
213+
std::vector<fizzy::ImportedFunction> unwrapped_funcs(imported_functions_size);
214+
fizzy::ImportedFunction (*unwrap_imported_func_fn)(const FizzyImportedFunction&) = &unwrap;
215+
std::transform(imported_functions, imported_functions + imported_functions_size,
216+
unwrapped_funcs.begin(), unwrap_imported_func_fn);
217+
218+
std::unique_ptr<fizzy::Module> unwrapped_module{unwrap(module)};
219+
auto imports = fizzy::resolve_imported_functions(*unwrapped_module, unwrapped_funcs);
220+
221+
auto instance = fizzy::instantiate(std::move(unwrapped_module), std::move(imports));
222+
223+
return wrap(instance.release());
224+
}
225+
catch (...)
226+
{
227+
return nullptr;
228+
}
229+
}
230+
191231
void fizzy_free_instance(FizzyInstance* instance)
192232
{
193233
delete unwrap(instance);

0 commit comments

Comments
 (0)