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>
18#include <wasmtime/engine.hh>
19#include <wasmtime/error.hh>
20#include <wasmtime/span.hh>
21#include <wasmtime/wat.hh>
22
23namespace wasmtime {
24namespace component {
25
33 WASMTIME_CLONE_WRAPPER(ExportIndex, wasmtime_component_export_index);
34};
35
39class Component {
40 WASMTIME_CLONE_WRAPPER(Component, wasmtime_component);
41
42#ifdef WASMTIME_FEATURE_COMPILER
43
44#ifdef WASMTIME_FEATURE_WAT
51 static Result<Component> compile(Engine &engine, std::string_view wat) {
52 auto wasm = wat2wasm(wat);
53 if (!wasm) {
54 return wasm.err();
55 }
56 auto bytes = wasm.ok();
57 return compile(engine, bytes);
58 }
59#endif // WASMTIME_FEATURE_WAT
60
72 static Result<Component> compile(Engine &engine, Span<uint8_t> wasm) {
73 wasmtime_component_t *ret = nullptr;
74 auto *error =
75 wasmtime_component_new(engine.capi(), wasm.data(), wasm.size(), &ret);
76 if (error != nullptr) {
77 return Error(error);
78 }
79 return Component(ret);
80 }
81#endif // WASMTIME_FEATURE_COMPILER
82
95 static Result<Component> deserialize(Engine &engine, Span<uint8_t> wasm) {
96 wasmtime_component_t *ret = nullptr;
97 auto *error = wasmtime_component_deserialize(engine.capi(), wasm.data(),
98 wasm.size(), &ret);
99 if (error != nullptr) {
100 return Error(error);
101 }
102 return Component(ret);
103 }
104
117 static Result<Component> deserialize_file(Engine &engine,
118 const std::string &path) {
119 wasmtime_component_t *ret = nullptr;
120 auto *error =
121 wasmtime_component_deserialize_file(engine.capi(), path.c_str(), &ret);
122 if (error != nullptr) {
123 return Error(error);
124 }
125 return Component(ret);
126 }
127
128#ifdef WASMTIME_FEATURE_COMPILER
135 Result<std::vector<uint8_t>> serialize() const {
136 wasm_byte_vec_t bytes;
137 auto *error = wasmtime_component_serialize(ptr.get(), &bytes);
138 if (error != nullptr) {
139 return Error(error);
140 }
141 std::vector<uint8_t> ret;
142 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
143 ret.assign(raw.begin(), raw.end());
144 wasm_byte_vec_delete(&bytes);
145 return ret;
146 }
147#endif // WASMTIME_FEATURE_COMPILER
148
156 std::optional<ExportIndex> export_index(ExportIndex *instance,
157 std::string_view name) {
159 capi(), instance ? instance->capi() : nullptr, name.data(),
160 name.size());
161 if (ret) {
162 return ExportIndex(ret);
163 }
164 return std::nullopt;
165 };
166
168 ComponentType type() const {
169 return ComponentType(wasmtime_component_type(ptr.get()));
170 }
171};
172
173} // namespace component
174} // namespace wasmtime
175
176#endif // WASMTIME_FEATURE_COMPONENT_MODEL
177
178#endif // WASMTIME_COMPONENT_COMPONENT_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Errors coming from Wasmtime.
Definition: error.hh:26
Fallible result type used for Wasmtime.
Definition: error.hh:70
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
Represents the type of a WebAssembly component.
Definition: component/types/component.hh:29
Representation of a compiled WebAssembly component.
Definition: component/component.hh:39
An index to an exported item within a particular component.
Definition: component/component.hh:32
WASM_API_EXTERN 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.
WASM_API_EXTERN wasmtime_component_type_t * wasmtime_component_type(const wasmtime_component_t *component)
Returns the type of this component.
struct wasmtime_component_t wasmtime_component_t
Representation of a component in the component model.
Definition: component/component.h:18
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
WASM_API_EXTERN 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.
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