Wasmtime
component/linker.hh
Go to the documentation of this file.
1
2
3#ifndef WASMTIME_COMPONENT_LINKER_HH
4#define WASMTIME_COMPONENT_LINKER_HH
5
6#include <wasmtime/conf.h>
7
8#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
9
10#include <memory>
11#include <string_view>
15#include <wasmtime/engine.hh>
16#include <wasmtime/module.hh>
17
18namespace wasmtime {
19namespace component {
20
29 WASMTIME_OWN_WRAPPER(LinkerInstance, wasmtime_component_linker_instance);
30
34 Result<std::monostate> add_module(std::string_view name,
35 const Module &module) {
37 ptr.get(), name.data(), name.size(), module.capi());
38 if (error != nullptr) {
39 return Error(error);
40 }
41 return std::monostate();
42 }
43
51 Result<LinkerInstance> add_instance(std::string_view name) {
54 ptr.get(), name.data(), name.size(), &ret);
55 if (error != nullptr) {
56 return Error(error);
57 }
58 return LinkerInstance(ret);
59 }
60
61private:
62 template <typename F>
63 static wasmtime_error_t *
64 raw_callback(void *env, wasmtime_context_t *store,
65 const wasmtime_component_func_type_t *ty_const,
66 wasmtime_component_val_t *args, size_t nargs,
67 wasmtime_component_val_t *results, size_t nresults) {
68 static_assert(alignof(Val) == alignof(wasmtime_component_val_t));
69 static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t));
71 const_cast<wasmtime_component_func_type_t *>(ty_const);
72 F *func = reinterpret_cast<F *>(env);
73 Span<Val> args_span(Val::from_capi(args), nargs);
74 Span<Val> results_span(Val::from_capi(results), nresults);
76 (*func)(Store::Context(store), *FuncType::from_capi(&ty), args_span,
77 results_span);
78
79 if (!result) {
80 return result.err().capi_release();
81 }
82 return nullptr;
83 }
84
85 template <typename F> static void raw_finalize(void *env) {
86 std::unique_ptr<F> ptr(reinterpret_cast<F *>(env));
87 }
88
89public:
91 template <typename F,
92 std::enable_if_t<
93 std::is_invocable_r_v<Result<std::monostate>, F, Store::Context,
94 const FuncType &, Span<Val>, Span<Val>>,
95 bool> = true>
96 Result<std::monostate> add_func(std::string_view name, F &&f) {
98 ptr.get(), name.data(), name.length(),
99 raw_callback<std::remove_reference_t<F>>,
100 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
101 .release(),
102 raw_finalize<std::remove_reference_t<F>>);
103
104 if (error != nullptr) {
105 return Error(error);
106 }
107
108 return std::monostate();
109 }
110
111private:
112 template <typename F>
113 static wasmtime_error_t *
114 raw_resource_destructor_callback(void *env, wasmtime_context_t *store,
115 uint32_t rep) {
116 F *func = reinterpret_cast<F *>(env);
117 Result<std::monostate> result = (*func)(Store::Context(store), rep);
118 if (!result) {
119 return result.err().capi_release();
120 }
121 return nullptr;
122 }
123
124public:
127 template <typename F,
128 std::enable_if_t<std::is_invocable_r_v<Result<std::monostate>, F,
129 Store::Context, uint32_t>,
130 bool> = true>
132 const ResourceType &ty, F &&f) {
134 ptr.get(), name.data(), name.length(), ty.capi(),
135 raw_resource_destructor_callback<std::remove_reference_t<F>>,
136 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
137 .release(),
138 raw_finalize<std::remove_reference_t<F>>);
139
140 if (error != nullptr) {
141 return Error(error);
142 }
143
144 return std::monostate();
145 }
146};
147
151class Linker {
152 WASMTIME_OWN_WRAPPER(Linker, wasmtime_component_linker);
153
155 explicit Linker(Engine &engine)
156 : ptr(wasmtime_component_linker_new(engine.capi())) {}
157
166 LinkerInstance root() {
169 return LinkerInstance(instance_capi);
170 }
171
176 define_unknown_imports_as_traps(const Component &component) {
178 ptr.get(), component.capi());
179 if (err)
180 return Error(err);
181 return std::monostate();
182 }
183
187 void allow_shadowing(bool allow) {
189 }
190
192 Result<Instance> instantiate(Store::Context cx, const Component &component) {
195 ptr.get(), cx.capi(), component.capi(), &ret);
196 if (error != nullptr) {
197 return Error(error);
198 }
199 return Instance(ret);
200 }
201
202#ifdef WASMTIME_FEATURE_WASI
211 Result<std::monostate> add_wasip2() {
213 if (error != nullptr) {
214 return Error(error);
215 }
216 return std::monostate();
217 }
218#endif // WASMTIME_FEATURE_WASI
219
220#ifdef WASMTIME_FEATURE_WASI_HTTP
227 Result<std::monostate> add_wasi_http() {
228 wasmtime_error_t *error =
230 if (error != nullptr) {
231 return Error(error);
232 }
233 return std::monostate();
234 }
235#endif // WASMTIME_FEATURE_WASI_HTTP
236
237#ifdef WASMTIME_FEATURE_COMPONENT_MODEL_ASYNC
238#ifdef WASMTIME_FEATURE_WASI
246 Result<std::monostate> add_wasip2_async() {
247 wasmtime_error_t *error =
249 if (error != nullptr) {
250 return Error(error);
251 }
252 return std::monostate();
253 }
254#endif // WASMTIME_FEATURE_WASI
255
256#ifdef WASMTIME_FEATURE_WASI_HTTP
264 Result<std::monostate> add_wasi_http_async() {
265 wasmtime_error_t *error =
267 if (error != nullptr) {
268 return Error(error);
269 }
270 return std::monostate();
271 }
272#endif // WASMTIME_FEATURE_WASI_HTTP
273#endif // WASMTIME_FEATURE_COMPONENT_MODEL_ASYNC
274};
275
276} // namespace component
277} // namespace wasmtime
278
279#endif // WASMTIME_FEATURE_COMPONENT_MODEL
280
281#endif // WASMTIME_COMPONENT_LINKER_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Errors coming from Wasmtime.
Definition: error.hh:26
Representation of a compiled WebAssembly module.
Definition: module.hh:28
Fallible result type used for Wasmtime.
Definition: error.hh:70
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:84
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: _store_class.hh:65
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: _store_class.hh:183
Representation of a compiled WebAssembly component.
Definition: component/component.hh:39
Type information about a component function.
Definition: component/types/func.hh:21
Class representing an instantiated WebAssembly component.
Definition: component/instance.hh:22
Helper class for linking modules together with name-based resolution.
Definition: component/linker.hh:28
Result< std::monostate > add_resource(std::string_view name, const ResourceType &ty, F &&f)
Defines a new resource in this linker with the provided destructor.
Definition: component/linker.hh:131
Result< std::monostate > add_func(std::string_view name, F &&f)
Defines a function within this linker instance.
Definition: component/linker.hh:96
Class used to instantiate a Component into an instance.
Definition: component/linker.hh:151
Definition: component/types/val.hh:151
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:528
static const Val * from_capi(const Raw *capi)
Definition: component/val.hh:533
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_instance_add_func(wasmtime_component_linker_instance_t *linker_instance, const char *name, size_t name_len, wasmtime_component_func_callback_t callback, void *data, void(*finalizer)(void *))
Define a function within this instance.
struct wasmtime_component_linker_instance_t wasmtime_component_linker_instance_t
Structure representing an "instance" being defined within a linker.
Definition: component/linker.h:26
WASM_API_EXTERN wasmtime_component_linker_instance_t * wasmtime_component_linker_root(wasmtime_component_linker_t *linker)
Returns the "root instance" of this linker, used to define names into the root namespace.
WASM_API_EXTERN void wasmtime_component_linker_allow_shadowing(wasmtime_component_linker_t *linker, bool allow)
Configures whether this linker allows later definitions to shadow previous definitions.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_add_wasip2_async(wasmtime_component_linker_t *linker)
Add all WASI interfaces into the linker provided using async bindings.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_add_wasi_http_async(wasmtime_component_linker_t *linker)
Add WASI HTTP interfaces into the linker provided using async bindings.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_add_wasip2(wasmtime_component_linker_t *linker)
Add all WASI interfaces into the linker provided.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_instance_add_resource(wasmtime_component_linker_instance_t *linker_instance, const char *name, size_t name_len, const wasmtime_component_resource_type_t *resource, wasmtime_component_resource_destructor_t destructor, void *data, void(*finalizer)(void *))
Defines a new resource type within this instance.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_instance_add_instance(wasmtime_component_linker_instance_t *linker_instance, const char *name, size_t name_len, wasmtime_component_linker_instance_t **linker_instance_out)
Defines a nested instance within this instance.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_add_wasi_http(wasmtime_component_linker_t *linker)
Add WASI HTTP interfaces into the linker provided.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_instance_add_module(wasmtime_component_linker_instance_t *linker_instance, const char *name, size_t name_len, const wasmtime_module_t *module)
Defines a wasmtime_module_t within this instance.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_instantiate(const wasmtime_component_linker_t *linker, wasmtime_context_t *context, const wasmtime_component_t *component, wasmtime_component_instance_t *instance_out)
Instantiates a component instance in a given wasmtime_context_t.
WASM_API_EXTERN wasmtime_component_linker_t * wasmtime_component_linker_new(const wasm_engine_t *engine)
Creates a new wasmtime_component_linker_t for the specified engine.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_linker_define_unknown_imports_as_traps(wasmtime_component_linker_t *linker, const wasmtime_component_t *component)
Defines all unknown imports of component as trapping functions.
struct wasmtime_component_func_type_t wasmtime_component_func_type_t
Opaque type representing a component function type.
Definition: component/types/func.h:18
Build-time defines for how the C API was built.
Representation of a instance in Wasmtime.
Definition: component/instance.h:24
Represents possible runtime values which a component function can either consume or produce.
Definition: component/val.h:383
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Errors generated by Wasmtime.