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) { return *this = Val(other); }
121
123 Val(Val &&other) {
124 val = other.val;
125 other.val.kind = WASMTIME_I32;
126 other.val.of.i32 = 0;
127 }
128
130 Val &operator=(Val &&other) {
132 val = other.val;
133 other.val.kind = WASMTIME_I32;
134 other.val.of.i32 = 0;
135 return *this;
136 }
137
140
142 ValKind kind() const {
143 switch (val.kind) {
144 case WASMTIME_I32:
145 return ValKind::I32;
146 case WASMTIME_I64:
147 return ValKind::I64;
148 case WASMTIME_F32:
149 return ValKind::F32;
150 case WASMTIME_F64:
151 return ValKind::F64;
152 case WASMTIME_FUNCREF:
153 return ValKind::FuncRef;
155 return ValKind::ExternRef;
156 case WASMTIME_ANYREF:
157 return ValKind::AnyRef;
158 case WASMTIME_EXNREF:
159 return ValKind::ExnRef;
160 case WASMTIME_V128:
161 return ValKind::V128;
162 }
163 std::abort();
164 }
165
168 int32_t i32() const {
169 if (val.kind != WASMTIME_I32) {
170 std::abort();
171 }
172 return val.of.i32;
173 }
174
177 int64_t i64() const {
178 if (val.kind != WASMTIME_I64) {
179 std::abort();
180 }
181 return val.of.i64;
182 }
183
186 float f32() const {
187 if (val.kind != WASMTIME_F32) {
188 std::abort();
189 }
190 return val.of.f32;
191 }
192
195 double f64() const {
196 if (val.kind != WASMTIME_F64) {
197 std::abort();
198 }
199 return val.of.f64;
200 }
201
204 V128 v128() const {
205 if (val.kind != WASMTIME_V128) {
206 std::abort();
207 }
208 return val.of.v128;
209 }
210
211#ifdef WASMTIME_FEATURE_GC
217 std::optional<ExternRef> externref() const;
218
224 std::optional<AnyRef> anyref() const;
225
231 std::optional<ExnRef> exnref() const;
232#endif
233
239 std::optional<Func> funcref() const;
240
243
247 const Raw *capi() const { return &val; }
248
252 Raw *capi() { return &val; }
253};
254
255} // namespace wasmtime
256
257#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:186
int32_t i32() const
Definition: _val_class.hh:168
double f64() const
Definition: _val_class.hh:195
const Raw * capi() const
Returns the underlying C API pointer.
Definition: _val_class.hh:247
int64_t i64() const
Definition: _val_class.hh:177
V128 v128() const
Definition: _val_class.hh:204
ValKind kind() const
Returns the kind of value that this value has.
Definition: _val_class.hh:142
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:252
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:139
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:130
std::optional< Func > funcref() const
Definition: val.hh:27
Val(Val &&other)
Move constructor to move the contents of other.
Definition: _val_class.hh:123
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:399
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:401
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:403
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