9#ifdef WASMTIME_FEATURE_GC
22#ifdef WASMTIME_FEATURE_GC
39 wasmtime_externref_t val;
41 static void finalizer(
void *ptr) {
42 std::unique_ptr<std::any> _ptr(
static_cast<std::any *
>(ptr));
47 explicit ExternRef(wasmtime_externref_t val) : val(val) {}
51 wasmtime_externref_clone(&other.val, &val);
55 ExternRef &operator=(
const ExternRef &other) {
56 wasmtime_externref_unroot(&val);
57 wasmtime_externref_clone(&other.val, &val);
64 wasmtime_externref_set_null(&other.val);
69 wasmtime_externref_unroot(&val);
71 wasmtime_externref_set_null(&other.val);
75 ~ExternRef() { wasmtime_externref_unroot(&val); }
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);
86 fprintf(stderr,
"failed to allocate a new externref");
92 std::any &data(Store::Context cx) {
93 return *
static_cast<std::any *
>(wasmtime_externref_data(cx.ptr, &val));
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);
105 uint32_t borrow_raw(Store::Context cx)
const {
106 return wasmtime_externref_to_raw(cx.capi(), &val);
113#ifdef WASMTIME_FEATURE_GC
120 wasmtime_anyref_t val;
124 explicit AnyRef(wasmtime_anyref_t val) : val(val) {}
127 AnyRef(
const AnyRef &other) { wasmtime_anyref_clone(&other.val, &val); }
130 AnyRef &operator=(
const AnyRef &other) {
131 wasmtime_anyref_unroot(&val);
132 wasmtime_anyref_clone(&other.val, &val);
139 wasmtime_anyref_set_null(&other.val);
143 AnyRef &operator=(AnyRef &&other) {
144 wasmtime_anyref_unroot(&val);
146 wasmtime_anyref_set_null(&other.val);
150 ~AnyRef() { wasmtime_anyref_unroot(&val); }
154 static AnyRef i31(Store::Context cx, uint32_t value) {
155 wasmtime_anyref_t other;
156 wasmtime_anyref_from_i31(cx.ptr, value, &other);
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);
169 uint32_t borrow_raw(Store::Context cx)
const {
170 return wasmtime_anyref_to_raw(cx.capi(), &val);
174 std::optional<uint32_t> u31(Store::Context cx)
const {
176 if (wasmtime_anyref_i31_get_u(cx.ptr, &val, &ret))
182 std::optional<int32_t> i31(Store::Context cx)
const {
184 if (wasmtime_anyref_i31_get_s(cx.ptr, &val, &ret))
190 bool is_i31(Store::Context cx)
const {
191 return wasmtime_anyref_is_i31(cx.ptr, &val);
195 inline bool is_eqref(Store::Context cx)
const;
198 inline bool is_struct(Store::Context cx)
const;
201 inline bool is_array(Store::Context cx)
const;
204 inline std::optional<EqRef> as_eqref(Store::Context cx)
const;
207 inline std::optional<StructRef> as_struct(Store::Context cx)
const;
210 inline std::optional<ArrayRef> as_array(Store::Context cx)
const;
245 friend class StructRef;
246 friend class ArrayRef;
283 Val(std::optional<Func> func);
287 Val(std::optional<ExternRef> ptr) : val{} {
290 val.
of.externref = ptr->val;
291 wasmtime_externref_set_null(&ptr->val);
293 wasmtime_externref_set_null(&val.
of.externref);
297 Val(std::optional<AnyRef> ptr) : val{} {
300 val.
of.anyref = ptr->val;
301 wasmtime_anyref_set_null(&ptr->val);
303 wasmtime_anyref_set_null(&val.
of.anyref);
327 other.val.of.i32 = 0;
335 other.val.of.i32 = 0;
354 return ValKind::FuncRef;
356 return ValKind::ExternRef;
358 return ValKind::AnyRef;
360 return ValKind::ExnRef;
362 return ValKind::V128;
421 if (wasmtime_externref_is_null(&val.
of.externref)) {
424 wasmtime_externref_t other;
425 wasmtime_externref_clone(&val.
of.externref, &other);
438 if (wasmtime_anyref_is_null(&val.
of.anyref)) {
441 wasmtime_anyref_t other;
442 wasmtime_anyref_clone(&val.
of.anyref, &other);
451 std::optional<Func>
funcref()
const;
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