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/_exnref_class.hh>
9#include <wasmtime/_externref_class.hh>
10#include <wasmtime/_func_class.hh>
11#include <wasmtime/_store_class.hh>
12#include <wasmtime/_structref_class.hh>
13#include <wasmtime/types/val.hh>
14#include <wasmtime/val.h>
15
16namespace wasmtime {
17
19struct V128 {
22
24 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
25
27 V128(const wasmtime_v128 &v) : v128{} {
28 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
29 }
30};
31
32enum class ValKind {
33 I32 = WASMTIME_I32,
34 I64 = WASMTIME_I64,
35 F32 = WASMTIME_F32,
36 F64 = WASMTIME_F64,
37 V128 = WASMTIME_V128,
38 FuncRef = WASMTIME_FUNCREF,
39 ExternRef = WASMTIME_EXTERNREF,
40 AnyRef = WASMTIME_ANYREF,
41 ExnRef = WASMTIME_EXNREF,
42};
43
54class Val {
55 friend class Global;
56 friend class Table;
57 friend class Func;
58
60
61 Val() : val{} {
62 val.kind = WASMTIME_I32;
63 val.of.i32 = 0;
64 }
65
66public:
68 Val(wasmtime_val_t val) : val(val) {}
69
71 Val(int32_t i32) : val{} {
72 val.kind = WASMTIME_I32;
73 val.of.i32 = i32;
74 }
76 Val(int64_t i64) : val{} {
77 val.kind = WASMTIME_I64;
78 val.of.i64 = i64;
79 }
81 Val(float f32) : val{} {
82 val.kind = WASMTIME_F32;
83 val.of.f32 = f32;
84 }
86 Val(double f64) : val{} {
87 val.kind = WASMTIME_F64;
88 val.of.f64 = f64;
89 }
91 Val(const V128 &v128) : val{} {
92 val.kind = WASMTIME_V128;
93 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
94 }
96 Val(std::optional<Func> func);
98 Val(Func func);
99#ifdef WASMTIME_FEATURE_GC
101 Val(std::optional<ExternRef> ptr);
103 Val(std::optional<AnyRef> ptr);
106 Val(ExternRef ptr);
109 Val(AnyRef ptr);
111 Val(std::optional<ExnRef> ptr);
113 Val(ExnRef ptr);
114#endif
115
117 Val(const Val &other) { wasmtime_val_clone(&other.val, &val); }
118
120 Val &operator=(const Val &other) {
122 wasmtime_val_clone(&other.val, &val);
123 return *this;
124 }
125
127 Val(Val &&other) {
128 val = other.val;
129 other.val.kind = WASMTIME_I32;
130 other.val.of.i32 = 0;
131 }
132
134 Val &operator=(Val &&other) {
136 val = other.val;
137 other.val.kind = WASMTIME_I32;
138 other.val.of.i32 = 0;
139 return *this;
140 }
141
144
146 ValKind kind() const {
147 switch (val.kind) {
148 case WASMTIME_I32:
149 return ValKind::I32;
150 case WASMTIME_I64:
151 return ValKind::I64;
152 case WASMTIME_F32:
153 return ValKind::F32;
154 case WASMTIME_F64:
155 return ValKind::F64;
156 case WASMTIME_FUNCREF:
157 return ValKind::FuncRef;
159 return ValKind::ExternRef;
160 case WASMTIME_ANYREF:
161 return ValKind::AnyRef;
162 case WASMTIME_EXNREF:
163 return ValKind::ExnRef;
164 case WASMTIME_V128:
165 return ValKind::V128;
166 }
167 std::abort();
168 }
169
172 int32_t i32() const {
173 if (val.kind != WASMTIME_I32) {
174 std::abort();
175 }
176 return val.of.i32;
177 }
178
181 int64_t i64() const {
182 if (val.kind != WASMTIME_I64) {
183 std::abort();
184 }
185 return val.of.i64;
186 }
187
190 float f32() const {
191 if (val.kind != WASMTIME_F32) {
192 std::abort();
193 }
194 return val.of.f32;
195 }
196
199 double f64() const {
200 if (val.kind != WASMTIME_F64) {
201 std::abort();
202 }
203 return val.of.f64;
204 }
205
208 V128 v128() const {
209 if (val.kind != WASMTIME_V128) {
210 std::abort();
211 }
212 return val.of.v128;
213 }
214
215#ifdef WASMTIME_FEATURE_GC
221 std::optional<ExternRef> externref() const;
222
228 std::optional<AnyRef> anyref() const;
229
235 std::optional<ExnRef> exnref() const;
236#endif
237
243 std::optional<Func> funcref() const;
244
247
251 const Raw *capi() const { return &val; }
252
256 Raw *capi() { return &val; }
257};
258
259} // namespace wasmtime
260
261#endif // WASMTIME_VAL_CLASS_HH
Representation of a WebAssembly anyref value.
Definition: _anyref_class.hh:23
A WebAssembly exception object.
Definition: _exnref_class.hh:29
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:54
Val(const Val &other)
Copy constructor to clone other.
Definition: _val_class.hh:117
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: _val_class.hh:71
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: _val_class.hh:81
float f32() const
Definition: _val_class.hh:190
int32_t i32() const
Definition: _val_class.hh:172
double f64() const
Definition: _val_class.hh:199
const Raw * capi() const
Returns the underlying C API pointer.
Definition: _val_class.hh:251
int64_t i64() const
Definition: _val_class.hh:181
V128 v128() const
Definition: _val_class.hh:208
ValKind kind() const
Returns the kind of value that this value has.
Definition: _val_class.hh:146
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: _val_class.hh:76
Raw * capi()
Returns the underlying C API pointer.
Definition: _val_class.hh:256
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: _val_class.hh:91
~Val()
Unroots the values in val, if any.
Definition: _val_class.hh:143
Val(wasmtime_val_t val)
Creates a new value from the raw C API representation.
Definition: _val_class.hh:68
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: _val_class.hh:86
Val & operator=(const Val &other)
Copy assignment to clone from other.
Definition: _val_class.hh:120
Val & operator=(Val &&other)
Move assignment to move the contents of other.
Definition: _val_class.hh:134
std::optional< Func > funcref() const
Definition: val.hh:27
Val(Val &&other)
Move constructor to move the contents of other.
Definition: _val_class.hh:127
Container for the v128 WebAssembly type.
Definition: _val_class.hh:19
V128()
Creates a new zero-value v128.
Definition: _val_class.hh:24
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: _val_class.hh:21
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: _val_class.hh:27
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