Wasmtime
component/types/component.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_COMPONENT_TYPES_COMPONENT_HH
6#define WASMTIME_COMPONENT_TYPES_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>
16#include <wasmtime/engine.hh>
17#include <wasmtime/helpers.hh>
19
20namespace wasmtime {
21
22namespace component {
23
24class ComponentItem;
25
30 WASMTIME_CLONE_WRAPPER(ComponentType, wasmtime_component_type);
31
33 size_t import_count(const Engine &engine) const {
34 return wasmtime_component_type_import_count(capi(), engine.capi());
35 }
36
38 std::optional<ComponentItem> import_get(const Engine &engine,
39 std::string_view name) const;
40
42 std::optional<std::pair<std::string_view, ComponentItem>>
43 import_nth(const Engine &engine, size_t nth) const;
44
46 size_t export_count(const Engine &engine) const {
47 return wasmtime_component_type_export_count(capi(), engine.capi());
48 }
49
51 std::optional<ComponentItem> export_get(const Engine &engine,
52 std::string_view name) const;
53
55 std::optional<std::pair<std::string_view, ComponentItem>>
56 export_nth(const Engine &engine, size_t nth) const;
57};
58
60class ModuleType;
61class FuncType;
62class ResourceType;
63class ValType;
64
70
71public:
73 explicit ComponentItem(wasmtime_component_item_t &&item) : item(item) {
76 }
77
80 wasmtime_component_item_clone(&other.item, &item);
81 }
82
86 wasmtime_component_item_clone(&other.item, &item);
87 return *this;
88 }
89
91 ComponentItem(ComponentItem &&other) : item(other.item) {
93 other.item.of.type.kind = WASMTIME_COMPONENT_VALTYPE_BOOL;
94 }
95
99 item = other.item;
101 other.item.of.type.kind = WASMTIME_COMPONENT_VALTYPE_BOOL;
102 return *this;
103 }
104
106
108 bool is_component() const {
110 }
111
115 }
116
118 bool is_module() const { return item.kind == WASMTIME_COMPONENT_ITEM_MODULE; }
119
121 bool is_component_func() const {
123 }
124
126 bool is_resource() const {
128 }
129
131 bool is_core_func() const {
133 }
134
136 bool is_type() const { return item.kind == WASMTIME_COMPONENT_ITEM_TYPE; }
137
140 const ComponentType &component() const {
141 assert(is_component());
142 return *ComponentType::from_capi(&item.of.component);
143 }
144
148
151 const ModuleType &module() const;
152
155 const FuncType &component_func() const;
156
159 const ResourceType &resource() const;
160
164 assert(is_core_func());
165 return item.of.core_func;
166 }
167
170 const ValType &type() const;
171};
172
173} // namespace component
174} // namespace wasmtime
175
180
181inline std::optional<wasmtime::component::ComponentItem>
182wasmtime::component::ComponentType::import_get(const wasmtime::Engine &engine,
183 std::string_view name) const {
186 capi(), engine.capi(), name.data(), name.size(), &item);
187 if (!found) {
188 return std::nullopt;
189 }
190 return wasmtime::component::ComponentItem(std::move(item));
191}
192
193inline std::optional<
194 std::pair<std::string_view, wasmtime::component::ComponentItem>>
195wasmtime::component::ComponentType::import_nth(const wasmtime::Engine &engine,
196 size_t nth) const {
198 const char *name_data;
199 size_t name_size;
201 capi(), engine.capi(), nth, &name_data, &name_size, &item);
202 if (!found) {
203 return std::nullopt;
204 }
205 return std::make_pair(std::string_view(name_data, name_size),
206 wasmtime::component::ComponentItem(std::move(item)));
207}
208
209inline std::optional<wasmtime::component::ComponentItem>
210wasmtime::component::ComponentType::export_get(const wasmtime::Engine &engine,
211 std::string_view name) const {
214 capi(), engine.capi(), name.data(), name.size(), &item);
215 if (!found) {
216 return std::nullopt;
217 }
218 return wasmtime::component::ComponentItem(std::move(item));
219}
220
221inline std::optional<
222 std::pair<std::string_view, wasmtime::component::ComponentItem>>
223wasmtime::component::ComponentType::export_nth(const wasmtime::Engine &engine,
224 size_t nth) const {
226 const char *name_data;
227 size_t name_size;
229 capi(), engine.capi(), nth, &name_data, &name_size, &item);
230 if (!found) {
231 return std::nullopt;
232 }
233 return std::make_pair(std::string_view(name_data, name_size),
234 wasmtime::component::ComponentItem(std::move(item)));
235}
236
239 assert(is_component_instance());
240 return *ComponentInstanceType::from_capi(&item.of.component_instance);
241}
242
245 assert(is_module());
246 return *ModuleType::from_capi(&item.of.module);
247}
248
251 assert(is_component_func());
252 return *FuncType::from_capi(&item.of.component_func);
253}
254
257 assert(is_resource());
258 return *ResourceType::from_capi(&item.of.resource);
259}
260
261inline const wasmtime::component::ValType &
263 assert(is_type());
264 return *ValType::from_capi(&item.of.type);
265}
266
267#endif // WASMTIME_FEATURE_COMPONENT_MODEL
268
269#endif // WASMTIME_COMPONENT_TYPES_COMPONENT_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Definition: types/func.hh:28
Represents the type of a component instance.
Definition: component/types/instance.hh:27
Represents a single item in a component's import or export list.
Definition: component/types/component.hh:68
ComponentItem(ComponentItem &&other)
Moves another item into this one.
Definition: component/types/component.hh:91
ComponentItem & operator=(ComponentItem &&other)
Moves another item into this one.
Definition: component/types/component.hh:97
bool is_core_func() const
Returns true if this is a core function.
Definition: component/types/component.hh:131
const ResourceType & resource() const
Definition: component/types/component.hh:256
const FuncType & component_func() const
Definition: component/types/component.hh:250
bool is_resource() const
Returns true if this is a resource.
Definition: component/types/component.hh:126
ComponentItem(wasmtime_component_item_t &&item)
Creates a component item from the raw C API representation.
Definition: component/types/component.hh:73
const ComponentType & component() const
Definition: component/types/component.hh:140
const ModuleType & module() const
Definition: component/types/component.hh:244
bool is_type() const
Returns true if this is a type.
Definition: component/types/component.hh:136
const ComponentInstanceType & component_instance() const
Definition: component/types/component.hh:238
ComponentItem(const ComponentItem &other)
Copies another item into this one.
Definition: component/types/component.hh:79
bool is_component_instance() const
Returns true if this is a component instance.
Definition: component/types/component.hh:113
ComponentItem & operator=(const ComponentItem &other)
Copies another item into this one.
Definition: component/types/component.hh:84
bool is_module() const
Returns true if this is a module.
Definition: component/types/component.hh:118
bool is_component() const
Returns true if this is a component.
Definition: component/types/component.hh:108
const ValType & type() const
Definition: component/types/component.hh:262
bool is_component_func() const
Returns true if this is a component function.
Definition: component/types/component.hh:121
wasmtime::FuncType::Ref core_func() const
Definition: component/types/component.hh:163
Represents the type of a WebAssembly component.
Definition: component/types/component.hh:29
Type information about a component function.
Definition: component/types/func.hh:21
Represents the type of a module.
Definition: component/types/module.hh:29
Definition: component/types/val.hh:151
Represents a component value type.
Definition: component/types/val.hh:184
static const ValType * from_capi(const wasmtime_component_valtype_t *capi)
Definition: component/types/val.hh:231
WASM_API_EXTERN wasmtime_component_type_t * wasmtime_component_type(const wasmtime_component_t *component)
Returns the type of this component.
#define WASMTIME_COMPONENT_ITEM_COMPONENT
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a component.
Definition: component/types/component.h:88
WASM_API_EXTERN bool wasmtime_component_type_export_get(const wasmtime_component_type_t *ty, const wasm_engine_t *engine, const char *name, size_t name_len, struct wasmtime_component_item_t *ret)
Retrieves the export with the specified name.
#define WASMTIME_COMPONENT_ITEM_COMPONENT_FUNC
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a component functio...
Definition: component/types/component.h:97
#define WASMTIME_COMPONENT_ITEM_RESOURCE
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a resource.
Definition: component/types/component.h:100
WASM_API_EXTERN bool wasmtime_component_type_import_get(const wasmtime_component_type_t *ty, const wasm_engine_t *engine, const char *name, size_t name_len, struct wasmtime_component_item_t *ret)
Retrieves the import with the specified name.
#define WASMTIME_COMPONENT_ITEM_COMPONENT_INSTANCE
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a component instanc...
Definition: component/types/component.h:91
WASM_API_EXTERN void wasmtime_component_item_delete(wasmtime_component_item_t *ptr)
Deallocates a component item.
#define WASMTIME_COMPONENT_ITEM_CORE_FUNC
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a core function.
Definition: component/types/component.h:103
WASM_API_EXTERN bool wasmtime_component_type_import_nth(const wasmtime_component_type_t *ty, const wasm_engine_t *engine, size_t nth, const char **name_ret, size_t *name_len_ret, struct wasmtime_component_item_t *type_ret)
Retrieves the nth import.
WASM_API_EXTERN bool wasmtime_component_type_export_nth(const wasmtime_component_type_t *ty, const wasm_engine_t *engine, size_t nth, const char **name_ret, size_t *name_len_ret, struct wasmtime_component_item_t *type_ret)
Retrieves the nth export.
#define WASMTIME_COMPONENT_ITEM_MODULE
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a module.
Definition: component/types/component.h:94
WASM_API_EXTERN size_t wasmtime_component_type_import_count(const wasmtime_component_type_t *ty, const wasm_engine_t *engine)
Returns the number of imports of a component type.
WASM_API_EXTERN size_t wasmtime_component_type_export_count(const wasmtime_component_type_t *ty, const wasm_engine_t *engine)
Returns the number of exports of a component type.
WASM_API_EXTERN void wasmtime_component_item_clone(const wasmtime_component_item_t *item, wasmtime_component_item_t *out)
Clones a component item.
#define WASMTIME_COMPONENT_ITEM_TYPE
Value of wasmtime_component_item_kind_t meaning that wasmtime_component_item_t is a type.
Definition: component/types/component.h:106
#define WASMTIME_COMPONENT_VALTYPE_BOOL
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a bool WIT ty...
Definition: component/types/val.h:342
Build-time defines for how the C API was built.
Represents a single item in a component's import or export list.
Definition: component/types/component.h:137
wasmtime_component_item_union_t of
The actual item.
Definition: component/types/component.h:141
wasmtime_component_item_kind_t kind
The type discriminant for the of union.
Definition: component/types/component.h:139
wasmtime_component_valtype_kind_t kind
The type discriminant for the of union.
Definition: component/types/val.h:465
wasmtime_component_valtype_t type
Definition: component/types/component.h:133
wasmtime_component_type_t * component
Definition: component/types/component.h:115
wasmtime_component_func_type_t * component_func
Definition: component/types/component.h:124
wasmtime_module_type_t * module
Definition: component/types/component.h:121
wasmtime_component_resource_type_t * resource
Definition: component/types/component.h:127
wasm_functype_t * core_func
Definition: component/types/component.h:130
wasmtime_component_instance_type_t * component_instance
Definition: component/types/component.h:118