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_delete(&wasmtime_ty);
97 wasmtime_valtype_clone(&other.wasmtime_ty, &wasmtime_ty);
98 return *this;
99 }
100 ~ValType() { wasmtime_valtype_delete(&wasmtime_ty); }
102 ValType(ValType &&other) : ptr(std::move(other.ptr)), ref(ptr.get()) {
103 wasmtime_ty = other.wasmtime_ty;
104 other.ref = nullptr;
105 other.wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_I32;
106 }
109 ptr = std::move(other.ptr);
110 ref = ptr.get();
111 wasmtime_ty = other.wasmtime_ty;
112 other.ref = nullptr;
113 other.wasmtime_ty.kind = WASMTIME_VALTYPE_KIND_I32;
114 return *this;
115 }
116
118 static ValType i32() { return ValType(wasm_valtype_new(WASM_I32)); }
119
121 static ValType i64() { return ValType(wasm_valtype_new(WASM_I64)); }
122
124 static ValType f32() { return ValType(wasm_valtype_new(WASM_F32)); }
125
127 static ValType f64() { return ValType(wasm_valtype_new(WASM_F64)); }
128
130 static ValType v128() {
133 return ValType(&ty);
134 }
135
137 static ValType funcref() { return ValType(wasm_valtype_new(WASM_FUNCREF)); }
138
141 return ValType(wasm_valtype_new(WASM_EXTERNREF));
142 }
143
145 static ValType anyref();
146
148 static ValType exnref();
149
151 ValType(const Engine &engine, const RefType &ref_type);
152
155 Ref *operator->() { return &ref; }
158 Ref *operator*() { return &ref; }
159
161 bool operator==(const Ref &other) const {
162 return wasmtime_wasm_valtype_equal(ptr.get(), other.ptr);
163 }
164
166 bool operator==(const ValType &other) const {
167 return wasmtime_wasm_valtype_equal(ptr.get(), other.ptr.get());
168 }
169
171 const wasmtime_valtype_t *wasmtime_capi() const { return &wasmtime_ty; }
172
174 const wasm_valtype_t *capi() const { return ptr.get(); }
175
177 bool is_i32() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_I32; }
178
180 bool is_i64() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_I64; }
181
183 bool is_f32() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_F32; }
184
186 bool is_f64() const { return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_F64; }
187
189 bool is_v128() const {
190 return wasmtime_ty.kind == WASMTIME_VALTYPE_KIND_V128;
191 }
192
194 const RefType *as_ref() const;
195};
196
197}; // namespace wasmtime
198
199#endif // WASMTIME_TYPES_VAL_CLASS_HH
Global compilation state in Wasmtime.
Definition: engine.hh:22
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:210
Type information about a WebAssembly table.
Definition: types/table.hh:16
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:127
static ValType v128()
Convenience constructor for the v128 value type.
Definition: types/_val_class.hh:130
static ValType exnref()
Convenience constructor for the exnref value type.
Definition: types/val.hh:316
bool is_i64() const
Returns if this is the i64 wasm type.
Definition: types/_val_class.hh:180
bool is_f32() const
Returns if this is the f32 wasm type.
Definition: types/_val_class.hh:183
bool is_f64() const
Returns if this is the f64 wasm type.
Definition: types/_val_class.hh:186
static ValType i64()
Convenience constructor for the i64 value type.
Definition: types/_val_class.hh:121
const RefType * as_ref() const
Returns if this is a reference type.
Definition: types/val.hh:292
const wasm_valtype_t * capi() const
Returns the underlying C API representation of this type.
Definition: types/_val_class.hh:174
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:189
static ValType i32()
Convenience constructor for the i32 value type.
Definition: types/_val_class.hh:118
static ValType f32()
Convenience constructor for the f32 value type.
Definition: types/_val_class.hh:124
Ref * operator->()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/_val_class.hh:155
Ref * operator*()
Returns the underlying Ref, a non-owning reference pointing to this instance.
Definition: types/_val_class.hh:158
ValType & operator=(ValType &&other)
Moves the memory owned by another value type into this one.
Definition: types/_val_class.hh:108
ValType(ValType &&other)
Moves the memory owned by another value type into this one.
Definition: types/_val_class.hh:102
bool is_i32() const
Returns if this is the i32 wasm type.
Definition: types/_val_class.hh:177
static ValType funcref()
Convenience constructor for the funcref value type.
Definition: types/_val_class.hh:137
bool operator==(const Ref &other) const
Equality operator, compares the underlying types for equality.
Definition: types/_val_class.hh:161
static ValType externref()
Convenience constructor for the externref value type.
Definition: types/_val_class.hh:140
static ValType anyref()
Convenience constructor for the anyref value type.
Definition: types/val.hh:308
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:171
bool operator==(const ValType &other) const
Equality operator, compares the underlying types for equality.
Definition: types/_val_class.hh:166
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:131
wasmtime_valtype_kind_t kind
Discriminant of which value type this is.
Definition: types/val.h:133
#define WASMTIME_VALTYPE_KIND_I64
The WebAssembly i64 type.
Definition: types/val.h:116
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_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:118
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:122
#define WASMTIME_VALTYPE_KIND_F64
The WebAssembly f64 type.
Definition: types/val.h:120
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.