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/_exnref_class.hh>
10#include <wasmtime/_externref_class.hh>
11#include <wasmtime/_func_class.hh>
12#include <wasmtime/_val_class.hh>
13
14namespace wasmtime {
15
16inline Val::Val(std::optional<Func> func) : val{} {
18 if (func) {
19 val.of.funcref = (*func).func;
20 } else {
21 wasmtime_funcref_set_null(&val.of.funcref);
22 }
23}
24
25inline Val::Val(Func func) : Val(std::optional(func)) {}
26
27inline std::optional<Func> Val::funcref() const {
28 if (val.kind != WASMTIME_FUNCREF) {
29 std::abort();
30 }
31 if (val.of.funcref.store_id == 0) {
32 return std::nullopt;
33 }
34 return Func(val.of.funcref);
35}
36
37#ifdef WASMTIME_FEATURE_GC
38
39inline Val::Val(std::optional<AnyRef> ptr) : val{} {
41 if (ptr) {
42 val.of.anyref = *ptr->capi();
43 wasmtime_anyref_set_null(ptr->capi());
44 } else {
45 wasmtime_anyref_set_null(&val.of.anyref);
46 }
47}
48
49inline Val::Val(AnyRef ptr) : Val(std::optional(ptr)) {}
50
51inline std::optional<AnyRef> Val::anyref() const {
52 if (val.kind != WASMTIME_ANYREF) {
53 std::abort();
54 }
55 if (wasmtime_anyref_is_null(&val.of.anyref)) {
56 return std::nullopt;
57 }
59 wasmtime_anyref_clone(&val.of.anyref, &other);
60 return AnyRef(other);
61}
62
63inline Val::Val(std::optional<ExternRef> ptr) : val{} {
65 if (ptr) {
66 val.of.externref = *ptr->capi();
67 wasmtime_externref_set_null(ptr->capi());
68 } else {
69 wasmtime_externref_set_null(&val.of.externref);
70 }
71}
72
73inline Val::Val(ExternRef ptr) : Val(std::optional(ptr)) {}
74
75inline std::optional<ExternRef> Val::externref() const {
76 if (val.kind != WASMTIME_EXTERNREF) {
77 std::abort();
78 }
79 if (wasmtime_externref_is_null(&val.of.externref)) {
80 return std::nullopt;
81 }
84 return ExternRef(other);
85}
86
87inline Val::Val(std::optional<ExnRef> ptr) : val{} {
89 if (ptr) {
90 val.of.exnref = *ptr->capi();
91 wasmtime_exnref_set_null(ptr->capi());
92 } else {
93 wasmtime_exnref_set_null(&val.of.exnref);
94 }
95}
96
97inline Val::Val(ExnRef ptr) : Val(std::optional(ptr)) {}
98
99inline std::optional<ExnRef> Val::exnref() const {
100 if (val.kind != WASMTIME_EXNREF) {
101 std::abort();
102 }
103 if (wasmtime_exnref_is_null(&val.of.exnref)) {
104 return std::nullopt;
105 }
106 wasmtime_exnref_t other;
107 wasmtime_exnref_clone(&val.of.exnref, &other);
108 return ExnRef(other);
109}
110
111#endif // WASMTIME_FEATURE_GC
112
114template <> struct detail::WasmType<V128> {
115 static const bool valid = true;
116 static ValType valtype() { return ValType::v128(); }
117 static void store(Store::Context cx, wasmtime_val_raw_t *p, const V128 &t) {
118 (void)cx;
119 memcpy(&p->v128[0], &t.v128[0], sizeof(wasmtime_v128));
120 }
121 static V128 load(Store::Context cx, wasmtime_val_raw_t *p) {
122 (void)cx;
123 return p->v128;
124 }
125};
126
127} // namespace wasmtime
128
129#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:54
std::optional< Func > funcref() const
Definition: val.hh:27
WASM_API_EXTERN void wasmtime_exnref_clone(const wasmtime_exnref_t *ref, wasmtime_exnref_t *out)
Creates a new reference pointing to the same exception that ref points to.
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 WebAssembly exception reference value.
Definition: val.h:103
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_exnref_t exnref
Field used if wasmtime_val_t::kind is WASMTIME_EXNREF.
Definition: val.h:248
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
#define WASMTIME_EXNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an exnref.
Definition: val.h:44