Skip to content

Commit 9791ba5

Browse files
committed
capi: Instantiate with resolving host functions
1 parent 87e998e commit 9791ba5

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

include/fizzy/fizzy.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ typedef struct FizzyExternalFunction
8181
void* context;
8282
} FizzyExternalFunction;
8383

84+
/// Imported function.
85+
typedef struct FizzyImportedFunction
86+
{
87+
/// Module name. NULL-terminated string. Cannot be NULL.
88+
const char* module;
89+
/// Function name. NULL-terminated string. Cannot be NULL.
90+
const char* name;
91+
/// External function, defining its type, pointer to function and context for calling it.
92+
FizzyExternalFunction external_function;
93+
} FizzyImportedFunction;
94+
8495
/// Validate binary module.
8596
bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size);
8697

@@ -130,6 +141,9 @@ bool fizzy_find_exported_function(
130141
FizzyInstance* fizzy_instantiate(const FizzyModule* module,
131142
const FizzyExternalFunction* imported_functions, size_t imported_functions_size);
132143

144+
struct FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* module,
145+
const FizzyImportedFunction* imported_functions, size_t imported_functions_size);
146+
133147
/// Free resources associated with the instance.
134148
/// If passed pointer is NULL, has no effect.
135149
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)