5#ifndef WASMTIME_LINKER_HH
6#define WASMTIME_LINKER_HH
30 std::unique_ptr<wasmtime_linker_t, deleter> ptr;
46 std::string_view name,
const Extern &item) {
48 detail::cvt_extern(item, raw);
51 name.data(), name.size(), &raw);
52 if (error !=
nullptr) {
55 return std::monostate();
64 if (error !=
nullptr) {
67 return std::monostate();
75 ptr.get(), cx.ptr, name.data(), name.size(), &instance.instance);
76 if (error !=
nullptr) {
79 return std::monostate();
89 if (error !=
nullptr) {
92 if (trap !=
nullptr) {
103 name.size(), m.ptr.get());
104 if (error !=
nullptr) {
107 return std::monostate();
112 [[nodiscard]] std::optional<Extern>
116 name.data(), name.size(), &item)) {
117 return detail::cvt_extern(item);
124 template <
typename F,
126 std::is_invocable_r_v<Result<std::monostate, Trap>, F,
Caller,
130 std::string_view name,
const FuncType &ty,
134 ptr.get(),
module.data(),
module.length(), name.data(), name.length(),
135 ty.ptr.get(), Func::raw_callback<std::remove_reference_t<F>>,
136 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
138 Func::raw_finalize<std::remove_reference_t<F>>);
140 if (error !=
nullptr) {
144 return std::monostate();
149 template <
typename F,
150 std::enable_if_t<WasmHostFunc<F>::Params::valid,
bool> =
true,
151 std::enable_if_t<WasmHostFunc<F>::Results::valid,
bool> =
true>
153 std::string_view name, F &&f) {
154 using HostFunc = WasmHostFunc<F>;
155 auto params = HostFunc::Params::types();
156 auto results = HostFunc::Results::types();
159 ptr.get(),
module.data(),
module.length(), name.data(), name.length(),
160 ty.ptr.get(), Func::raw_callback_unchecked<std::remove_reference_t<F>>,
161 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
163 Func::raw_finalize<std::remove_reference_t<F>>);
165 if (error !=
nullptr) {
169 return std::monostate();
178 if (error !=
nullptr) {
Structure provided to host functions to lookup caller information or acquire a Store::Context.
Definition: func.hh:28
Global compilation state in Wasmtime.
Definition: engine.hh:21
Errors coming from Wasmtime.
Definition: error.hh:25
Type information for a WebAssembly function.
Definition: types/func.hh:15
static FuncType from_iters(P params, R results)
Creates a new function type from the given list of parameters and results.
Definition: types/func.hh:75
Representation of a WebAssembly function.
Definition: func.hh:302
A WebAssembly instance.
Definition: instance.hh:31
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:25
Result< std::monostate > define(Store::Context cx, std::string_view module, std::string_view name, const Extern &item)
Defines the provided item into this linker with the given name.
Definition: linker.hh:45
Result< std::monostate > func_wrap(std::string_view module, std::string_view name, F &&f)
Definition: linker.hh:152
Linker(Engine &engine)
Creates a new linker which will instantiate in the given engine.
Definition: linker.hh:34
Result< std::monostate > func_new(std::string_view module, std::string_view name, const FuncType &ty, F &&f)
Definition: linker.hh:129
void allow_shadowing(bool allow)
Definition: linker.hh:40
Result< std::monostate > module(Store::Context cx, std::string_view name, const Module &m)
Definition: linker.hh:100
std::optional< Extern > get(Store::Context cx, std::string_view module, std::string_view name)
Definition: linker.hh:113
Result< Func > get_default(Store::Context cx, std::string_view name)
Definition: linker.hh:174
TrapResult< Instance > instantiate(Store::Context cx, const Module &m)
Definition: linker.hh:84
Result< std::monostate > define_instance(Store::Context cx, std::string_view name, Instance instance)
Definition: linker.hh:73
Result< std::monostate > define_wasi()
Definition: linker.hh:62
Representation of a compiled WebAssembly module.
Definition: module.hh:27
Fallible result type used for Wasmtime.
Definition: error.hh:82
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: store.hh:60
Information about a WebAssembly trap.
Definition: trap.hh:113
wasmtime_linker_t * wasmtime_linker_new(wasm_engine_t *engine)
Creates a new linker for the specified engine.
bool wasmtime_linker_get(const wasmtime_linker_t *linker, wasmtime_context_t *store, const char *module, size_t module_len, const char *name, size_t name_len, wasmtime_extern_t *item)
Loads an item by name from this linker.
wasmtime_error_t * wasmtime_linker_define_func_unchecked(wasmtime_linker_t *linker, const char *module, size_t module_len, const char *name, size_t name_len, const wasm_functype_t *ty, wasmtime_func_unchecked_callback_t cb, void *data, void(*finalizer)(void *))
Defines a new function in this linker.
wasmtime_error_t * wasmtime_linker_get_default(const wasmtime_linker_t *linker, wasmtime_context_t *store, const char *name, size_t name_len, wasmtime_func_t *func)
Acquires the "default export" of the named module in this linker.
wasmtime_error_t * wasmtime_linker_define_wasi(wasmtime_linker_t *linker)
Defines WASI functions in this linker.
wasmtime_error_t * wasmtime_linker_define(wasmtime_linker_t *linker, wasmtime_context_t *store, const char *module, size_t module_len, const char *name, size_t name_len, const wasmtime_extern_t *item)
Defines a new item in this linker.
void wasmtime_linker_allow_shadowing(wasmtime_linker_t *linker, bool allow_shadowing)
Configures whether this linker allows later definitions to shadow previous definitions.
wasmtime_error_t * wasmtime_linker_module(wasmtime_linker_t *linker, wasmtime_context_t *store, const char *name, size_t name_len, const wasmtime_module_t *module)
Defines automatic instantiations of a wasm_module_t in this linker.
wasmtime_error_t * wasmtime_linker_define_instance(wasmtime_linker_t *linker, wasmtime_context_t *store, const char *name, size_t name_len, const wasmtime_instance_t *instance)
Defines an instance under the specified name in this linker.
void wasmtime_linker_delete(wasmtime_linker_t *linker)
Deletes a linker.
wasmtime_error_t * wasmtime_linker_define_func(wasmtime_linker_t *linker, const char *module, size_t module_len, const char *name, size_t name_len, const wasm_functype_t *ty, wasmtime_func_callback_t cb, void *data, void(*finalizer)(void *))
Defines a new function in this linker.
wasmtime_error_t * wasmtime_linker_instantiate(const wasmtime_linker_t *linker, wasmtime_context_t *store, const wasmtime_module_t *module, wasmtime_instance_t *instance, wasm_trap_t **trap)
Instantiates a wasm_module_t with the items defined in this linker.
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:165
Container for different kinds of extern items.
Definition: extern.h:133
Representation of a function in Wasmtime.
Definition: extern.h:25
Representation of a instance in Wasmtime.
Definition: instance.h:26
Object used to conveniently link together and instantiate wasm modules.