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:
29 HeapType(const HeapType &other) { wasmtime_heaptype_clone(&other.ty, &ty); }
31 HeapType &operator=(const HeapType &other) {
33 wasmtime_heaptype_clone(&other.ty, &ty);
34 return *this;
35 }
36 ~HeapType() {
37 if (is_concrete())
39 }
41 HeapType(HeapType &&other) {
42 ty = other.ty;
44 }
48 ty = other.ty;
50 return *this;
51 }
52
55
57 static HeapType noextern() {
59 }
60
63
65 HeapType(const FuncType &ty) {
67 this->ty.of.concrete_func = FuncType(ty).capi_release();
68 }
69
72
75
78
81
84
87
89 HeapType(const ArrayType &ty) {
91 this->ty.of.concrete_array = ArrayType(ty).capi_release();
92 }
93
96
98 HeapType(const StructType &ty) {
100 this->ty.of.concrete_struct = StructType(ty).capi_release();
101 }
102
105
107 HeapType(const ExnType &ty) {
109 this->ty.of.concrete_exn = ExnType(ty).capi_release();
110 }
111
114
116 bool is_concrete() const {
117 switch (ty.kind) {
122 return true;
123 default:
124 return false;
125 }
126 }
127
129 bool is_extern() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXTERN; }
130
132 bool is_noextern() const {
134 }
135
137 bool is_func() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_FUNC; }
138
141 std::optional<FuncType::Ref> as_concrete_func() const {
143 return FuncType::Ref(ty.of.concrete_func);
144 }
145 return std::nullopt;
146 }
147
149 bool is_nofunc() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOFUNC; }
150
152 bool is_any() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ANY; }
153
155 bool is_none() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NONE; }
156
158 bool is_eq() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EQ; }
159
161 bool is_i31() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_I31; }
162
164 bool is_array() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_ARRAY; }
165
169 static_assert(sizeof(ArrayType) == sizeof(wasmtime_array_type_t *));
171 return reinterpret_cast<const ArrayType *>(&ty.of.concrete_array);
172 }
173 return nullptr;
174 }
175
177 bool is_struct() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_STRUCT; }
178
182 static_assert(sizeof(StructType) == sizeof(wasmtime_struct_type_t *));
184 return reinterpret_cast<const StructType *>(&ty.of.concrete_struct);
185 }
186 return nullptr;
187 }
188
190 bool is_exn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_EXN; }
191
194 const ExnType *as_concrete_exn() const {
195 static_assert(sizeof(ExnType) == sizeof(wasmtime_exn_type_t *));
197 return reinterpret_cast<const ExnType *>(&ty.of.concrete_exn);
198 }
199 return nullptr;
200 }
201
203 bool is_noexn() const { return ty.kind == WASMTIME_HEAPTYPE_KIND_NOEXN; }
204
206 const wasmtime_heaptype_t *capi() const { return &ty; }
207};
208
210class RefType {
212
213public:
215 RefType(const RefType &other) { wasmtime_reftype_clone(&other.ty, &ty); }
217 RefType &operator=(const RefType &other) {
219 wasmtime_reftype_clone(&other.ty, &ty);
220 return *this;
221 }
224 RefType(RefType &&other) {
225 ty = other.ty;
227 }
231 ty = other.ty;
233 return *this;
234 }
235
239 ty.nullable = nullable;
241 }
242
244 bool nullable() const { return ty.nullable; }
245
247 const HeapType &heaptype() const {
248 static_assert(sizeof(HeapType) == sizeof(wasmtime_heaptype_t));
249 return *reinterpret_cast<const HeapType *>(&ty.heaptype);
250 }
251
253 static RefType externref() { return RefType(true, HeapType::extern_()); }
254
256 static RefType nullexternref() { return RefType(true, HeapType::noextern()); }
257
259 static RefType funcref() { return RefType(true, HeapType::func()); }
260
262 static RefType nullfuncref() { return RefType(true, HeapType::nofunc()); }
263
265 static RefType anyref() { return RefType(true, HeapType::any()); }
266
268 static RefType eqref() { return RefType(true, HeapType::eq()); }
269
271 static RefType i31ref() { return RefType(true, HeapType::i31()); }
272
274 static RefType arrayref() { return RefType(true, HeapType::array()); }
275
277 static RefType structref() { return RefType(true, HeapType::struct_()); }
278
280 static RefType nullref() { return RefType(true, HeapType::none()); }
281
283 static RefType exnref() { return RefType(true, HeapType::exn()); }
284
286 static RefType nullexnref() { return RefType(true, HeapType::noexn()); }
287
289 const wasmtime_reftype_t *capi() const { return &ty; }
290};
291
292inline const RefType *ValType::as_ref() const {
293 static_assert(sizeof(RefType) == sizeof(wasmtime_reftype_t));
294 if (wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_REF) {
295 return reinterpret_cast<const RefType *>(&wasmtime_ty.reftype);
296 }
297 return nullptr;
298}
299
300inline ValType::ValType(const Engine &engine, const RefType &ty)
301 : ref(nullptr) {
302 wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_REF;
303 wasmtime_reftype_clone(ty.capi(), &wasmtime_ty.reftype);
304 ptr.reset(wasmtime_valtype_to_wasm(engine.capi(), &wasmtime_ty));
305 ref = ptr.get();
306}
307
311 ty.reftype.nullable = true;
313 return ValType(&ty);
314}
315
319 ty.reftype.nullable = true;
321 return ValType(&ty);
322}
323
325inline std::ostream &operator<<(std::ostream &os, const HeapType &e) {
326 const wasmtime_heaptype_t *ty = e.capi();
327 switch (ty->kind) {
329 os << "extern";
330 break;
332 os << "noextern";
333 break;
335 os << "func";
336 break;
338 os << "$func";
339 break;
341 os << "nofunc";
342 break;
344 os << "any";
345 break;
347 os << "none";
348 break;
350 os << "eq";
351 break;
353 os << "i31";
354 break;
356 os << "array";
357 break;
359 os << "$array";
360 break;
362 os << "struct";
363 break;
365 os << "$struct";
366 break;
368 os << "exn";
369 break;
371 os << "noexn";
372 break;
373 default:
374 os << "unknown";
375 break;
376 }
377 return os;
378}
379
381inline std::ostream &operator<<(std::ostream &os, const RefType &e) {
382 os << "(ref ";
383 if (e.nullable())
384 os << "null ";
385 os << e.heaptype();
386 os << ")";
387 return os;
388}
389
391inline std::ostream &operator<<(std::ostream &os, const ValType &e) {
392 const wasmtime_valtype_t *ty = e.wasmtime_capi();
393 switch (ty->kind) {
395 os << "i32";
396 break;
398 os << "i64";
399 break;
401 os << "f32";
402 break;
404 os << "f64";
405 break;
407 os << "v128";
408 break;
410 os << *e.as_ref();
411 break;
412 default:
413 os << "unknown";
414 break;
415 }
416 return os;
417}
418
419}; // namespace wasmtime
420
421#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:168
static HeapType eq()
Constructor for the eq heap type.
Definition: types/val.hh:80
bool is_i31() const
Is this the abstract i31 heap type?
Definition: types/val.hh:161
HeapType(const StructType &ty)
Constructor for a concrete struct heap type.
Definition: types/val.hh:98
bool is_nofunc() const
Is this the abstract nofunc heap type?
Definition: types/val.hh:149
bool is_none() const
Is this the abstract none heap type?
Definition: types/val.hh:155
HeapType(const HeapType &other)
Copy constructor.
Definition: types/val.hh:29
HeapType(const FuncType &ty)
Constructor for a concrete function heap type.
Definition: types/val.hh:65
HeapType & operator=(const HeapType &other)
Copy assignment operator.
Definition: types/val.hh:31
bool is_func() const
Is this the abstract func heap type?
Definition: types/val.hh:137
bool is_noextern() const
Is this the abstract noextern heap type?
Definition: types/val.hh:132
static HeapType nofunc()
Constructor for the nofunc heap type.
Definition: types/val.hh:71
bool is_exn() const
Is this the abstract exn heap type?
Definition: types/val.hh:190
static HeapType any()
Constructor for the any heap type.
Definition: types/val.hh:74
HeapType(HeapType &&other)
Move constructor.
Definition: types/val.hh:41
bool is_struct() const
Is this the abstract struct heap type?
Definition: types/val.hh:177
const wasmtime_heaptype_t * capi() const
Returns the underlying C API heap type.
Definition: types/val.hh:206
bool is_any() const
Is this the abstract any heap type?
Definition: types/val.hh:152
static HeapType exn()
Constructor for the exn heap type.
Definition: types/val.hh:104
const ExnType * as_concrete_exn() const
If this is a concrete exception type, returns the underlying exception type.
Definition: types/val.hh:194
static HeapType struct_()
Constructor for the struct heap type.
Definition: types/val.hh:95
HeapType & operator=(HeapType &&other)
Move assignment operator.
Definition: types/val.hh:46
bool is_concrete() const
Is this a concrete heap type?
Definition: types/val.hh:116
static HeapType noexn()
Constructor for the noexn heap type.
Definition: types/val.hh:113
static HeapType i31()
Constructor for the i31 heap type.
Definition: types/val.hh:83
bool is_extern() const
Is this the abstract extern heap type?
Definition: types/val.hh:129
std::optional< FuncType::Ref > as_concrete_func() const
If this is a concrete function type, returns the underlying function type.
Definition: types/val.hh:141
static HeapType none()
Constructor for the none heap type.
Definition: types/val.hh:77
HeapType(const ArrayType &ty)
Constructor for a concrete array heap type.
Definition: types/val.hh:89
bool is_eq() const
Is this the abstract eq heap type?
Definition: types/val.hh:158
static HeapType extern_()
Constructor for the extern heap type.
Definition: types/val.hh:54
static HeapType noextern()
Constructor for the noextern heap type.
Definition: types/val.hh:57
const StructType * as_concrete_struct() const
If this is a concrete struct type, returns the underlying struct type.
Definition: types/val.hh:181
bool is_array() const
Is this the abstract array heap type?
Definition: types/val.hh:164
static HeapType array()
Constructor for the array heap type.
Definition: types/val.hh:86
bool is_noexn() const
Is this the abstract noexn heap type?
Definition: types/val.hh:203
HeapType(const ExnType &ty)
Constructor for a concrete exception heap type.
Definition: types/val.hh:107
static HeapType func()
Constructor for the func heap type.
Definition: types/val.hh:62
Representation of a reference type in WebAssembly.
Definition: types/val.hh:210
static RefType structref()
Convenience constructor for the wasm structref type.
Definition: types/val.hh:277
static RefType eqref()
Convenience constructor for the wasm eqref type.
Definition: types/val.hh:268
const HeapType & heaptype() const
Returns the heap type of this reference type.
Definition: types/val.hh:247
static RefType nullexnref()
Convenience constructor for the wasm nullexnref type.
Definition: types/val.hh:286
RefType(RefType &&other)
Move constructor.
Definition: types/val.hh:224
static RefType externref()
Convenience constructor for the wasm externref type.
Definition: types/val.hh:253
static RefType funcref()
Convenience constructor for the wasm funcref type.
Definition: types/val.hh:259
static RefType nullfuncref()
Convenience constructor for the wasm nullfuncref type.
Definition: types/val.hh:262
static RefType nullexternref()
Convenience constructor for the wasm nullexternref type.
Definition: types/val.hh:256
bool nullable() const
Returns whether this reference type is nullable.
Definition: types/val.hh:244
static RefType arrayref()
Convenience constructor for the wasm arrayref type.
Definition: types/val.hh:274
RefType(bool nullable, const HeapType &heaptype)
Constructs a reference type with the given nullability and heap type.
Definition: types/val.hh:238
RefType & operator=(const RefType &other)
Copy assignment operator.
Definition: types/val.hh:217
const wasmtime_reftype_t * capi() const
Returns the underlying C API reference type.
Definition: types/val.hh:289
RefType & operator=(RefType &&other)
Move assignment operator.
Definition: types/val.hh:229
static RefType nullref()
Convenience constructor for the wasm nullref type.
Definition: types/val.hh:280
static RefType i31ref()
Convenience constructor for the wasm i31ref type.
Definition: types/val.hh:271
RefType(const RefType &other)
Copy constructor.
Definition: types/val.hh:215
static RefType exnref()
Convenience constructor for the wasm exnref type.
Definition: types/val.hh:283
static RefType anyref()
Convenience constructor for the wasm anyref type.
Definition: types/val.hh:265
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:316
const RefType * as_ref() const
Returns if this is a reference type.
Definition: types/val.hh:292
static ValType anyref()
Convenience constructor for the anyref value type.
Definition: types/val.hh:308
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