Wasmtime
linker.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_LINKER_HH
6#define WASMTIME_LINKER_HH
7
8#include <wasmtime/engine.hh>
9#include <wasmtime/error.hh>
10#include <wasmtime/extern.hh>
11#include <wasmtime/helpers.hh>
12#include <wasmtime/instance.hh>
13#include <wasmtime/linker.h>
14#include <wasmtime/store.hh>
15#include <wasmtime/trap.hh>
16
17namespace wasmtime {
18
26class Linker {
27 WASMTIME_OWN_WRAPPER(Linker, wasmtime_linker);
28
30 explicit Linker(Engine &engine) : ptr(wasmtime_linker_new(engine.capi())) {}
31
35 void allow_shadowing(bool allow) {
36 wasmtime_linker_allow_shadowing(ptr.get(), allow);
37 }
38
40 Result<std::monostate> define(Store::Context cx, std::string_view module,
41 std::string_view name, const Extern &item) {
43 detail::cvt_extern(item, raw);
44 auto *error =
45 wasmtime_linker_define(ptr.get(), cx.ptr, module.data(), module.size(),
46 name.data(), name.size(), &raw);
47 if (error != nullptr) {
48 return Error(error);
49 }
50 return std::monostate();
51 }
52
53#ifdef WASMTIME_FEATURE_WASI
58 Result<std::monostate> define_wasi() {
59 auto *error = wasmtime_linker_define_wasi(ptr.get());
60 if (error != nullptr) {
61 return Error(error);
62 }
63 return std::monostate();
64 }
65#endif // WASMTIME_FEATURE_WASI
66
70 define_instance(Store::Context cx, std::string_view name, Instance instance) {
72 ptr.get(), cx.ptr, name.data(), name.size(), &instance.instance);
73 if (error != nullptr) {
74 return Error(error);
75 }
76 return std::monostate();
77 }
78
81 TrapResult<Instance> instantiate(Store::Context cx, const Module &m) {
82 wasmtime_instance_t instance;
83 wasm_trap_t *trap = nullptr;
84 auto *error = wasmtime_linker_instantiate(ptr.get(), cx.ptr, m.capi(),
85 &instance, &trap);
86 if (error != nullptr) {
87 return TrapError(Error(error));
88 }
89 if (trap != nullptr) {
90 return TrapError(Trap(trap));
91 }
92 return Instance(instance);
93 }
94
97 Result<std::monostate> module(Store::Context cx, std::string_view name,
98 const Module &m) {
99 auto *error = wasmtime_linker_module(ptr.get(), cx.ptr, name.data(),
100 name.size(), m.capi());
101 if (error != nullptr) {
102 return Error(error);
103 }
104 return std::monostate();
105 }
106
109 [[nodiscard]] std::optional<Extern>
110 get(Store::Context cx, std::string_view module, std::string_view name) {
112 if (wasmtime_linker_get(ptr.get(), cx.ptr, module.data(), module.size(),
113 name.data(), name.size(), &item)) {
114 return detail::cvt_extern(item);
115 }
116 return std::nullopt;
117 }
118
121 template <typename F,
122 std::enable_if_t<
123 std::is_invocable_r_v<Result<std::monostate, Trap>, F, Caller,
125 bool> = true>
126 Result<std::monostate> func_new(std::string_view module,
127 std::string_view name, const FuncType &ty,
128 F &&f) {
129
130 auto *error = wasmtime_linker_define_func(
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))
134 .release(),
135 Func::raw_finalize<std::remove_reference_t<F>>);
136
137 if (error != nullptr) {
138 return Error(error);
139 }
140
141 return std::monostate();
142 }
143
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>
149 Result<std::monostate> func_wrap(std::string_view module,
150 std::string_view name, F &&f) {
151 using HostFunc = WasmHostFunc<F>;
152 auto params = HostFunc::Params::types();
153 auto results = HostFunc::Results::types();
154 auto ty = FuncType::from_iters(params, results);
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))
159 .release(),
160 Func::raw_finalize<std::remove_reference_t<F>>);
161
162 if (error != nullptr) {
163 return Error(error);
164 }
165
166 return std::monostate();
167 }
168
171 Result<Func> get_default(Store::Context cx, std::string_view name) {
172 wasmtime_func_t item;
173 auto *error = wasmtime_linker_get_default(ptr.get(), cx.ptr, name.data(),
174 name.size(), &item);
175 if (error != nullptr) {
176 return Error(error);
177 }
178 return Func(item);
179 }
180
183 Result<std::monostate> define_unknown_imports_as_traps(Module &module) {
185 ptr.get(), module.capi());
186 if (error != nullptr) {
187 return Error(error);
188 }
189 return std::monostate();
190 }
191
196 define_unknown_imports_as_default_values(Store::Context cx, Module &module) {
198 ptr.get(), cx.ptr, module.capi());
199 if (error != nullptr) {
200 return Error(error);
201 }
202 return std::monostate();
203 }
204};
205
206} // namespace wasmtime
207
208#endif // WASMTIME_LINKER_HH
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.