5#ifndef WASMTIME_COMPONENT_TYPES_VAL_HH
6#define WASMTIME_COMPONENT_TYPES_VAL_HH
10#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
16#include <wasmtime/helpers.hh>
27 WASMTIME_CLONE_EQUAL_WRAPPER(
ListType, wasmtime_component_list_type);
37 WASMTIME_CLONE_EQUAL_WRAPPER(
RecordType, wasmtime_component_record_type);
40 size_t field_count()
const {
45 std::optional<std::pair<std::string_view, ValType>>
46 field_nth(
size_t nth)
const;
53 WASMTIME_CLONE_EQUAL_WRAPPER(
TupleType, wasmtime_component_tuple_type);
56 size_t types_count()
const {
61 std::optional<ValType> types_nth(
size_t nth)
const;
68 WASMTIME_CLONE_EQUAL_WRAPPER(
VariantType, wasmtime_component_variant_type);
71 size_t case_count()
const {
76 std::optional<std::pair<std::string_view, std::optional<ValType>>>
77 case_nth(
size_t nth)
const;
84 WASMTIME_CLONE_EQUAL_WRAPPER(
EnumType, wasmtime_component_enum_type);
87 size_t names_count()
const {
92 std::optional<std::string_view> names_nth(
size_t nth)
const {
93 const char *name_ptr =
nullptr;
97 return std::string_view(name_ptr, name_len);
107 WASMTIME_CLONE_EQUAL_WRAPPER(
OptionType, wasmtime_component_option_type);
117 WASMTIME_CLONE_EQUAL_WRAPPER(
ResultType, wasmtime_component_result_type);
120 std::optional<ValType> ok()
const;
123 std::optional<ValType> err()
const;
130 WASMTIME_CLONE_EQUAL_WRAPPER(
FlagsType, wasmtime_component_flags_type);
133 size_t names_count()
const {
138 std::optional<std::string_view> names_nth(
size_t nth)
const {
139 const char *name_ptr =
nullptr;
143 return std::string_view(name_ptr, name_len);
152 WASMTIME_CLONE_EQUAL_WRAPPER(
ResourceType, wasmtime_component_resource_type);
165 WASMTIME_CLONE_EQUAL_WRAPPER(
FutureType, wasmtime_component_future_type);
168 std::optional<ValType> ty()
const;
175 WASMTIME_CLONE_EQUAL_WRAPPER(
StreamType, wasmtime_component_stream_type);
178 std::optional<ValType> ty()
const;
361 ty.
of.own = own.capi_release();
487 return *ListType::from_capi(&ty.
of.
list);
493 return *RecordType::from_capi(&ty.
of.
record);
499 return *TupleType::from_capi(&ty.
of.
tuple);
505 return *VariantType::from_capi(&ty.
of.
variant);
511 return *EnumType::from_capi(&ty.
of.
enum_);
517 return *OptionType::from_capi(&ty.
of.
option);
523 return *ResultType::from_capi(&ty.
of.
result);
529 return *FlagsType::from_capi(&ty.
of.
flags);
535 return *ResourceType::from_capi(&ty.
of.own);
541 return *ResourceType::from_capi(&ty.
of.
borrow);
547 return *FutureType::from_capi(&ty.
of.
future);
553 return *StreamType::from_capi(&ty.
of.
stream);
562inline ValType ListType::element()
const {
565 return ValType(std::move(type_ret));
568inline std::optional<std::pair<std::string_view, ValType>>
569RecordType::field_nth(
size_t nth)
const {
570 const char *name_ptr =
nullptr;
574 &name_len, &type_ret)) {
575 return std::make_pair(std::string_view(name_ptr, name_len),
576 ValType(std::move(type_ret)));
581inline std::optional<ValType> TupleType::types_nth(
size_t nth)
const {
584 return ValType(std::move(type_ret));
589inline std::optional<std::pair<std::string_view, std::optional<ValType>>>
590VariantType::case_nth(
size_t nth)
const {
591 const char *name_ptr =
nullptr;
593 bool has_payload =
false;
596 ptr.get(), nth, &name_ptr, &name_len, &has_payload, &payload_ret)) {
599 return std::make_pair(
600 std::string_view(name_ptr, name_len),
601 has_payload ? std::optional<ValType>(ValType(std::move(payload_ret)))
605inline ValType OptionType::ty()
const {
608 return ValType(std::move(type_ret));
611inline std::optional<ValType> ResultType::ok()
const {
614 return ValType(std::move(type_ret));
619inline std::optional<ValType> ResultType::err()
const {
622 return ValType(std::move(type_ret));
627inline std::optional<ValType> FutureType::ty()
const {
630 return ValType(std::move(type_ret));
635inline std::optional<ValType> StreamType::ty()
const {
638 return ValType(std::move(type_ret));
Type information about a WebAssembly value.
Definition: types/val.hh:66
Represents a component enum type.
Definition: component/types/val.hh:83
Represents a component flags type.
Definition: component/types/val.hh:129
Represents a component future type.
Definition: component/types/val.hh:164
Represents a component list type.
Definition: component/types/val.hh:26
Represents a component option type.
Definition: component/types/val.hh:106
Represents a component record type.
Definition: component/types/val.hh:36
Definition: component/types/val.hh:151
ResourceType(uint32_t ty)
Creates a new host resource type with the specified ty identifier.
Definition: component/types/val.hh:157
Represents a component result type.
Definition: component/types/val.hh:116
Represents a component stream type.
Definition: component/types/val.hh:174
Represents a component tuple type.
Definition: component/types/val.hh:52
Represents a component value type.
Definition: component/types/val.hh:184
bool is_enum() const
Returns true if this is an enum type.
Definition: component/types/val.hh:446
bool is_u32() const
Returns true if this is a u32 type.
Definition: component/types/val.hh:410
bool is_char() const
Returns true if this is a char type.
Definition: component/types/val.hh:422
ValType & operator=(const ValType &other)
Copies another type into this one.
Definition: component/types/val.hh:206
const FlagsType & flags() const
Returns the flags type, asserting that this is indeed a flags.
Definition: component/types/val.hh:527
const EnumType & enum_() const
Returns the enum type, asserting that this is indeed a enum.
Definition: component/types/val.hh:509
const ResourceType & borrow() const
Returns the borrow type, asserting that this is indeed a borrow.
Definition: component/types/val.hh:539
bool is_tuple() const
Returns true if this is a tuple type.
Definition: component/types/val.hh:438
bool is_variant() const
Returns true if this is a variant type.
Definition: component/types/val.hh:441
wasmtime_component_valtype_t * capi()
Returns the underlying C API pointer.
Definition: component/types/val.hh:559
static ValType new_s8()
Creates an s8 value type.
Definition: component/types/val.hh:250
static ValType new_s16()
Creates an s16 value type.
Definition: component/types/val.hh:255
ValType(ListType list)
Creates a list value type.
Definition: component/types/val.hh:310
const FutureType & future() const
Returns the future type, asserting that this is indeed a future.
Definition: component/types/val.hh:545
bool is_borrow() const
Returns true if this is a borrow type.
Definition: component/types/val.hh:465
wasmtime_component_valtype_kind_t kind() const
Returns the kind of this value type.
Definition: component/types/val.hh:386
static ValType new_bool()
Creates a bool value type.
Definition: component/types/val.hh:245
const wasmtime_component_valtype_t * capi() const
Returns the underlying C API pointer.
Definition: component/types/val.hh:557
ValType(ValType &&other)
Moves another type into this one.
Definition: component/types/val.hh:213
static ValType new_string()
Creates a string value type.
Definition: component/types/val.hh:305
const StreamType & stream() const
Returns the stream type, asserting that this is indeed a stream.
Definition: component/types/val.hh:551
bool is_own() const
Returns true if this is an own type.
Definition: component/types/val.hh:462
bool is_error_context() const
Returns true if this is an error context type.
Definition: component/types/val.hh:480
bool is_string() const
Returns true if this is a string type.
Definition: component/types/val.hh:425
bool is_list() const
Returns true if this is a list type.
Definition: component/types/val.hh:430
static ValType new_f32()
Creates an f32 value type.
Definition: component/types/val.hh:290
ValType(const ValType &other)
Copies another type into this one.
Definition: component/types/val.hh:201
const RecordType & record() const
Returns the record type, asserting that this is indeed a record.
Definition: component/types/val.hh:491
bool is_future() const
Returns true if this is a future type.
Definition: component/types/val.hh:470
ValType(EnumType enum_)
Creates an enum value type.
Definition: component/types/val.hh:334
ValType(ResultType result)
Creates a result value type.
Definition: component/types/val.hh:346
ValType(OptionType option)
Creates an option value type.
Definition: component/types/val.hh:340
static ValType new_u16()
Creates a u16 value type.
Definition: component/types/val.hh:275
static ValType new_u64()
Creates a u64 value type.
Definition: component/types/val.hh:285
ValType(FlagsType flags)
Creates a flags value type.
Definition: component/types/val.hh:352
bool is_f32() const
Returns true if this is an f32 type.
Definition: component/types/val.hh:416
bool is_s16() const
Returns true if this is an s16 type.
Definition: component/types/val.hh:395
static ValType new_char()
Creates a char value type.
Definition: component/types/val.hh:300
static ValType new_u8()
Creates a u8 value type.
Definition: component/types/val.hh:270
const ListType & list() const
Returns the list type, asserting that this is indeed a list.
Definition: component/types/val.hh:485
ValType(FutureType future)
Creates a future value type.
Definition: component/types/val.hh:374
ValType(VariantType variant)
Creates a variant value type.
Definition: component/types/val.hh:328
bool is_u8() const
Returns true if this is a u8 type.
Definition: component/types/val.hh:404
const TupleType & tuple() const
Returns the tuple type, asserting that this is indeed a tuple.
Definition: component/types/val.hh:497
ValType(wasmtime_component_valtype_t &&ty)
Creates a component value type from the raw C API representation.
Definition: component/types/val.hh:195
bool is_flags() const
Returns true if this is a flags type.
Definition: component/types/val.hh:459
static ValType new_borrow(ResourceType borrow)
Creates an borrow value type.
Definition: component/types/val.hh:366
bool operator!=(const ValType &other) const
Compares two types to see if they're different.
Definition: component/types/val.hh:242
bool is_s64() const
Returns true if this is an s64 type.
Definition: component/types/val.hh:401
ValType(RecordType record)
Creates a record value type.
Definition: component/types/val.hh:316
bool is_u64() const
Returns true if this is a u64 type.
Definition: component/types/val.hh:413
static ValType new_f64()
Creates an f64 value type.
Definition: component/types/val.hh:295
static ValType new_s64()
Creates an s64 value type.
Definition: component/types/val.hh:265
ValType & operator=(ValType &&other)
Moves another type into this one.
Definition: component/types/val.hh:218
static ValType new_s32()
Creates an s32 value type.
Definition: component/types/val.hh:260
static ValType new_u32()
Creates a u32 value type.
Definition: component/types/val.hh:280
const VariantType & variant() const
Returns the variant type, asserting that this is indeed a variant.
Definition: component/types/val.hh:503
bool is_s8() const
Returns true if this is an s8 type.
Definition: component/types/val.hh:392
ValType(StreamType stream)
Creates a stream value type.
Definition: component/types/val.hh:380
bool is_s32() const
Returns true if this is an s32 type.
Definition: component/types/val.hh:398
bool is_f64() const
Returns true if this is an f64 type.
Definition: component/types/val.hh:419
static ValType new_own(ResourceType own)
Creates an own value type.
Definition: component/types/val.hh:358
bool operator==(const ValType &other) const
Compares two types to see if they're the same.
Definition: component/types/val.hh:237
bool is_result() const
Returns true if this is a result type.
Definition: component/types/val.hh:454
bool is_record() const
Returns true if this is a record type.
Definition: component/types/val.hh:433
const ResultType & result() const
Returns the result type, asserting that this is indeed a result.
Definition: component/types/val.hh:521
ValType(TupleType tuple)
Creates a tuple value type.
Definition: component/types/val.hh:322
const OptionType & option() const
Returns the option type, asserting that this is indeed a option.
Definition: component/types/val.hh:515
bool is_bool() const
Returns true if this is a bool type.
Definition: component/types/val.hh:389
bool is_u16() const
Returns true if this is a u16 type.
Definition: component/types/val.hh:407
bool is_option() const
Returns true if this is an option type.
Definition: component/types/val.hh:449
bool is_stream() const
Returns true if this is a stream type.
Definition: component/types/val.hh:475
static const ValType * from_capi(const wasmtime_component_valtype_t *capi)
Definition: component/types/val.hh:231
Represents a component variant type.
Definition: component/types/val.hh:67
WASM_API_EXTERN bool wasmtime_component_result_type_err(const wasmtime_component_result_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the err type of a component result type. The returned type must be deallocated with wasmtime_...
WASM_API_EXTERN void wasmtime_component_option_type_ty(const wasmtime_component_option_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the inner type of a component option type.
#define WASMTIME_COMPONENT_VALTYPE_S16
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a s16 WIT typ...
Definition: component/types/val.h:348
#define WASMTIME_COMPONENT_VALTYPE_U64
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a u64 WIT typ...
Definition: component/types/val.h:366
WASM_API_EXTERN size_t wasmtime_component_tuple_type_types_count(const wasmtime_component_tuple_type_t *ty)
Returns the number of types in a component tuple type.
WASM_API_EXTERN size_t wasmtime_component_enum_type_names_count(const wasmtime_component_enum_type_t *ty)
Returns the number of names in a component enum type.
WASM_API_EXTERN bool wasmtime_component_tuple_type_types_nth(const wasmtime_component_tuple_type_t *ty, size_t nth, struct wasmtime_component_valtype_t *type_ret)
Returns the nth type in a component tuple type.
#define WASMTIME_COMPONENT_VALTYPE_OWN
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a resource ow...
Definition: component/types/val.h:405
WASM_API_EXTERN size_t wasmtime_component_flags_type_names_count(const wasmtime_component_flags_type_t *ty)
Returns the number of names in a component flags type.
WASM_API_EXTERN size_t wasmtime_component_variant_type_case_count(const wasmtime_component_variant_type_t *ty)
Returns the number of cases in a component variant type.
WASM_API_EXTERN bool wasmtime_component_stream_type_ty(const wasmtime_component_stream_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the inner type of a component stream type.
WASM_API_EXTERN void wasmtime_component_valtype_clone(const wasmtime_component_valtype_t *ty, wasmtime_component_valtype_t *out)
Clones a component value type.
WASM_API_EXTERN bool wasmtime_component_record_type_field_nth(const wasmtime_component_record_type_t *ty, size_t nth, const char **name_ret, size_t *name_len_ret, struct wasmtime_component_valtype_t *type_ret)
Returns the nth field in a component record type.
#define WASMTIME_COMPONENT_VALTYPE_STREAM
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a stream WIT ...
Definition: component/types/val.h:414
#define WASMTIME_COMPONENT_VALTYPE_S64
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a s64 WIT typ...
Definition: component/types/val.h:354
#define WASMTIME_COMPONENT_VALTYPE_F64
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a f64 WIT typ...
Definition: component/types/val.h:372
WASM_API_EXTERN bool wasmtime_component_result_type_ok(const wasmtime_component_result_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the ok type of a component result type. The returned type must be deallocated with wasmtime_c...
#define WASMTIME_COMPONENT_VALTYPE_S8
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a s8 WIT type...
Definition: component/types/val.h:345
#define WASMTIME_COMPONENT_VALTYPE_BORROW
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a resource bo...
Definition: component/types/val.h:408
WASM_API_EXTERN bool wasmtime_component_variant_type_case_nth(const wasmtime_component_variant_type_t *ty, size_t nth, const char **name_ret, size_t *name_len_ret, bool *has_payload_ret, struct wasmtime_component_valtype_t *payload_ret)
Returns the nth case in a component variant type. The returned payload type must be deallocated with ...
#define WASMTIME_COMPONENT_VALTYPE_STRING
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a string WIT ...
Definition: component/types/val.h:378
WASM_API_EXTERN void wasmtime_component_valtype_delete(wasmtime_component_valtype_t *ptr)
Deallocates a component value type.
#define WASMTIME_COMPONENT_VALTYPE_FUTURE
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a future WIT ...
Definition: component/types/val.h:411
WASM_API_EXTERN bool wasmtime_component_future_type_ty(const wasmtime_component_future_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the inner type of a component future type.
WASM_API_EXTERN bool wasmtime_component_flags_type_names_nth(const wasmtime_component_flags_type_t *ty, size_t nth, const char **name_ret, size_t *name_len_ret)
Returns the nth name in a component flags type.
WASM_API_EXTERN bool wasmtime_component_enum_type_names_nth(const wasmtime_component_enum_type_t *ty, size_t nth, const char **name_ret, size_t *name_len_ret)
Returns the nth name in a component enum type.
#define WASMTIME_COMPONENT_VALTYPE_VARIANT
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a variant WIT...
Definition: component/types/val.h:390
#define WASMTIME_COMPONENT_VALTYPE_RECORD
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a record WIT ...
Definition: component/types/val.h:384
WASM_API_EXTERN size_t wasmtime_component_record_type_field_count(const wasmtime_component_record_type_t *ty)
Returns the number of fields in a component record type.
#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
uint8_t wasmtime_component_valtype_kind_t
Discriminant used in wasmtime_component_valtype_t::kind.
Definition: component/types/val.h:420
WASM_API_EXTERN bool wasmtime_component_valtype_equal(const wasmtime_component_valtype_t *a, const wasmtime_component_valtype_t *b)
Compares two component value types for equality.
#define WASMTIME_COMPONENT_VALTYPE_RESULT
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a result WIT ...
Definition: component/types/val.h:399
#define WASMTIME_COMPONENT_VALTYPE_ERROR_CONTEXT
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is an error cont...
Definition: component/types/val.h:417
WASM_API_EXTERN void wasmtime_component_list_type_element(const wasmtime_component_list_type_t *ty, struct wasmtime_component_valtype_t *type_ret)
Returns the element type of a component list type.
#define WASMTIME_COMPONENT_VALTYPE_U32
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a u32 WIT typ...
Definition: component/types/val.h:363
#define WASMTIME_COMPONENT_VALTYPE_OPTION
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a option WIT ...
Definition: component/types/val.h:396
#define WASMTIME_COMPONENT_VALTYPE_ENUM
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a enum WIT ty...
Definition: component/types/val.h:393
#define WASMTIME_COMPONENT_VALTYPE_CHAR
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a char WIT ty...
Definition: component/types/val.h:375
#define WASMTIME_COMPONENT_VALTYPE_TUPLE
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a tuple WIT t...
Definition: component/types/val.h:387
#define WASMTIME_COMPONENT_VALTYPE_U8
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a u8 WIT type...
Definition: component/types/val.h:357
#define WASMTIME_COMPONENT_VALTYPE_F32
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a f32 WIT typ...
Definition: component/types/val.h:369
#define WASMTIME_COMPONENT_VALTYPE_LIST
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a list WIT ty...
Definition: component/types/val.h:381
#define WASMTIME_COMPONENT_VALTYPE_FLAGS
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a flags WIT t...
Definition: component/types/val.h:402
#define WASMTIME_COMPONENT_VALTYPE_S32
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a s32 WIT typ...
Definition: component/types/val.h:351
#define WASMTIME_COMPONENT_VALTYPE_U16
Value of wasmtime_component_valtype_kind_t meaning that wasmtime_component_valtype_t is a u16 WIT typ...
Definition: component/types/val.h:360
Build-time defines for how the C API was built.
WASM_API_EXTERN wasmtime_component_resource_type_t * wasmtime_component_resource_type_new_host(uint32_t ty)
Creates a new resource type representing a host-defined resource.
Represents a single value type in the component model.
Definition: component/types/val.h:463
wasmtime_component_valtype_union_t of
The actual type.
Definition: component/types/val.h:467
wasmtime_component_valtype_kind_t kind
The type discriminant for the of union.
Definition: component/types/val.h:465
wasmtime_component_flags_type_t * flags
Definition: component/types/val.h:447
wasmtime_component_option_type_t * option
Definition: component/types/val.h:441
wasmtime_component_record_type_t * record
Definition: component/types/val.h:429
wasmtime_component_resource_type_t * borrow
Definition: component/types/val.h:453
wasmtime_component_list_type_t * list
Definition: component/types/val.h:426
wasmtime_component_tuple_type_t * tuple
Definition: component/types/val.h:432
wasmtime_component_future_type_t * future
Definition: component/types/val.h:456
wasmtime_component_result_type_t * result
Definition: component/types/val.h:444
wasmtime_component_enum_type_t * enum_
Definition: component/types/val.h:438
wasmtime_component_stream_type_t * stream
Definition: component/types/val.h:459
wasmtime_component_variant_type_t * variant
Definition: component/types/val.h:435