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
42 ExternRef(const ExternRef &other) {
43 wasmtime_externref_clone(&other.val, &val);
44 }
45
47 ExternRef &operator=(const ExternRef &other) {
49 wasmtime_externref_clone(&other.val, &val);
50 return *this;
51 }
52
55 val = other.val;
56 wasmtime_externref_set_null(&other.val);
57 }
58
62 val = other.val;
63 wasmtime_externref_set_null(&other.val);
64 return *this;
65 }
66
68
74 explicit ExternRef(Store::Context cx, std::any val) {
75 void *ptr = std::make_unique<std::any>(val).release();
76 bool ok = wasmtime_externref_new(cx.ptr, ptr, finalizer, &this->val);
77 if (!ok) {
78 fprintf(stderr, "failed to allocate a new externref");
79 abort();
80 }
81 }
82
84 std::any &data(Store::Context cx) {
85 return *static_cast<std::any *>(wasmtime_externref_data(cx.ptr, &val));
86 }
87
90 uint32_t take_raw(Store::Context cx) {
91 uint32_t ret = wasmtime_externref_to_raw(cx.raw_context(), &val);
92 wasmtime_externref_set_null(&val);
93 return ret;
94 }
95
97 uint32_t borrow_raw(Store::Context cx) const {
98 return wasmtime_externref_to_raw(cx.raw_context(), &val);
99 }
100};
101
105class AnyRef {
106 friend class Val;
107
109
110public:
112 explicit AnyRef(wasmtime_anyref_t val) : val(val) {}
113
115 AnyRef(const AnyRef &other) { wasmtime_anyref_clone(&other.val, &val); }
116
118 AnyRef &operator=(const AnyRef &other) {
120 wasmtime_anyref_clone(&other.val, &val);
121 return *this;
122 }
123
125 AnyRef(AnyRef &&other) {
126 val = other.val;
127 wasmtime_anyref_set_null(&other.val);
128 }
129
133 val = other.val;
134 wasmtime_anyref_set_null(&other.val);
135 return *this;
136 }
137
139
142 static AnyRef i31(Store::Context cx, uint32_t value) {
143 wasmtime_anyref_t other;
144 wasmtime_anyref_from_i31(cx.ptr, value, &other);
145 return AnyRef(other);
146 }
147
151 uint32_t ret = wasmtime_anyref_to_raw(cx.raw_context(), &val);
152 wasmtime_anyref_set_null(&val);
153 return ret;
154 }
155
157 uint32_t borrow_raw(Store::Context cx) const {
158 return wasmtime_anyref_to_raw(cx.raw_context(), &val);
159 }
160
162 std::optional<uint32_t> u31(Store::Context cx) const {
163 uint32_t ret = 0;
164 if (wasmtime_anyref_i31_get_u(cx.ptr, &val, &ret))
165 return ret;
166 return std::nullopt;
167 }
168
170 std::optional<int32_t> i31(Store::Context cx) const {
171 int32_t ret = 0;
172 if (wasmtime_anyref_i31_get_s(cx.ptr, &val, &ret))
173 return ret;
174 return std::nullopt;
175 }
176};
177
179struct V128 {
182
184 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
185
187 V128(const wasmtime_v128 &v) : v128{} {
188 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
189 }
190};
191
192class Func;
193
204class Val {
205 friend class Global;
206 friend class Table;
207 friend class Func;
208
209 wasmtime_val_t val;
210
211 Val() : val{} {
212 val.kind = WASMTIME_I32;
213 val.of.i32 = 0;
214 }
215 Val(wasmtime_val_t val) : val(val) {}
216
217public:
219 Val(int32_t i32) : val{} {
220 val.kind = WASMTIME_I32;
221 val.of.i32 = i32;
222 }
224 Val(int64_t i64) : val{} {
225 val.kind = WASMTIME_I64;
226 val.of.i64 = i64;
227 }
229 Val(float f32) : val{} {
230 val.kind = WASMTIME_F32;
231 val.of.f32 = f32;
232 }
234 Val(double f64) : val{} {
235 val.kind = WASMTIME_F64;
236 val.of.f64 = f64;
237 }
239 Val(const V128 &v128) : val{} {
240 val.kind = WASMTIME_V128;
241 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
242 }
244 Val(std::optional<Func> func);
246 Val(Func func);
248 Val(std::optional<ExternRef> ptr) : val{} {
250 if (ptr) {
251 val.of.externref = ptr->val;
252 wasmtime_externref_set_null(&ptr->val);
253 } else {
254 wasmtime_externref_set_null(&val.of.externref);
255 }
256 }
258 Val(std::optional<AnyRef> ptr) : val{} {
259 val.kind = WASMTIME_ANYREF;
260 if (ptr) {
261 val.of.anyref = ptr->val;
262 wasmtime_anyref_set_null(&ptr->val);
263 } else {
264 wasmtime_anyref_set_null(&val.of.anyref);
265 }
266 }
269 Val(ExternRef ptr);
272 Val(AnyRef ptr);
273
275 Val(const Val &other) { wasmtime_val_clone(&other.val, &val); }
276
278 Val &operator=(const Val &other) {
280 wasmtime_val_clone(&other.val, &val);
281 return *this;
282 }
283
285 Val(Val &&other) {
286 val = other.val;
287 other.val.kind = WASMTIME_I32;
288 other.val.of.i32 = 0;
289 }
290
292 Val &operator=(Val &&other) {
294 val = other.val;
295 other.val.kind = WASMTIME_I32;
296 other.val.of.i32 = 0;
297 return *this;
298 }
299
302
304 ValKind kind() const {
305 switch (val.kind) {
306 case WASMTIME_I32:
307 return ValKind::I32;
308 case WASMTIME_I64:
309 return ValKind::I64;
310 case WASMTIME_F32:
311 return ValKind::F32;
312 case WASMTIME_F64:
313 return ValKind::F64;
314 case WASMTIME_FUNCREF:
315 return ValKind::FuncRef;
317 return ValKind::ExternRef;
318 case WASMTIME_ANYREF:
319 return ValKind::AnyRef;
320 case WASMTIME_V128:
321 return ValKind::V128;
322 }
323 std::abort();
324 }
325
328 int32_t i32() const {
329 if (val.kind != WASMTIME_I32) {
330 std::abort();
331 }
332 return val.of.i32;
333 }
334
337 int64_t i64() const {
338 if (val.kind != WASMTIME_I64) {
339 std::abort();
340 }
341 return val.of.i64;
342 }
343
346 float f32() const {
347 if (val.kind != WASMTIME_F32) {
348 std::abort();
349 }
350 return val.of.f32;
351 }
352
355 double f64() const {
356 if (val.kind != WASMTIME_F64) {
357 std::abort();
358 }
359 return val.of.f64;
360 }
361
364 V128 v128() const {
365 if (val.kind != WASMTIME_V128) {
366 std::abort();
367 }
368 return val.of.v128;
369 }
370
376 std::optional<ExternRef> externref() const {
377 if (val.kind != WASMTIME_EXTERNREF) {
378 std::abort();
379 }
380 if (wasmtime_externref_is_null(&val.of.externref)) {
381 return std::nullopt;
382 }
385 return ExternRef(other);
386 }
387
393 std::optional<AnyRef> anyref() const {
394 if (val.kind != WASMTIME_ANYREF) {
395 std::abort();
396 }
397 if (wasmtime_anyref_is_null(&val.of.anyref)) {
398 return std::nullopt;
399 }
400 wasmtime_anyref_t other;
401 wasmtime_anyref_clone(&val.of.anyref, &other);
402 return AnyRef(other);
403 }
404
410 std::optional<Func> funcref() const;
411};
412
413} // namespace wasmtime
414
415// fill in `Func` constructors for `Val`
416#include <wasmtime/func.hh>
417
418#endif // WASMTIME_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:105
AnyRef(AnyRef &&other)
Move constructor to move the contents of other.
Definition: val.hh:125
static AnyRef i31(Store::Context cx, uint32_t value)
Definition: val.hh:142
AnyRef & operator=(AnyRef &&other)
Move assignment to move the contents of other.
Definition: val.hh:131
AnyRef & operator=(const AnyRef &other)
Copy assignment to clone from other.
Definition: val.hh:118
std::optional< int32_t > i31(Store::Context cx) const
If this is an i31, get the value sign-extended.
Definition: val.hh:170
uint32_t borrow_raw(Store::Context cx) const
Returns wasmtime_anyref_to_raw.
Definition: val.hh:157
AnyRef(const AnyRef &other)
Copy constructor to clone other.
Definition: val.hh:115
std::optional< uint32_t > u31(Store::Context cx) const
If this is an i31, get the value zero-extended.
Definition: val.hh:162
uint32_t take_raw(Store::Context cx)
Definition: val.hh:150
AnyRef(wasmtime_anyref_t val)
Creates a new AnyRef directly from its C-API representation.
Definition: val.hh:112
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
ExternRef & operator=(const ExternRef &other)
Copy assignment to clone from other.
Definition: val.hh:47
ExternRef(Store::Context cx, std::any val)
Definition: val.hh:74
ExternRef & operator=(ExternRef &&other)
Move assignment to move the contents of other.
Definition: val.hh:60
ExternRef(ExternRef &&other)
Move constructor to move the contents of other.
Definition: val.hh:54
uint32_t borrow_raw(Store::Context cx) const
Returns wasmtime_externref_to_raw.
Definition: val.hh:97
ExternRef(const ExternRef &other)
Copy constructor to clone other.
Definition: val.hh:42
uint32_t take_raw(Store::Context cx)
Definition: val.hh:90
std::any & data(Store::Context cx)
Returns the underlying host data associated with this ExternRef.
Definition: val.hh:84
Representation of a WebAssembly function.
Definition: func.hh:336
A WebAssembly global.
Definition: global.hh:28
An interior pointer into a Store.
Definition: store.hh:60
wasmtime_context_t * raw_context()
Returns the raw context pointer for the C API.
Definition: store.hh:153
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: val.hh:204
Val(const Val &other)
Copy constructor to clone other.
Definition: val.hh:275
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: val.hh:219
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: val.hh:229
float f32() const
Definition: val.hh:346
int32_t i32() const
Definition: val.hh:328
double f64() const
Definition: val.hh:355
std::optional< AnyRef > anyref() const
Definition: val.hh:393
int64_t i64() const
Definition: val.hh:337
V128 v128() const
Definition: val.hh:364
ValKind kind() const
Returns the kind of value that this value has.
Definition: val.hh:304
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: val.hh:224
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: val.hh:239
Val(std::optional< ExternRef > ptr)
Creates a new externref value.
Definition: val.hh:248
~Val()
Unroots the values in val, if any.
Definition: val.hh:301
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: val.hh:234
Val & operator=(const Val &other)
Copy assignment to clone from other.
Definition: val.hh:278
Val(std::optional< AnyRef > ptr)
Creates a new anyref value.
Definition: val.hh:258
Val & operator=(Val &&other)
Move assignment to move the contents of other.
Definition: val.hh:292
std::optional< ExternRef > externref() const
Definition: val.hh:376
std::optional< Func > funcref() const
Definition: func.hh:649
Val(Val &&other)
Move constructor to move the contents of other.
Definition: val.hh:285
Container for the v128 WebAssembly type.
Definition: val.hh:179
V128()
Creates a new zero-value v128.
Definition: val.hh:184
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: val.hh:181
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: val.hh:187
A WebAssembly value in the any hierarchy of GC types.
Definition: val.h:43
A host-defined un-forgeable reference to pass into WebAssembly.
Definition: val.h:178
Container for different kinds of wasm values.
Definition: val.h:456
wasmtime_valkind_t kind
Discriminant of which field of of is valid.
Definition: val.h:458
wasmtime_valunion_t of
Container for the extern item's value.
Definition: val.h:460
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:329
wasmtime_v128 v128
Field used if wasmtime_val_t::kind is WASMTIME_V128.
Definition: val.h:346
float32_t f32
Field used if wasmtime_val_t::kind is WASMTIME_F32.
Definition: val.h:333
int64_t i64
Field used if wasmtime_val_t::kind is WASMTIME_I64.
Definition: val.h:331
float64_t f64
Field used if wasmtime_val_t::kind is WASMTIME_F64.
Definition: val.h:335
wasmtime_anyref_t anyref
Field used if wasmtime_val_t::kind is WASMTIME_ANYREF.
Definition: val.h:337
wasmtime_externref_t externref
Field used if wasmtime_val_t::kind is WASMTIME_EXTERNREF.
Definition: val.h:339
#define WASMTIME_EXTERNREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an externref.
Definition: val.h:308
uint32_t wasmtime_externref_to_raw(wasmtime_context_t *context, const wasmtime_externref_t *ref)
Converts a wasmtime_externref_t to a raw value suitable for storing into a wasmtime_val_raw_t.
#define WASMTIME_ANYREF
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an anyref.
Definition: val.h:311
void wasmtime_val_unroot(wasmtime_val_t *val)
Unroot the value contained by val.
void wasmtime_val_clone(const wasmtime_val_t *src, wasmtime_val_t *dst)
Clones the value pointed to by src into the dst provided.
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.
void wasmtime_anyref_unroot(wasmtime_anyref_t *ref)
Unroots the ref provided within the context.
#define WASMTIME_I32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i32.
Definition: val.h:294
uint8_t wasmtime_v128[16]
A 128-bit value representing the WebAssembly v128 type. Bytes are stored in little-endian order.
Definition: val.h:315
#define WASMTIME_F32
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f32.
Definition: val.h:298
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:305
#define WASMTIME_F64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a f64.
Definition: val.h:300
void wasmtime_anyref_clone(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 ...
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_externref_unroot(wasmtime_externref_t *ref)
Unroots the pointer ref from the context provided.
uint32_t wasmtime_anyref_to_raw(wasmtime_context_t *context, const wasmtime_anyref_t *ref)
Converts a wasmtime_anyref_t to a raw value suitable for storing into a wasmtime_val_raw_t.
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...
#define WASMTIME_I64
Value of wasmtime_valkind_t meaning that wasmtime_val_t is an i64.
Definition: val.h:296
#define WASMTIME_V128
Value of wasmtime_valkind_t meaning that wasmtime_val_t is a v128.
Definition: val.h:302