Wasmtime
_val_class.hh
1#ifndef WASMTIME_VAL_CLASS_HH
2#define WASMTIME_VAL_CLASS_HH
3
4#include <optional>
5#include <wasmtime/_anyref_class.hh>
6#include <wasmtime/_arrayref_class.hh>
7#include <wasmtime/_eqref_class.hh>
8#include <wasmtime/_externref_class.hh>
9#include <wasmtime/_func_class.hh>
10#include <wasmtime/_store_class.hh>
11#include <wasmtime/_structref_class.hh>
12#include <wasmtime/types/val.hh>
13#include <wasmtime/val.h>
14
15namespace wasmtime {
16
18struct V128 {
21
23 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
24
26 V128(const wasmtime_v128 &v) : v128{} {
27 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
28 }
29};
30
31enum class ValKind {
32 I32 = WASMTIME_I32,
33 I64 = WASMTIME_I64,
34 F32 = WASMTIME_F32,
35 F64 = WASMTIME_F64,
36 V128 = WASMTIME_V128,
37 FuncRef = WASMTIME_FUNCREF,
38 ExternRef = WASMTIME_EXTERNREF,
39 AnyRef = WASMTIME_ANYREF,
40 ExnRef = WASMTIME_EXNREF,
41};
42
53class Val {
54 friend class Global;
55 friend class Table;
56 friend class Func;
57
59
60 Val() : val{} {
61 val.kind = WASMTIME_I32;
62 val.of.i32 = 0;
63 }
64
65public:
67 Val(wasmtime_val_t val) : val(val) {}
68
70 Val(int32_t i32) : val{} {
71 val.kind = WASMTIME_I32;
72 val.of.i32 = i32;
73 }
75 Val(int64_t i64) : val{} {
76 val.kind = WASMTIME_I64;
77 val.of.i64 = i64;
78 }
80 Val(float f32) : val{} {
81 val.kind = WASMTIME_F32;
82 val.of.f32 = f32;
83 }
85 Val(double f64) : val{} {
86 val.kind = WASMTIME_F64;
87 val.of.f64 = f64;
88 }
90 Val(const V128 &v128) : val{} {
91 val.kind = WASMTIME_V128;
92 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
93 }
95 Val(std::optional<Func> func);
97 Val(Func func);
98#ifdef WASMTIME_FEATURE_GC
100 Val(std::optional<ExternRef> ptr);
102 Val(std::optional<AnyRef> ptr);
105 Val(ExternRef ptr);
108 Val(AnyRef ptr);
109#endif
110
112 Val(const Val &other) { wasmtime_val_clone(&other.val, &val); }
113
115 Val &operator=(const Val &other) {
117 wasmtime_val_clone(&other.val, &val);
118 return *this;
119 }
120
122 Val(Val &&other) {
123 val = other.val;
124 other.val.kind = WASMTIME_I32;
125 other.val.of.i32 = 0;
126 }
127
129 Val &operator=(Val &&other) {
131 val = other.val;
132 other.val.kind = WASMTIME_I32;
133 other.val.of.i32 = 0;
134 return *this;
135 }
136
139
141 ValKind kind() const {
142 switch (val.kind) {
143 case WASMTIME_I32:
144 return ValKind::I32;
145 case WASMTIME_I64:
146 return ValKind::I64;
147 case WASMTIME_F32:
148 return ValKind::F32;
149 case WASMTIME_F64:
150 return ValKind::F64;
151 case WASMTIME_FUNCREF:
152 return ValKind::FuncRef;
154 return ValKind::ExternRef;
155 case WASMTIME_ANYREF:
156 return ValKind::AnyRef;
157 case WASMTIME_EXNREF:
158 return ValKind::ExnRef;
159 case WASMTIME_V128:
160 return ValKind::V128;
161 }
162 std::abort();
163 }
164
167 int32_t i32() const {
168 if (val.kind != WASMTIME_I32) {
169 std::abort();
170 }
171 return val.of.i32;
172 }
173
176 int64_t i64() const {
177 if (val.kind != WASMTIME_I64) {
178 std::abort();
179 }
180 return val.of.i64;
181 }
182
185 float f32() const {
186 if (val.kind != WASMTIME_F32) {
187 std::abort();
188 }
189 return val.of.f32;
190 }
191
194 double f64() const {
195 if (val.kind != WASMTIME_F64) {
196 std::abort();
197 }
198 return val.of.f64;
199 }
200
203 V128 v128() const {
204 if (val.kind != WASMTIME_V128) {
205 std::abort();
206 }
207 return val.of.v128;
208 }
209
210#ifdef WASMTIME_FEATURE_GC
216 std::optional<ExternRef> externref() const;
217
223 std::optional<AnyRef> anyref() const;
224#endif
225
231 std::optional<Func> funcref() const;
232
235
239 const Raw *capi() const { return &val; }
240
244 Raw *capi() { return &val; }
245};
246
247} // namespace wasmtime
248
249#endif // WASMTIME_VAL_CLASS_HH
Representation of a WebAssembly anyref value.
Definition: _anyref_class.hh:21
Representation of a WebAssembly externref value.
Definition: _externref_class.hh:27
Representation of a WebAssembly function.
Definition: _func_class.hh:108
A WebAssembly global.
Definition: global.hh:28
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: _val_class.hh:53
Val(const Val &other)
Copy constructor to clone other.
Definition: _val_class.hh:112
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: _val_class.hh:70
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: _val_class.hh:80
float f32() const
Definition: _val_class.hh:185
int32_t i32() const
Definition: _val_class.hh:167
double f64() const
Definition: _val_class.hh:194
const Raw * capi() const
Returns the underlying C API pointer.
Definition: _val_class.hh:239
int64_t i64() const
Definition: _val_class.hh:176
V128 v128() const
Definition: _val_class.hh:203
ValKind kind() const
Returns the kind of value that this value has.
Definition: _val_class.hh:141
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: _val_class.hh:75
Raw * capi()
Returns the underlying C API pointer.
Definition: _val_class.hh:244
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: _val_class.hh:90
~Val()
Unroots the values in val, if any.
Definition: _val_class.hh:138
Val(wasmtime_val_t val)
Creates a new value from the raw C API representation.
Definition: _val_class.hh:67
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: _val_class.hh:85
Val & operator=(const Val &other)
Copy assignment to clone from other.
Definition: _val_class.hh:115
Val & operator=(Val &&other)
Move assignment to move the contents of other.
Definition: _val_class.hh:129
std::optional< Func > funcref() const
Definition: val.hh:26
Val(Val &&other)
Move constructor to move the contents of other.
Definition: _val_class.hh:122
Container for the v128 WebAssembly type.
Definition: _val_class.hh:18
V128()
Creates a new zero-value v128.
Definition: _val_class.hh:23
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: _val_class.hh:20
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: _val_class.hh:26
Container for different kinds of wasm values.
Definition: val.h:376
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:378
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:380
int32_t i32
Field used if wasmtime_val_t::kind is WASMTIME_I32.
Definition: val.h:235
wasmtime_v128 v128
Field used if wasmtime_val_t::kind is WASMTIME_V128.
Definition: val.h:256
float32_t f32
Field used if wasmtime_val_t::kind is WASMTIME_F32.
Definition: val.h:239
int64_t i64
Field used if wasmtime_val_t::kind is WASMTIME_I64.
Definition: val.h:237
float64_t f64
Field used if wasmtime_val_t::kind is WASMTIME_F64.
Definition: val.h:241
#define WASMTIME_EXTERNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an externref.
Definition: val.h:38
#define WASMTIME_ANYREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an anyref.
Definition: val.h:41
#define WASMTIME_I32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i32.
Definition: val.h:22
uint8_t wasmtime_v128[16]
A 128-bit value representing the WebAssembly v128 type. Bytes are stored in little-endian order.
Definition: val.h:49
#define WASMTIME_F32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f32.
Definition: val.h:26
#define WASMTIME_FUNCREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a funcref.
Definition: val.h:33
WASM_API_EXTERN void wasmtime_val_unroot(wasmtime_val_t *val)
Unroot the value contained by val.
#define WASMTIME_F64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f64.
Definition: val.h:28
WASM_API_EXTERN void wasmtime_val_clone(const wasmtime_val_t *src, wasmtime_val_t *dst)
Clones the value pointed to by src into the dst provided.
struct wasmtime_val wasmtime_val_t
Convenience alias for wasmtime_val_t.
#define WASMTIME_I64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i64.
Definition: val.h:24
#define WASMTIME_EXNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an exnref.
Definition: val.h:44
#define WASMTIME_V128
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a v128.
Definition: val.h:30