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#include <wasmtime/store.hh>
10#include <wasmtime/types/val.hh>
11#include <wasmtime/val.h>
12
13namespace wasmtime {
14
28class ExternRef {
29 friend class Val;
30
32
33 static void finalizer(void *ptr) {
34 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
35 }
36
37public:
39 explicit ExternRef(wasmtime_externref_t val) : val(val) {}
40
46 template <typename T> explicit ExternRef(Store::Context cx, T val) {
47 void *ptr = std::make_unique<std::any>(std::move(val)).release();
48 bool ok = wasmtime_externref_new(cx.ptr, ptr, finalizer, &this->val);
49 if (!ok) {
50 fprintf(stderr, "failed to allocate a new externref");
51 abort();
52 }
53 }
54
58 wasmtime_externref_clone(cx.ptr, &val, &other);
59 return ExternRef(other);
60 }
61
63 std::any &data(Store::Context cx) {
64 return *static_cast<std::any *>(wasmtime_externref_data(cx.ptr, &val));
65 }
66
70 wasmtime_externref_unroot(cx.ptr, &val);
71 }
72
76 const wasmtime_externref_t *raw() const { return &val; }
77};
78
82class AnyRef {
83 friend class Val;
84
86
87public:
89 explicit AnyRef(wasmtime_anyref_t val) : val(val) {}
90
93 static AnyRef i31(Store::Context cx, uint32_t value) {
95 wasmtime_anyref_from_i31(cx.ptr, value, &other);
96 return AnyRef(other);
97 }
98
101 wasmtime_anyref_t other;
102 wasmtime_anyref_clone(cx.ptr, &val, &other);
103 return AnyRef(other);
104 }
105
109 wasmtime_anyref_unroot(cx.ptr, &val);
110 }
111
115 const wasmtime_anyref_t *raw() const { return &val; }
116
118 std::optional<uint32_t> u31(Store::Context cx) const {
119 uint32_t ret = 0;
120 if (wasmtime_anyref_i31_get_u(cx.ptr, &val, &ret))
121 return ret;
122 return std::nullopt;
123 }
124
126 std::optional<int32_t> i31(Store::Context cx) const {
127 int32_t ret = 0;
128 if (wasmtime_anyref_i31_get_s(cx.ptr, &val, &ret))
129 return ret;
130 return std::nullopt;
131 }
132};
133
135struct V128 {
138
140 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
141
143 V128(const wasmtime_v128 &v) : v128{} {
144 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
145 }
146};
147
148class Func;
149
160class Val {
161 friend class Global;
162 friend class Table;
163 friend class Func;
164
165 wasmtime_val_t val;
166
167 Val() : val{} {
168 val.kind = WASMTIME_I32;
169 val.of.i32 = 0;
170 }
171 Val(wasmtime_val_t val) : val(val) {}
172
173public:
175 Val(int32_t i32) : val{} {
176 val.kind = WASMTIME_I32;
177 val.of.i32 = i32;
178 }
180 Val(int64_t i64) : val{} {
181 val.kind = WASMTIME_I64;
182 val.of.i64 = i64;
183 }
185 Val(float f32) : val{} {
186 val.kind = WASMTIME_F32;
187 val.of.f32 = f32;
188 }
190 Val(double f64) : val{} {
191 val.kind = WASMTIME_F64;
192 val.of.f64 = f64;
193 }
195 Val(const V128 &v128) : val{} {
196 val.kind = WASMTIME_V128;
197 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
198 }
200 Val(std::optional<Func> func);
202 Val(Func func);
204 Val(std::optional<ExternRef> ptr) : val{} {
206 if (ptr) {
207 val.of.externref = ptr->val;
208 } else {
209 wasmtime_externref_set_null(&val.of.externref);
210 }
211 }
213 Val(std::optional<AnyRef> ptr) : val{} {
214 val.kind = WASMTIME_ANYREF;
215 if (ptr) {
216 val.of.anyref = ptr->val;
217 } else {
218 wasmtime_anyref_set_null(&val.of.anyref);
219 }
220 }
223 Val(ExternRef ptr);
226 Val(AnyRef ptr);
227
229 ValKind kind() const {
230 switch (val.kind) {
231 case WASMTIME_I32:
232 return ValKind::I32;
233 case WASMTIME_I64:
234 return ValKind::I64;
235 case WASMTIME_F32:
236 return ValKind::F32;
237 case WASMTIME_F64:
238 return ValKind::F64;
239 case WASMTIME_FUNCREF:
240 return ValKind::FuncRef;
242 return ValKind::ExternRef;
243 case WASMTIME_ANYREF:
244 return ValKind::AnyRef;
245 case WASMTIME_V128:
246 return ValKind::V128;
247 }
248 std::abort();
249 }
250
253 int32_t i32() const {
254 if (val.kind != WASMTIME_I32) {
255 std::abort();
256 }
257 return val.of.i32;
258 }
259
262 int64_t i64() const {
263 if (val.kind != WASMTIME_I64) {
264 std::abort();
265 }
266 return val.of.i64;
267 }
268
271 float f32() const {
272 if (val.kind != WASMTIME_F32) {
273 std::abort();
274 }
275 return val.of.f32;
276 }
277
280 double f64() const {
281 if (val.kind != WASMTIME_F64) {
282 std::abort();
283 }
284 return val.of.f64;
285 }
286
289 V128 v128() const {
290 if (val.kind != WASMTIME_V128) {
291 std::abort();
292 }
293 return val.of.v128;
294 }
295
301 std::optional<ExternRef> externref(Store::Context cx) const {
302 if (val.kind != WASMTIME_EXTERNREF) {
303 std::abort();
304 }
305 if (wasmtime_externref_is_null(&val.of.externref)) {
306 return std::nullopt;
307 }
309 wasmtime_externref_clone(cx.ptr, &val.of.externref, &other);
310 return ExternRef(other);
311 }
312
318 std::optional<AnyRef> anyref(Store::Context cx) const {
319 if (val.kind != WASMTIME_ANYREF) {
320 std::abort();
321 }
322 if (wasmtime_anyref_is_null(&val.of.anyref)) {
323 return std::nullopt;
324 }
325 wasmtime_anyref_t other;
326 wasmtime_anyref_clone(cx.ptr, &val.of.anyref, &other);
327 return AnyRef(other);
328 }
329
335 std::optional<Func> funcref() const;
336
339 wasmtime_val_unroot(cx.ptr, &val);
340 }
341};
342
343} // namespace wasmtime
344
345#endif // WASMTIME_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:82
const wasmtime_anyref_t * raw() const
Definition: val.hh:115
static AnyRef i31(Store::Context cx, uint32_t value)
Definition: val.hh:93
void unroot(Store::Context cx)
Definition: val.hh:108
AnyRef clone(Store::Context cx)
Creates a new AnyRef which is separately rooted from this one.
Definition: val.hh:100
std::optional< int32_t > i31(Store::Context cx) const
If this is an i31, get the value sign-extended.
Definition: val.hh:126
std::optional< uint32_t > u31(Store::Context cx) const
If this is an i31, get the value zero-extended.
Definition: val.hh:118
AnyRef(wasmtime_anyref_t val)
Creates a new AnyRef directly from its C-API representation.
Definition: val.hh:89
Representation of a WebAssembly externref value.
Definition: val.hh:28
ExternRef(wasmtime_externref_t val)
Creates a new ExternRef directly from its C-API representation.
Definition: val.hh:39
const wasmtime_externref_t * raw() const
Definition: val.hh:76
ExternRef clone(Store::Context cx)
Creates a new ExternRef which is separately rooted from this one.
Definition: val.hh:56
void unroot(Store::Context cx)
Definition: val.hh:69
ExternRef(Store::Context cx, T val)
Definition: val.hh:46
std::any & data(Store::Context cx)
Returns the underlying host data associated with this ExternRef.
Definition: val.hh:63
Representation of a WebAssembly function.
Definition: wasmtime.hh:347
A WebAssembly global.
Definition: wasmtime.hh:707
An interior pointer into a Store.
Definition: store.hh:60
A WebAssembly table.
Definition: wasmtime.hh:766
Representation of a generic WebAssembly value.
Definition: val.hh:160
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: val.hh:175
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: val.hh:185
float f32() const
Definition: val.hh:271
std::optional< AnyRef > anyref(Store::Context cx) const
Definition: val.hh:318
int32_t i32() const
Definition: val.hh:253
double f64() const
Definition: val.hh:280
int64_t i64() const
Definition: val.hh:262
void unroot(Store::Context cx)
Unroots any GC references this Val points to within the cx provided.
Definition: val.hh:338
V128 v128() const
Definition: val.hh:289
ValKind kind() const
Returns the kind of value that this value has.
Definition: val.hh:229
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: val.hh:180
std::optional< ExternRef > externref(Store::Context cx) const
Definition: val.hh:301
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: val.hh:195
Val(std::optional< ExternRef > ptr)
Creates a new externref value.
Definition: val.hh:204
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: val.hh:190
Val(std::optional< AnyRef > ptr)
Creates a new anyref value.
Definition: val.hh:213
std::optional< Func > funcref() const
Definition: wasmtime.hh:659
Container for the v128 WebAssembly type.
Definition: val.hh:135
V128()
Creates a new zero-value v128.
Definition: val.hh:140
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: val.hh:137
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: val.hh:143
A WebAssembly value in the any hierarchy of GC types.
Definition: val.h:39
A host-defined un-forgeable reference to pass into WebAssembly.
Definition: val.h:174
Container for different kinds of wasm values.
Definition: val.h:441
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:443
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:445
ValKind
Different kinds of types accepted by Wasmtime.
Definition: types/val.hh:16
int32_t i32
Field used if wasmtime_val_t::kind is WASMTIME_I32.
Definition: val.h:321
wasmtime_v128 v128
Field used if wasmtime_val_t::kind is WASMTIME_V128.
Definition: val.h:338
float32_t f32
Field used if wasmtime_val_t::kind is WASMTIME_F32.
Definition: val.h:325
int64_t i64
Field used if wasmtime_val_t::kind is WASMTIME_I64.
Definition: val.h:323
float64_t f64
Field used if wasmtime_val_t::kind is WASMTIME_F64.
Definition: val.h:327
wasmtime_anyref_t anyref
Field used if wasmtime_val_t::kind is WASMTIME_ANYREF.
Definition: val.h:329
wasmtime_externref_t externref
Field used if wasmtime_val_t::kind is WASMTIME_EXTERNREF.
Definition: val.h:331
#define WASMTIME_EXTERNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an externref.
Definition: val.h:300
void wasmtime_anyref_unroot(wasmtime_context_t *context, wasmtime_anyref_t *ref)
Unroots the ref provided within the context.
#define WASMTIME_ANYREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an anyref.
Definition: val.h:303
void * wasmtime_externref_data(wasmtime_context_t *context, const wasmtime_externref_t *data)
Get an externref's wrapped data.
void wasmtime_anyref_from_i31(wasmtime_context_t *context, uint32_t i31val, wasmtime_anyref_t *out)
Create a new i31ref value.
#define WASMTIME_I32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i32.
Definition: val.h:286
void wasmtime_anyref_clone(wasmtime_context_t *context, const wasmtime_anyref_t *anyref, wasmtime_anyref_t *out)
Creates a new reference pointing to the same data that anyref points to (depending on the configured ...
uint8_t wasmtime_v128[16]
A 128-bit value representing the WebAssembly v128 type. Bytes are stored in little-endian order.
Definition: val.h:307
#define WASMTIME_F32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f32.
Definition: val.h:290
bool wasmtime_anyref_i31_get_s(wasmtime_context_t *context, const wasmtime_anyref_t *anyref, int32_t *dst)
Get the anyref's underlying i31ref value, sign extended, if any.
#define WASMTIME_FUNCREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a funcref.
Definition: val.h:297
#define WASMTIME_F64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f64.
Definition: val.h:292
void wasmtime_externref_unroot(wasmtime_context_t *context, wasmtime_externref_t *ref)
Unroots the pointer ref from the context provided.
bool wasmtime_anyref_i31_get_u(wasmtime_context_t *context, const wasmtime_anyref_t *anyref, uint32_t *dst)
Get the anyref's underlying i31ref value, zero extended, if any.
bool wasmtime_externref_new(wasmtime_context_t *context, void *data, void(*finalizer)(void *), wasmtime_externref_t *out)
Create a new externref value.
void wasmtime_val_unroot(wasmtime_context_t *context, wasmtime_val_t *val)
Unroot the value contained by val.
void wasmtime_externref_clone(wasmtime_context_t *context, 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...
#define WASMTIME_I64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i64.
Definition: val.h:288
#define WASMTIME_V128
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a v128.
Definition: val.h:294