Skip to content

Commit 66c71d0

Browse files
committed
capi: Instantiate with resolving host functions
1 parent 87e998e commit 66c71d0

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
@@ -130,6 +130,17 @@ bool fizzy_find_exported_function(
130130
FizzyInstance* fizzy_instantiate(const FizzyModule* module,
131131
const FizzyExternalFunction* imported_functions, size_t imported_functions_size);
132132

133+
/// Imported function.
134+
typedef struct FizzyImportedFunction
135+
{
136+
const char* module;
137+
const char* name;
138+
FizzyExternalFunction external_function;
139+
} FizzyImportedFunction;
140+
141+
struct FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* module,
142+
const FizzyImportedFunction* imported_functions, size_t imported_functions_size);
143+
133144
/// Free resources associated with the instance.
134145
/// If passed pointer is NULL, has no effect.
135146
void fizzy_free_instance(FizzyInstance* instance);

lib/fizzy/capi.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ inline fizzy::ExternalFunction unwrap(const FizzyExternalFunction& external_func
111111
return fizzy::ExternalFunction{
112112
unwrap(external_func.function, external_func.context), unwrap(external_func.type)};
113113
}
114+
115+
inline fizzy::ImportedFunction unwrap(const FizzyImportedFunction& imported_func)
116+
{
117+
fizzy::ImportedFunction result;
118+
result.module = imported_func.module ? std::string{imported_func.module} : std::string{};
119+
result.name = imported_func.name ? std::string{imported_func.name} : std::string{};
120+
121+
fizzy::FuncType type = unwrap(imported_func.external_function.type);
122+
result.inputs = std::move(type.inputs);
123+
result.output = type.outputs.empty() ? std::nullopt : std::make_optional(type.outputs[0]);
124+
125+
result.function =
126+
unwrap(imported_func.external_function.function, imported_func.external_function.context);
127+
128+
return result;
129+
}
130+
114131
} // namespace
115132

116133
extern "C" {
@@ -182,6 +199,29 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
182199
}
183200
}
184201

202+
FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* module,
203+
const FizzyImportedFunction* imported_functions, size_t imported_functions_size)
204+
{
205+
try
206+
{
207+
std::vector<fizzy::ImportedFunction> unwrapped_funcs(imported_functions_size);
208+
fizzy::ImportedFunction (*unwrap_imported_func_fn)(const FizzyImportedFunction&) = &unwrap;
209+
std::transform(imported_functions, imported_functions + imported_functions_size,
210+
unwrapped_funcs.begin(), unwrap_imported_func_fn);
211+
212+
std::unique_ptr<const fizzy::Module> unwrapped_module{unwrap(module)};
213+
auto imports = fizzy::resolve_imported_functions(*unwrapped_module, unwrapped_funcs);
214+
215+
auto instance = fizzy::instantiate(std::move(unwrapped_module), std::move(imports));
216+
217+
return wrap(instance.release());
218+
}
219+
catch (...)
220+
{
221+
return nullptr;
222+
}
223+
}
224+
185225
void fizzy_free_instance(FizzyInstance* instance)
186226
{
187227
delete unwrap(instance);

0 commit comments

Comments
 (0)