5#ifndef WASMTIME_LINKER_HH
6#define WASMTIME_LINKER_HH
11#include <wasmtime/helpers.hh>
35 void allow_shadowing(
bool allow) {
41 std::string_view name,
const Extern &item) {
43 detail::cvt_extern(item, raw);
46 name.data(), name.size(), &raw);
47 if (error !=
nullptr) {
50 return std::monostate();
53#ifdef WASMTIME_FEATURE_WASI
60 if (error !=
nullptr) {
63 return std::monostate();
72 ptr.get(), cx.ptr, name.data(), name.size(), &instance.instance);
73 if (error !=
nullptr) {
76 return std::monostate();
86 if (error !=
nullptr) {
89 if (trap !=
nullptr) {
100 name.size(), m.capi());
101 if (error !=
nullptr) {
104 return std::monostate();
109 [[nodiscard]] std::optional<Extern>
110 get(
Store::Context cx, std::string_view module, std::string_view name) {
113 name.data(), name.size(), &item)) {
114 return detail::cvt_extern(item);
121 template <
typename F,
123 std::is_invocable_r_v<Result<std::monostate, Trap>, F,
Caller,
127 std::string_view name,
const FuncType &ty,
131 ptr.get(), module.data(), module.length(), name.data(), name.length(),
132 ty.ptr.get(), Func::raw_callback<std::remove_reference_t<F>>,
133 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
135 Func::raw_finalize<std::remove_reference_t<F>>);
137 if (error !=
nullptr) {
141 return std::monostate();
146 template <
typename F,
147 std::enable_if_t<WasmHostFunc<F>::Params::valid,
bool> =
true,
148 std::enable_if_t<WasmHostFunc<F>::Results::valid,
bool> =
true>
150 std::string_view name, F &&f) {
151 using HostFunc = WasmHostFunc<F>;
152 auto params = HostFunc::Params::types();
153 auto results = HostFunc::Results::types();
156 ptr.get(), module.data(), module.length(), name.data(), name.length(),
157 ty.ptr.get(), Func::raw_callback_unchecked<std::remove_reference_t<F>>,
158 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
160 Func::raw_finalize<std::remove_reference_t<F>>);
162 if (error !=
nullptr) {
166 return std::monostate();
175 if (error !=
nullptr) {
185 ptr.get(), module.capi());
186 if (error !=
nullptr) {
189 return std::monostate();
198 ptr.get(), cx.ptr, module.capi());
199 if (error !=
nullptr) {
202 return std::monostate();
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:22
Errors coming from Wasmtime.
Definition: error.hh:26
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:336
A WebAssembly instance.
Definition: instance.hh:31
Helper class for linking modules together with name-based resolution.
Definition: linker.hh:26
Representation of a compiled WebAssembly module.
Definition: module.hh:28
Fallible result type used for Wasmtime.
Definition: error.hh:70
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: store.hh:66
Information about a WebAssembly trap.
Definition: trap.hh:113
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN wasmtime_error_t * wasmtime_linker_define_wasi(wasmtime_linker_t *linker)
Defines WASI functions in this linker.
WASM_API_EXTERN void wasmtime_linker_allow_shadowing(wasmtime_linker_t *linker, bool allow_shadowing)
Configures whether this linker allows later definitions to shadow previous definitions.
WASM_API_EXTERN 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.
WASM_API_EXTERN wasmtime_error_t * wasmtime_linker_define_unknown_imports_as_default_values(wasmtime_linker_t *linker, wasmtime_context_t *store, wasmtime_module_t *module)
Ensures that module can be instantiated with this linker by defining all functions otherwise missing ...
WASM_API_EXTERN wasmtime_error_t * wasmtime_linker_define_unknown_imports_as_traps(wasmtime_linker_t *linker, wasmtime_module_t *module)
Ensures that module can be instantiated with this linker by defining all functions otherwise missing ...
WASM_API_EXTERN wasmtime_linker_t * wasmtime_linker_new(wasm_engine_t *engine)
Creates a new linker for the specified engine.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:153
Container for different kinds of extern items.
Definition: extern.h:145
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.