Wasmtime
types/_structref_class.hh
1#ifndef WASMTIME_TYPES_STRUCTREF_CLASS_HH
2#define WASMTIME_TYPES_STRUCTREF_CLASS_HH
3
4#include <wasmtime/conf.h>
5
6#ifdef WASMTIME_FEATURE_GC
7
8#include <memory>
9#include <vector>
10#include <wasmtime/engine.hh>
11#include <wasmtime/types/_val_class.hh>
13
14namespace wasmtime {
15
19
21
22public:
24 StorageType(const StorageType &other) {
25 wasmtime_storage_type_clone(&other.ty, &ty);
26 }
30 wasmtime_storage_type_clone(&other.ty, &ty);
31 return *this;
32 }
36 ty = other.ty;
38 }
42 ty = other.ty;
44 return *this;
45 }
46
48 StorageType(const ValType &ty);
49
52
54 static StorageType i16() {
56 }
57
59 const wasmtime_storage_type_t *capi() const { return &ty; }
60
62 bool is_i8() const { return ty.kind == WASMTIME_STORAGE_TYPE_KIND_I8; }
63
65 bool is_i16() const { return ty.kind == WASMTIME_STORAGE_TYPE_KIND_I16; }
66
69 std::optional<ValType::Ref> as_valtype() const {
71 return ty.valtype;
72 return std::nullopt;
73 }
74};
75
80class FieldType {
82
83public:
86
88 FieldType(const FieldType &other) {
89 wasmtime_field_type_clone(&other.ty, &ty);
90 }
92 FieldType &operator=(const FieldType &other) {
94 wasmtime_field_type_clone(&other.ty, &ty);
95 return *this;
96 }
100 ty = other.ty;
102 }
106 ty = other.ty;
108 return *this;
109 }
110
113 this->ty.mutable_ = is_mutable;
114 wasmtime_storage_type_clone(ty.capi(), &this->ty.storage);
115 }
116
118 static FieldType mut_(const StorageType &ty) { return FieldType(true, ty); }
119
121 static FieldType const_(const StorageType &ty) {
122 return FieldType(false, ty);
123 }
124
126 bool is_mutable() const { return ty.mutable_; }
127
129 const StorageType &storage_type() const {
130 static_assert(sizeof(StorageType) == sizeof(wasmtime_storage_type_t));
131 return *reinterpret_cast<const StorageType *>(&ty.storage);
132 }
133
135 const wasmtime_field_type_t *capi() const { return &ty; }
136};
137
145#define wasmtime_struct_type_clone wasmtime_struct_type_copy
146 WASMTIME_CLONE_WRAPPER(StructType, wasmtime_struct_type)
147#undef wasmtime_struct_type_clone
148
150 StructType(const Engine &engine, const std::vector<FieldType> &fields)
152 engine.capi(),
153 reinterpret_cast<const wasmtime_field_type_t *>(fields.data()),
154 fields.size()))
155
156 {
157 static_assert(sizeof(FieldType) == sizeof(wasmtime_field_type_t));
158 }
159
160 size_t num_fields() const {
161 return wasmtime_struct_type_num_fields(ptr.get());
162 }
163
164 std::optional<FieldType> field(size_t index) const {
165 if (index >= wasmtime_struct_type_num_fields(ptr.get()))
166 return std::nullopt;
168 wasmtime_struct_type_field(ptr.get(), index, &ty);
169 return FieldType(ty);
170 }
171};
172
173} // namespace wasmtime
174
175#endif // WASMTIME_FEATURE_GC
176
177#endif // WASMTIME_TYPES_STRUCTREF_CLASS_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
Describes the storage type and mutability of a struct field or array element.
Definition: types/_structref_class.hh:80
static FieldType const_(const StorageType &ty)
Constructs an immutable field type with the given storage type.
Definition: types/_structref_class.hh:121
FieldType(bool is_mutable, const StorageType &ty)
Constructs a field type with the given mutability and storage type.
Definition: types/_structref_class.hh:112
static FieldType mut_(const StorageType &ty)
Constructs a mutable field type with the given storage type.
Definition: types/_structref_class.hh:118
bool is_mutable() const
Returns whether this field type is mutable.
Definition: types/_structref_class.hh:126
const wasmtime_field_type_t * capi() const
Returns the underlying C API field type.
Definition: types/_structref_class.hh:135
FieldType & operator=(FieldType &&other)
Move assignment operator.
Definition: types/_structref_class.hh:104
FieldType(wasmtime_field_type ty)
Constructs a field type from a C API field type.
Definition: types/_structref_class.hh:85
const StorageType & storage_type() const
Returns the storage type of this field type.
Definition: types/_structref_class.hh:129
FieldType(FieldType &&other)
Move constructor.
Definition: types/_structref_class.hh:99
FieldType & operator=(const FieldType &other)
Copy assignment operator.
Definition: types/_structref_class.hh:92
FieldType(const FieldType &other)
Copy constructor.
Definition: types/_structref_class.hh:88
Storage type for a struct field or array element.
Definition: types/_structref_class.hh:17
StorageType(const StorageType &other)
Copy constructor.
Definition: types/_structref_class.hh:24
const wasmtime_storage_type_t * capi() const
Returns the underlying C API storage type.
Definition: types/_structref_class.hh:59
StorageType & operator=(StorageType &&other)
Move assignment operator.
Definition: types/_structref_class.hh:40
static StorageType i16()
Constructs a storage type for a 16-bit integer.
Definition: types/_structref_class.hh:54
StorageType & operator=(const StorageType &other)
Copy assignment operator.
Definition: types/_structref_class.hh:28
bool is_i8() const
Returns whether this storage type is an 8-bit integer.
Definition: types/_structref_class.hh:62
std::optional< ValType::Ref > as_valtype() const
If this storage type is a value type, returns the underlying value type.
Definition: types/_structref_class.hh:69
StorageType(StorageType &&other)
Move constructor.
Definition: types/_structref_class.hh:35
bool is_i16() const
Returns whether this storage type is a 16-bit integer.
Definition: types/_structref_class.hh:65
static StorageType i8()
Constructs a storage type for an 8-bit integer.
Definition: types/_structref_class.hh:51
Owned handle to a WebAssembly struct type definition.
Definition: types/_structref_class.hh:144
Type information about a WebAssembly value.
Definition: types/_val_class.hh:19
Build-time defines for how the C API was built.
Describes the type and mutability of a struct field or array element.
Definition: types/structref.h:56
bool mutable_
Definition: types/structref.h:59
wasmtime_storage_type_t storage
The type stored in this field.
Definition: types/structref.h:61
A storage type descriptor for struct/array fields.
Definition: types/structref.h:29
wasmtime_storage_type_kind_t kind
The kind of storage type this is.
Definition: types/structref.h:31
wasm_valtype_t * valtype
Definition: types/structref.h:34
#define WASMTIME_STORAGE_TYPE_KIND_I8
An 8-bit packed integer.
Definition: types/structref.h:22
WASM_API_EXTERN bool wasmtime_struct_type_field(const wasmtime_struct_type_t *ty, size_t index, wasmtime_field_type_t *out)
Get the field type of a struct type's field by index.
WASM_API_EXTERN wasmtime_struct_type_t * wasmtime_struct_type_new(const wasm_engine_t *engine, const wasmtime_field_type_t *fields, size_t nfields)
Create a new struct type.
#define WASMTIME_STORAGE_TYPE_KIND_I16
A 16-bit packed integer.
Definition: types/structref.h:24
WASM_API_EXTERN void wasmtime_storage_type_clone(const wasmtime_storage_type_t *storage, wasmtime_storage_type_t *out)
Clone a storage type into out.
WASM_API_EXTERN void wasmtime_storage_type_delete(wasmtime_storage_type_t *storage)
Delete a storage type.
#define WASMTIME_STORAGE_TYPE_KIND_VALTYPE
A regular value type (i32, f64, funcref, etc).
Definition: types/structref.h:26
WASM_API_EXTERN void wasmtime_field_type_clone(const wasmtime_field_type_t *field, wasmtime_field_type_t *out)
Clone a field type into out.
WASM_API_EXTERN void wasmtime_field_type_delete(wasmtime_field_type_t *field)
Delete a field type.
WASM_API_EXTERN size_t wasmtime_struct_type_num_fields(const wasmtime_struct_type_t *ty)
Get the number of fields in a struct type.
uint8_t wasmtime_storage_type_kind_t
Discriminant for storage types in struct/array field types.
Definition: types/structref.h:19