12#ifdef WASMTIME_FEATURE_GC
39 explicit EqRef(wasmtime_eqref_t val) : val(val) {}
42 EqRef(
const EqRef &other) { wasmtime_eqref_clone(&other.val, &val); }
45 EqRef &operator=(
const EqRef &other) {
46 wasmtime_eqref_unroot(&val);
47 wasmtime_eqref_clone(&other.val, &val);
52 EqRef(EqRef &&other) {
54 wasmtime_eqref_set_null(&other.val);
58 EqRef &operator=(EqRef &&other) {
59 wasmtime_eqref_unroot(&val);
61 wasmtime_eqref_set_null(&other.val);
65 ~EqRef() { wasmtime_eqref_unroot(&val); }
68 static EqRef from_i31(Store::Context cx, uint32_t val) {
70 wasmtime_eqref_from_i31(cx.capi(), val, &out);
75 bool is_i31(Store::Context cx)
const {
76 return wasmtime_eqref_is_i31(cx.capi(), &val);
81 std::optional<uint32_t> i31_get_u(Store::Context cx)
const {
83 if (wasmtime_eqref_i31_get_u(cx.capi(), &val, &dst))
90 std::optional<int32_t> i31_get_s(Store::Context cx)
const {
92 if (wasmtime_eqref_i31_get_s(cx.capi(), &val, &dst))
98 bool is_struct(Store::Context cx)
const {
99 return wasmtime_eqref_is_struct(cx.capi(), &val);
103 bool is_array(Store::Context cx)
const {
104 return wasmtime_eqref_is_array(cx.capi(), &val);
108 AnyRef to_anyref()
const {
109 wasmtime_anyref_t out;
110 wasmtime_eqref_to_anyref(&val, &out);
117 inline StructRef as_struct(Store::Context cx)
const;
122 inline ArrayRef as_array(Store::Context cx)
const;
131 wasmtime_storage_kind_t kind;
136 static FieldType mut_(wasmtime_storage_kind_t k) {
return {k,
true}; }
138 static FieldType const_(wasmtime_storage_kind_t k) {
return {k,
false}; }
149 void operator()(wasmtime_struct_type_t *p)
const {
150 wasmtime_struct_type_delete(p);
153 std::unique_ptr<wasmtime_struct_type_t, Deleter> ptr;
157 static StructType create(
const Engine &engine,
158 const std::vector<FieldType> &fields) {
159 static_assert(
sizeof(FieldType) ==
sizeof(wasmtime_field_type_t));
160 auto *raw = wasmtime_struct_type_new(
162 reinterpret_cast<const wasmtime_field_type_t *
>(fields.data()),
170 const wasmtime_struct_type_t *capi()
const {
return ptr.get(); }
173 StructType() =
default;
174 friend class StructRefPre;
184 friend class StructRef;
185 WASMTIME_OWN_WRAPPER(StructRefPre, wasmtime_struct_ref_pre)
189 static StructRefPre create(Store::Context cx,
const StructType &ty) {
190 auto *raw = wasmtime_struct_ref_pre_new(cx.capi(), ty.capi());
191 StructRefPre pre(raw);
207 wasmtime_structref_t val;
211 explicit StructRef(wasmtime_structref_t val) : val(val) {}
214 StructRef(
const StructRef &other) {
215 wasmtime_structref_clone(&other.val, &val);
219 StructRef &operator=(
const StructRef &other) {
220 wasmtime_structref_unroot(&val);
221 wasmtime_structref_clone(&other.val, &val);
226 StructRef(StructRef &&other) {
228 wasmtime_structref_set_null(&other.val);
232 StructRef &operator=(StructRef &&other) {
233 wasmtime_structref_unroot(&val);
235 wasmtime_structref_set_null(&other.val);
240 ~StructRef() { wasmtime_structref_unroot(&val); }
243 static Result<StructRef> create(Store::Context cx,
const StructRefPre &pre,
244 const std::vector<Val> &fields) {
245 std::vector<wasmtime_val_t> c_fields;
246 c_fields.reserve(fields.size());
247 for (
auto &f : fields) {
248 c_fields.push_back(f.val);
251 wasmtime_structref_t out;
252 auto *err = wasmtime_structref_new(cx.capi(), pre.capi(), c_fields.data(),
253 c_fields.size(), &out);
255 return Result<StructRef>(Error(err));
256 return Result<StructRef>(StructRef(out));
260 Result<Val> field(Store::Context cx,
size_t index)
const {
262 auto *err = wasmtime_structref_field(cx.capi(), &val, index, &out);
264 return Result<Val>(Error(err));
265 return Result<Val>(Val(out));
269 Result<std::monostate> set_field(Store::Context cx,
size_t index,
270 const Val &value)
const {
272 wasmtime_structref_set_field(cx.capi(), &val, index, &value.val);
274 return Result<std::monostate>(Error(err));
275 return Result<std::monostate>(std::monostate{});
279 AnyRef to_anyref()
const {
280 wasmtime_anyref_t out;
281 wasmtime_structref_to_anyref(&val, &out);
286 EqRef to_eqref()
const {
287 wasmtime_eqref_t out;
288 wasmtime_structref_to_eqref(&val, &out);
293inline StructRef EqRef::as_struct(Store::Context cx)
const {
294 wasmtime_structref_t out;
295 wasmtime_eqref_as_struct(cx.capi(), &val, &out);
296 return StructRef(out);
304 void operator()(wasmtime_array_type_t *p)
const {
305 wasmtime_array_type_delete(p);
308 std::unique_ptr<wasmtime_array_type_t, Deleter> ptr;
312 static ArrayType create(
const Engine &engine,
const FieldType &field) {
313 static_assert(
sizeof(FieldType) ==
sizeof(wasmtime_field_type_t));
314 auto *raw = wasmtime_array_type_new(
315 engine.capi(),
reinterpret_cast<const wasmtime_field_type_t *
>(&field));
322 const wasmtime_array_type_t *capi()
const {
return ptr.get(); }
325 ArrayType() =
default;
326 friend class ArrayRefPre;
336 friend class ArrayRef;
337 WASMTIME_OWN_WRAPPER(ArrayRefPre, wasmtime_array_ref_pre)
341 static ArrayRefPre create(Store::Context cx,
const ArrayType &ty) {
342 auto *raw = wasmtime_array_ref_pre_new(cx.capi(), ty.capi());
343 ArrayRefPre pre(raw);
359 wasmtime_arrayref_t val;
363 explicit ArrayRef(wasmtime_arrayref_t val) : val(val) {}
366 ArrayRef(
const ArrayRef &other) { wasmtime_arrayref_clone(&other.val, &val); }
369 ArrayRef &operator=(
const ArrayRef &other) {
370 wasmtime_arrayref_unroot(&val);
371 wasmtime_arrayref_clone(&other.val, &val);
376 ArrayRef(ArrayRef &&other) {
378 wasmtime_arrayref_set_null(&other.val);
382 ArrayRef &operator=(ArrayRef &&other) {
383 wasmtime_arrayref_unroot(&val);
385 wasmtime_arrayref_set_null(&other.val);
390 ~ArrayRef() { wasmtime_arrayref_unroot(&val); }
393 static Result<ArrayRef> create(Store::Context cx,
const ArrayRefPre &pre,
394 const Val &elem, uint32_t len) {
395 wasmtime_arrayref_t out;
397 wasmtime_arrayref_new(cx.capi(), pre.capi(), &elem.val, len, &out);
399 return Result<ArrayRef>(Error(err));
400 return Result<ArrayRef>(ArrayRef(out));
404 Result<uint32_t> len(Store::Context cx)
const {
406 auto *err = wasmtime_arrayref_len(cx.capi(), &val, &out);
408 return Result<uint32_t>(Error(err));
409 return Result<uint32_t>(out);
413 Result<Val> get(Store::Context cx, uint32_t index)
const {
415 auto *err = wasmtime_arrayref_get(cx.capi(), &val, index, &out);
417 return Result<Val>(Error(err));
418 return Result<Val>(Val(out));
422 Result<std::monostate> set(Store::Context cx, uint32_t index,
423 const Val &value)
const {
424 auto *err = wasmtime_arrayref_set(cx.capi(), &val, index, &value.val);
426 return Result<std::monostate>(Error(err));
427 return Result<std::monostate>(std::monostate{});
431 AnyRef to_anyref()
const {
432 wasmtime_anyref_t out;
433 wasmtime_arrayref_to_anyref(&val, &out);
438 EqRef to_eqref()
const {
439 wasmtime_eqref_t out;
440 wasmtime_arrayref_to_eqref(&val, &out);
445inline ArrayRef EqRef::as_array(Store::Context cx)
const {
446 wasmtime_arrayref_t out;
447 wasmtime_eqref_as_array(cx.capi(), &val, &out);
448 return ArrayRef(out);
452inline bool AnyRef::is_eqref(Store::Context cx)
const {
453 return wasmtime_anyref_is_eqref(cx.capi(), &val);
455inline bool AnyRef::is_struct(Store::Context cx)
const {
456 return wasmtime_anyref_is_struct(cx.capi(), &val);
458inline bool AnyRef::is_array(Store::Context cx)
const {
459 return wasmtime_anyref_is_array(cx.capi(), &val);
461inline std::optional<EqRef> AnyRef::as_eqref(Store::Context cx)
const {
462 wasmtime_eqref_t out;
463 if (wasmtime_anyref_as_eqref(cx.capi(), &val, &out))
467inline std::optional<StructRef> AnyRef::as_struct(Store::Context cx)
const {
468 wasmtime_structref_t out;
469 if (wasmtime_anyref_as_struct(cx.capi(), &val, &out))
470 return StructRef(out);
473inline std::optional<ArrayRef> AnyRef::as_array(Store::Context cx)
const {
474 wasmtime_arrayref_t out;
475 if (wasmtime_anyref_as_array(cx.capi(), &val, &out))
476 return ArrayRef(out);
Container for different kinds of wasm values.
Definition: val.h:546
@ AnyRef
WebAssembly's anyref type.