Wasmtime
val.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_VAL_HH
6#define WASMTIME_VAL_HH
7
8#include <optional>
9#ifdef WASMTIME_FEATURE_GC
10#include <wasmtime/gc.h>
11#endif // WASMTIME_FEATURE_GC
12#include <wasmtime/store.hh>
13#include <wasmtime/types/val.hh>
14#include <wasmtime/val.h>
15
16namespace wasmtime {
17
18class EqRef;
19class StructRef;
20class ArrayRef;
21
22#ifdef WASMTIME_FEATURE_GC
36class ExternRef {
37 friend class Val;
38
39 wasmtime_externref_t val;
40
41 static void finalizer(void *ptr) {
42 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
43 }
44
45public:
47 explicit ExternRef(wasmtime_externref_t val) : val(val) {}
48
50 ExternRef(const ExternRef &other) {
51 wasmtime_externref_clone(&other.val, &val);
52 }
53
55 ExternRef &operator=(const ExternRef &other) {
56 wasmtime_externref_unroot(&val);
57 wasmtime_externref_clone(&other.val, &val);
58 return *this;
59 }
60
62 ExternRef(ExternRef &&other) {
63 val = other.val;
64 wasmtime_externref_set_null(&other.val);
65 }
66
68 ExternRef &operator=(ExternRef &&other) {
69 wasmtime_externref_unroot(&val);
70 val = other.val;
71 wasmtime_externref_set_null(&other.val);
72 return *this;
73 }
74
75 ~ExternRef() { wasmtime_externref_unroot(&val); }
76
82 explicit ExternRef(Store::Context cx, std::any val) {
83 void *ptr = std::make_unique<std::any>(val).release();
84 bool ok = wasmtime_externref_new(cx.ptr, ptr, finalizer, &this->val);
85 if (!ok) {
86 fprintf(stderr, "failed to allocate a new externref");
87 abort();
88 }
89 }
90
92 std::any &data(Store::Context cx) {
93 return *static_cast<std::any *>(wasmtime_externref_data(cx.ptr, &val));
94 }
95
98 uint32_t take_raw(Store::Context cx) {
99 uint32_t ret = wasmtime_externref_to_raw(cx.capi(), &val);
100 wasmtime_externref_set_null(&val);
101 return ret;
102 }
103
105 uint32_t borrow_raw(Store::Context cx) const {
106 return wasmtime_externref_to_raw(cx.capi(), &val);
107 }
108};
109#endif // WASMTIME_FEATURE_GC
110
111class EqRef;
112
113#ifdef WASMTIME_FEATURE_GC
117class AnyRef {
118 friend class Val;
119
120 wasmtime_anyref_t val;
121
122public:
124 explicit AnyRef(wasmtime_anyref_t val) : val(val) {}
125
127 AnyRef(const AnyRef &other) { wasmtime_anyref_clone(&other.val, &val); }
128
130 AnyRef &operator=(const AnyRef &other) {
131 wasmtime_anyref_unroot(&val);
132 wasmtime_anyref_clone(&other.val, &val);
133 return *this;
134 }
135
137 AnyRef(AnyRef &&other) {
138 val = other.val;
139 wasmtime_anyref_set_null(&other.val);
140 }
141
143 AnyRef &operator=(AnyRef &&other) {
144 wasmtime_anyref_unroot(&val);
145 val = other.val;
146 wasmtime_anyref_set_null(&other.val);
147 return *this;
148 }
149
150 ~AnyRef() { wasmtime_anyref_unroot(&val); }
151
154 static AnyRef i31(Store::Context cx, uint32_t value) {
155 wasmtime_anyref_t other;
156 wasmtime_anyref_from_i31(cx.ptr, value, &other);
157 return AnyRef(other);
158 }
159
162 uint32_t take_raw(Store::Context cx) {
163 uint32_t ret = wasmtime_anyref_to_raw(cx.capi(), &val);
164 wasmtime_anyref_set_null(&val);
165 return ret;
166 }
167
169 uint32_t borrow_raw(Store::Context cx) const {
170 return wasmtime_anyref_to_raw(cx.capi(), &val);
171 }
172
174 std::optional<uint32_t> u31(Store::Context cx) const {
175 uint32_t ret = 0;
176 if (wasmtime_anyref_i31_get_u(cx.ptr, &val, &ret))
177 return ret;
178 return std::nullopt;
179 }
180
182 std::optional<int32_t> i31(Store::Context cx) const {
183 int32_t ret = 0;
184 if (wasmtime_anyref_i31_get_s(cx.ptr, &val, &ret))
185 return ret;
186 return std::nullopt;
187 }
188
190 bool is_i31(Store::Context cx) const {
191 return wasmtime_anyref_is_i31(cx.ptr, &val);
192 }
193
195 inline bool is_eqref(Store::Context cx) const;
196
198 inline bool is_struct(Store::Context cx) const;
199
201 inline bool is_array(Store::Context cx) const;
202
204 inline std::optional<EqRef> as_eqref(Store::Context cx) const;
205
207 inline std::optional<StructRef> as_struct(Store::Context cx) const;
208
210 inline std::optional<ArrayRef> as_array(Store::Context cx) const;
211};
212#endif // WASMTIME_FEATURE_GC
213
215struct V128 {
218
220 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
221
223 V128(const wasmtime_v128 &v) : v128{} {
224 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
225 }
226};
227
228class Func;
229
240class Val {
241 friend class Global;
242 friend class Table;
243 friend class Func;
244 friend class Exn;
245 friend class StructRef;
246 friend class ArrayRef;
247
248 wasmtime_val_t val;
249
250 Val() : val{} {
251 val.kind = WASMTIME_I32;
252 val.of.i32 = 0;
253 }
254 Val(wasmtime_val_t val) : val(val) {}
255
256public:
258 Val(int32_t i32) : val{} {
259 val.kind = WASMTIME_I32;
260 val.of.i32 = i32;
261 }
263 Val(int64_t i64) : val{} {
264 val.kind = WASMTIME_I64;
265 val.of.i64 = i64;
266 }
268 Val(float f32) : val{} {
269 val.kind = WASMTIME_F32;
270 val.of.f32 = f32;
271 }
273 Val(double f64) : val{} {
274 val.kind = WASMTIME_F64;
275 val.of.f64 = f64;
276 }
278 Val(const V128 &v128) : val{} {
279 val.kind = WASMTIME_V128;
280 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
281 }
283 Val(std::optional<Func> func);
285 Val(Func func);
287 Val(std::optional<ExternRef> ptr) : val{} {
289 if (ptr) {
290 val.of.externref = ptr->val;
291 wasmtime_externref_set_null(&ptr->val);
292 } else {
293 wasmtime_externref_set_null(&val.of.externref);
294 }
295 }
297 Val(std::optional<AnyRef> ptr) : val{} {
298 val.kind = WASMTIME_ANYREF;
299 if (ptr) {
300 val.of.anyref = ptr->val;
301 wasmtime_anyref_set_null(&ptr->val);
302 } else {
303 wasmtime_anyref_set_null(&val.of.anyref);
304 }
305 }
308 Val(ExternRef ptr);
311 Val(AnyRef ptr);
312
314 Val(const Val &other) { wasmtime_val_clone(&other.val, &val); }
315
317 Val &operator=(const Val &other) {
319 wasmtime_val_clone(&other.val, &val);
320 return *this;
321 }
322
324 Val(Val &&other) {
325 val = other.val;
326 other.val.kind = WASMTIME_I32;
327 other.val.of.i32 = 0;
328 }
329
331 Val &operator=(Val &&other) {
333 val = other.val;
334 other.val.kind = WASMTIME_I32;
335 other.val.of.i32 = 0;
336 return *this;
337 }
338
341
343 ValKind kind() const {
344 switch (val.kind) {
345 case WASMTIME_I32:
346 return ValKind::I32;
347 case WASMTIME_I64:
348 return ValKind::I64;
349 case WASMTIME_F32:
350 return ValKind::F32;
351 case WASMTIME_F64:
352 return ValKind::F64;
353 case WASMTIME_FUNCREF:
354 return ValKind::FuncRef;
356 return ValKind::ExternRef;
357 case WASMTIME_ANYREF:
358 return ValKind::AnyRef;
359 case WASMTIME_EXNREF:
360 return ValKind::ExnRef;
361 case WASMTIME_V128:
362 return ValKind::V128;
363 }
364 std::abort();
365 }
366
369 int32_t i32() const {
370 if (val.kind != WASMTIME_I32) {
371 std::abort();
372 }
373 return val.of.i32;
374 }
375
378 int64_t i64() const {
379 if (val.kind != WASMTIME_I64) {
380 std::abort();
381 }
382 return val.of.i64;
383 }
384
387 float f32() const {
388 if (val.kind != WASMTIME_F32) {
389 std::abort();
390 }
391 return val.of.f32;
392 }
393
396 double f64() const {
397 if (val.kind != WASMTIME_F64) {
398 std::abort();
399 }
400 return val.of.f64;
401 }
402
405 V128 v128() const {
406 if (val.kind != WASMTIME_V128) {
407 std::abort();
408 }
409 return val.of.v128;
410 }
411
417 std::optional<ExternRef> externref() const {
418 if (val.kind != WASMTIME_EXTERNREF) {
419 std::abort();
420 }
421 if (wasmtime_externref_is_null(&val.of.externref)) {
422 return std::nullopt;
423 }
424 wasmtime_externref_t other;
425 wasmtime_externref_clone(&val.of.externref, &other);
426 return ExternRef(other);
427 }
428
434 std::optional<AnyRef> anyref() const {
435 if (val.kind != WASMTIME_ANYREF) {
436 std::abort();
437 }
438 if (wasmtime_anyref_is_null(&val.of.anyref)) {
439 return std::nullopt;
440 }
441 wasmtime_anyref_t other;
442 wasmtime_anyref_clone(&val.of.anyref, &other);
443 return AnyRef(other);
444 }
445
451 std::optional<Func> funcref() const;
452};
453
454} // namespace wasmtime
455
456// fill in `Func` constructors for `Val`
457#include <wasmtime/func.hh>
458
459#endif // WASMTIME_VAL_HH
A WebAssembly exception object.
Definition: exn.hh:30
Representation of a WebAssembly function.
Definition: func.hh:339
A WebAssembly global.
Definition: global.hh:28
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: val.hh:240
Val(const Val &other)
Copy constructor to clone other.
Definition: val.hh:314
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: val.hh:258
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: val.hh:268
float f32() const
Definition: val.hh:387
int32_t i32() const
Definition: val.hh:369
double f64() const
Definition: val.hh:396
std::optional< AnyRef > anyref() const
Definition: val.hh:434
int64_t i64() const
Definition: val.hh:378
V128 v128() const
Definition: val.hh:405
ValKind kind() const
Returns the kind of value that this value has.
Definition: val.hh:343
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: val.hh:263
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: val.hh:278
Val(std::optional< ExternRef > ptr)
Creates a new externref value.
Definition: val.hh:287
~Val()
Unroots the values in val, if any.
Definition: val.hh:340
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: val.hh:273
Val & operator=(const Val &other)
Copy assignment to clone from other.
Definition: val.hh:317
Val(std::optional< AnyRef > ptr)
Creates a new anyref value.
Definition: val.hh:297
Val & operator=(Val &&other)
Move assignment to move the contents of other.
Definition: val.hh:331
std::optional< ExternRef > externref() const
Definition: val.hh:417
std::optional< Func > funcref() const
Definition: func.hh:652
Val(Val &&other)
Move constructor to move the contents of other.
Definition: val.hh:324
Container for the v128 WebAssembly type.
Definition: val.hh:215
V128()
Creates a new zero-value v128.
Definition: val.hh:220
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: val.hh:217
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: val.hh:223
Container for different kinds of wasm values.
Definition: val.h:546
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:548
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:550
ValKind
Different kinds of types accepted by Wasmtime.
Definition: types/val.hh:16
@ ExternRef
WebAssembly's externref type from the reference types.
@ AnyRef
WebAssembly's anyref type.
int32_t i32
Field used if wasmtime_val_t::kind is WASMTIME_I32.
Definition: val.h:415
wasmtime_v128 v128
Field used if wasmtime_val_t::kind is WASMTIME_V128.
Definition: val.h:436
float32_t f32
Field used if wasmtime_val_t::kind is WASMTIME_F32.
Definition: val.h:419
int64_t i64
Field used if wasmtime_val_t::kind is WASMTIME_I64.
Definition: val.h:417
float64_t f64
Field used if wasmtime_val_t::kind is WASMTIME_F64.
Definition: val.h:421
#define WASMTIME_EXTERNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an externref.
Definition: val.h:391
#define WASMTIME_ANYREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an anyref.
Definition: val.h:394
#define WASMTIME_I32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i32.
Definition: val.h:377
uint8_t wasmtime_v128[16]
A 128-bit value representing the WebAssembly v128 type. Bytes are stored in little-endian order.
Definition: val.h:401
#define WASMTIME_F32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f32.
Definition: val.h:381
#define WASMTIME_FUNCREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a funcref.
Definition: val.h:388
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:383
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.
#define WASMTIME_I64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i64.
Definition: val.h:379
#define WASMTIME_EXNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an exnref.
Definition: val.h:397
#define WASMTIME_V128
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a v128.
Definition: val.h:385