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 /* `other` may be `*this` or owned by it, so clone it before destroying */ \
88 Raw old = raw; \
89 copy(this == &other ? old : other.raw); \
90 std::swap(old, raw); \
91 destroy(); \
92 raw = old; \
93 return *this; \
94 } \
95 \
96 \
99 name(name &&other) { name::transfer(std::move(other.raw), raw); } \
100 \
101 \
104 name &operator=(name &&other) { \
105 destroy(); \
106 name::transfer(std::move(other.raw), raw); \
107 return *this; \
108 } \
109 \
110 ~name() { destroy(); } \
111 \
112 \
115 const Raw *capi() const { return &raw; } \
116 \
117private:
120class RecordField {
121 friend class Record;
122
124
125 // This value can't be constructed or destructed, it's only used in iteration
126 // of `Record`.
127 RecordField() = delete;
128 ~RecordField() = delete;
129
130 static const RecordField *
131 from_capi(const wasmtime_component_valrecord_entry_t *capi) {
132 return reinterpret_cast<const RecordField *>(capi);
133 }
134
135public:
137 std::string_view name() const {
138 return std::string_view{entry.name.data, entry.name.size};
139 }
140
142 const Val &value() const { return *detail::val_from_capi(&entry.val); }
143};
144
147class Record {
148 friend class Val;
149
152 static void transfer(Raw &&from, Raw &to) {
153 to = from;
154 from.size = 0;
155 from.data = nullptr;
156 }
158 void copy(const Raw &other) {
161
162 void destroy() { wasmtime_component_valrecord_delete(&raw); }
163
164public:
166 Record(std::vector<std::pair<std::string_view, Val>> entries);
167
169 size_t size() const { return raw.size; }
170
172 const RecordField *begin() const { return RecordField::from_capi(raw.data); }
173
175 const RecordField *end() const {
176 return RecordField::from_capi(raw.data + raw.size);
178};
179
181class List {
182 friend class Val;
185
186 static void transfer(Raw &&from, Raw &to) {
187 to = from;
188 from.size = 0;
189 from.data = nullptr;
190 }
191
192 void copy(const Raw &other) { wasmtime_component_vallist_copy(&raw, &other); }
193
194 void destroy() { wasmtime_component_vallist_delete(&raw); }
195
196public:
198 List(std::vector<Val> entries);
199
201 size_t size() const { return raw.size; }
202
204 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
205
207 const Val *end() const {
208 return reinterpret_cast<const Val *>(raw.data + raw.size);
209 }
210};
213class Tuple {
214 friend class Val;
215
218 static void transfer(Raw &&from, Raw &to) {
219 to = from;
220 from.size = 0;
221 from.data = nullptr;
222 }
224 void copy(const Raw &other) {
227
228 void destroy() { wasmtime_component_valtuple_delete(&raw); }
229
230public:
232 Tuple(std::vector<Val> entries);
233
235 size_t size() const { return raw.size; }
236
238 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
239
241 const Val *end() const {
242 return reinterpret_cast<const Val *>(raw.data + raw.size);
243 }
244};
245
247class Variant {
248 friend class Val;
249
251
252 static void transfer(wasmtime_component_valvariant_t &&from,
254 to = from;
255 from.discriminant.size = 0;
256 from.discriminant.data = nullptr;
257 from.val = nullptr;
258 }
259
260 void copy(const wasmtime_component_valvariant_t &other) {
262 if (other.val) {
264 wasmtime_component_val_clone(other.val, &clone);
265 raw.val = wasmtime_component_val_new(&clone);
266 } else {
267 raw.val = nullptr;
268 }
270
271 void destroy() {
274 }
275
276public:
278 Variant(std::string_view discriminant, std::optional<Val> x);
279
281 std::string_view discriminant() const {
282 return std::string_view(raw.discriminant.data, raw.discriminant.size);
283 }
284
286 const Val *value() const { return detail::val_from_capi(raw.val); }
287};
288
290class WitOption {
291 friend class Val;
292
294
295 static void transfer(Raw &&from, Raw &to) {
296 to = from;
297 from = nullptr;
298 }
299
300 void copy(const Raw &other) {
301 if (other) {
303 wasmtime_component_val_clone(other, &clone);
304 raw = wasmtime_component_val_new(&clone);
305 } else {
306 raw = nullptr;
307 }
308 }
309
310 void destroy() { wasmtime_component_val_free(raw); }
311
312public:
314 explicit WitOption(std::optional<Val> val);
315
317 const Val *value() const { return detail::val_from_capi(raw); }
318};
319
321class WitResult {
322 friend class Val;
323
325
326 static void transfer(Raw &&from, Raw &to) {
327 to = from;
328 from.val = nullptr;
329 }
330
331 void copy(const Raw &other) {
332 raw.is_ok = other.is_ok;
333 if (other.val) {
335 wasmtime_component_val_clone(other.val, &clone);
336 raw.val = wasmtime_component_val_new(&clone);
337 } else {
338 raw.val = nullptr;
339 }
340 }
342 void destroy() {
343 if (raw.val)
345 }
346
347public:
349 static WitResult ok(std::optional<Val> val);
350
352 static WitResult err(std::optional<Val> val);
353
355 bool is_ok() const { return raw.is_ok; }
356
358 const Val *payload() const { return detail::val_from_capi(raw.val); }
359};
360
362class Flag {
363 friend class Flags;
364
366
367 static void transfer(Raw &&from, Raw &to) {
368 to = from;
369 from.size = 0;
370 from.data = nullptr;
371 }
372
373 void copy(const Raw &other) { wasm_name_copy(&raw, &other); }
374
375 void destroy() { wasm_name_delete(&raw); }
376
377public:
379 Flag(std::string_view name) { wasm_name_new(&raw, name.size(), name.data()); }
380
382 std::string_view name() const { return std::string_view{raw.data, raw.size}; }
383};
384
386class Flags {
387 friend class Val;
388
390
391 static void transfer(Raw &&from, Raw &to) {
392 to = from;
393 from.size = 0;
394 from.data = nullptr;
396
397 void copy(const Raw &other) {
399 }
400
401 void destroy() { wasmtime_component_valflags_delete(&raw); }
402
403public:
405 Flags(std::vector<Flag> flags) {
406 wasmtime_component_valflags_new_uninit(&raw, flags.size());
407 auto dst = raw.data;
408 for (auto &&val : flags)
409 Flag::transfer(std::move(val.raw), *dst++);
410 }
411
413 size_t size() const { return raw.size; }
414
416 const Flag *begin() const { return reinterpret_cast<const Flag *>(raw.data); }
417
419 const Flag *end() const {
420 return reinterpret_cast<const Flag *>(raw.data + raw.size);
422};
423
425class MapEntry {
426 friend class Map;
427
430 // This value can't be constructed or destructed, it's only used in iteration
431 // of `Map`.
432 MapEntry() = delete;
433 ~MapEntry() = delete;
434
435 static const MapEntry *
436 from_capi(const wasmtime_component_valmap_entry_t *capi) {
437 return reinterpret_cast<const MapEntry *>(capi);
438 }
439
440public:
442 const Val &key() const { return *detail::val_from_capi(&entry.key); }
443
445 const Val &value() const { return *detail::val_from_capi(&entry.value); }
447
450class Map {
451 friend class Val;
454
455 static void transfer(Raw &&from, Raw &to) {
456 to = from;
457 from.size = 0;
458 from.data = nullptr;
459 }
460
461 void copy(const Raw &other) { wasmtime_component_valmap_copy(&raw, &other); }
462
463 void destroy() { wasmtime_component_valmap_delete(&raw); }
464
465public:
467 Map(std::vector<std::pair<Val, Val>> entries);
468
470 size_t size() const { return raw.size; }
471
473 const MapEntry *begin() const { return MapEntry::from_capi(raw.data); }
474
476 const MapEntry *end() const {
477 return MapEntry::from_capi(raw.data + raw.size);
478 }
479};
480
481class ResourceHost;
482
485class ResourceAny {
486 WASMTIME_CLONE_WRAPPER(ResourceAny, wasmtime_component_resource_any);
487
489 bool owned() const { return wasmtime_component_resource_any_owned(capi()); }
492 ResourceType type() const {
495 return ResourceType(ty);
496 }
497
501 wasmtime_error_t *err =
503 if (err)
504 return Error(err);
505 return std::monostate();
506 }
507
509 Result<ResourceHost> to_host(Store::Context cx) const;
510};
511
514class ResourceHost {
515 WASMTIME_CLONE_WRAPPER(ResourceHost, wasmtime_component_resource_host);
516
519 ResourceHost(bool owned, uint32_t rep, uint32_t ty)
520 : ptr(wasmtime_component_resource_host_new(owned, rep, ty)) {}
521
523 bool owned() const { return wasmtime_component_resource_host_owned(capi()); }
524
526 uint32_t rep() const { return wasmtime_component_resource_host_rep(capi()); }
527
529 uint32_t type() const {
531 }
534 Result<ResourceAny> to_any(Store::Context cx) const {
536 wasmtime_error_t *err =
538 if (err)
539 return Error(err);
540 return ResourceAny(out);
541 }
542};
543
544inline Result<ResourceHost> ResourceAny::to_host(Store::Context cx) const {
546 wasmtime_error_t *err =
548 if (err)
549 return Error(err);
550 return ResourceHost(out);
552
556class Val {
557 friend class Variant;
558 friend class WitOption;
559 friend class WitResult;
560
562
563 static void transfer(Raw &&from, Raw &to) {
564 to = from;
566 from.of.boolean = false;
567 }
568
569 void copy(const Raw &other) { wasmtime_component_val_clone(&other, &raw); }
570
571 void destroy() { wasmtime_component_val_delete(&raw); }
572
573public:
575 Val(bool v) {
577 raw.of.boolean = v;
578 }
579
581 Val(uint8_t v) {
583 raw.of.u8 = v;
584 }
585
587 Val(int8_t v) {
589 raw.of.s8 = v;
590 }
591
593 Val(uint16_t v) {
595 raw.of.u16 = v;
596 }
597
599 Val(int16_t v) {
601 raw.of.s16 = v;
602 }
603
605 Val(uint32_t v) {
607 raw.of.u32 = v;
608 }
609
611 Val(int32_t v) {
613 raw.of.s32 = v;
614 }
615
617 Val(uint64_t v) {
619 raw.of.u64 = v;
620 }
621
623 Val(int64_t v) {
625 raw.of.s64 = v;
627
629 Val(float v) {
631 raw.of.f32 = v;
632 }
633
635 Val(double v) {
637 raw.of.f64 = v;
638 }
639
641 static Val char_(uint32_t v) {
644 .of = {.character = v},
645 };
646 return Val(std::move(raw));
647 }
648
650 static Val string(std::string_view v) {
653 wasm_byte_vec_new(&raw.of.string, v.size(), v.data());
654 return Val(std::move(raw));
655 }
656
660 List::transfer(std::move(v.raw), raw.of.list);
661 }
662
666 Record::transfer(std::move(r.raw), raw.of.record);
667 }
668
672 Tuple::transfer(std::move(v.raw), raw.of.tuple);
673 }
674
676 Val(Variant v) {
678 Variant::transfer(std::move(v.raw), raw.of.variant);
680
682 Val(WitOption v) {
684 WitOption::transfer(std::move(v.raw), raw.of.option);
686
688 Val(WitResult r) {
690 WitResult::transfer(std::move(r.raw), raw.of.result);
692
694 static Val enum_(std::string_view discriminant) {
697 wasm_byte_vec_new(&raw.of.enumeration, discriminant.size(),
698 discriminant.data());
699 return Val(std::move(raw));
701
703 Val(Flags f) {
705 Flags::transfer(std::move(f.raw), raw.of.flags);
707
709 Val(Map m) {
711 Map::transfer(std::move(m.raw), raw.of.map);
712 }
713
717 raw.of.resource = r.capi_release();
719
721 bool is_bool() const { return raw.kind == WASMTIME_COMPONENT_BOOL; }
722
724 bool get_bool() const {
725 assert(is_bool());
726 return raw.of.boolean;
728
730 bool is_u8() const { return raw.kind == WASMTIME_COMPONENT_U8; }
731
733 uint8_t get_u8() const {
734 assert(is_u8());
735 return raw.of.u8;
737
739 bool is_s8() const { return raw.kind == WASMTIME_COMPONENT_S8; }
740
742 int8_t get_s8() const {
743 assert(is_s8());
744 return raw.of.s8;
746
748 bool is_u16() const { return raw.kind == WASMTIME_COMPONENT_U16; }
749
751 uint16_t get_u16() const {
752 assert(is_u16());
753 return raw.of.u16;
755
757 bool is_s16() const { return raw.kind == WASMTIME_COMPONENT_S16; }
758
760 int16_t get_s16() const {
761 assert(is_s16());
762 return raw.of.s16;
764
766 bool is_u32() const { return raw.kind == WASMTIME_COMPONENT_U32; }
767
769 uint32_t get_u32() const {
770 assert(is_u32());
771 return raw.of.u32;
773
775 bool is_s32() const { return raw.kind == WASMTIME_COMPONENT_S32; }
776
778 int32_t get_s32() const {
779 assert(is_s32());
780 return raw.of.s32;
782
784 bool is_u64() const { return raw.kind == WASMTIME_COMPONENT_U64; }
785
787 uint64_t get_u64() const {
788 assert(is_u64());
789 return raw.of.u64;
791
793 bool is_s64() const { return raw.kind == WASMTIME_COMPONENT_S64; }
794
796 int64_t get_s64() const {
797 assert(is_s64());
798 return raw.of.s64;
800
802 bool is_f32() const { return raw.kind == WASMTIME_COMPONENT_F32; }
803
805 float get_f32() const {
806 assert(is_f32());
807 return raw.of.f32;
809
811 bool is_f64() const { return raw.kind == WASMTIME_COMPONENT_F64; }
812
814 double get_f64() const {
815 assert(is_f64());
816 return raw.of.f64;
818
820 bool is_string() const { return raw.kind == WASMTIME_COMPONENT_STRING; }
821
823 std::string_view get_string() const {
824 assert(is_string());
825 return std::string_view(raw.of.string.data, raw.of.string.size);
827
829 bool is_list() const { return raw.kind == WASMTIME_COMPONENT_LIST; }
830
832 const List &get_list() const {
833 assert(is_list());
834 return *List::from_capi(&raw.of.list);
836
838 bool is_record() const { return raw.kind == WASMTIME_COMPONENT_RECORD; }
839
841 const Record &get_record() const {
842 assert(is_record());
843 return *Record::from_capi(&raw.of.record);
845
847 bool is_tuple() const { return raw.kind == WASMTIME_COMPONENT_TUPLE; }
848
850 const Tuple &get_tuple() const {
851 assert(is_tuple());
852 return *Tuple::from_capi(&raw.of.tuple);
854
856 bool is_variant() const { return raw.kind == WASMTIME_COMPONENT_VARIANT; }
857
859 const Variant &get_variant() const {
860 assert(is_variant());
861 return *Variant::from_capi(&raw.of.variant);
863
865 bool is_option() const { return raw.kind == WASMTIME_COMPONENT_OPTION; }
866
868 const WitOption &get_option() const {
869 assert(is_option());
870 return *WitOption::from_capi(&raw.of.option);
872
874 bool is_enum() const { return raw.kind == WASMTIME_COMPONENT_ENUM; }
875
877 std::string_view get_enum() const {
878 assert(is_enum());
879 return std::string_view(raw.of.enumeration.data, raw.of.enumeration.size);
881
883 bool is_result() const { return raw.kind == WASMTIME_COMPONENT_RESULT; }
884
886 const WitResult &get_result() const {
887 assert(is_result());
888 return *WitResult::from_capi(&raw.of.result);
890
892 bool is_flags() const { return raw.kind == WASMTIME_COMPONENT_FLAGS; }
893
895 const Flags &get_flags() const {
896 assert(is_flags());
897 return *Flags::from_capi(&raw.of.flags);
898 }
899
901 bool is_resource() const { return raw.kind == WASMTIME_COMPONENT_RESOURCE; }
902
904 const ResourceAny &get_resource() const {
905 assert(is_resource());
906 return *ResourceAny::from_capi(&raw.of.resource);
908
910 bool is_map() const { return raw.kind == WASMTIME_COMPONENT_MAP; }
911
913 const Map &get_map() const {
914 assert(is_map());
915 return *Map::from_capi(&raw.of.map);
916 }
918
919#undef VAL_REPR
920
921inline Record::Record(std::vector<std::pair<std::string_view, Val>> entries) {
922 wasmtime_component_valrecord_new_uninit(&raw, entries.size());
923 auto dst = raw.data;
924 for (auto &&[name, val] : entries) {
925 wasm_byte_vec_new(&dst->name, name.size(), name.data());
926 new (&dst->val) Val(std::move(val));
927 dst++;
928 }
929}
930
931inline Map::Map(std::vector<std::pair<Val, Val>> entries) {
932 wasmtime_component_valmap_new_uninit(&raw, entries.size());
933 auto dst = raw.data;
934 for (auto &&[key, val] : entries) {
935 new (&dst->key) Val(std::move(key));
936 new (&dst->value) Val(std::move(val));
937 dst++;
938 }
939}
941inline List::List(std::vector<Val> values) {
942 wasmtime_component_vallist_new_uninit(&raw, values.size());
943 auto dst = raw.data;
944 for (auto &&val : values)
945 new (dst++) Val(std::move(val));
946}
947
948inline Tuple::Tuple(std::vector<Val> values) {
949 wasmtime_component_valtuple_new_uninit(&raw, values.size());
950 auto dst = raw.data;
951 for (auto &&val : values)
952 new (dst++) Val(std::move(val));
953}
954
955inline Variant::Variant(std::string_view discriminant, std::optional<Val> x) {
956 wasm_name_new(&raw.discriminant, discriminant.size(), discriminant.data());
957 if (x) {
958 raw.val = wasmtime_component_val_new(&x->raw);
959 } else {
960 raw.val = nullptr;
961 }
962}
963
964inline WitOption::WitOption(std::optional<Val> v) {
965 if (v) {
966 raw = wasmtime_component_val_new(&v->raw);
967 } else {
968 raw = nullptr;
969 }
970}
971
972inline WitResult WitResult::ok(std::optional<Val> v) {
974 raw.is_ok = true;
975 if (v) {
976 raw.val = wasmtime_component_val_new(&v->raw);
977 } else {
978 raw.val = nullptr;
979 }
980 return WitResult(std::move(raw));
981}
982
983inline WitResult WitResult::err(std::optional<Val> v) {
985 raw.is_ok = false;
986 if (v) {
987 raw.val = wasmtime_component_val_new(&v->raw);
988 } else {
989 raw.val = nullptr;
990 }
991 return WitResult(std::move(raw));
992}
993
994} // namespace component
995} // namespace wasmtime
996
997#endif // WASMTIME_FEATURE_COMPONENT_MODEL
998
999#endif // WASMTIME_COMPONENT_VAL_HH
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_class.hh:65
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: _store_class.hh:183
Representation of a generic WebAssembly value.
Definition: _val_class.hh:54
Class representing a component model flags value.
Definition: component/val.hh:338
std::string_view name() const
Returns the name of this flag.
Definition: component/val.hh:358
Flag(Raw &&capi)
Definition: component/val.hh:341
Class representing a component model flags value.
Definition: component/val.hh:362
static const Flags * from_capi(const Raw *capi)
Definition: component/val.hh:365
size_t size() const
Returns the number of flags.
Definition: component/val.hh:389
const Flag * end() const
Returns an iterator to the end of the flags.
Definition: component/val.hh:395
Flags(Raw &&capi)
Definition: component/val.hh:365
const Flag * begin() const
Returns an iterator to the beginning of the flags.
Definition: component/val.hh:392
Class representing a component model list, a sequence of values.
Definition: component/val.hh:157
static const List * from_capi(const Raw *capi)
Definition: component/val.hh:160
size_t size() const
Returns the number of entries in the list.
Definition: component/val.hh:177
const Val * begin() const
Returns an iterator to the beginning of the list.
Definition: component/val.hh:180
List(Raw &&capi)
Definition: component/val.hh:160
const Val * end() const
Returns an iterator to the end of the list.
Definition: component/val.hh:183
Class representing an entry in a map value.
Definition: component/val.hh:401
const Val & value() const
Returns the value of this map entry.
Definition: component/val.hh:421
const Val & key() const
Returns the key of this map entry.
Definition: component/val.hh:418
Class representing a component model map, a collection of key/value pairs.
Definition: component/val.hh:426
size_t size() const
Returns the number of entries in the map.
Definition: component/val.hh:446
const MapEntry * end() const
Returns an iterator to the end of the map entries.
Definition: component/val.hh:452
Map(Raw &&capi)
Definition: component/val.hh:429
const MapEntry * begin() const
Returns an iterator to the beginning of the map entries.
Definition: component/val.hh:449
static const Map * from_capi(const Raw *capi)
Definition: component/val.hh:429
Class representing a field in a record value.
Definition: component/val.hh:96
const Val & value() const
Returns the value of this record field.
Definition: component/val.hh:118
std::string_view name() const
Returns the name of this record field.
Definition: component/val.hh:113
Class representing a component model record, a list of name/value pairs.
Definition: component/val.hh:123
size_t size() const
Returns the number of entries in the record.
Definition: component/val.hh:145
static const Record * from_capi(const Raw *capi)
Definition: component/val.hh:126
const RecordField * end() const
Returns an iterator to the end of the record fields.
Definition: component/val.hh:151
const RecordField * begin() const
Returns an iterator to the beginning of the record fields.
Definition: component/val.hh:148
Record(Raw &&capi)
Definition: component/val.hh:126
Definition: component/val.hh:461
Definition: component/types/val.hh:151
Class representing a component model tuple.
Definition: component/val.hh:189
const Val * begin() const
Returns an iterator to the beginning of the tuple.
Definition: component/val.hh:214
Tuple(Raw &&capi)
Definition: component/val.hh:192
size_t size() const
Returns the number of entries in the tuple.
Definition: component/val.hh:211
static const Tuple * from_capi(const Raw *capi)
Definition: component/val.hh:192
const Val * end() const
Returns an iterator to the end of the tuple.
Definition: component/val.hh:217
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:532
const Variant & get_variant() const
Returns the variant value, only valid if is_variant().
Definition: component/val.hh:835
bool is_bool() const
Returns whether this value is a boolean.
Definition: component/val.hh:697
bool is_flags() const
Returns whether this value is flags.
Definition: component/val.hh:868
Val(Raw &&capi)
Definition: component/val.hh:537
bool get_bool() const
Returns the boolean value, only valid if is_bool().
Definition: component/val.hh:700
const Flags & get_flags() const
Returns the flags value, only valid if is_flags().
Definition: component/val.hh:871
std::string_view get_enum() const
Returns the enum discriminant, only valid if is_enum().
Definition: component/val.hh:853
bool is_string() const
Returns whether this value is a string.
Definition: component/val.hh:796
const WitResult & get_result() const
Returns the result value, only valid if is_result().
Definition: component/val.hh:862
bool is_map() const
Returns whether this value is a map.
Definition: component/val.hh:886
const WitOption & get_option() const
Returns the option value, only valid if is_option().
Definition: component/val.hh:844
int32_t get_s32() const
Returns the s32 value, only valid if is_s32().
Definition: component/val.hh:754
static Val char_(uint32_t v)
Creates a new char value.
Definition: component/val.hh:617
bool is_s16() const
Returns whether this value is a s16.
Definition: component/val.hh:733
Val(WitOption v)
Creates a new option value.
Definition: component/val.hh:658
int64_t get_s64() const
Returns the s64 value, only valid if is_s64().
Definition: component/val.hh:772
double get_f64() const
Returns the f64 value, only valid if is_f64().
Definition: component/val.hh:790
bool is_record() const
Returns whether this value is a record.
Definition: component/val.hh:814
static Val enum_(std::string_view discriminant)
Creates a new enum value.
Definition: component/val.hh:670
Val(int32_t v)
Creates a new s32 value.
Definition: component/val.hh:587
bool is_f64() const
Returns whether this value is a f64.
Definition: component/val.hh:787
bool is_option() const
Returns whether this value is an option.
Definition: component/val.hh:841
Val(WitResult r)
Creates a new result value.
Definition: component/val.hh:664
uint8_t get_u8() const
Returns the u8 value, only valid if is_u8().
Definition: component/val.hh:709
std::string_view get_string() const
Returns the string value, only valid if is_string().
Definition: component/val.hh:799
Val(double v)
Creates a new f64 value.
Definition: component/val.hh:611
bool is_s32() const
Returns whether this value is a s32.
Definition: component/val.hh:751
Val(float v)
Creates a new f32 value.
Definition: component/val.hh:605
static Val string(std::string_view v)
Creates a new string value.
Definition: component/val.hh:626
float get_f32() const
Returns the f32 value, only valid if is_f32().
Definition: component/val.hh:781
const Record & get_record() const
Returns the record value, only valid if is_record().
Definition: component/val.hh:817
bool is_result() const
Returns whether this value is a result.
Definition: component/val.hh:859
int16_t get_s16() const
Returns the s16 value, only valid if is_s16().
Definition: component/val.hh:736
Val(uint64_t v)
Creates a new u64 value.
Definition: component/val.hh:593
bool is_u8() const
Returns whether this value is a u8.
Definition: component/val.hh:706
const Map & get_map() const
Returns the map value, only valid if is_map().
Definition: component/val.hh:889
bool is_s64() const
Returns whether this value is a s64.
Definition: component/val.hh:769
const ResourceAny & get_resource() const
Returns the resource value, only valid if is_resource().
Definition: component/val.hh:880
Val(Tuple v)
Creates a new tuple value.
Definition: component/val.hh:646
Val(int16_t v)
Creates a new s16 value.
Definition: component/val.hh:575
bool is_u16() const
Returns whether this value is a u16.
Definition: component/val.hh:724
uint64_t get_u64() const
Returns the u64 value, only valid if is_u64().
Definition: component/val.hh:763
bool is_f32() const
Returns whether this value is a f32.
Definition: component/val.hh:778
bool is_list() const
Returns whether this value is a list.
Definition: component/val.hh:805
bool is_u64() const
Returns whether this value is a u64.
Definition: component/val.hh:760
bool is_s8() const
Returns whether this value is a s8.
Definition: component/val.hh:715
uint32_t get_u32() const
Returns the u32 value, only valid if is_u32().
Definition: component/val.hh:745
Val(int64_t v)
Creates a new s64 value.
Definition: component/val.hh:599
bool is_variant() const
Returns whether this value is a variant.
Definition: component/val.hh:832
int8_t get_s8() const
Returns the s8 value, only valid if is_s8().
Definition: component/val.hh:718
bool is_u32() const
Returns whether this value is a u32.
Definition: component/val.hh:742
bool is_enum() const
Returns whether this value is an enum.
Definition: component/val.hh:850
const List & get_list() const
Returns the list value, only valid if is_list().
Definition: component/val.hh:808
bool is_tuple() const
Returns whether this value is a tuple.
Definition: component/val.hh:823
uint16_t get_u16() const
Returns the u16 value, only valid if is_u16().
Definition: component/val.hh:727
bool is_resource() const
Returns whether this value is a resource.
Definition: component/val.hh:877
Val(uint32_t v)
Creates a new u32 value.
Definition: component/val.hh:581
const Tuple & get_tuple() const
Returns the tuple value, only valid if is_tuple().
Definition: component/val.hh:826
Class representing a component model variant value.
Definition: component/val.hh:223
Variant(Raw &&capi)
Definition: component/val.hh:226
const Val * value() const
Returns the optional payload value associated with this variant value.
Definition: component/val.hh:262
std::string_view discriminant() const
Returns the name of the discriminant of this value.
Definition: component/val.hh:257
static const Variant * from_capi(const Raw *capi)
Definition: component/val.hh:226
Class representing a component model option value.
Definition: component/val.hh:266
const Val * value() const
Returns the optional payload value associated with this option.
Definition: component/val.hh:293
static const WitOption * from_capi(const Raw *capi)
Definition: component/val.hh:269
WitOption(Raw &&capi)
Definition: component/val.hh:269
Class representing a component model result value.
Definition: component/val.hh:297
WitResult(Raw &&capi)
Definition: component/val.hh:300
const Val * payload() const
Returns the optional payload value associated with this result.
Definition: component/val.hh:334
bool is_ok() const
Returns whether this result is the ok variant.
Definition: component/val.hh:331
static WitResult ok(std::optional< Val > val)
Constructs a new result value with the ok variant.
Definition: component/val.hh:948
static const WitResult * from_capi(const Raw *capi)
Definition: component/val.hh:300
static WitResult err(std::optional< Val > val)
Constructs a new result value with the err variant.
Definition: component/val.hh:959
#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
WASM_API_EXTERN void wasmtime_component_valmap_copy(wasmtime_component_valmap_t *dst, const wasmtime_component_valmap_t *src)
Copy src to dst.
#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_MAP
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a map.
Definition: component/val.h:271
#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.
WASM_API_EXTERN void wasmtime_component_valmap_new_uninit(wasmtime_component_valmap_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.
WASM_API_EXTERN void wasmtime_component_valmap_delete(wasmtime_component_valmap_t *value)
Delete value.
#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:383
wasmtime_component_valunion_t of
Value of type kind.
Definition: component/val.h:387
wasmtime_component_valkind_t kind
The type discriminant.
Definition: component/val.h:385
A vec of a wasm_name_t
Definition: component/val.h:302
wasm_name_t * data
Definition: component/val.h:302
size_t size
Definition: component/val.h:302
A vec of a struct wasmtime_component_val
Definition: component/val.h:298
struct wasmtime_component_val * data
Definition: component/val.h:298
size_t size
Definition: component/val.h:298
A pair of a key and a value that represents one entry in a value with kind WASMTIME_COMPONENT_MAP.
Definition: component/val.h:403
wasmtime_component_val_t key
The key of this entry.
Definition: component/val.h:405
wasmtime_component_val_t value
The value of this entry.
Definition: component/val.h:407
A vec of a struct wasmtime_component_valmap_entry
Definition: component/val.h:303
size_t size
Definition: component/val.h:303
struct wasmtime_component_valmap_entry * data
Definition: component/val.h:303
A pair of a name and a value that represents one entry in a value with kind WASMTIME_COMPONENT_RECORD...
Definition: component/val.h:392
wasmtime_component_val_t val
The value of this entry.
Definition: component/val.h:396
wasm_name_t name
The name of this entry.
Definition: component/val.h:394
A vec of a struct wasmtime_component_valrecord_entry
Definition: component/val.h:300
struct wasmtime_component_valrecord_entry * data
Definition: component/val.h:300
size_t size
Definition: component/val.h:300
Represents a result type.
Definition: component/val.h:316
bool is_ok
The discriminant of the result.
Definition: component/val.h:318
struct wasmtime_component_val * val
Definition: component/val.h:321
A vec of a struct wasmtime_component_val
Definition: component/val.h:301
size_t size
Definition: component/val.h:301
struct wasmtime_component_val * data
Definition: component/val.h:301
Represents a variant type.
Definition: component/val.h:308
struct wasmtime_component_val * val
The payload of the variant.
Definition: component/val.h:312
wasm_name_t discriminant
The discriminant of the variant.
Definition: component/val.h:310
Errors generated by Wasmtime.
uint16_t u16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U16.
Definition: component/val.h:336
wasm_name_t string
Definition: component/val.h:353
wasmtime_component_valmap_t map
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_MAP.
Definition: component/val.h:375
int64_t s64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S64.
Definition: component/val.h:342
bool boolean
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_BOOL.
Definition: component/val.h:328
wasmtime_component_resource_any_t * resource
Definition: component/val.h:378
wasmtime_component_valvariant_t variant
Definition: component/val.h:363
uint32_t u32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U32.
Definition: component/val.h:340
wasmtime_component_vallist_t list
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_LIST.
Definition: component/val.h:355
int32_t s32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S32.
Definition: component/val.h:338
float32_t f32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F32.
Definition: component/val.h:346
float64_t f64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F64.
Definition: component/val.h:348
struct wasmtime_component_val * option
Definition: component/val.h:368
int16_t s16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S16.
Definition: component/val.h:334
uint8_t u8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U8.
Definition: component/val.h:332
wasmtime_component_valrecord_t record
Definition: component/val.h:358
uint64_t u64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U64.
Definition: component/val.h:344
wasmtime_component_valresult_t result
Definition: component/val.h:371
wasmtime_component_valflags_t flags
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_FLAGS.
Definition: component/val.h:373
wasmtime_component_valtuple_t tuple
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_TUPLE.
Definition: component/val.h:360
int8_t s8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S8.
Definition: component/val.h:330
wasm_name_t enumeration
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_ENUM.
Definition: component/val.h:365
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