Wasmtime
component/component.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_COMPONENT_COMPONENT_HH
6#define WASMTIME_COMPONENT_COMPONENT_HH
7
8#include <wasmtime/conf.h>
9
10#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
11
12#include <memory>
13#include <optional>
14#include <string_view>
15#include <vector>
17#include <wasmtime/engine.hh>
18#include <wasmtime/error.hh>
19#include <wasmtime/span.hh>
20#include <wasmtime/wat.hh>
21
22namespace wasmtime {
23namespace component {
24
32 friend class Component;
33
34 struct deleter {
35 void operator()(wasmtime_component_export_index_t *p) const {
37 }
38 };
39
40 std::unique_ptr<wasmtime_component_export_index_t, deleter> ptr;
41
42public:
45
48 : ptr(wasmtime_component_export_index_clone(other.ptr.get())) {}
51 ptr.reset(wasmtime_component_export_index_clone(other.ptr.get()));
52 return *this;
53 }
54
55 ~ExportIndex() = default;
57 ExportIndex(ExportIndex &&other) = default;
59 ExportIndex &operator=(ExportIndex &&other) = default;
60
62 const wasmtime_component_export_index_t *capi() const { return ptr.get(); }
63
65 wasmtime_component_export_index_t *capi() { return ptr.get(); }
66};
67
71class Component {
72 struct deleter {
73 void operator()(wasmtime_component_t *p) const {
75 }
76 };
77
78 std::unique_ptr<wasmtime_component_t, deleter> ptr;
79
80 Component(wasmtime_component_t *raw) : ptr(raw) {}
81
82public:
84 Component(const Component &other)
85 : ptr(wasmtime_component_clone(other.ptr.get())) {}
87 Component &operator=(const Component &other) {
88 ptr.reset(wasmtime_component_clone(other.ptr.get()));
89 return *this;
90 }
91 ~Component() = default;
93 Component(Component &&other) = default;
95 Component &operator=(Component &&other) = default;
96
98 const wasmtime_component_t *capi() const { return ptr.get(); }
99
101 wasmtime_component_t *capi() { return ptr.get(); }
102
103#ifdef WASMTIME_FEATURE_COMPILER
110 static Result<Component> compile(Engine &engine, std::string_view wat) {
111 auto wasm = wat2wasm(wat);
112 if (!wasm) {
113 return wasm.err();
114 }
115 auto bytes = wasm.ok();
116 return compile(engine, bytes);
117 }
118
131 wasmtime_component_t *ret = nullptr;
132 auto *error =
133 wasmtime_component_new(engine.capi(), wasm.data(), wasm.size(), &ret);
134 if (error != nullptr) {
135 return Error(error);
136 }
137 return Component(ret);
138 }
139#endif // WASMTIME_FEATURE_COMPILER
140
154 wasmtime_component_t *ret = nullptr;
155 auto *error = wasmtime_component_deserialize(engine.capi(), wasm.data(),
156 wasm.size(), &ret);
157 if (error != nullptr) {
158 return Error(error);
159 }
160 return Component(ret);
161 }
162
176 const std::string &path) {
177 wasmtime_component_t *ret = nullptr;
178 auto *error =
179 wasmtime_component_deserialize_file(engine.capi(), path.c_str(), &ret);
180 if (error != nullptr) {
181 return Error(error);
182 }
183 return Component(ret);
184 }
185
186#ifdef WASMTIME_FEATURE_COMPILER
194 wasm_byte_vec_t bytes;
195 auto *error = wasmtime_component_serialize(ptr.get(), &bytes);
196 if (error != nullptr) {
197 return Error(error);
198 }
199 std::vector<uint8_t> ret;
200 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
201 ret.assign(raw.begin(), raw.end());
202 wasm_byte_vec_delete(&bytes);
203 return ret;
204 }
205#endif // WASMTIME_FEATURE_COMPILER
206
214 std::optional<ExportIndex> export_index(ExportIndex *instance,
215 std::string_view name) {
217 capi(), instance ? instance->capi() : nullptr, name.data(),
218 name.size());
219 if (ret) {
220 return ExportIndex(ret);
221 }
222 return std::nullopt;
223 };
224};
225
226} // namespace component
227} // namespace wasmtime
228
229#endif // WASMTIME_FEATURE_COMPONENT_MODEL
230
231#endif // WASMTIME_COMPONENT_COMPONENT_HH
Global compilation state in Wasmtime.
Definition: engine.hh:21
const wasm_engine_t * capi() const
Returns the underlying C API pointer.
Definition: engine.hh:61
Errors coming from Wasmtime.
Definition: error.hh:25
Fallible result type used for Wasmtime.
Definition: error.hh:82
Span class used when c++20 is not available.
Definition: span.hh:47
std::size_t size() const
Returns number of data that referred by Span class.
Definition: span.hh:81
iterator end() const
Returns end iterator.
Definition: span.hh:87
T * data() const
Returns pointer to data.
Definition: span.hh:78
iterator begin() const
Returns begin iterator.
Definition: span.hh:84
Representation of a compiled WebAssembly component.
Definition: component/component.hh:71
static Result< Component > deserialize_file(Engine &engine, const std::string &path)
Deserializes a component from an on-disk file.
Definition: component/component.hh:175
Component & operator=(Component &&other)=default
Moves resources from another component into this one.
const wasmtime_component_t * capi() const
Returns the underlying C API pointer.
Definition: component/component.hh:98
Result< std::vector< uint8_t > > serialize() const
Serializes this component to a list of bytes.
Definition: component/component.hh:193
Component & operator=(const Component &other)
Copies another component into this one.
Definition: component/component.hh:87
Component(Component &&other)=default
Moves resources from another component into this one.
static Result< Component > compile(Engine &engine, std::string_view wat)
Compiles a component from the WebAssembly text format.
Definition: component/component.hh:110
static Result< Component > deserialize(Engine &engine, Span< uint8_t > wasm)
Deserializes a previous list of bytes created with serialize.
Definition: component/component.hh:153
wasmtime_component_t * capi()
Returns the underlying C API pointer.
Definition: component/component.hh:101
static Result< Component > compile(Engine &engine, Span< uint8_t > wasm)
Compiles a component from the WebAssembly binary format.
Definition: component/component.hh:130
std::optional< ExportIndex > export_index(ExportIndex *instance, std::string_view name)
Returns the export index for the export named name in this component.
Definition: component/component.hh:214
Component(const Component &other)
Copies another component into this one.
Definition: component/component.hh:84
An index to an exported item within a particular component.
Definition: component/component.hh:31
ExportIndex(ExportIndex &&other)=default
Moves resources from another component into this one.
ExportIndex(wasmtime_component_export_index_t *raw)
Constructs an ExportIndex from the underlying C API struct.
Definition: component/component.hh:44
const wasmtime_component_export_index_t * capi() const
Returns the underlying C API pointer.
Definition: component/component.hh:62
ExportIndex & operator=(const ExportIndex &other)
Copies another index into this one.
Definition: component/component.hh:50
ExportIndex(const ExportIndex &other)
Copies another index into this one.
Definition: component/component.hh:47
ExportIndex & operator=(ExportIndex &&other)=default
Moves resources from another component into this one.
wasmtime_component_export_index_t * capi()
Returns the underlying C API pointer.
Definition: component/component.hh:65
wasmtime_error_t * wasmtime_component_deserialize(const wasm_engine_t *engine, const uint8_t *buf, size_t len, wasmtime_component_t **component_out)
Build a component from serialized data.
wasmtime_component_t * wasmtime_component_clone(const wasmtime_component_t *component)
Creates a shallow clone of the specified component, increasing the internal reference count.
struct wasmtime_component_export_index_t wasmtime_component_export_index_t
A value which represents a known export of a component.
Definition: component/component.h:113
wasmtime_component_export_index_t * wasmtime_component_export_index_clone(const wasmtime_component_export_index_t *index)
Creates a new separately-owned copy of the specified index.
struct wasmtime_component_t wasmtime_component_t
Representation of a component in the component model.
Definition: component/component.h:17
wasmtime_component_export_index_t * wasmtime_component_get_export_index(const wasmtime_component_t *component, const wasmtime_component_export_index_t *instance_export_index, const char *name, size_t name_len)
Looks up a specific export of this component by name optionally nested within the instance provided.
wasmtime_error_t * wasmtime_component_new(const wasm_engine_t *engine, const uint8_t *buf, size_t len, wasmtime_component_t **component_out)
Compiles a WebAssembly binary into a wasmtime_component_t.
wasmtime_error_t * wasmtime_component_serialize(const wasmtime_component_t *component, wasm_byte_vec_t *ret)
This function serializes compiled component artifacts as blob data.
void wasmtime_component_delete(wasmtime_component_t *component)
Deletes a wasmtime_component_t created by wasmtime_component_new.
void wasmtime_component_export_index_delete(wasmtime_component_export_index_t *export_index)
Deletes a wasmtime_component_export_index_t.
wasmtime_error_t * wasmtime_component_deserialize_file(const wasm_engine_t *engine, const char *path, wasmtime_component_t **component_out)
Deserialize a component from an on-disk file.
Build-time defines for how the C API was built.
A list of bytes.
Definition: wasm.h:102
size_t size
Length of this vector.
Definition: wasm.h:102
wasm_byte_t * data
Pointer to the base of this vector.
Definition: wasm.h:102
void wasm_byte_vec_delete(wasm_byte_vec_t *)
Deletes a byte vector.
Result< std::vector< uint8_t > > wat2wasm(std::string_view wat)
Converts the WebAssembly text format into the WebAssembly binary format.
Definition: wat.hh:30