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/error.hh>
12#include <wasmtime/types/_val_class.hh>
14
15namespace wasmtime {
16
20
22
23public:
25 StorageType(const StorageType &other) {
26 wasmtime_storage_type_clone(&other.ty, &ty);
27 }
30 return *this = StorageType(other);
31 }
35 ty = other.ty;
37 }
41 ty = other.ty;
43 return *this;
44 }
45
47 StorageType(const ValType &ty);
48
51
53 static StorageType i16() {
55 }
56
58 const wasmtime_storage_type_t *capi() const { return &ty; }
59
61 bool is_i8() const { return ty.kind == WASMTIME_STORAGE_TYPE_KIND_I8; }
62
64 bool is_i16() const { return ty.kind == WASMTIME_STORAGE_TYPE_KIND_I16; }
65
68 std::optional<ValType::Ref> as_valtype() const {
70 return ty.valtype;
71 return std::nullopt;
72 }
73};
74
79class FieldType {
81
82public:
85
87 FieldType(const FieldType &other) {
88 wasmtime_field_type_clone(&other.ty, &ty);
89 }
91 FieldType &operator=(const FieldType &other) {
92 return *this = FieldType(other);
93 }
97 ty = other.ty;
99 }
103 ty = other.ty;
105 return *this;
106 }
107
110 this->ty.mutable_ = is_mutable;
111 wasmtime_storage_type_clone(ty.capi(), &this->ty.storage);
112 }
113
115 static FieldType mut_(const StorageType &ty) { return FieldType(true, ty); }
116
118 static FieldType const_(const StorageType &ty) {
119 return FieldType(false, ty);
120 }
121
123 bool is_mutable() const { return ty.mutable_; }
124
126 const StorageType &storage_type() const {
127 static_assert(sizeof(StorageType) == sizeof(wasmtime_storage_type_t));
128 return *reinterpret_cast<const StorageType *>(&ty.storage);
129 }
130
132 const wasmtime_field_type_t *capi() const { return &ty; }
133};
134
142#define wasmtime_struct_type_clone wasmtime_struct_type_copy
143 WASMTIME_CLONE_WRAPPER(StructType, wasmtime_struct_type)
144#undef wasmtime_struct_type_clone
145
147 static Result<StructType> create(const Engine &engine,
148 const std::vector<FieldType> &fields) {
149 static_assert(sizeof(FieldType) == sizeof(wasmtime_field_type_t));
150 wasmtime_struct_type_t *result = nullptr;
151 auto error = wasmtime_struct_type_new(
152 engine.capi(),
153 reinterpret_cast<const wasmtime_field_type_t *>(fields.data()),
154 fields.size(), &result);
155 if (error)
156 return Error(error);
157 return StructType(result);
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:23
Errors coming from Wasmtime.
Definition: error.hh:26
Describes the storage type and mutability of a struct field or array element.
Definition: types/_structref_class.hh:79
static FieldType const_(const StorageType &ty)
Constructs an immutable field type with the given storage type.
Definition: types/_structref_class.hh:118
FieldType(bool is_mutable, const StorageType &ty)
Constructs a field type with the given mutability and storage type.
Definition: types/_structref_class.hh:109
static FieldType mut_(const StorageType &ty)
Constructs a mutable field type with the given storage type.
Definition: types/_structref_class.hh:115
bool is_mutable() const
Returns whether this field type is mutable.
Definition: types/_structref_class.hh:123
const wasmtime_field_type_t * capi() const
Returns the underlying C API field type.
Definition: types/_structref_class.hh:132
FieldType & operator=(FieldType &&other)
Move assignment operator.
Definition: types/_structref_class.hh:101
FieldType(wasmtime_field_type ty)
Constructs a field type from a C API field type.
Definition: types/_structref_class.hh:84
const StorageType & storage_type() const
Returns the storage type of this field type.
Definition: types/_structref_class.hh:126
FieldType(FieldType &&other)
Move constructor.
Definition: types/_structref_class.hh:96
FieldType & operator=(const FieldType &other)
Copy assignment operator.
Definition: types/_structref_class.hh:91
FieldType(const FieldType &other)
Copy constructor.
Definition: types/_structref_class.hh:87
Fallible result type used for Wasmtime.
Definition: error.hh:70
Storage type for a struct field or array element.
Definition: types/_structref_class.hh:18
StorageType(const StorageType &other)
Copy constructor.
Definition: types/_structref_class.hh:25
const wasmtime_storage_type_t * capi() const
Returns the underlying C API storage type.
Definition: types/_structref_class.hh:58
StorageType & operator=(StorageType &&other)
Move assignment operator.
Definition: types/_structref_class.hh:39
static StorageType i16()
Constructs a storage type for a 16-bit integer.
Definition: types/_structref_class.hh:53
StorageType & operator=(const StorageType &other)
Copy assignment operator.
Definition: types/_structref_class.hh:29
bool is_i8() const
Returns whether this storage type is an 8-bit integer.
Definition: types/_structref_class.hh:61
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:68
StorageType(StorageType &&other)
Move constructor.
Definition: types/_structref_class.hh:34
bool is_i16() const
Returns whether this storage type is a 16-bit integer.
Definition: types/_structref_class.hh:64
static StorageType i8()
Constructs a storage type for an 8-bit integer.
Definition: types/_structref_class.hh:50
Owned handle to a WebAssembly struct type definition.
Definition: types/_structref_class.hh:141
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:57
bool mutable_
Definition: types/structref.h:60
wasmtime_storage_type_t storage
The type stored in this field.
Definition: types/structref.h:62
A storage type descriptor for struct/array fields.
Definition: types/structref.h:30
wasmtime_storage_type_kind_t kind
The kind of storage type this is.
Definition: types/structref.h:32
wasm_valtype_t * valtype
Definition: types/structref.h:35
#define WASMTIME_STORAGE_TYPE_KIND_I8
An 8-bit packed integer.
Definition: types/structref.h:23
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.
struct wasmtime_struct_type wasmtime_struct_type_t
An opaque handle to a WebAssembly struct type definition.
Definition: types/structref.h:81
#define WASMTIME_STORAGE_TYPE_KIND_I16
A 16-bit packed integer.
Definition: types/structref.h:25
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.
WASM_API_EXTERN wasmtime_error_t * wasmtime_struct_type_new(const wasm_engine_t *engine, const wasmtime_field_type_t *fields, size_t nfields, wasmtime_struct_type_t **out)
Create a new struct type.
#define WASMTIME_STORAGE_TYPE_KIND_VALTYPE
A regular value type (i32, f64, funcref, etc).
Definition: types/structref.h:27
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:20