Wasmtime
types/val.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TYPES_VAL_HH
6#define WASMTIME_TYPES_VAL_HH
7
8#include <memory>
9#include <ostream>
10#include <wasm.h>
11#include <wasmtime/types/_structref_class.hh>
12#include <wasmtime/types/_val_class.hh>
16#include <wasmtime/types/val.h>
17#include <wasmtime/val.h>
18
19namespace wasmtime {
20
22class HeapType {
24
25 HeapType(wasmtime_heaptype_kind_t ty) { this->ty.kind = ty; }
26
27public:
31 }
32
34 HeapType(const HeapType &other) { wasmtime_heaptype_clone(&other.ty, &ty); }
36 HeapType &operator=(const HeapType &other) { return *this = HeapType(other); }
37 ~HeapType() {
38 if (is_concrete())
40 }
42 HeapType(HeapType &&other) {
43 ty = other.ty;
45 }
49 ty = other.ty;
51 return *this;
52 }
53
56
58 static HeapType noextern() {
60 }
61
64
66 HeapType(const FuncType &ty) {
68 this->ty.of.concrete_func = FuncType(ty).capi_release();
69 }
70
73
76
79
82
85
88
90 HeapType(const ArrayType &ty) {
92 this->ty.of.concrete_array = ArrayType(ty).capi_release();
93 }
94
97
99 HeapType(const StructType &ty) {
101 this->ty.of.concrete_struct = StructType(ty).capi_release();
102 }
103
106
109
111 bool is_concrete() const {
112 switch (ty.kind) {
116 return true;
117 default:
118 return false;
119 }
120 }
121
123 bool is_extern() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXTERN; }
124
126 bool is_noextern() const {
128 }
129
131 bool is_func() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_FUNC; }
132
135 std::optional<FuncType::Ref> as_concrete_func() const {
137 return FuncType::Ref(ty.of.concrete_func);
138 }
139 return std::nullopt;
140 }
141
143 bool is_nofunc() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOFUNC; }
144
146 bool is_any() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ANY; }
147
149 bool is_none() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NONE; }
150
152 bool is_eq() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EQ; }
153
155 bool is_i31() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_I31; }
156
158 bool is_array() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ARRAY; }
159
163 static_assert(sizeof(ArrayType) == sizeof(wasmtime_array_type_t *));
165 return reinterpret_cast<const ArrayType *>(&ty.of.concrete_array);
166 }
167 return nullptr;
168 }
169
171 bool is_struct() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_STRUCT; }
172
176 static_assert(sizeof(StructType) == sizeof(wasmtime_struct_type_t *));
178 return reinterpret_cast<const StructType *>(&ty.of.concrete_struct);
179 }
180 return nullptr;
181 }
182
184 bool is_exn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXN; }
185
187 bool is_noexn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOEXN; }
188
190 const wasmtime_heaptype_t *capi() const { return &ty; }
191};
192
194class RefType {
196
197public:
199 RefType(const RefType &other) { wasmtime_reftype_clone(&other.ty, &ty); }
201 RefType &operator=(const RefType &other) { return *this = RefType(other); }
204 RefType(RefType &&other) {
205 ty = other.ty;
207 }
211 ty = other.ty;
213 return *this;
214 }
215
219 ty.nullable = nullable;
221 }
222
224 bool nullable() const { return ty.nullable; }
225
227 const HeapType &heaptype() const {
228 static_assert(sizeof(HeapType) == sizeof(wasmtime_heaptype_t));
229 return *reinterpret_cast<const HeapType *>(&ty.heaptype);
230 }
231
233 static RefType externref() { return RefType(true, HeapType::extern_()); }
234
236 static RefType nullexternref() { return RefType(true, HeapType::noextern()); }
237
239 static RefType funcref() { return RefType(true, HeapType::func()); }
240
242 static RefType nullfuncref() { return RefType(true, HeapType::nofunc()); }
243
245 static RefType anyref() { return RefType(true, HeapType::any()); }
246
248 static RefType eqref() { return RefType(true, HeapType::eq()); }
249
251 static RefType i31ref() { return RefType(true, HeapType::i31()); }
252
254 static RefType arrayref() { return RefType(true, HeapType::array()); }
255
257 static RefType structref() { return RefType(true, HeapType::struct_()); }
258
260 static RefType nullref() { return RefType(true, HeapType::none()); }
261
263 static RefType exnref() { return RefType(true, HeapType::exn()); }
264
266 static RefType nullexnref() { return RefType(true, HeapType::noexn()); }
267
269 const wasmtime_reftype_t *capi() const { return &ty; }
270};
271
272inline const RefType *ValType::as_ref() const {
273 static_assert(sizeof(RefType) == sizeof(wasmtime_reftype_t));
274 if (wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_REF) {
275 return reinterpret_cast<const RefType *>(&wasmtime_ty.reftype);
276 }
277 return nullptr;
278}
279
280inline ValType::ValType(const Engine &engine, const RefType &ty)
281 : ref(nullptr) {
282 wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_REF;
283 wasmtime_reftype_clone(ty.capi(), &wasmtime_ty.reftype);
284 ptr.reset(wasmtime_valtype_to_wasm(engine.capi(), &wasmtime_ty));
285 ref = ptr.get();
286}
287
291 ty.reftype.nullable = true;
293 return ValType(&ty);
294}
295
299 ty.reftype.nullable = true;
301 return ValType(&ty);
302}
303
305inline std::ostream &operator<<(std::ostream &os, const HeapType &e) {
306 const wasmtime_heaptype_t *ty = e.capi();
307 switch (ty->kind) {
309 os << "extern";
310 break;
312 os << "noextern";
313 break;
315 os << "func";
316 break;
318 os << "$func";
319 break;
321 os << "nofunc";
322 break;
324 os << "any";
325 break;
327 os << "none";
328 break;
330 os << "eq";
331 break;
333 os << "i31";
334 break;
336 os << "array";
337 break;
339 os << "$array";
340 break;
342 os << "struct";
343 break;
345 os << "$struct";
346 break;
348 os << "exn";
349 break;
351 os << "noexn";
352 break;
353 default:
354 os << "unknown";
355 break;
356 }
357 return os;
358}
359
361inline std::ostream &operator<<(std::ostream &os, const RefType &e) {
362 os << "(ref ";
363 if (e.nullable())
364 os << "null ";
365 os << e.heaptype();
366 os << ")";
367 return os;
368}
369
371inline std::ostream &operator<<(std::ostream &os, const ValType &e) {
372 const wasmtime_valtype_t *ty = e.wasmtime_capi();
373 switch (ty->kind) {
375 os << "i32";
376 break;
378 os << "i64";
379 break;
381 os << "f32";
382 break;
384 os << "f64";
385 break;
387 os << "v128";
388 break;
390 os << *e.as_ref();
391 break;
392 default:
393 os << "unknown";
394 break;
395 }
396 return os;
397}
398
399}; // namespace wasmtime
400
401#endif // WASMTIME_TYPES_VAL_HH
Owned handle to a WebAssembly array type definition.
Definition: types/arrayref.hh:18
Global compilation state in Wasmtime.
Definition: engine.hh:23
Definition: types/func.hh:29
Type information for a WebAssembly function.
Definition: types/func.hh:15
wasm_functype_t * capi_release()
Releases the underlying C API pointer.
Definition: types/func.hh:105
Representation of a heap type in WebAssembly.
Definition: types/val.hh:22
const ArrayType * as_concrete_array() const
If this is a concrete array type, returns the underlying array type.
Definition: types/val.hh:162
static HeapType eq()
Constructor for the eq heap type.
Definition: types/val.hh:81
bool is_i31() const
Is this the abstract i31 heap type?
Definition: types/val.hh:155
HeapType(const StructType &ty)
Constructor for a concrete struct heap type.
Definition: types/val.hh:99
bool is_nofunc() const
Is this the abstract nofunc heap type?
Definition: types/val.hh:143
bool is_none() const
Is this the abstract none heap type?
Definition: types/val.hh:149
HeapType(const HeapType &other)
Copy constructor.
Definition: types/val.hh:34
HeapType(const FuncType &ty)
Constructor for a concrete function heap type.
Definition: types/val.hh:66
HeapType & operator=(const HeapType &other)
Copy assignment operator.
Definition: types/val.hh:36
bool is_func() const
Is this the abstract func heap type?
Definition: types/val.hh:131
bool is_noextern() const
Is this the abstract noextern heap type?
Definition: types/val.hh:126
static HeapType nofunc()
Constructor for the nofunc heap type.
Definition: types/val.hh:72
bool is_exn() const
Is this the abstract exn heap type?
Definition: types/val.hh:184
static HeapType any()
Constructor for the any heap type.
Definition: types/val.hh:75
HeapType(wasmtime_heaptype_t &ty)
Constructor from the raw C API representation.
Definition: types/val.hh:29
HeapType(HeapType &&other)
Move constructor.
Definition: types/val.hh:42
bool is_struct() const
Is this the abstract struct heap type?
Definition: types/val.hh:171
const wasmtime_heaptype_t * capi() const
Returns the underlying C API heap type.
Definition: types/val.hh:190
bool is_any() const
Is this the abstract any heap type?
Definition: types/val.hh:146
static HeapType exn()
Constructor for the exn heap type.
Definition: types/val.hh:105
static HeapType struct_()
Constructor for the struct heap type.
Definition: types/val.hh:96
HeapType & operator=(HeapType &&other)
Move assignment operator.
Definition: types/val.hh:47
bool is_concrete() const
Is this a concrete heap type?
Definition: types/val.hh:111
static HeapType noexn()
Constructor for the noexn heap type.
Definition: types/val.hh:108
static HeapType i31()
Constructor for the i31 heap type.
Definition: types/val.hh:84
bool is_extern() const
Is this the abstract extern heap type?
Definition: types/val.hh:123
std::optional< FuncType::Ref > as_concrete_func() const
If this is a concrete function type, returns the underlying function type.
Definition: types/val.hh:135
static HeapType none()
Constructor for the none heap type.
Definition: types/val.hh:78
HeapType(const ArrayType &ty)
Constructor for a concrete array heap type.
Definition: types/val.hh:90
bool is_eq() const
Is this the abstract eq heap type?
Definition: types/val.hh:152
static HeapType extern_()
Constructor for the extern heap type.
Definition: types/val.hh:55
static HeapType noextern()
Constructor for the noextern heap type.
Definition: types/val.hh:58
const StructType * as_concrete_struct() const
If this is a concrete struct type, returns the underlying struct type.
Definition: types/val.hh:175
bool is_array() const
Is this the abstract array heap type?
Definition: types/val.hh:158
static HeapType array()
Constructor for the array heap type.
Definition: types/val.hh:87
bool is_noexn() const
Is this the abstract noexn heap type?
Definition: types/val.hh:187
static HeapType func()
Constructor for the func heap type.
Definition: types/val.hh:63
Representation of a reference type in WebAssembly.
Definition: types/val.hh:194
static RefType structref()
Convenience constructor for the wasm structref type.
Definition: types/val.hh:257
static RefType eqref()
Convenience constructor for the wasm eqref type.
Definition: types/val.hh:248
const HeapType & heaptype() const
Returns the heap type of this reference type.
Definition: types/val.hh:227
static RefType nullexnref()
Convenience constructor for the wasm nullexnref type.
Definition: types/val.hh:266
RefType(RefType &&other)
Move constructor.
Definition: types/val.hh:204
static RefType externref()
Convenience constructor for the wasm externref type.
Definition: types/val.hh:233
static RefType funcref()
Convenience constructor for the wasm funcref type.
Definition: types/val.hh:239
static RefType nullfuncref()
Convenience constructor for the wasm nullfuncref type.
Definition: types/val.hh:242
static RefType nullexternref()
Convenience constructor for the wasm nullexternref type.
Definition: types/val.hh:236
bool nullable() const
Returns whether this reference type is nullable.
Definition: types/val.hh:224
static RefType arrayref()
Convenience constructor for the wasm arrayref type.
Definition: types/val.hh:254
RefType(bool nullable, const HeapType &heaptype)
Constructs a reference type with the given nullability and heap type.
Definition: types/val.hh:218
RefType & operator=(const RefType &other)
Copy assignment operator.
Definition: types/val.hh:201
const wasmtime_reftype_t * capi() const
Returns the underlying C API reference type.
Definition: types/val.hh:269
RefType & operator=(RefType &&other)
Move assignment operator.
Definition: types/val.hh:209
static RefType nullref()
Convenience constructor for the wasm nullref type.
Definition: types/val.hh:260
static RefType i31ref()
Convenience constructor for the wasm i31ref type.
Definition: types/val.hh:251
RefType(const RefType &other)
Copy constructor.
Definition: types/val.hh:199
static RefType exnref()
Convenience constructor for the wasm exnref type.
Definition: types/val.hh:263
static RefType anyref()
Convenience constructor for the wasm anyref type.
Definition: types/val.hh:245
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
static ValType exnref()
Convenience constructor for the exnref value type.
Definition: types/val.hh:296
const RefType * as_ref() const
Returns if this is a reference type.
Definition: types/val.hh:272
static ValType anyref()
Convenience constructor for the anyref value type.
Definition: types/val.hh:288
const wasmtime_valtype_t * wasmtime_capi() const
Returns the underlying C API representation of this type.
Definition: types/_val_class.hh:175
std::ostream & operator<<(std::ostream &os, const Error &e)
Used to print an error.
Definition: error.hh:58
A WebAssembly heap type.
Definition: types/val.h:72
wasmtime_heaptype_union_t of
Payload of this heap type, with fields indicated by kind.
Definition: types/val.h:77
wasmtime_heaptype_kind_t kind
Discriminant of which heap type this is, and may indicate fields of of to use.
Definition: types/val.h:75
A WebAssembly reference type.
Definition: types/val.h:90
wasmtime_heaptype_t heaptype
The heap type of this reference type.
Definition: types/val.h:94
bool nullable
Whether this reference type is nullable.
Definition: types/val.h:92
A WebAssembly value type.
Definition: types/val.h:127
wasmtime_reftype_t reftype
Payload of this value type, only used with WASMTIME_VALTYPE_KIND_REF.
Definition: types/val.h:132
wasmtime_valtype_kind_t kind
Discriminant of which value type this is.
Definition: types/val.h:129
struct wasmtime_array_type wasmtime_array_type_t
An opaque handle to a WebAssembly array type definition.
Definition: types/arrayref.h:25
struct wasmtime_struct_type wasmtime_struct_type_t
An opaque handle to a WebAssembly struct type definition.
Definition: types/structref.h:81
WASM_API_EXTERN void wasmtime_heaptype_clone(const wasmtime_heaptype_t *ty, wasmtime_heaptype_t *out)
Clones ty into out.
#define WASMTIME_HEAPTYPE_KIND_NONE
Heap type for the abstract none type.
Definition: types/val.h:43
#define WASMTIME_VALTYPE_KIND_I64
The WebAssembly i64 type.
Definition: types/val.h:112
#define WASMTIME_HEAPTYPE_KIND_STRUCT
Heap type for the abstract struct type.
Definition: types/val.h:53
#define WASMTIME_HEAPTYPE_KIND_EXN
Heap type for the abstract exn type.
Definition: types/val.h:57
#define WASMTIME_VALTYPE_KIND_REF
A WebAssembly reference type.
Definition: types/val.h:120
#define WASMTIME_HEAPTYPE_KIND_CONCRETE_STRUCT
Heap type for a concrete struct type.
Definition: types/val.h:55
#define WASMTIME_HEAPTYPE_KIND_ARRAY
Heap type for the abstract array type.
Definition: types/val.h:49
#define WASMTIME_HEAPTYPE_KIND_CONCRETE_ARRAY
Heap type for a concrete array type.
Definition: types/val.h:51
#define WASMTIME_HEAPTYPE_KIND_EQ
Heap type for the abstract eq type.
Definition: types/val.h:45
#define WASMTIME_HEAPTYPE_KIND_EXTERN
Heap type for the abstract extern type.
Definition: types/val.h:31
#define WASMTIME_HEAPTYPE_KIND_I31
Heap type for the abstract i31 type.
Definition: types/val.h:47
WASM_API_EXTERN void wasmtime_heaptype_delete(wasmtime_heaptype_t *ty)
Deletes any payload of ty, if applicable.
#define WASMTIME_HEAPTYPE_KIND_CONCRETE_FUNC
Heap type for a concrete function type.
Definition: types/val.h:37
WASM_API_EXTERN wasm_valtype_t * wasmtime_valtype_to_wasm(const wasm_engine_t *engine, const wasmtime_valtype_t *ty)
Converts ty into a wasm_valtype_t and returns a pointer to it.
#define WASMTIME_VALTYPE_KIND_I32
The WebAssembly i32 type.
Definition: types/val.h:110
WASM_API_EXTERN void wasmtime_reftype_delete(wasmtime_reftype_t *ty)
Deletes any payload of ty, if applicable.
#define WASMTIME_HEAPTYPE_KIND_FUNC
Heap type for the abstract func type.
Definition: types/val.h:35
#define WASMTIME_VALTYPE_KIND_F32
The WebAssembly f32 type.
Definition: types/val.h:114
#define WASMTIME_HEAPTYPE_KIND_NOEXN
Heap type for the abstract noexn type.
Definition: types/val.h:59
WASM_API_EXTERN void wasmtime_reftype_clone(const wasmtime_reftype_t *ty, wasmtime_reftype_t *out)
Clones ty into out.
uint8_t wasmtime_heaptype_kind_t
Discriminant located in wasmtime_heaptype_t.kind
Definition: types/val.h:28
#define WASMTIME_HEAPTYPE_KIND_ANY
Heap type for the abstract any type.
Definition: types/val.h:41
#define WASMTIME_VALTYPE_KIND_V128
The WebAssembly v128 type.
Definition: types/val.h:118
#define WASMTIME_HEAPTYPE_KIND_NOEXTERN
Heap type for the abstract noextern type.
Definition: types/val.h:33
#define WASMTIME_VALTYPE_KIND_F64
The WebAssembly f64 type.
Definition: types/val.h:116
#define WASMTIME_HEAPTYPE_KIND_NOFUNC
Heap type for the abstract nofunc type.
Definition: types/val.h:39
wasmtime_array_type_t * concrete_array
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_ARRAY.
Definition: types/val.h:66
wasm_functype_t * concrete_func
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_FUNC.
Definition: types/val.h:64
wasmtime_struct_type_t * concrete_struct
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_STRUCT.
Definition: types/val.h:68