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
74 const wasmtime_externref_t *raw() const { return &val; }
75};
76
80class AnyRef {
81 friend class Val;
82
84
85public:
87 explicit AnyRef(wasmtime_anyref_t val) : val(val) {}
88
91 static AnyRef i31(Store::Context cx, uint32_t value) {
93 wasmtime_anyref_from_i31(cx.ptr, value, &other);
94 return AnyRef(other);
95 }
96
100 wasmtime_anyref_clone(cx.ptr, &val, &other);
101 return AnyRef(other);
102 }
103
106 void unroot(Store::Context cx) { wasmtime_anyref_unroot(cx.ptr, &val); }
107
111 const wasmtime_anyref_t *raw() const { return &val; }
112
114 std::optional<uint32_t> u31(Store::Context cx) const {
115 uint32_t ret = 0;
116 if (wasmtime_anyref_i31_get_u(cx.ptr, &val, &ret))
117 return ret;
118 return std::nullopt;
119 }
120
122 std::optional<int32_t> i31(Store::Context cx) const {
123 int32_t ret = 0;
124 if (wasmtime_anyref_i31_get_s(cx.ptr, &val, &ret))
125 return ret;
126 return std::nullopt;
127 }
128};
129
131struct V128 {
134
136 V128() : v128{} { memset(&v128[0], 0, sizeof(wasmtime_v128)); }
137
139 V128(const wasmtime_v128 &v) : v128{} {
140 memcpy(&v128[0], &v[0], sizeof(wasmtime_v128));
141 }
142};
143
144class Func;
145
156class Val {
157 friend class Global;
158 friend class Table;
159 friend class Func;
160
161 wasmtime_val_t val;
162
163 Val() : val{} {
164 val.kind = WASMTIME_I32;
165 val.of.i32 = 0;
166 }
167 Val(wasmtime_val_t val) : val(val) {}
168
169public:
171 Val(int32_t i32) : val{} {
172 val.kind = WASMTIME_I32;
173 val.of.i32 = i32;
174 }
176 Val(int64_t i64) : val{} {
177 val.kind = WASMTIME_I64;
178 val.of.i64 = i64;
179 }
181 Val(float f32) : val{} {
182 val.kind = WASMTIME_F32;
183 val.of.f32 = f32;
184 }
186 Val(double f64) : val{} {
187 val.kind = WASMTIME_F64;
188 val.of.f64 = f64;
189 }
191 Val(const V128 &v128) : val{} {
192 val.kind = WASMTIME_V128;
193 memcpy(&val.of.v128[0], &v128.v128[0], sizeof(wasmtime_v128));
194 }
196 Val(std::optional<Func> func);
198 Val(Func func);
200 Val(std::optional<ExternRef> ptr) : val{} {
202 if (ptr) {
203 val.of.externref = ptr->val;
204 } else {
205 wasmtime_externref_set_null(&val.of.externref);
206 }
207 }
209 Val(std::optional<AnyRef> ptr) : val{} {
210 val.kind = WASMTIME_ANYREF;
211 if (ptr) {
212 val.of.anyref = ptr->val;
213 } else {
214 wasmtime_anyref_set_null(&val.of.anyref);
215 }
216 }
219 Val(ExternRef ptr);
222 Val(AnyRef ptr);
223
225 ValKind kind() const {
226 switch (val.kind) {
227 case WASMTIME_I32:
228 return ValKind::I32;
229 case WASMTIME_I64:
230 return ValKind::I64;
231 case WASMTIME_F32:
232 return ValKind::F32;
233 case WASMTIME_F64:
234 return ValKind::F64;
235 case WASMTIME_FUNCREF:
236 return ValKind::FuncRef;
238 return ValKind::ExternRef;
239 case WASMTIME_ANYREF:
240 return ValKind::AnyRef;
241 case WASMTIME_V128:
242 return ValKind::V128;
243 }
244 std::abort();
245 }
246
249 int32_t i32() const {
250 if (val.kind != WASMTIME_I32) {
251 std::abort();
252 }
253 return val.of.i32;
254 }
255
258 int64_t i64() const {
259 if (val.kind != WASMTIME_I64) {
260 std::abort();
261 }
262 return val.of.i64;
263 }
264
267 float f32() const {
268 if (val.kind != WASMTIME_F32) {
269 std::abort();
270 }
271 return val.of.f32;
272 }
273
276 double f64() const {
277 if (val.kind != WASMTIME_F64) {
278 std::abort();
279 }
280 return val.of.f64;
281 }
282
285 V128 v128() const {
286 if (val.kind != WASMTIME_V128) {
287 std::abort();
288 }
289 return val.of.v128;
290 }
291
297 std::optional<ExternRef> externref(Store::Context cx) const {
298 if (val.kind != WASMTIME_EXTERNREF) {
299 std::abort();
300 }
301 if (wasmtime_externref_is_null(&val.of.externref)) {
302 return std::nullopt;
303 }
305 wasmtime_externref_clone(cx.ptr, &val.of.externref, &other);
306 return ExternRef(other);
307 }
308
314 std::optional<AnyRef> anyref(Store::Context cx) const {
315 if (val.kind != WASMTIME_ANYREF) {
316 std::abort();
317 }
318 if (wasmtime_anyref_is_null(&val.of.anyref)) {
319 return std::nullopt;
320 }
321 wasmtime_anyref_t other;
322 wasmtime_anyref_clone(cx.ptr, &val.of.anyref, &other);
323 return AnyRef(other);
324 }
325
331 std::optional<Func> funcref() const;
332
334 void unroot(Store::Context cx) { wasmtime_val_unroot(cx.ptr, &val); }
335};
336
337} // namespace wasmtime
338
339// fill in `Func` constructors for `Val`
340#include <wasmtime/func.hh>
341
342#endif // WASMTIME_VAL_HH
Representation of a WebAssembly anyref value.
Definition: val.hh:80
const wasmtime_anyref_t * raw() const
Definition: val.hh:111
static AnyRef i31(Store::Context cx, uint32_t value)
Definition: val.hh:91
void unroot(Store::Context cx)
Definition: val.hh:106
AnyRef clone(Store::Context cx)
Creates a new AnyRef which is separately rooted from this one.
Definition: val.hh:98
std::optional< int32_t > i31(Store::Context cx) const
If this is an i31, get the value sign-extended.
Definition: val.hh:122
std::optional< uint32_t > u31(Store::Context cx) const
If this is an i31, get the value zero-extended.
Definition: val.hh:114
AnyRef(wasmtime_anyref_t val)
Creates a new AnyRef directly from its C-API representation.
Definition: val.hh:87
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:74
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: func.hh:302
A WebAssembly global.
Definition: global.hh:28
An interior pointer into a Store.
Definition: store.hh:60
A WebAssembly table.
Definition: table.hh:31
Representation of a generic WebAssembly value.
Definition: val.hh:156
Val(int32_t i32)
Creates a new i32 WebAssembly value.
Definition: val.hh:171
Val(float f32)
Creates a new f32 WebAssembly value.
Definition: val.hh:181
float f32() const
Definition: val.hh:267
std::optional< AnyRef > anyref(Store::Context cx) const
Definition: val.hh:314
int32_t i32() const
Definition: val.hh:249
double f64() const
Definition: val.hh:276
int64_t i64() const
Definition: val.hh:258
void unroot(Store::Context cx)
Unroots any GC references this Val points to within the cx provided.
Definition: val.hh:334
V128 v128() const
Definition: val.hh:285
ValKind kind() const
Returns the kind of value that this value has.
Definition: val.hh:225
Val(int64_t i64)
Creates a new i64 WebAssembly value.
Definition: val.hh:176
std::optional< ExternRef > externref(Store::Context cx) const
Definition: val.hh:297
Val(const V128 &v128)
Creates a new v128 WebAssembly value.
Definition: val.hh:191
Val(std::optional< ExternRef > ptr)
Creates a new externref value.
Definition: val.hh:200
Val(double f64)
Creates a new f64 WebAssembly value.
Definition: val.hh:186
Val(std::optional< AnyRef > ptr)
Creates a new anyref value.
Definition: val.hh:209
std::optional< Func > funcref() const
Definition: func.hh:614
Container for the v128 WebAssembly type.
Definition: val.hh:131
V128()
Creates a new zero-value v128.
Definition: val.hh:136
wasmtime_v128 v128
The little-endian bytes of the v128 value.
Definition: val.hh:133
V128(const wasmtime_v128 &v)
Creates a new V128 from its C API representation.
Definition: val.hh:139
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