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;
25class ComponentExtern;
26
31 WASMTIME_CLONE_WRAPPER(ComponentType, wasmtime_component_type);
32
34 size_t import_count(const Engine &engine) const {
35 return wasmtime_component_type_import_count(capi(), engine.capi());
36 }
37
42 std::optional<ComponentExtern> import_get(const Engine &engine,
43 std::string_view name) const;
44
49 std::optional<std::pair<std::string_view, ComponentExtern>>
50 import_nth(const Engine &engine, size_t nth) const;
51
53 size_t export_count(const Engine &engine) const {
54 return wasmtime_component_type_export_count(capi(), engine.capi());
55 }
56
61 std::optional<ComponentExtern> export_get(const Engine &engine,
62 std::string_view name) const;
63
68 std::optional<std::pair<std::string_view, ComponentExtern>>
69 export_nth(const Engine &engine, size_t nth) const;
70};
71
73class ModuleType;
74class FuncType;
75class ResourceType;
76class ValType;
77
83
84public:
86 explicit ComponentItem(wasmtime_component_item_t &&item) : item(item) {
89 }
90
93 wasmtime_component_item_clone(&other.item, &item);
94 }
95
99 wasmtime_component_item_clone(&other.item, &item);
100 return *this;
101 }
102
104 ComponentItem(ComponentItem &&other) : item(other.item) {
106 other.item.of.type.kind = WASMTIME_COMPONENT_VALTYPE_BOOL;
107 }
108
112 item = other.item;
114 other.item.of.type.kind = WASMTIME_COMPONENT_VALTYPE_BOOL;
115 return *this;
116 }
117
119
121 bool is_component() const {
123 }
124
128 }
129
131 bool is_module() const { return item.kind == WASMTIME_COMPONENT_ITEM_MODULE; }
132
134 bool is_component_func() const {
136 }
137
139 bool is_resource() const {
141 }
142
144 bool is_core_func() const {
146 }
147
149 bool is_type() const { return item.kind == WASMTIME_COMPONENT_ITEM_TYPE; }
150
153 const ComponentType &component() const {
154 assert(is_component());
155 return *ComponentType::from_capi(&item.of.component);
156 }
157
161
164 const ModuleType &module() const;
165
168 const FuncType &component_func() const;
169
172 const ResourceType &resource() const;
173
177 assert(is_core_func());
178 return item.of.core_func;
179 }
180
183 const ValType &type() const;
184};
185
199 WASMTIME_CLONE_WRAPPER(ComponentExtern, wasmtime_component_extern);
200
202 ComponentItem type() const;
203
206 std::optional<std::string_view> implements() const {
207 size_t len = 0;
208 const char *ptr = wasmtime_component_extern_implements(capi(), &len);
209 if (ptr == nullptr) {
210 return std::nullopt;
211 }
212 return std::string_view(ptr, len);
213 }
214
222 bool is_implements(std::string_view name) const {
223 return wasmtime_component_extern_is_implements(capi(), name.data(),
224 name.size());
225 }
226
229 std::optional<std::string_view> external_id() const {
230 size_t len = 0;
231 const char *ptr = wasmtime_component_extern_external_id(capi(), &len);
232 if (ptr == nullptr) {
233 return std::nullopt;
234 }
235 return std::string_view(ptr, len);
236 }
237};
238
239} // namespace component
240} // namespace wasmtime
241
246
248wasmtime::component::ComponentExtern::type() const {
250 wasmtime_component_extern_type(capi(), &item);
251 return wasmtime::component::ComponentItem(std::move(item));
252}
253
254inline std::optional<wasmtime::component::ComponentExtern>
255wasmtime::component::ComponentType::import_get(const wasmtime::Engine &engine,
256 std::string_view name) const {
258 bool found = wasmtime_component_type_import_get(capi(), engine.capi(),
259 name.data(), name.size(), &e);
260 if (!found) {
261 return std::nullopt;
262 }
264}
265
266inline std::optional<
267 std::pair<std::string_view, wasmtime::component::ComponentExtern>>
268wasmtime::component::ComponentType::import_nth(const wasmtime::Engine &engine,
269 size_t nth) const {
271 const char *name_data;
272 size_t name_size;
273 bool found = wasmtime_component_type_import_nth(capi(), engine.capi(), nth,
274 &name_data, &name_size, &e);
275 if (!found) {
276 return std::nullopt;
277 }
278 return std::make_pair(std::string_view(name_data, name_size),
280}
281
282inline std::optional<wasmtime::component::ComponentExtern>
283wasmtime::component::ComponentType::export_get(const wasmtime::Engine &engine,
284 std::string_view name) const {
286 bool found = wasmtime_component_type_export_get(capi(), engine.capi(),
287 name.data(), name.size(), &e);
288 if (!found) {
289 return std::nullopt;
290 }
292}
293
294inline std::optional<
295 std::pair<std::string_view, wasmtime::component::ComponentExtern>>
296wasmtime::component::ComponentType::export_nth(const wasmtime::Engine &engine,
297 size_t nth) const {
299 const char *name_data;
300 size_t name_size;
301 bool found = wasmtime_component_type_export_nth(capi(), engine.capi(), nth,
302 &name_data, &name_size, &e);
303 if (!found) {
304 return std::nullopt;
305 }
306 return std::make_pair(std::string_view(name_data, name_size),
308}
309
312 assert(is_component_instance());
313 return *ComponentInstanceType::from_capi(&item.of.component_instance);
314}
315
318 assert(is_module());
319 return *ModuleType::from_capi(&item.of.module);
320}
321
324 assert(is_component_func());
325 return *FuncType::from_capi(&item.of.component_func);
326}
327
330 assert(is_resource());
331 return *ResourceType::from_capi(&item.of.resource);
332}
333
334inline const wasmtime::component::ValType &
336 assert(is_type());
337 return *ValType::from_capi(&item.of.type);
338}
339
340#endif // WASMTIME_FEATURE_COMPONENT_MODEL
341
342#endif // WASMTIME_COMPONENT_TYPES_COMPONENT_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Definition: types/func.hh:29
Full description of a single import or export of a component or a component instance.
Definition: component/types/component.hh:198
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:81
ComponentItem(ComponentItem &&other)
Moves another item into this one.
Definition: component/types/component.hh:104
ComponentItem & operator=(ComponentItem &&other)
Moves another item into this one.
Definition: component/types/component.hh:110
bool is_core_func() const
Returns true if this is a core function.
Definition: component/types/component.hh:144
const ResourceType & resource() const
Definition: component/types/component.hh:329
const FuncType & component_func() const
Definition: component/types/component.hh:323
bool is_resource() const
Returns true if this is a resource.
Definition: component/types/component.hh:139
ComponentItem(wasmtime_component_item_t &&item)
Creates a component item from the raw C API representation.
Definition: component/types/component.hh:86
const ComponentType & component() const
Definition: component/types/component.hh:153
const ModuleType & module() const
Definition: component/types/component.hh:317
bool is_type() const
Returns true if this is a type.
Definition: component/types/component.hh:149
const ComponentInstanceType & component_instance() const
Definition: component/types/component.hh:311
ComponentItem(const ComponentItem &other)
Copies another item into this one.
Definition: component/types/component.hh:92
bool is_component_instance() const
Returns true if this is a component instance.
Definition: component/types/component.hh:126
ComponentItem & operator=(const ComponentItem &other)
Copies another item into this one.
Definition: component/types/component.hh:97
bool is_module() const
Returns true if this is a module.
Definition: component/types/component.hh:131
bool is_component() const
Returns true if this is a component.
Definition: component/types/component.hh:121
const ValType & type() const
Definition: component/types/component.hh:335
bool is_component_func() const
Returns true if this is a component function.
Definition: component/types/component.hh:134
wasmtime::FuncType::Ref core_func() const
Definition: component/types/component.hh:176
Represents the type of a WebAssembly component.
Definition: component/types/component.hh:30
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:197
static const ValType * from_capi(const wasmtime_component_valtype_t *capi)
Definition: component/types/val.hh:244
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
#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_extern_is_implements(const wasmtime_component_extern_t *e, const char *name, size_t len)
Returns whether this import/export e has an implements attribute which matches the name provided (whi...
#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 const char * wasmtime_component_extern_implements(const wasmtime_component_extern_t *e, size_t *len)
Returns the implements attribute of this import/export.
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_extern_t **ret)
Retrieves the nth export.
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_get(const wasmtime_component_type_t *ty, const wasm_engine_t *engine, const char *name, size_t name_len, struct wasmtime_component_extern_t **ret)
Retrieves the import with the specified name.
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_extern_t **ret)
Retrieves the export with the specified name.
WASM_API_EXTERN void wasmtime_component_extern_type(const wasmtime_component_extern_t *e, wasmtime_component_item_t *ret)
Returns the type of the item that this import/export refers to.
struct wasmtime_component_extern_t wasmtime_component_extern_t
Full description of a single component or component instance export or import.
Definition: component/types/component.h:166
#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 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_extern_t **ret)
Retrieves the nth import.
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.
WASM_API_EXTERN const char * wasmtime_component_extern_external_id(const wasmtime_component_extern_t *e, size_t *len)
Returns the external-id attribute of this import/export.
#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:379
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:508
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