Wasmtime
component/val.hh
Go to the documentation of this file.
1
2
3#ifndef WASMTIME_COMPONENT_VAL_HH
4#define WASMTIME_COMPONENT_VAL_HH
5
6#include <wasmtime/conf.h>
7
8#ifdef WASMTIME_FEATURE_COMPONENT_MODEL
9
10#include <assert.h>
11#include <memory>
12#include <optional>
13#include <string_view>
14#include <utility>
15#include <vector>
18#include <wasmtime/store.hh>
19
20namespace wasmtime {
21namespace component {
22
23class Val;
24class Record;
25
26namespace detail {
27
30inline const Val *val_from_capi(const wasmtime_component_val_t *capi) {
31 return reinterpret_cast<const Val *>(capi);
32}
33
34} // namespace detail
35
39#define VAL_REPR(name, raw_type) \
40private: \
41 using Raw = raw_type; \
42 Raw raw; \
43 \
44public: \
45 \
48 explicit name(Raw &&capi) { name::transfer(std::move(capi), raw); } \
49 \
50 \
54 static const name *from_capi(const Raw *capi) { \
55 return reinterpret_cast<const name *>(capi); \
56 } \
57 \
58 \
62 static name *from_capi(Raw *capi) { return reinterpret_cast<name *>(capi); } \
63 \
64 \
68 static const Raw *to_capi(const name *capi) { \
69 return reinterpret_cast<const Raw *>(capi); \
70 } \
71 \
72 \
76 static Raw *to_capi(name *capi) { return reinterpret_cast<Raw *>(capi); } \
77 \
78 \
81 name(const name &other) { copy(other.raw); } \
82 \
83 \
86 name &operator=(const name &other) { \
87 destroy(); \
88 copy(other.raw); \
89 return *this; \
90 } \
91 \
92 \
95 name(name &&other) { name::transfer(std::move(other.raw), raw); } \
96 \
97 \
100 name &operator=(name &&other) { \
101 destroy(); \
102 name::transfer(std::move(other.raw), raw); \
103 return *this; \
104 } \
105 \
106 ~name() { destroy(); } \
107 \
108 \
111 const Raw *capi() const { return &raw; } \
112 \
113private:
116class RecordField {
117 friend class Record;
118
120
121 // This value can't be constructed or destructed, it's only used in iteration
122 // of `Record`.
123 RecordField() = delete;
124 ~RecordField() = delete;
125
126 static const RecordField *
127 from_capi(const wasmtime_component_valrecord_entry_t *capi) {
128 return reinterpret_cast<const RecordField *>(capi);
129 }
130
131public:
133 std::string_view name() const {
134 return std::string_view{entry.name.data, entry.name.size};
135 }
136
138 const Val &value() const { return *detail::val_from_capi(&entry.val); }
139};
140
143class Record {
144 friend class Val;
145
148 static void transfer(Raw &&from, Raw &to) {
149 to = from;
150 from.size = 0;
151 from.data = nullptr;
152 }
154 void copy(const Raw &other) {
157
158 void destroy() { wasmtime_component_valrecord_delete(&raw); }
159
160public:
162 Record(std::vector<std::pair<std::string_view, Val>> entries);
163
165 size_t size() const { return raw.size; }
166
168 const RecordField *begin() const { return RecordField::from_capi(raw.data); }
169
171 const RecordField *end() const {
172 return RecordField::from_capi(raw.data + raw.size);
174};
175
177class List {
178 friend class Val;
181
182 static void transfer(Raw &&from, Raw &to) {
183 to = from;
184 from.size = 0;
185 from.data = nullptr;
186 }
187
188 void copy(const Raw &other) { wasmtime_component_vallist_copy(&raw, &other); }
189
190 void destroy() { wasmtime_component_vallist_delete(&raw); }
191
192public:
194 List(std::vector<Val> entries);
195
197 size_t size() const { return raw.size; }
198
200 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
201
203 const Val *end() const {
204 return reinterpret_cast<const Val *>(raw.data + raw.size);
205 }
206};
209class Tuple {
210 friend class Val;
211
214 static void transfer(Raw &&from, Raw &to) {
215 to = from;
216 from.size = 0;
217 from.data = nullptr;
218 }
220 void copy(const Raw &other) {
223
224 void destroy() { wasmtime_component_valtuple_delete(&raw); }
225
226public:
228 Tuple(std::vector<Val> entries);
229
231 size_t size() const { return raw.size; }
232
234 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
235
237 const Val *end() const {
238 return reinterpret_cast<const Val *>(raw.data + raw.size);
239 }
240};
241
243class Variant {
244 friend class Val;
245
247
248 static void transfer(wasmtime_component_valvariant_t &&from,
250 to = from;
251 from.discriminant.size = 0;
252 from.discriminant.data = nullptr;
253 from.val = nullptr;
254 }
255
256 void copy(const wasmtime_component_valvariant_t &other) {
258 if (other.val) {
260 wasmtime_component_val_clone(other.val, &clone);
261 raw.val = wasmtime_component_val_new(&clone);
262 } else {
263 raw.val = nullptr;
264 }
266
267 void destroy() {
270 }
271
272public:
274 Variant(std::string_view discriminant, std::optional<Val> x);
275
277 std::string_view discriminant() const {
278 return std::string_view(raw.discriminant.data, raw.discriminant.size);
279 }
280
282 const Val *value() const { return detail::val_from_capi(raw.val); }
283};
284
286class WitOption {
287 friend class Val;
288
290
291 static void transfer(Raw &&from, Raw &to) {
292 to = from;
293 from = nullptr;
294 }
295
296 void copy(const Raw &other) {
297 if (other) {
299 wasmtime_component_val_clone(other, &clone);
300 raw = wasmtime_component_val_new(&clone);
301 } else {
302 raw = nullptr;
303 }
304 }
305
306 void destroy() { wasmtime_component_val_free(raw); }
307
308public:
310 explicit WitOption(std::optional<Val> val);
311
313 const Val *value() const { return detail::val_from_capi(raw); }
314};
315
317class WitResult {
318 friend class Val;
319
321
322 static void transfer(Raw &&from, Raw &to) {
323 to = from;
324 from.val = nullptr;
325 }
326
327 void copy(const Raw &other) {
328 raw.is_ok = other.is_ok;
329 if (other.val) {
331 wasmtime_component_val_clone(other.val, &clone);
332 raw.val = wasmtime_component_val_new(&clone);
333 } else {
334 raw.val = nullptr;
335 }
336 }
338 void destroy() {
339 if (raw.val)
341 }
342
343public:
345 static WitResult ok(std::optional<Val> val);
346
348 static WitResult err(std::optional<Val> val);
349
351 bool is_ok() const { return raw.is_ok; }
352
354 const Val *payload() const { return detail::val_from_capi(raw.val); }
355};
356
358class Flag {
359 friend class Flags;
360
362
363 static void transfer(Raw &&from, Raw &to) {
364 to = from;
365 from.size = 0;
366 from.data = nullptr;
367 }
368
369 void copy(const Raw &other) { wasm_name_copy(&raw, &other); }
370
371 void destroy() { wasm_name_delete(&raw); }
372
373public:
375 Flag(std::string_view name) { wasm_name_new(&raw, name.size(), name.data()); }
376
378 std::string_view name() const { return std::string_view{raw.data, raw.size}; }
379};
380
382class Flags {
383 friend class Val;
384
386
387 static void transfer(Raw &&from, Raw &to) {
388 to = from;
389 from.size = 0;
390 from.data = nullptr;
392
393 void copy(const Raw &other) {
395 }
396
397 void destroy() { wasmtime_component_valflags_delete(&raw); }
398
399public:
401 Flags(std::vector<Flag> flags) {
402 wasmtime_component_valflags_new_uninit(&raw, flags.size());
403 auto dst = raw.data;
404 for (auto &&val : flags)
405 Flag::transfer(std::move(val.raw), *dst++);
406 }
407
409 size_t size() const { return raw.size; }
410
412 const Flag *begin() const { return reinterpret_cast<const Flag *>(raw.data); }
413
415 const Flag *end() const {
416 return reinterpret_cast<const Flag *>(raw.data + raw.size);
417 }
418};
419
420class ResourceHost;
421
424class ResourceAny {
425 WASMTIME_CLONE_WRAPPER(ResourceAny, wasmtime_component_resource_any);
426
428 bool owned() const { return wasmtime_component_resource_any_owned(capi()); }
431 ResourceType type() const {
434 return ResourceType(ty);
435 }
436
440 wasmtime_error_t *err =
442 if (err)
443 return Error(err);
444 return std::monostate();
445 }
446
448 Result<ResourceHost> to_host(Store::Context cx) const;
449};
450
453class ResourceHost {
454 WASMTIME_CLONE_WRAPPER(ResourceHost, wasmtime_component_resource_host);
455
458 ResourceHost(bool owned, uint32_t rep, uint32_t ty)
459 : ptr(wasmtime_component_resource_host_new(owned, rep, ty)) {}
460
462 bool owned() const { return wasmtime_component_resource_host_owned(capi()); }
463
465 uint32_t rep() const { return wasmtime_component_resource_host_rep(capi()); }
466
468 uint32_t type() const {
470 }
473 Result<ResourceAny> to_any(Store::Context cx) const {
475 wasmtime_error_t *err =
477 if (err)
478 return Error(err);
479 return ResourceAny(out);
480 }
481};
482
483inline Result<ResourceHost> ResourceAny::to_host(Store::Context cx) const {
485 wasmtime_error_t *err =
487 if (err)
488 return Error(err);
489 return ResourceHost(out);
491
495class Val {
496 friend class Variant;
497 friend class WitOption;
498 friend class WitResult;
499
501
502 static void transfer(Raw &&from, Raw &to) {
503 to = from;
505 from.of.boolean = false;
506 }
507
508 void copy(const Raw &other) { wasmtime_component_val_clone(&other, &raw); }
509
510 void destroy() { wasmtime_component_val_delete(&raw); }
511
512public:
514 Val(bool v) {
516 raw.of.boolean = v;
517 }
518
520 Val(uint8_t v) {
522 raw.of.u8 = v;
523 }
524
526 Val(int8_t v) {
528 raw.of.s8 = v;
529 }
530
532 Val(uint16_t v) {
534 raw.of.u16 = v;
535 }
536
538 Val(int16_t v) {
540 raw.of.s16 = v;
541 }
542
544 Val(uint32_t v) {
546 raw.of.u32 = v;
547 }
548
550 Val(int32_t v) {
552 raw.of.s32 = v;
553 }
554
556 Val(uint64_t v) {
558 raw.of.u64 = v;
559 }
560
562 Val(int64_t v) {
564 raw.of.s64 = v;
566
568 Val(float v) {
570 raw.of.f32 = v;
571 }
572
574 Val(double v) {
576 raw.of.f64 = v;
577 }
578
580 static Val char_(uint32_t v) {
583 .of = {.character = v},
584 };
585 return Val(std::move(raw));
586 }
587
589 static Val string(std::string_view v) {
592 wasm_byte_vec_new(&raw.of.string, v.size(), v.data());
593 return Val(std::move(raw));
594 }
595
599 List::transfer(std::move(v.raw), raw.of.list);
600 }
601
605 Record::transfer(std::move(r.raw), raw.of.record);
606 }
607
611 Tuple::transfer(std::move(v.raw), raw.of.tuple);
612 }
613
615 Val(Variant v) {
617 Variant::transfer(std::move(v.raw), raw.of.variant);
619
621 Val(WitOption v) {
623 WitOption::transfer(std::move(v.raw), raw.of.option);
625
627 Val(WitResult r) {
629 WitResult::transfer(std::move(r.raw), raw.of.result);
631
633 static Val enum_(std::string_view discriminant) {
636 wasm_byte_vec_new(&raw.of.enumeration, discriminant.size(),
637 discriminant.data());
638 return Val(std::move(raw));
640
644 Flags::transfer(std::move(f.raw), raw.of.flags);
645 }
646
650 raw.of.resource = r.capi_release();
652
654 bool is_bool() const { return raw.kind == WASMTIME_COMPONENT_BOOL; }
655
657 bool get_bool() const {
658 assert(is_bool());
659 return raw.of.boolean;
661
663 bool is_u8() const { return raw.kind == WASMTIME_COMPONENT_U8; }
664
666 uint8_t get_u8() const {
667 assert(is_u8());
668 return raw.of.u8;
670
672 bool is_s8() const { return raw.kind == WASMTIME_COMPONENT_S8; }
673
675 int8_t get_s8() const {
676 assert(is_s8());
677 return raw.of.s8;
679
681 bool is_u16() const { return raw.kind == WASMTIME_COMPONENT_U16; }
682
684 uint16_t get_u16() const {
685 assert(is_u16());
686 return raw.of.u16;
688
690 bool is_s16() const { return raw.kind == WASMTIME_COMPONENT_S16; }
691
693 int16_t get_s16() const {
694 assert(is_s16());
695 return raw.of.s16;
697
699 bool is_u32() const { return raw.kind == WASMTIME_COMPONENT_U32; }
700
702 uint32_t get_u32() const {
703 assert(is_u32());
704 return raw.of.u32;
706
708 bool is_s32() const { return raw.kind == WASMTIME_COMPONENT_S32; }
709
711 int32_t get_s32() const {
712 assert(is_s32());
713 return raw.of.s32;
715
717 bool is_u64() const { return raw.kind == WASMTIME_COMPONENT_U64; }
718
720 uint64_t get_u64() const {
721 assert(is_u64());
722 return raw.of.u64;
724
726 bool is_s64() const { return raw.kind == WASMTIME_COMPONENT_S64; }
727
729 int64_t get_s64() const {
730 assert(is_s64());
731 return raw.of.s64;
733
735 bool is_f32() const { return raw.kind == WASMTIME_COMPONENT_F32; }
736
738 float get_f32() const {
739 assert(is_f32());
740 return raw.of.f32;
742
744 bool is_f64() const { return raw.kind == WASMTIME_COMPONENT_F64; }
745
747 double get_f64() const {
748 assert(is_f64());
749 return raw.of.f64;
751
753 bool is_string() const { return raw.kind == WASMTIME_COMPONENT_STRING; }
754
756 std::string_view get_string() const {
757 assert(is_string());
758 return std::string_view(raw.of.string.data, raw.of.string.size);
760
762 bool is_list() const { return raw.kind == WASMTIME_COMPONENT_LIST; }
763
765 const List &get_list() const {
766 assert(is_list());
767 return *List::from_capi(&raw.of.list);
769
771 bool is_record() const { return raw.kind == WASMTIME_COMPONENT_RECORD; }
772
774 const Record &get_record() const {
775 assert(is_record());
776 return *Record::from_capi(&raw.of.record);
778
780 bool is_tuple() const { return raw.kind == WASMTIME_COMPONENT_TUPLE; }
781
783 const Tuple &get_tuple() const {
784 assert(is_tuple());
785 return *Tuple::from_capi(&raw.of.tuple);
787
789 bool is_variant() const { return raw.kind == WASMTIME_COMPONENT_VARIANT; }
790
792 const Variant &get_variant() const {
793 assert(is_variant());
794 return *Variant::from_capi(&raw.of.variant);
796
798 bool is_option() const { return raw.kind == WASMTIME_COMPONENT_OPTION; }
799
801 const WitOption &get_option() const {
802 assert(is_option());
803 return *WitOption::from_capi(&raw.of.option);
805
807 bool is_enum() const { return raw.kind == WASMTIME_COMPONENT_ENUM; }
808
810 std::string_view get_enum() const {
811 assert(is_enum());
812 return std::string_view(raw.of.enumeration.data, raw.of.enumeration.size);
814
816 bool is_result() const { return raw.kind == WASMTIME_COMPONENT_RESULT; }
817
819 const WitResult &get_result() const {
820 assert(is_result());
822 }
823
825 bool is_flags() const { return raw.kind == WASMTIME_COMPONENT_FLAGS; }
826
828 const Flags &get_flags() const {
829 assert(is_flags());
830 return *Flags::from_capi(&raw.of.flags);
832
834 bool is_resource() const { return raw.kind == WASMTIME_COMPONENT_RESOURCE; }
835
837 const ResourceAny &get_resource() const {
838 assert(is_resource());
839 return *ResourceAny::from_capi(&raw.of.resource);
840 }
841};
842
843#undef VAL_REPR
844
845inline Record::Record(std::vector<std::pair<std::string_view, Val>> entries) {
846 wasmtime_component_valrecord_new_uninit(&raw, entries.size());
847 auto dst = raw.data;
848 for (auto &&[name, val] : entries) {
849 wasm_byte_vec_new(&dst->name, name.size(), name.data());
850 new (&dst->val) Val(std::move(val));
851 dst++;
852 }
853}
855inline List::List(std::vector<Val> values) {
856 wasmtime_component_vallist_new_uninit(&raw, values.size());
857 auto dst = raw.data;
858 for (auto &&val : values)
859 new (dst++) Val(std::move(val));
860}
861
862inline Tuple::Tuple(std::vector<Val> values) {
863 wasmtime_component_valtuple_new_uninit(&raw, values.size());
864 auto dst = raw.data;
865 for (auto &&val : values)
866 new (dst++) Val(std::move(val));
867}
868
869inline Variant::Variant(std::string_view discriminant, std::optional<Val> x) {
870 wasm_name_new(&raw.discriminant, discriminant.size(), discriminant.data());
871 if (x) {
872 raw.val = wasmtime_component_val_new(&x->raw);
873 } else {
874 raw.val = nullptr;
875 }
876}
877
878inline WitOption::WitOption(std::optional<Val> v) {
879 if (v) {
880 raw = wasmtime_component_val_new(&v->raw);
881 } else {
882 raw = nullptr;
883 }
884}
885
886inline WitResult WitResult::ok(std::optional<Val> v) {
888 raw.is_ok = true;
889 if (v) {
890 raw.val = wasmtime_component_val_new(&v->raw);
891 } else {
892 raw.val = nullptr;
893 }
894 return WitResult(std::move(raw));
895}
896
897inline WitResult WitResult::err(std::optional<Val> v) {
899 raw.is_ok = false;
900 if (v) {
901 raw.val = wasmtime_component_val_new(&v->raw);
902 } else {
903 raw.val = nullptr;
904 }
905 return WitResult(std::move(raw));
906}
907
908} // namespace component
909} // namespace wasmtime
910
911#endif // WASMTIME_FEATURE_COMPONENT_MODEL
912
913#endif // WASMTIME_COMPONENT_VAL_H
Errors coming from Wasmtime.
Definition: error.hh:26
Fallible result type used for Wasmtime.
Definition: error.hh:70
An interior pointer into a Store.
Definition: store.hh:66
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:160
Representation of a generic WebAssembly value.
Definition: val.hh:204
Class representing a component model flags value.
Definition: component/val.hh:334
std::string_view name() const
Returns the name of this flag.
Definition: component/val.hh:354
Flag(Raw &&capi)
Definition: component/val.hh:337
Class representing a component model flags value.
Definition: component/val.hh:358
static const Flags * from_capi(const Raw *capi)
Definition: component/val.hh:361
size_t size() const
Returns the number of flags.
Definition: component/val.hh:385
const Flag * end() const
Returns an iterator to the end of the flags.
Definition: component/val.hh:391
Flags(Raw &&capi)
Definition: component/val.hh:361
const Flag * begin() const
Returns an iterator to the beginning of the flags.
Definition: component/val.hh:388
Class representing a component model list, a sequence of values.
Definition: component/val.hh:153
static const List * from_capi(const Raw *capi)
Definition: component/val.hh:156
size_t size() const
Returns the number of entries in the list.
Definition: component/val.hh:173
const Val * begin() const
Returns an iterator to the beginning of the list.
Definition: component/val.hh:176
List(Raw &&capi)
Definition: component/val.hh:156
const Val * end() const
Returns an iterator to the end of the list.
Definition: component/val.hh:179
Class representing a field in a record value.
Definition: component/val.hh:92
const Val & value() const
Returns the value of this record field.
Definition: component/val.hh:114
std::string_view name() const
Returns the name of this record field.
Definition: component/val.hh:109
Class representing a component model record, a list of name/value pairs.
Definition: component/val.hh:119
size_t size() const
Returns the number of entries in the record.
Definition: component/val.hh:141
static const Record * from_capi(const Raw *capi)
Definition: component/val.hh:122
const RecordField * end() const
Returns an iterator to the end of the record fields.
Definition: component/val.hh:147
const RecordField * begin() const
Returns an iterator to the beginning of the record fields.
Definition: component/val.hh:144
Record(Raw &&capi)
Definition: component/val.hh:122
Definition: component/val.hh:400
Definition: component/types/val.hh:151
Class representing a component model tuple.
Definition: component/val.hh:185
const Val * begin() const
Returns an iterator to the beginning of the tuple.
Definition: component/val.hh:210
Tuple(Raw &&capi)
Definition: component/val.hh:188
size_t size() const
Returns the number of entries in the tuple.
Definition: component/val.hh:207
static const Tuple * from_capi(const Raw *capi)
Definition: component/val.hh:188
const Val * end() const
Returns an iterator to the end of the tuple.
Definition: component/val.hh:213
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:471
const Variant & get_variant() const
Returns the variant value, only valid if is_variant().
Definition: component/val.hh:768
bool is_bool() const
Returns whether this value is a boolean.
Definition: component/val.hh:630
bool is_flags() const
Returns whether this value is flags.
Definition: component/val.hh:801
Val(Raw &&capi)
Definition: component/val.hh:476
bool get_bool() const
Returns the boolean value, only valid if is_bool().
Definition: component/val.hh:633
const Flags & get_flags() const
Returns the flags value, only valid if is_flags().
Definition: component/val.hh:804
std::string_view get_enum() const
Returns the enum discriminant, only valid if is_enum().
Definition: component/val.hh:786
bool is_string() const
Returns whether this value is a string.
Definition: component/val.hh:729
const WitResult & get_result() const
Returns the result value, only valid if is_result().
Definition: component/val.hh:795
const WitOption & get_option() const
Returns the option value, only valid if is_option().
Definition: component/val.hh:777
int32_t get_s32() const
Returns the s32 value, only valid if is_s32().
Definition: component/val.hh:687
static Val char_(uint32_t v)
Creates a new char value.
Definition: component/val.hh:556
bool is_s16() const
Returns whether this value is a s16.
Definition: component/val.hh:666
Val(WitOption v)
Creates a new option value.
Definition: component/val.hh:597
int64_t get_s64() const
Returns the s64 value, only valid if is_s64().
Definition: component/val.hh:705
double get_f64() const
Returns the f64 value, only valid if is_f64().
Definition: component/val.hh:723
bool is_record() const
Returns whether this value is a record.
Definition: component/val.hh:747
static Val enum_(std::string_view discriminant)
Creates a new enum value.
Definition: component/val.hh:609
Val(int32_t v)
Creates a new s32 value.
Definition: component/val.hh:526
bool is_f64() const
Returns whether this value is a f64.
Definition: component/val.hh:720
bool is_option() const
Returns whether this value is an option.
Definition: component/val.hh:774
Val(WitResult r)
Creates a new result value.
Definition: component/val.hh:603
uint8_t get_u8() const
Returns the u8 value, only valid if is_u8().
Definition: component/val.hh:642
std::string_view get_string() const
Returns the string value, only valid if is_string().
Definition: component/val.hh:732
Val(double v)
Creates a new f64 value.
Definition: component/val.hh:550
bool is_s32() const
Returns whether this value is a s32.
Definition: component/val.hh:684
Val(float v)
Creates a new f32 value.
Definition: component/val.hh:544
static Val string(std::string_view v)
Creates a new string value.
Definition: component/val.hh:565
float get_f32() const
Returns the f32 value, only valid if is_f32().
Definition: component/val.hh:714
const Record & get_record() const
Returns the record value, only valid if is_record().
Definition: component/val.hh:750
bool is_result() const
Returns whether this value is a result.
Definition: component/val.hh:792
int16_t get_s16() const
Returns the s16 value, only valid if is_s16().
Definition: component/val.hh:669
Val(uint64_t v)
Creates a new u64 value.
Definition: component/val.hh:532
bool is_u8() const
Returns whether this value is a u8.
Definition: component/val.hh:639
bool is_s64() const
Returns whether this value is a s64.
Definition: component/val.hh:702
const ResourceAny & get_resource() const
Returns the flags value, only valid if is_flags().
Definition: component/val.hh:813
Val(Tuple v)
Creates a new tuple value.
Definition: component/val.hh:585
Val(int16_t v)
Creates a new s16 value.
Definition: component/val.hh:514
bool is_u16() const
Returns whether this value is a u16.
Definition: component/val.hh:657
uint64_t get_u64() const
Returns the u64 value, only valid if is_u64().
Definition: component/val.hh:696
bool is_f32() const
Returns whether this value is a f32.
Definition: component/val.hh:711
bool is_list() const
Returns whether this value is a list.
Definition: component/val.hh:738
bool is_u64() const
Returns whether this value is a u64.
Definition: component/val.hh:693
bool is_s8() const
Returns whether this value is a s8.
Definition: component/val.hh:648
uint32_t get_u32() const
Returns the u32 value, only valid if is_u32().
Definition: component/val.hh:678
Val(int64_t v)
Creates a new s64 value.
Definition: component/val.hh:538
bool is_variant() const
Returns whether this value is a variant.
Definition: component/val.hh:765
int8_t get_s8() const
Returns the s8 value, only valid if is_s8().
Definition: component/val.hh:651
bool is_u32() const
Returns whether this value is a u32.
Definition: component/val.hh:675
bool is_enum() const
Returns whether this value is an enum.
Definition: component/val.hh:783
const List & get_list() const
Returns the list value, only valid if is_list().
Definition: component/val.hh:741
bool is_tuple() const
Returns whether this value is a tuple.
Definition: component/val.hh:756
uint16_t get_u16() const
Returns the u16 value, only valid if is_u16().
Definition: component/val.hh:660
bool is_resource() const
Returns whether this value is a resource.
Definition: component/val.hh:810
Val(uint32_t v)
Creates a new u32 value.
Definition: component/val.hh:520
const Tuple & get_tuple() const
Returns the tuple value, only valid if is_tuple().
Definition: component/val.hh:759
Class representing a component model variant value.
Definition: component/val.hh:219
Variant(Raw &&capi)
Definition: component/val.hh:222
const Val * value() const
Returns the optional payload value associated with this variant value.
Definition: component/val.hh:258
std::string_view discriminant() const
Returns the name of the discriminant of this value.
Definition: component/val.hh:253
static const Variant * from_capi(const Raw *capi)
Definition: component/val.hh:222
Class representing a component model option value.
Definition: component/val.hh:262
const Val * value() const
Returns the optional payload value associated with this option.
Definition: component/val.hh:289
static const WitOption * from_capi(const Raw *capi)
Definition: component/val.hh:265
WitOption(Raw &&capi)
Definition: component/val.hh:265
Class representing a component model result value.
Definition: component/val.hh:293
WitResult(Raw &&capi)
Definition: component/val.hh:296
const Val * payload() const
Returns the optional payload value associated with this result.
Definition: component/val.hh:330
bool is_ok() const
Returns whether this result is the ok variant.
Definition: component/val.hh:327
static WitResult ok(std::optional< Val > val)
Constructs a new result value with the ok variant.
Definition: component/val.hh:862
static const WitResult * from_capi(const Raw *capi)
Definition: component/val.hh:296
static WitResult err(std::optional< Val > val)
Constructs a new result value with the err variant.
Definition: component/val.hh:873
#define WASMTIME_COMPONENT_U8
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u8.
Definition: component/val.h:211
#define WASMTIME_COMPONENT_U16
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u16.
Definition: component/val.h:217
WASM_API_EXTERN void wasmtime_component_vallist_copy(wasmtime_component_vallist_t *dst, const wasmtime_component_vallist_t *src)
Copy src to dst.
#define WASMTIME_COMPONENT_ENUM
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a enum.
Definition: component/val.h:256
#define WASMTIME_COMPONENT_CHAR
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a char.
Definition: component/val.h:238
#define WASMTIME_COMPONENT_RESOURCE
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a resource.
Definition: component/val.h:268
struct wasmtime_component_resource_host wasmtime_component_resource_host_t
Represents a host-defined component resource.
Definition: component/val.h:116
#define WASMTIME_COMPONENT_RESULT
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a result.
Definition: component/val.h:262
#define WASMTIME_COMPONENT_S8
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s8.
Definition: component/val.h:208
WASM_API_EXTERN void wasmtime_component_vallist_delete(wasmtime_component_vallist_t *value)
Delete value.
WASM_API_EXTERN bool wasmtime_component_resource_any_owned(const wasmtime_component_resource_any_t *resource)
Returns whether this resource is an own, or a borrow in the component model.
#define WASMTIME_COMPONENT_BOOL
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a bool.
Definition: component/val.h:205
#define WASMTIME_COMPONENT_LIST
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a list.
Definition: component/val.h:244
#define WASMTIME_COMPONENT_RECORD
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a record.
Definition: component/val.h:247
WASM_API_EXTERN void wasmtime_component_val_clone(const wasmtime_component_val_t *src, wasmtime_component_val_t *dst)
Performs a deep copy of the provided src, storing the results into dst.
WASM_API_EXTERN void wasmtime_component_vallist_new_uninit(wasmtime_component_vallist_t *out, size_t size)
Create a vec with length size.
#define WASMTIME_COMPONENT_U64
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u64.
Definition: component/val.h:229
WASM_API_EXTERN void wasmtime_component_valflags_new_uninit(wasmtime_component_valflags_t *out, size_t size)
Create a vec with length size.
#define WASMTIME_COMPONENT_S64
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s64.
Definition: component/val.h:226
WASM_API_EXTERN bool wasmtime_component_resource_host_owned(const wasmtime_component_resource_host_t *resource)
Returns whether this host-defined resource is an own or a borrow in the component model.
struct wasmtime_component_resource_any wasmtime_component_resource_any_t
Represents a component resource which can be either guest-owned or host-owned.
Definition: component/val.h:40
#define WASMTIME_COMPONENT_U32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u32.
Definition: component/val.h:223
WASM_API_EXTERN wasmtime_component_resource_type_t * wasmtime_component_resource_any_type(const wasmtime_component_resource_any_t *resource)
Gets the type of a component resource.
WASM_API_EXTERN void wasmtime_component_valflags_delete(wasmtime_component_valflags_t *value)
Delete value.
#define WASMTIME_COMPONENT_VARIANT
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a variant.
Definition: component/val.h:253
#define WASMTIME_COMPONENT_TUPLE
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a tuple.
Definition: component/val.h:250
WASM_API_EXTERN void wasmtime_component_valrecord_new_uninit(wasmtime_component_valrecord_t *out, size_t size)
Create a vec with length size.
#define WASMTIME_COMPONENT_S16
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s16.
Definition: component/val.h:214
WASM_API_EXTERN void wasmtime_component_valrecord_delete(wasmtime_component_valrecord_t *value)
Delete value.
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_resource_any_to_host(wasmtime_context_t *ctx, const wasmtime_component_resource_any_t *resource, wasmtime_component_resource_host_t **ret)
Attempts to convert a wasmtime_component_resource_any_t into a wasmtime_component_resource_host_t.
WASM_API_EXTERN wasmtime_component_val_t * wasmtime_component_val_new(wasmtime_component_val_t *val)
Allocates a new wasmtime_component_val_t on the heap, initializing it with the contents of val.
WASM_API_EXTERN void wasmtime_component_val_free(wasmtime_component_val_t *ptr)
Deallocates the heap-allocated value at ptr.
#define WASMTIME_COMPONENT_OPTION
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a option.
Definition: component/val.h:259
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_resource_any_drop(wasmtime_context_t *ctx, const wasmtime_component_resource_any_t *resource)
Drops a component resource.
#define WASMTIME_COMPONENT_FLAGS
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is flags.
Definition: component/val.h:265
WASM_API_EXTERN void wasmtime_component_valflags_copy(wasmtime_component_valflags_t *dst, const wasmtime_component_valflags_t *src)
Copy src to dst.
#define WASMTIME_COMPONENT_S32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s32.
Definition: component/val.h:220
#define WASMTIME_COMPONENT_F32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a f32.
Definition: component/val.h:232
WASM_API_EXTERN void wasmtime_component_valrecord_copy(wasmtime_component_valrecord_t *dst, const wasmtime_component_valrecord_t *src)
Copy src to dst.
WASM_API_EXTERN wasmtime_component_resource_host_t * wasmtime_component_resource_host_new(bool owned, uint32_t rep, uint32_t ty)
Creates a new host-defined component resource.
WASM_API_EXTERN uint32_t wasmtime_component_resource_host_type(const wasmtime_component_resource_host_t *resource)
Gets the "type" of a host-defined component resource.
#define WASMTIME_COMPONENT_F64
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a f64.
Definition: component/val.h:235
WASM_API_EXTERN uint32_t wasmtime_component_resource_host_rep(const wasmtime_component_resource_host_t *resource)
Gets the "rep" of a host-defined component resource.
WASM_API_EXTERN void wasmtime_component_valtuple_delete(wasmtime_component_valtuple_t *value)
Delete value.
WASM_API_EXTERN void wasmtime_component_val_delete(wasmtime_component_val_t *value)
Deallocates any memory owned by value.
WASM_API_EXTERN void wasmtime_component_valtuple_new_uninit(wasmtime_component_valtuple_t *out, size_t size)
Create a vec with length size.
#define WASMTIME_COMPONENT_STRING
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a string.
Definition: component/val.h:241
WASM_API_EXTERN wasmtime_error_t * wasmtime_component_resource_host_to_any(wasmtime_context_t *ctx, const wasmtime_component_resource_host_t *resource, wasmtime_component_resource_any_t **ret)
Same as wasmtime_component_resource_any_to_host except for converting the other way around.
WASM_API_EXTERN void wasmtime_component_valtuple_copy(wasmtime_component_valtuple_t *dst, const wasmtime_component_valtuple_t *src)
Copy src to dst.
#define VAL_REPR(name, raw_type)
Definition: component/val.hh:39
const Val * val_from_capi(const wasmtime_component_val_t *capi)
Definition: component/val.hh:30
Build-time defines for how the C API was built.
struct wasmtime_component_resource_type wasmtime_component_resource_type_t
Represents the type of a component resource.
Definition: resource.h:21
A list of bytes.
Definition: wasm.h:102
size_t size
Length of this vector.
Definition: wasm.h:102
wasm_byte_t * data
Pointer to the base of this vector.
Definition: wasm.h:102
Represents possible runtime values which a component function can either consume or produce.
Definition: component/val.h:376
wasmtime_component_valunion_t of
Value of type kind.
Definition: component/val.h:380
wasmtime_component_valkind_t kind
The type discriminant.
Definition: component/val.h:378
A vec of a wasm_name_t
Definition: component/val.h:298
wasm_name_t * data
Definition: component/val.h:298
size_t size
Definition: component/val.h:298
A vec of a struct wasmtime_component_val
Definition: component/val.h:294
struct wasmtime_component_val * data
Definition: component/val.h:294
size_t size
Definition: component/val.h:294
A pair of a name and a value that represents one entry in a value with kind WASMTIME_COMPONENT_RECORD...
Definition: component/val.h:385
wasmtime_component_val_t val
The value of this entry.
Definition: component/val.h:389
wasm_name_t name
The name of this entry.
Definition: component/val.h:387
A vec of a struct wasmtime_component_valrecord_entry
Definition: component/val.h:296
struct wasmtime_component_valrecord_entry * data
Definition: component/val.h:296
size_t size
Definition: component/val.h:296
Represents a result type.
Definition: component/val.h:311
bool is_ok
The discriminant of the result.
Definition: component/val.h:313
struct wasmtime_component_val * val
Definition: component/val.h:316
A vec of a struct wasmtime_component_val
Definition: component/val.h:297
size_t size
Definition: component/val.h:297
struct wasmtime_component_val * data
Definition: component/val.h:297
Represents a variant type.
Definition: component/val.h:303
struct wasmtime_component_val * val
The payload of the variant.
Definition: component/val.h:307
wasm_name_t discriminant
The discriminant of the variant.
Definition: component/val.h:305
Errors generated by Wasmtime.
uint16_t u16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U16.
Definition: component/val.h:331
wasm_name_t string
Definition: component/val.h:348
int64_t s64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S64.
Definition: component/val.h:337
bool boolean
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_BOOL.
Definition: component/val.h:323
wasmtime_component_resource_any_t * resource
Definition: component/val.h:371
wasmtime_component_valvariant_t variant
Definition: component/val.h:358
uint32_t u32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U32.
Definition: component/val.h:335
wasmtime_component_vallist_t list
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_LIST.
Definition: component/val.h:350
int32_t s32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S32.
Definition: component/val.h:333
float32_t f32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F32.
Definition: component/val.h:341
float64_t f64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F64.
Definition: component/val.h:343
struct wasmtime_component_val * option
Definition: component/val.h:363
int16_t s16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S16.
Definition: component/val.h:329
uint8_t u8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U8.
Definition: component/val.h:327
wasmtime_component_valrecord_t record
Definition: component/val.h:353
uint64_t u64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U64.
Definition: component/val.h:339
wasmtime_component_valresult_t result
Definition: component/val.h:366
wasmtime_component_valflags_t flags
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_FLAGS.
Definition: component/val.h:368
wasmtime_component_valtuple_t tuple
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_TUPLE.
Definition: component/val.h:355
int8_t s8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S8.
Definition: component/val.h:325
wasm_name_t enumeration
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_ENUM.
Definition: component/val.h:360
void wasm_byte_vec_new(wasm_byte_vec_t *out, size_t, wasm_byte_t const[])
Copies the specified data into a new byte vector.
#define wasm_name_new
Convenience alias.
Definition: wasm.h:107
#define wasm_name_copy
Convenience alias.
Definition: wasm.h:110
#define wasm_name_delete
Convenience alias.
Definition: wasm.h:111