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 struct deleter {
30 void operator()(wasmtime_component_linker_instance_t *p) const {
32 }
33 };
34
35 std::unique_ptr<wasmtime_component_linker_instance_t, deleter> ptr;
36
37public:
40 : ptr(ptr) {}
41
43 const wasmtime_component_linker_instance_t *capi() const { return ptr.get(); }
44
47
51 Result<std::monostate> add_module(std::string_view name, Module &module) {
53 ptr.get(), name.data(), name.size(), module.capi());
54 if (error != nullptr) {
55 return Error(error);
56 }
57 return std::monostate();
58 }
59
67 Result<LinkerInstance> add_instance(std::string_view name) {
70 ptr.get(), name.data(), name.size(), &ret);
71 if (error != nullptr) {
72 return Error(error);
73 }
74 return LinkerInstance(ret);
75 }
76
77private:
78 template <typename F>
79 static wasmtime_error_t *
80 raw_callback(void *env, wasmtime_context_t *store,
81 const wasmtime_component_val_t *args, size_t nargs,
82 wasmtime_component_val_t *results, size_t nresults) {
83 static_assert(alignof(Val) == alignof(wasmtime_component_val_t));
84 static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t));
85 F *func = reinterpret_cast<F *>(env);
86 Span<const Val> args_span(Val::from_capi(args), nargs);
87 Span<Val> results_span(Val::from_capi(results), nresults);
89 (*func)(Store::Context(store), args_span, results_span);
90 if (!result) {
91 return result.err().release();
92 }
93 return nullptr;
94 }
95
96 template <typename F> static void raw_finalize(void *env) {
97 std::unique_ptr<F> ptr(reinterpret_cast<F *>(env));
98 }
99
100public:
102 template <typename F,
103 std::enable_if_t<
104 std::is_invocable_r_v<Result<std::monostate>, F, Store::Context,
105 Span<const Val>, Span<Val>>,
106 bool> = true>
107 Result<std::monostate> add_func(std::string_view name, F &&f) {
109 ptr.get(), name.data(), name.length(),
110 raw_callback<std::remove_reference_t<F>>,
111 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
112 .release(),
113 raw_finalize<std::remove_reference_t<F>>);
114
115 if (error != nullptr) {
116 return Error(error);
117 }
118
119 return std::monostate();
120 }
121
122private:
123 template <typename F>
124 static wasmtime_error_t *
125 raw_resource_destructor_callback(void *env, wasmtime_context_t *store,
126 uint32_t rep) {
127 F *func = reinterpret_cast<F *>(env);
128 Result<std::monostate> result = (*func)(Store::Context(store), rep);
129 if (!result) {
130 return result.err().release();
131 }
132 return nullptr;
133 }
134
135public:
138 template <typename F,
139 std::enable_if_t<std::is_invocable_r_v<Result<std::monostate>, F,
140 Store::Context, uint32_t>,
141 bool> = true>
143 const ResourceType &ty, F &&f) {
145 ptr.get(), name.data(), name.length(), ty.capi(),
146 raw_resource_destructor_callback<std::remove_reference_t<F>>,
147 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
148 .release(),
149 raw_finalize<std::remove_reference_t<F>>);
150
151 if (error != nullptr) {
152 return Error(error);
153 }
154
155 return std::monostate();
156 }
157};
158
162class Linker {
163 struct deleter {
164 void operator()(wasmtime_component_linker_t *p) const {
166 }
167 };
168
169 std::unique_ptr<wasmtime_component_linker_t, deleter> ptr;
170
171public:
173 explicit Linker(Engine &engine)
174 : ptr(wasmtime_component_linker_new(engine.capi())) {}
175
187 return LinkerInstance(instance_capi);
188 }
189
193 void allow_shadowing(bool allow) {
195 }
196
201 ptr.get(), cx.capi(), component.capi(), &ret);
202 if (error != nullptr) {
203 return Error(error);
204 }
205 return Instance(ret);
206 }
207
208#ifdef WASMTIME_FEATURE_WASI
219 if (error != nullptr) {
220 return Error(error);
221 }
222 return std::monostate();
223 }
224#endif // WASMTIME_FEATURE_WASI
225
227 const wasmtime_component_linker_t *capi() const { return ptr.get(); }
228
230 wasmtime_component_linker_t *capi() { return ptr.get(); }
231};
232
233} // namespace component
234} // namespace wasmtime
235
236#endif // WASMTIME_FEATURE_COMPONENT_MODEL
237
238#endif // WASMTIME_COMPONENT_LINKER_H
Global compilation state in Wasmtime.
Definition: engine.hh:21
Errors coming from Wasmtime.
Definition: error.hh:25
Representation of a compiled WebAssembly module.
Definition: module.hh:27
const wasmtime_module_t * capi() const
Returns the underlying C API pointer.
Definition: module.hh:188
Fallible result type used for Wasmtime.
Definition: error.hh:82
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:96
Span class used when c++20 is not available.
Definition: span.hh:47
An interior pointer into a Store.
Definition: store.hh:60
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:154
Representation of a compiled WebAssembly component.
Definition: component/component.hh:71
const wasmtime_component_t * capi() const
Returns the underlying C API pointer.
Definition: component/component.hh:98
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
wasmtime_component_linker_instance_t * capi()
Returns the underlying C API pointer.
Definition: component/linker.hh:46
Result< LinkerInstance > add_instance(std::string_view name)
Adds an new instance to this linker instance under the specified name.
Definition: component/linker.hh:67
Result< std::monostate > add_module(std::string_view name, Module &module)
Adds a module to this linker instance under the specified name.
Definition: component/linker.hh:51
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:142
const wasmtime_component_linker_instance_t * capi() const
Returns the underlying C API pointer.
Definition: component/linker.hh:43
Result< std::monostate > add_func(std::string_view name, F &&f)
Defines a function within this linker instance.
Definition: component/linker.hh:107
LinkerInstance(wasmtime_component_linker_instance_t *ptr)
Creates a new linker instance from the given C API pointer.
Definition: component/linker.hh:39
Class used to instantiate a Component into an instance.
Definition: component/linker.hh:162
Linker(Engine &engine)
Creates a new linker which will instantiate in the given engine.
Definition: component/linker.hh:173
void allow_shadowing(bool allow)
Definition: component/linker.hh:193
Result< std::monostate > add_wasip2()
Adds WASIp2 API definitions to this linker.
Definition: component/linker.hh:217
wasmtime_component_linker_t * capi()
Returns the underlying C API pointer.
Definition: component/linker.hh:230
Result< Instance > instantiate(Store::Context cx, Component &component)
Instantiates the given component within this linker.
Definition: component/linker.hh:198
LinkerInstance root()
Gets the "root" instance of this linker which can be used to define items into the linker under the t...
Definition: component/linker.hh:184
const wasmtime_component_linker_t * capi() const
Returns the underlying C API pointer.
Definition: component/linker.hh:227
Definition: component/val.hh:397
const wasmtime_component_resource_type_t * capi() const
Returns the underlying C API pointer.
Definition: component/val.hh:440
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:593
static const Val * from_capi(const Raw *capi)
Definition: component/val.hh:598
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.
wasmtime_component_linker_t * wasmtime_component_linker_new(const wasm_engine_t *engine)
Creates a new wasmtime_component_linker_t for the specified engine.
struct wasmtime_component_linker_instance_t wasmtime_component_linker_instance_t
Structure representing an "instance" being defined within a linker.
Definition: component/linker.h:24
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.
void wasmtime_component_linker_allow_shadowing(wasmtime_component_linker_t *linker, bool allow)
Configures whether this linker allows later definitions to shadow previous definitions.
void wasmtime_component_linker_instance_delete(wasmtime_component_linker_instance_t *linker_instance)
Deletes a wasmtime_component_linker_instance_t.
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.
struct wasmtime_component_linker_t wasmtime_component_linker_t
A type used to instantiate a wasmtime_component_t.
Definition: component/linker.h:21
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.
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.
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.
wasmtime_error_t * wasmtime_component_linker_add_wasip2(wasmtime_component_linker_t *linker)
Add all WASI interfaces into the linker provided.
void wasmtime_component_linker_delete(wasmtime_component_linker_t *linker)
Deletes a wasmtime_component_linker_t created by wasmtime_component_linker_new.
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:425
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Errors generated by Wasmtime.