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, Module &module) {
36 ptr.get(), name.data(), name.size(), module.capi());
37 if (error != nullptr) {
38 return Error(error);
39 }
40 return std::monostate();
41 }
42
50 Result<LinkerInstance> add_instance(std::string_view name) {
53 ptr.get(), name.data(), name.size(), &ret);
54 if (error != nullptr) {
55 return Error(error);
56 }
57 return LinkerInstance(ret);
58 }
59
60private:
61 template <typename F>
62 static wasmtime_error_t *
63 raw_callback(void *env, wasmtime_context_t *store,
64 const wasmtime_component_func_type_t *ty_const,
65 wasmtime_component_val_t *args, size_t nargs,
66 wasmtime_component_val_t *results, size_t nresults) {
67 static_assert(alignof(Val) == alignof(wasmtime_component_val_t));
68 static_assert(sizeof(Val) == sizeof(wasmtime_component_val_t));
70 const_cast<wasmtime_component_func_type_t *>(ty_const);
71 F *func = reinterpret_cast<F *>(env);
72 Span<Val> args_span(Val::from_capi(args), nargs);
73 Span<Val> results_span(Val::from_capi(results), nresults);
75 (*func)(Store::Context(store), *FuncType::from_capi(&ty), args_span,
76 results_span);
77
78 if (!result) {
79 return result.err().capi_release();
80 }
81 return nullptr;
82 }
83
84 template <typename F> static void raw_finalize(void *env) {
85 std::unique_ptr<F> ptr(reinterpret_cast<F *>(env));
86 }
87
88public:
90 template <typename F,
91 std::enable_if_t<
92 std::is_invocable_r_v<Result<std::monostate>, F, Store::Context,
93 const FuncType &, Span<Val>, Span<Val>>,
94 bool> = true>
95 Result<std::monostate> add_func(std::string_view name, F &&f) {
97 ptr.get(), name.data(), name.length(),
98 raw_callback<std::remove_reference_t<F>>,
99 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
100 .release(),
101 raw_finalize<std::remove_reference_t<F>>);
102
103 if (error != nullptr) {
104 return Error(error);
105 }
106
107 return std::monostate();
108 }
109
110private:
111 template <typename F>
112 static wasmtime_error_t *
113 raw_resource_destructor_callback(void *env, wasmtime_context_t *store,
114 uint32_t rep) {
115 F *func = reinterpret_cast<F *>(env);
116 Result<std::monostate> result = (*func)(Store::Context(store), rep);
117 if (!result) {
118 return result.err().capi_release();
119 }
120 return nullptr;
121 }
122
123public:
126 template <typename F,
127 std::enable_if_t<std::is_invocable_r_v<Result<std::monostate>, F,
128 Store::Context, uint32_t>,
129 bool> = true>
131 const ResourceType &ty, F &&f) {
133 ptr.get(), name.data(), name.length(), ty.capi(),
134 raw_resource_destructor_callback<std::remove_reference_t<F>>,
135 std::make_unique<std::remove_reference_t<F>>(std::forward<F>(f))
136 .release(),
137 raw_finalize<std::remove_reference_t<F>>);
138
139 if (error != nullptr) {
140 return Error(error);
141 }
142
143 return std::monostate();
144 }
145};
146
150class Linker {
151 WASMTIME_OWN_WRAPPER(Linker, wasmtime_component_linker);
152
154 explicit Linker(Engine &engine)
155 : ptr(wasmtime_component_linker_new(engine.capi())) {}
156
165 LinkerInstance root() {
168 return LinkerInstance(instance_capi);
169 }
170
175 define_unknown_imports_as_traps(const Component &component) {
177 ptr.get(), component.capi());
178 if (err)
179 return Error(err);
180 return std::monostate();
181 }
182
186 void allow_shadowing(bool allow) {
188 }
189
191 Result<Instance> instantiate(Store::Context cx, Component &component) {
194 ptr.get(), cx.capi(), component.capi(), &ret);
195 if (error != nullptr) {
196 return Error(error);
197 }
198 return Instance(ret);
199 }
200
201#ifdef WASMTIME_FEATURE_WASI
210 Result<std::monostate> add_wasip2() {
212 if (error != nullptr) {
213 return Error(error);
214 }
215 return std::monostate();
216 }
217#endif // WASMTIME_FEATURE_WASI
218};
219
220} // namespace component
221} // namespace wasmtime
222
223#endif // WASMTIME_FEATURE_COMPONENT_MODEL
224
225#endif // WASMTIME_COMPONENT_LINKER_H
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.hh:66
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:160
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:130
Result< std::monostate > add_func(std::string_view name, F &&f)
Defines a function within this linker instance.
Definition: component/linker.hh:95
Class used to instantiate a Component into an instance.
Definition: component/linker.hh:150
Definition: component/types/val.hh:151
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:471
static const Val * from_capi(const Raw *capi)
Definition: component/val.hh:476
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:25
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(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_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:376
An interior pointer into a wasmtime_store_t which is used as "context" for many functions.
Errors generated by Wasmtime.