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) {
38 wasmtime_heaptype_clone(&other.ty, &ty);
39 return *this;
40 }
41 ~HeapType() {
42 if (is_concrete())
44 }
46 HeapType(HeapType &&other) {
47 ty = other.ty;
49 }
53 ty = other.ty;
55 return *this;
56 }
57
60
62 static HeapType noextern() {
64 }
65
68
70 HeapType(const FuncType &ty) {
72 this->ty.of.concrete_func = FuncType(ty).capi_release();
73 }
74
77
80
83
86
89
92
94 HeapType(const ArrayType &ty) {
96 this->ty.of.concrete_array = ArrayType(ty).capi_release();
97 }
98
101
103 HeapType(const StructType &ty) {
105 this->ty.of.concrete_struct = StructType(ty).capi_release();
106 }
107
110
112 HeapType(const ExnType &ty) {
114 this->ty.of.concrete_exn = ExnType(ty).capi_release();
115 }
116
119
121 bool is_concrete() const {
122 switch (ty.kind) {
127 return true;
128 default:
129 return false;
130 }
131 }
132
134 bool is_extern() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXTERN; }
135
137 bool is_noextern() const {
139 }
140
142 bool is_func() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_FUNC; }
143
146 std::optional<FuncType::Ref> as_concrete_func() const {
148 return FuncType::Ref(ty.of.concrete_func);
149 }
150 return std::nullopt;
151 }
152
154 bool is_nofunc() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOFUNC; }
155
157 bool is_any() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ANY; }
158
160 bool is_none() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NONE; }
161
163 bool is_eq() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EQ; }
164
166 bool is_i31() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_I31; }
167
169 bool is_array() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ARRAY; }
170
174 static_assert(sizeof(ArrayType) == sizeof(wasmtime_array_type_t *));
176 return reinterpret_cast<const ArrayType *>(&ty.of.concrete_array);
177 }
178 return nullptr;
179 }
180
182 bool is_struct() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_STRUCT; }
183
187 static_assert(sizeof(StructType) == sizeof(wasmtime_struct_type_t *));
189 return reinterpret_cast<const StructType *>(&ty.of.concrete_struct);
190 }
191 return nullptr;
192 }
193
195 bool is_exn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXN; }
196
199 const ExnType *as_concrete_exn() const {
200 static_assert(sizeof(ExnType) == sizeof(wasmtime_exn_type_t *));
202 return reinterpret_cast<const ExnType *>(&ty.of.concrete_exn);
203 }
204 return nullptr;
205 }
206
208 bool is_noexn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOEXN; }
209
211 const wasmtime_heaptype_t *capi() const { return &ty; }
212};
213
215class RefType {
217
218public:
220 RefType(const RefType &other) { wasmtime_reftype_clone(&other.ty, &ty); }
222 RefType &operator=(const RefType &other) {
224 wasmtime_reftype_clone(&other.ty, &ty);
225 return *this;
226 }
229 RefType(RefType &&other) {
230 ty = other.ty;
232 }
236 ty = other.ty;
238 return *this;
239 }
240
244 ty.nullable = nullable;
246 }
247
249 bool nullable() const { return ty.nullable; }
250
252 const HeapType &heaptype() const {
253 static_assert(sizeof(HeapType) == sizeof(wasmtime_heaptype_t));
254 return *reinterpret_cast<const HeapType *>(&ty.heaptype);
255 }
256
258 static RefType externref() { return RefType(true, HeapType::extern_()); }
259
261 static RefType nullexternref() { return RefType(true, HeapType::noextern()); }
262
264 static RefType funcref() { return RefType(true, HeapType::func()); }
265
267 static RefType nullfuncref() { return RefType(true, HeapType::nofunc()); }
268
270 static RefType anyref() { return RefType(true, HeapType::any()); }
271
273 static RefType eqref() { return RefType(true, HeapType::eq()); }
274
276 static RefType i31ref() { return RefType(true, HeapType::i31()); }
277
279 static RefType arrayref() { return RefType(true, HeapType::array()); }
280
282 static RefType structref() { return RefType(true, HeapType::struct_()); }
283
285 static RefType nullref() { return RefType(true, HeapType::none()); }
286
288 static RefType exnref() { return RefType(true, HeapType::exn()); }
289
291 static RefType nullexnref() { return RefType(true, HeapType::noexn()); }
292
294 const wasmtime_reftype_t *capi() const { return &ty; }
295};
296
297inline const RefType *ValType::as_ref() const {
298 static_assert(sizeof(RefType) == sizeof(wasmtime_reftype_t));
299 if (wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_REF) {
300 return reinterpret_cast<const RefType *>(&wasmtime_ty.reftype);
301 }
302 return nullptr;
303}
304
305inline ValType::ValType(const Engine &engine, const RefType &ty)
306 : ref(nullptr) {
307 wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_REF;
308 wasmtime_reftype_clone(ty.capi(), &wasmtime_ty.reftype);
309 ptr.reset(wasmtime_valtype_to_wasm(engine.capi(), &wasmtime_ty));
310 ref = ptr.get();
311}
312
316 ty.reftype.nullable = true;
318 return ValType(&ty);
319}
320
324 ty.reftype.nullable = true;
326 return ValType(&ty);
327}
328
330inline std::ostream &operator<<(std::ostream &os, const HeapType &e) {
331 const wasmtime_heaptype_t *ty = e.capi();
332 switch (ty->kind) {
334 os << "extern";
335 break;
337 os << "noextern";
338 break;
340 os << "func";
341 break;
343 os << "$func";
344 break;
346 os << "nofunc";
347 break;
349 os << "any";
350 break;
352 os << "none";
353 break;
355 os << "eq";
356 break;
358 os << "i31";
359 break;
361 os << "array";
362 break;
364 os << "$array";
365 break;
367 os << "struct";
368 break;
370 os << "$struct";
371 break;
373 os << "exn";
374 break;
376 os << "noexn";
377 break;
378 default:
379 os << "unknown";
380 break;
381 }
382 return os;
383}
384
386inline std::ostream &operator<<(std::ostream &os, const RefType &e) {
387 os << "(ref ";
388 if (e.nullable())
389 os << "null ";
390 os << e.heaptype();
391 os << ")";
392 return os;
393}
394
396inline std::ostream &operator<<(std::ostream &os, const ValType &e) {
397 const wasmtime_valtype_t *ty = e.wasmtime_capi();
398 switch (ty->kind) {
400 os << "i32";
401 break;
403 os << "i64";
404 break;
406 os << "f32";
407 break;
409 os << "f64";
410 break;
412 os << "v128";
413 break;
415 os << *e.as_ref();
416 break;
417 default:
418 os << "unknown";
419 break;
420 }
421 return os;
422}
423
424}; // namespace wasmtime
425
426#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:22
Owned handle to a WebAssembly exception type definition.
Definition: types/exnref.hh:22
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:104
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:173
static HeapType eq()
Constructor for the eq heap type.
Definition: types/val.hh:85
bool is_i31() const
Is this the abstract i31 heap type?
Definition: types/val.hh:166
HeapType(const StructType &ty)
Constructor for a concrete struct heap type.
Definition: types/val.hh:103
bool is_nofunc() const
Is this the abstract nofunc heap type?
Definition: types/val.hh:154
bool is_none() const
Is this the abstract none heap type?
Definition: types/val.hh:160
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:70
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:142
bool is_noextern() const
Is this the abstract noextern heap type?
Definition: types/val.hh:137
static HeapType nofunc()
Constructor for the nofunc heap type.
Definition: types/val.hh:76
bool is_exn() const
Is this the abstract exn heap type?
Definition: types/val.hh:195
static HeapType any()
Constructor for the any heap type.
Definition: types/val.hh:79
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:46
bool is_struct() const
Is this the abstract struct heap type?
Definition: types/val.hh:182
const wasmtime_heaptype_t * capi() const
Returns the underlying C API heap type.
Definition: types/val.hh:211
bool is_any() const
Is this the abstract any heap type?
Definition: types/val.hh:157
static HeapType exn()
Constructor for the exn heap type.
Definition: types/val.hh:109
const ExnType * as_concrete_exn() const
If this is a concrete exception type, returns the underlying exception type.
Definition: types/val.hh:199
static HeapType struct_()
Constructor for the struct heap type.
Definition: types/val.hh:100
HeapType & operator=(HeapType &&other)
Move assignment operator.
Definition: types/val.hh:51
bool is_concrete() const
Is this a concrete heap type?
Definition: types/val.hh:121
static HeapType noexn()
Constructor for the noexn heap type.
Definition: types/val.hh:118
static HeapType i31()
Constructor for the i31 heap type.
Definition: types/val.hh:88
bool is_extern() const
Is this the abstract extern heap type?
Definition: types/val.hh:134
std::optional< FuncType::Ref > as_concrete_func() const
If this is a concrete function type, returns the underlying function type.
Definition: types/val.hh:146
static HeapType none()
Constructor for the none heap type.
Definition: types/val.hh:82
HeapType(const ArrayType &ty)
Constructor for a concrete array heap type.
Definition: types/val.hh:94
bool is_eq() const
Is this the abstract eq heap type?
Definition: types/val.hh:163
static HeapType extern_()
Constructor for the extern heap type.
Definition: types/val.hh:59
static HeapType noextern()
Constructor for the noextern heap type.
Definition: types/val.hh:62
const StructType * as_concrete_struct() const
If this is a concrete struct type, returns the underlying struct type.
Definition: types/val.hh:186
bool is_array() const
Is this the abstract array heap type?
Definition: types/val.hh:169
static HeapType array()
Constructor for the array heap type.
Definition: types/val.hh:91
bool is_noexn() const
Is this the abstract noexn heap type?
Definition: types/val.hh:208
HeapType(const ExnType &ty)
Constructor for a concrete exception heap type.
Definition: types/val.hh:112
static HeapType func()
Constructor for the func heap type.
Definition: types/val.hh:67
Representation of a reference type in WebAssembly.
Definition: types/val.hh:215
static RefType structref()
Convenience constructor for the wasm structref type.
Definition: types/val.hh:282
static RefType eqref()
Convenience constructor for the wasm eqref type.
Definition: types/val.hh:273
const HeapType & heaptype() const
Returns the heap type of this reference type.
Definition: types/val.hh:252
static RefType nullexnref()
Convenience constructor for the wasm nullexnref type.
Definition: types/val.hh:291
RefType(RefType &&other)
Move constructor.
Definition: types/val.hh:229
static RefType externref()
Convenience constructor for the wasm externref type.
Definition: types/val.hh:258
static RefType funcref()
Convenience constructor for the wasm funcref type.
Definition: types/val.hh:264
static RefType nullfuncref()
Convenience constructor for the wasm nullfuncref type.
Definition: types/val.hh:267
static RefType nullexternref()
Convenience constructor for the wasm nullexternref type.
Definition: types/val.hh:261
bool nullable() const
Returns whether this reference type is nullable.
Definition: types/val.hh:249
static RefType arrayref()
Convenience constructor for the wasm arrayref type.
Definition: types/val.hh:279
RefType(bool nullable, const HeapType &heaptype)
Constructs a reference type with the given nullability and heap type.
Definition: types/val.hh:243
RefType & operator=(const RefType &other)
Copy assignment operator.
Definition: types/val.hh:222
const wasmtime_reftype_t * capi() const
Returns the underlying C API reference type.
Definition: types/val.hh:294
RefType & operator=(RefType &&other)
Move assignment operator.
Definition: types/val.hh:234
static RefType nullref()
Convenience constructor for the wasm nullref type.
Definition: types/val.hh:285
static RefType i31ref()
Convenience constructor for the wasm i31ref type.
Definition: types/val.hh:276
RefType(const RefType &other)
Copy constructor.
Definition: types/val.hh:220
static RefType exnref()
Convenience constructor for the wasm exnref type.
Definition: types/val.hh:288
static RefType anyref()
Convenience constructor for the wasm anyref type.
Definition: types/val.hh:270
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
static ValType exnref()
Convenience constructor for the exnref value type.
Definition: types/val.hh:321
const RefType * as_ref() const
Returns if this is a reference type.
Definition: types/val.hh:297
static ValType anyref()
Convenience constructor for the anyref value type.
Definition: types/val.hh:313
const wasmtime_valtype_t * wasmtime_capi() const
Returns the underlying C API representation of this type.
Definition: types/_val_class.hh:171
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:76
wasmtime_heaptype_union_t of
Payload of this heap type, with fields indicated by kind.
Definition: types/val.h:81
wasmtime_heaptype_kind_t kind
Discriminant of which heap type this is, and may indicate fields of of to use.
Definition: types/val.h:79
A WebAssembly reference type.
Definition: types/val.h:94
wasmtime_heaptype_t heaptype
The heap type of this reference type.
Definition: types/val.h:98
bool nullable
Whether this reference type is nullable.
Definition: types/val.h:96
A WebAssembly value type.
Definition: types/val.h:131
wasmtime_reftype_t reftype
Payload of this value type, only used with WASMTIME_VALTYPE_KIND_REF.
Definition: types/val.h:136
wasmtime_valtype_kind_t kind
Discriminant of which value type this is.
Definition: types/val.h:133
struct wasmtime_array_type wasmtime_array_type_t
An opaque handle to a WebAssembly array type definition.
Definition: types/arrayref.h:25
struct wasmtime_exn_type wasmtime_exn_type_t
A type of a WebAssembly exception.
Definition: types/exnref.h:16
struct wasmtime_struct_type wasmtime_struct_type_t
An opaque handle to a WebAssembly struct type definition.
Definition: types/structref.h:80
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:116
#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:124
#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:114
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:118
#define WASMTIME_HEAPTYPE_KIND_NOEXN
Heap type for the abstract noexn type.
Definition: types/val.h:61
WASM_API_EXTERN void wasmtime_reftype_clone(const wasmtime_reftype_t *ty, wasmtime_reftype_t *out)
Clones ty into out.
#define WASMTIME_HEAPTYPE_KIND_CONCRETE_EXN
Heap type for a concrete exception type.
Definition: types/val.h:59
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:122
#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:120
#define WASMTIME_HEAPTYPE_KIND_NOFUNC
Heap type for the abstract nofunc type.
Definition: types/val.h:39
wasmtime_exn_type_t * concrete_exn
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_EXN.
Definition: types/val.h:72
wasmtime_array_type_t * concrete_array
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_ARRAY.
Definition: types/val.h:68
wasm_functype_t * concrete_func
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_FUNC.
Definition: types/val.h:66
wasmtime_struct_type_t * concrete_struct
Used with WASMTIME_HEAPTYPE_KIND_CONCRETE_STRUCT.
Definition: types/val.h:70