Wasmtime
types/_val_class.hh
1#ifndef WASMTIME_TYPES_VAL_CLASS_HH
2#define WASMTIME_TYPES_VAL_CLASS_HH
3
4#include <memory>
5#include <wasm.h>
6#include <wasmtime/engine.hh>
8#include <wasmtime/val.h>
9
10namespace wasmtime {
11
12class RefType;
13
19class ValType {
20 friend class TableType;
21 friend class GlobalType;
22 friend class FuncType;
23 struct deleter {
24 void operator()(wasm_valtype_t *p) const { wasm_valtype_delete(p); }
25 };
26
27 std::unique_ptr<wasm_valtype_t, deleter> ptr;
28
29public:
32 class Ref {
33 friend class ValType;
34
35 const wasm_valtype_t *ptr;
36
37 public:
39 Ref(const wasm_valtype_t *ptr) : ptr(ptr) {}
41 Ref(const ValType &ty) : Ref(ty.ptr.get()) {}
42
44 bool operator==(const Ref &other) const {
45 return wasmtime_wasm_valtype_equal(ptr, other.ptr);
46 }
47 };
48
51 class ListRef {
52 const wasm_valtype_vec_t *list;
53
54 public:
56 ListRef(const wasm_valtype_vec_t *list) : list(list) {}
57
59 typedef const Ref *iterator;
60
62 iterator begin() const {
63 return reinterpret_cast<Ref *>(&list->data[0]); // NOLINT
64 }
65
67 iterator end() const {
68 return reinterpret_cast<Ref *>(&list->data[list->size]); // NOLINT
69 }
70
72 size_t size() const { return list->size; }
73 };
74
75private:
76 Ref ref;
77 wasmtime_valtype_t wasmtime_ty;
78 ValType(wasm_valtype_t *ptr) : ptr(ptr), ref(ptr) {
79 wasmtime_valtype_new(ptr, &wasmtime_ty);
80 }
81 ValType(wasmtime_valtype_t *ty)
82 : ptr(wasmtime_valtype_to_wasm(nullptr, ty)), ref(nullptr),
83 wasmtime_ty(*ty) {
84 ref = ptr.get();
85 }
86
87public:
89 ValType(Ref other) : ValType(wasm_valtype_copy(other.ptr)) {}
91 ValType(const ValType &other) : ValType(wasm_valtype_copy(other.ptr.get())) {}
93 ValType &operator=(const ValType &other) {
94 ptr.reset(wasm_valtype_copy(other.ptr.get()));
95 ref = ptr.get();
96 wasmtime_valtype_t cloned;
97 wasmtime_valtype_clone(&other.wasmtime_ty, &cloned);
98 wasmtime_valtype_delete(&wasmtime_ty);
99 wasmtime_ty = cloned;
100 return *this;
101 }
102 ~ValType() { wasmtime_valtype_delete(&wasmtime_ty); }
104 ValType(ValType &&other) : ptr(std::move(other.ptr)), ref(ptr.get()) {
105 wasmtime_ty = other.wasmtime_ty;
106 other.ref = nullptr;
107 other.wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_I32;
108 }
111 ptr = std::move(other.ptr);
112 ref = ptr.get();
113 wasmtime_valtype_t old = wasmtime_ty;
114 wasmtime_ty = other.wasmtime_ty;
115 other.ref = nullptr;
116 other.wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_I32;
118 return *this;
119 }
120
122 static ValType i32() { return ValType(wasm_valtype_new(WASM_I32)); }
123
125 static ValType i64() { return ValType(wasm_valtype_new(WASM_I64)); }
126
128 static ValType f32() { return ValType(wasm_valtype_new(WASM_F32)); }
129
131 static ValType f64() { return ValType(wasm_valtype_new(WASM_F64)); }
132
134 static ValType v128() {
137 return ValType(&ty);
138 }
139
141 static ValType funcref() { return ValType(wasm_valtype_new(WASM_FUNCREF)); }
142
145 return ValType(wasm_valtype_new(WASM_EXTERNREF));
146 }
147
149 static ValType anyref();
150
152 static ValType exnref();
153
155 ValType(const Engine &engine, const RefType &ref_type);
156
159 Ref *operator->() { return &ref; }
162 Ref *operator*() { return &ref; }
163
165 bool operator==(const Ref &other) const {
166 return wasmtime_wasm_valtype_equal(ptr.get(), other.ptr);
167 }
168
170 bool operator==(const ValType &other) const {
171 return wasmtime_wasm_valtype_equal(ptr.get(), other.ptr.get());
172 }
173
175 const wasmtime_valtype_t *wasmtime_capi() const { return &wasmtime_ty; }
176
178 const wasm_valtype_t *capi() const { return ptr.get(); }
179
181 bool is_i32() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_I32; }
182
184 bool is_i64() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_I64; }
185
187 bool is_f32() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_F32; }
188
190 bool is_f64() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_F64; }
191
193 bool is_v128() const {
194 return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_V128;
195 }
196
198 const RefType *as_ref() const;
199};
200
201}; // namespace wasmtime
202
203#endif // WASMTIME_TYPES_VAL_CLASS_HH
Global compilation state in Wasmtime.
Definition: engine.hh:23
Type information for a WebAssembly function.
Definition: types/func.hh:15
Type information about a WebAssembly global.
Definition: types/global.hh:15
Representation of a reference type in WebAssembly.
Definition: types/val.hh:194
Type information about a WebAssembly table.
Definition: types/table.hh:18
Non-owning reference to a list of ValType instances. Must not be used after the original owner is del...
Definition: types/_val_class.hh:51
ListRef(const wasm_valtype_vec_t *list)
Creates a list from the raw underlying C API.
Definition: types/_val_class.hh:56
size_t size() const
Returns how many types are in this list.
Definition: types/_val_class.hh:72
iterator begin() const
Pointer to the beginning of iteration.
Definition: types/_val_class.hh:62
iterator end() const
Pointer to the end of iteration.
Definition: types/_val_class.hh:67
const Ref * iterator
This list iterates over a list of ValType::Ref instances.
Definition: types/_val_class.hh:59
Non-owning reference to a ValType, must not be used after the original ValType is deleted.
Definition: types/_val_class.hh:32
Ref(const ValType &ty)
Copy constructor.
Definition: types/_val_class.hh:41
Ref(const wasm_valtype_t *ptr)
Instantiates from the raw C API representation.
Definition: types/_val_class.hh:39
bool operator==(const Ref &other) const
Tests if this type is equal to another.
Definition: types/_val_class.hh:44
Type information about a WebAssembly value.
Definition: types/_val_class.hh:19
static ValType f64()
Convenience constructor for the f64 value type.
Definition: types/_val_class.hh:131
static ValType v128()
Convenience constructor for the v128 value type.
Definition: types/_val_class.hh:134
static ValType exnref()
Convenience constructor for the exnref value type.
Definition: types/val.hh:296
bool is_i64() const
Returns if this is the i64 wasm type.
Definition: types/_val_class.hh:184
bool is_f32() const
Returns if this is the f32 wasm type.
Definition: types/_val_class.hh:187
bool is_f64() const
Returns if this is the f64 wasm type.
Definition: types/_val_class.hh:190
static ValType i64()
Convenience constructor for the i64 value type.
Definition: types/_val_class.hh:125
const RefType * as_ref() const
Returns if this is a reference type.
Definition: types/val.hh:272
const wasm_valtype_t * capi() const
Returns the underlying C API representation of this type.
Definition: types/_val_class.hh:178
ValType & operator=(const ValType &other)
Copies the contents of another type into this one.
Definition: types/_val_class.hh:93
bool is_v128() const
Returns if this is the v128 wasm type.
Definition: types/_val_class.hh:193
static ValType i32()
Convenience constructor for the i32 value type.
Definition: types/_val_class.hh:122
static ValType f32()
Convenience constructor for the f32 value type.
Definition: types/_val_class.hh:128
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/_val_class.hh:159
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/_val_class.hh:162
ValType & operator=(ValType &&other)
Moves the memory owned by another value type into this one.
Definition: types/_val_class.hh:110
ValType(ValType &&other)
Moves the memory owned by another value type into this one.
Definition: types/_val_class.hh:104
bool is_i32() const
Returns if this is the i32 wasm type.
Definition: types/_val_class.hh:181
static ValType funcref()
Convenience constructor for the funcref value type.
Definition: types/_val_class.hh:141
bool operator==(const Ref &other) const
Equality operator, compares the underlying types for equality.
Definition: types/_val_class.hh:165
static ValType externref()
Convenience constructor for the externref value type.
Definition: types/_val_class.hh:144
static ValType anyref()
Convenience constructor for the anyref value type.
Definition: types/val.hh:288
ValType(const ValType &other)
Copies one type to a new one.
Definition: types/_val_class.hh:91
ValType(Ref other)
Copies a Ref to a new owned value.
Definition: types/_val_class.hh:89
const wasmtime_valtype_t * wasmtime_capi() const
Returns the underlying C API representation of this type.
Definition: types/_val_class.hh:175
bool operator==(const ValType &other) const
Equality operator, compares the underlying types for equality.
Definition: types/_val_class.hh:170
An object representing the type of a value.
A list of wasm_valtype_t values.
Definition: wasm.h:183
wasm_valtype_t ** data
Pointer to the base of this vector.
Definition: wasm.h:183
size_t size
Length of this vector.
Definition: wasm.h:183
A WebAssembly value type.
Definition: types/val.h:127
wasmtime_valtype_kind_t kind
Discriminant of which value type this is.
Definition: types/val.h:129
#define WASMTIME_VALTYPE_KIND_I64
The WebAssembly i64 type.
Definition: types/val.h:112
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_valtype_new(const wasm_valtype_t *ty, wasmtime_valtype_t *out)
Creates a new type in out from the type in ty.
WASM_API_EXTERN void wasmtime_valtype_clone(const wasmtime_valtype_t *ty, wasmtime_valtype_t *out)
Clones ty into out.
#define WASMTIME_VALTYPE_KIND_F32
The WebAssembly f32 type.
Definition: types/val.h:114
WASM_API_EXTERN bool wasmtime_wasm_valtype_equal(const wasm_valtype_t *a, const wasm_valtype_t *b)
Returns whether a is logically equal to b.
WASM_API_EXTERN void wasmtime_valtype_delete(wasmtime_valtype_t *ty)
Deletes any payload of ty, if applicable.
#define WASMTIME_VALTYPE_KIND_V128
The WebAssembly v128 type.
Definition: types/val.h:118
#define WASMTIME_VALTYPE_KIND_F64
The WebAssembly f64 type.
Definition: types/val.h:116
void wasm_valtype_delete(wasm_valtype_t *)
Deletes a type.
wasm_valtype_t * wasm_valtype_copy(const wasm_valtype_t *)
Creates a new value which matches the provided one.
wasm_valtype_t * wasm_valtype_new(wasm_valkind_t)
Creates a new value type from the specified kind.