Wasmtime
val.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_VAL_HH
6#define WASMTIME_VAL_HH
7
8#include <wasmtime/_anyref_class.hh>
9#include <wasmtime/_externref_class.hh>
10#include <wasmtime/_func_class.hh>
11#include <wasmtime/_val_class.hh>
12
13namespace wasmtime {
14
15inline Val::Val(std::optional<Func> func) : val{} {
17 if (func) {
18 val.of.funcref = (*func).func;
19 } else {
20 wasmtime_funcref_set_null(&val.of.funcref);
21 }
22}
23
24inline Val::Val(Func func) : Val(std::optional(func)) {}
25
26inline std::optional<Func> Val::funcref() const {
27 if (val.kind != WASMTIME_FUNCREF) {
28 std::abort();
29 }
30 if (val.of.funcref.store_id == 0) {
31 return std::nullopt;
32 }
33 return Func(val.of.funcref);
34}
35
36#ifdef WASMTIME_FEATURE_GC
37
38inline Val::Val(std::optional<AnyRef> ptr) : val{} {
40 if (ptr) {
41 val.of.anyref = *ptr->capi();
42 wasmtime_anyref_set_null(ptr->capi());
43 } else {
44 wasmtime_anyref_set_null(&val.of.anyref);
45 }
46}
47
48inline Val::Val(AnyRef ptr) : Val(std::optional(ptr)) {}
49
50inline std::optional<AnyRef> Val::anyref() const {
51 if (val.kind != WASMTIME_ANYREF) {
52 std::abort();
53 }
54 if (wasmtime_anyref_is_null(&val.of.anyref)) {
55 return std::nullopt;
56 }
58 wasmtime_anyref_clone(&val.of.anyref, &other);
59 return AnyRef(other);
60}
61
62inline Val::Val(std::optional<ExternRef> ptr) : val{} {
64 if (ptr) {
65 val.of.externref = *ptr->capi();
66 wasmtime_externref_set_null(ptr->capi());
67 } else {
68 wasmtime_externref_set_null(&val.of.externref);
69 }
70}
71
72inline Val::Val(ExternRef ptr) : Val(std::optional(ptr)) {}
73
74inline std::optional<ExternRef> Val::externref() const {
75 if (val.kind != WASMTIME_EXTERNREF) {
76 std::abort();
77 }
78 if (wasmtime_externref_is_null(&val.of.externref)) {
79 return std::nullopt;
80 }
83 return ExternRef(other);
84}
85
86#endif // WASMTIME_FEATURE_GC
87
89template <> struct detail::WasmType<V128> {
90 static const bool valid = true;
91 static ValType valtype() { return ValType::v128(); }
92 static void store(Store::Context cx, wasmtime_val_raw_t *p, const V128 &t) {
93 (void)cx;
94 memcpy(&p->v128[0], &t.v128[0], sizeof(wasmtime_v128));
95 }
96 static V128 load(Store::Context cx, wasmtime_val_raw_t *p) {
97 (void)cx;
98 return p->v128;
99 }
100};
101
102} // namespace wasmtime
103
104#endif // WASMTIME_VAL_HH
Representation of a WebAssembly function.
Definition: _func_class.hh:108
static ValType v128()
Convenience constructor for the v128 value type.
Definition: types/_val_class.hh:130
Representation of a generic WebAssembly value.
Definition: _val_class.hh:53
std::optional< Func > funcref() const
Definition: val.hh:26
WASM_API_EXTERN void wasmtime_externref_clone(const wasmtime_externref_t *ref, wasmtime_externref_t *out)
Creates a new reference pointing to the same data that ref points to (depending on the configured col...
A WebAssembly value in the any hierarchy of GC types.
Definition: val.h:77
A host-defined un-forgeable reference to pass into WebAssembly.
Definition: val.h:136
uint64_t store_id
Definition: extern.h:32
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
Container for possible wasm values.
Definition: val.h:290
wasmtime_v128 v128
Definition: val.h:310
wasmtime_func_t funcref
Definition: val.h:254
wasmtime_anyref_t anyref
Field used if wasmtime_val_t::kind is WASMTIME_ANYREF.
Definition: val.h:244
wasmtime_externref_t externref
Field used if wasmtime_val_t::kind is WASMTIME_EXTERNREF.
Definition: val.h:246
#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
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_FUNCREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a funcref.
Definition: val.h:33