Wasmtime
structref.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_STRUCTREF_HH
6#define WASMTIME_STRUCTREF_HH
7
8#include <wasmtime/_structref_class.hh>
9
10#ifdef WASMTIME_FEATURE_GC
11
12#include <wasmtime/_anyref_class.hh>
13#include <wasmtime/_eqref_class.hh>
14#include <wasmtime/_val_class.hh>
15
16namespace wasmtime {
17
18inline Result<StructRef> StructRef::create(Store::Context cx,
19 const StructRefPre &pre,
20 const std::vector<Val> &fields) {
22 auto *err = wasmtime_structref_new(
23 cx.capi(), pre.capi(),
24 reinterpret_cast<const wasmtime_val_t *>(fields.data()), fields.size(),
25 &out);
26 if (err)
27 return Result<StructRef>(Error(err));
28 return Result<StructRef>(StructRef(out));
29}
30
31inline Result<Val> StructRef::field(Store::Context cx, size_t index) const {
33 auto *err = wasmtime_structref_field(cx.capi(), &raw, index, &out);
34 if (err)
35 return Result<Val>(Error(err));
36 return Result<Val>(Val(out));
37}
38
39inline Result<std::monostate>
40StructRef::set_field(Store::Context cx, size_t index, const Val &value) const {
41 auto *err =
42 wasmtime_structref_set_field(cx.capi(), &raw, index, value.capi());
43 if (err)
44 return Result<std::monostate>(Error(err));
45 return Result<std::monostate>(std::monostate{});
46}
47
48inline AnyRef StructRef::to_anyref() const {
51 return AnyRef(out);
52}
53
54inline EqRef StructRef::to_eqref() const {
57 return EqRef(out);
58}
59
60} // namespace wasmtime
61
62#endif // WASMTIME_FEATURE_GC
63
64#endif // WASMTIME_STRUCTREF_HH
WASM_API_EXTERN wasmtime_error_t * wasmtime_structref_set_field(wasmtime_context_t *context, const wasmtime_structref_t *structref, size_t index, const wasmtime_val_t *val)
Set a field of a struct.
WASM_API_EXTERN void wasmtime_structref_to_eqref(const wasmtime_structref_t *structref, wasmtime_eqref_t *out)
Upcast a structref to an eqref.
WASM_API_EXTERN wasmtime_error_t * wasmtime_structref_field(wasmtime_context_t *context, const wasmtime_structref_t *structref, size_t index, wasmtime_val_t *out)
Read a field from a struct.
WASM_API_EXTERN wasmtime_error_t * wasmtime_structref_new(wasmtime_context_t *context, const wasmtime_struct_ref_pre_t *pre, const wasmtime_val_t *fields, size_t nfields, wasmtime_structref_t *out)
Allocate a new struct instance.
WASM_API_EXTERN void wasmtime_structref_to_anyref(const wasmtime_structref_t *structref, wasmtime_anyref_t *out)
Upcast a structref to an anyref.
A WebAssembly value in the any hierarchy of GC types.
Definition: val.h:77
A WebAssembly eqref value.
Definition: val.h:163
A WebAssembly structref value.
Definition: val.h:187
Container for different kinds of wasm values.
Definition: val.h:376