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/instance.hh>
12#include <wasmtime/linker.h>
13#include <wasmtime/store.hh>
14#include <wasmtime/trap.hh>
15
16namespace wasmtime {
17
25class Linker {
26 struct deleter {
27 void operator()(wasmtime_linker_t *p) const { wasmtime_linker_delete(p); }
28 };
29
30 std::unique_ptr<wasmtime_linker_t, deleter> ptr;
31
32public:
34 explicit Linker(Engine &engine)
35 : ptr(wasmtime_linker_new(engine.ptr.get())) {}
36
40 void allow_shadowing(bool allow) {
41 wasmtime_linker_allow_shadowing(ptr.get(), allow);
42 }
43
46 std::string_view name, const Extern &item) {
48 detail::cvt_extern(item, raw);
49 auto *error =
50 wasmtime_linker_define(ptr.get(), cx.ptr, module.data(), module.size(),
51 name.data(), name.size(), &raw);
52 if (error != nullptr) {
53 return Error(error);
54 }
55 return std::monostate();
56 }
57
63 auto *error = wasmtime_linker_define_wasi(ptr.get());
64 if (error != nullptr) {
65 return Error(error);
66 }
67 return std::monostate();
68 }
69
73 define_instance(Store::Context cx, std::string_view name, Instance instance) {
75 ptr.get(), cx.ptr, name.data(), name.size(), &instance.instance);
76 if (error != nullptr) {
77 return Error(error);
78 }
79 return std::monostate();
80 }
81
85 wasmtime_instance_t instance;
86 wasm_trap_t *trap = nullptr;
87 auto *error = wasmtime_linker_instantiate(ptr.get(), cx.ptr, m.ptr.get(),
88 &instance, &trap);
89 if (error != nullptr) {
90 return TrapError(Error(error));
91 }
92 if (trap != nullptr) {
93 return TrapError(Trap(trap));
94 }
95 return Instance(instance);
96 }
97
101 const Module &m) {
102 auto *error = wasmtime_linker_module(ptr.get(), cx.ptr, name.data(),
103 name.size(), m.ptr.get());
104 if (error != nullptr) {
105 return Error(error);
106 }
107 return std::monostate();
108 }
109
112 [[nodiscard]] std::optional<Extern>
113 get(Store::Context cx, std::string_view module, std::string_view name) {
115 if (wasmtime_linker_get(ptr.get(), cx.ptr, module.data(), module.size(),
116 name.data(), name.size(), &item)) {
117 return detail::cvt_extern(item);
118 }
119 return std::nullopt;
120 }
121
124 template <typename F,
125 std::enable_if_t<
126 std::is_invocable_r_v<Result<std::monostate, Trap>, F, Caller,
128 bool> = true>
130 std::string_view name, const FuncType &ty,
131 F &&f) {
132
133 auto *error = wasmtime_linker_define_func(
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))
137 .release(),
138 Func::raw_finalize<std::remove_reference_t<F>>);
139
140 if (error != nullptr) {
141 return Error(error);
142 }
143
144 return std::monostate();
145 }
146
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();
157 auto ty = FuncType::from_iters(params, results);
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))
162 .release(),
163 Func::raw_finalize<std::remove_reference_t<F>>);
164
165 if (error != nullptr) {
166 return Error(error);
167 }
168
169 return std::monostate();
170 }
171
174 Result<Func> get_default(Store::Context cx, std::string_view name) {
175 wasmtime_func_t item;
176 auto *error = wasmtime_linker_get_default(ptr.get(), cx.ptr, name.data(),
177 name.size(), &item);
178 if (error != nullptr) {
179 return Error(error);
180 }
181 return Func(item);
182 }
183};
184
185} // namespace wasmtime
186
187#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: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.