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
49 static Result<Component> compile(Engine &engine, std::string_view wat) {
50 auto wasm = wat2wasm(wat);
51 if (!wasm) {
52 return wasm.err();
53 }
54 auto bytes = wasm.ok();
55 return compile(engine, bytes);
56 }
57
69 static Result<Component> compile(Engine &engine, Span<uint8_t> wasm) {
70 wasmtime_component_t *ret = nullptr;
71 auto *error =
72 wasmtime_component_new(engine.capi(), wasm.data(), wasm.size(), &ret);
73 if (error != nullptr) {
74 return Error(error);
75 }
76 return Component(ret);
77 }
78#endif // WASMTIME_FEATURE_COMPILER
79
92 static Result<Component> deserialize(Engine &engine, Span<uint8_t> wasm) {
93 wasmtime_component_t *ret = nullptr;
94 auto *error = wasmtime_component_deserialize(engine.capi(), wasm.data(),
95 wasm.size(), &ret);
96 if (error != nullptr) {
97 return Error(error);
98 }
99 return Component(ret);
100 }
101
114 static Result<Component> deserialize_file(Engine &engine,
115 const std::string &path) {
116 wasmtime_component_t *ret = nullptr;
117 auto *error =
118 wasmtime_component_deserialize_file(engine.capi(), path.c_str(), &ret);
119 if (error != nullptr) {
120 return Error(error);
121 }
122 return Component(ret);
123 }
124
125#ifdef WASMTIME_FEATURE_COMPILER
132 Result<std::vector<uint8_t>> serialize() const {
133 wasm_byte_vec_t bytes;
134 auto *error = wasmtime_component_serialize(ptr.get(), &bytes);
135 if (error != nullptr) {
136 return Error(error);
137 }
138 std::vector<uint8_t> ret;
139 Span<uint8_t> raw(reinterpret_cast<uint8_t *>(bytes.data), bytes.size);
140 ret.assign(raw.begin(), raw.end());
141 wasm_byte_vec_delete(&bytes);
142 return ret;
143 }
144#endif // WASMTIME_FEATURE_COMPILER
145
153 std::optional<ExportIndex> export_index(ExportIndex *instance,
154 std::string_view name) {
156 capi(), instance ? instance->capi() : nullptr, name.data(),
157 name.size());
158 if (ret) {
159 return ExportIndex(ret);
160 }
161 return std::nullopt;
162 };
163
165 ComponentType type() const {
166 return ComponentType(wasmtime_component_type(ptr.get()));
167 }
168};
169
170} // namespace component
171} // namespace wasmtime
172
173#endif // WASMTIME_FEATURE_COMPONENT_MODEL
174
175#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