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>
17#include <wasmtime/store.hh>
18
19namespace wasmtime {
20namespace component {
21
22class Val;
23class Record;
24
25namespace detail {
26
29inline const Val *val_from_capi(const wasmtime_component_val_t *capi) {
30 return reinterpret_cast<const Val *>(capi);
31}
32
33} // namespace detail
34
38#define VAL_REPR(name, raw_type) \
39private: \
40 using Raw = raw_type; \
41 Raw raw; \
42 \
43public: \
44 \
47 explicit name(Raw &&capi) { name::transfer(std::move(capi), raw); } \
48 \
49 \
53 static const name *from_capi(const Raw *capi) { \
54 return reinterpret_cast<const name *>(capi); \
55 } \
56 \
57 \
61 static name *from_capi(Raw *capi) { return reinterpret_cast<name *>(capi); } \
62 \
63 \
67 static const Raw *to_capi(const name *capi) { \
68 return reinterpret_cast<const Raw *>(capi); \
69 } \
70 \
71 \
75 static Raw *to_capi(name *capi) { return reinterpret_cast<Raw *>(capi); } \
76 \
77 \
80 name(const name &other) { copy(other.raw); } \
81 \
82 \
85 name &operator=(const name &other) { \
86 destroy(); \
87 copy(other.raw); \
88 return *this; \
89 } \
90 \
91 \
94 name(name &&other) { name::transfer(std::move(other.raw), raw); } \
95 \
96 \
99 name &operator=(name &&other) { \
100 destroy(); \
101 name::transfer(std::move(other.raw), raw); \
102 return *this; \
103 } \
104 \
105 ~name() { destroy(); } \
106 \
107 \
110 const Raw *capi() const { return &raw; } \
111 \
112private:
115class RecordField {
116 friend class Record;
117
119
120 // This value can't be constructed or destructed, it's only used in iteration
121 // of `Record`.
122 RecordField() = delete;
123 ~RecordField() = delete;
124
125 static const RecordField *
126 from_capi(const wasmtime_component_valrecord_entry_t *capi) {
127 return reinterpret_cast<const RecordField *>(capi);
128 }
129
130public:
132 std::string_view name() const {
133 return std::string_view{entry.name.data, entry.name.size};
134 }
135
137 const Val &value() const { return *detail::val_from_capi(&entry.val); }
138};
139
142class Record {
143 friend class Val;
144
147 static void transfer(Raw &&from, Raw &to) {
148 to = from;
149 from.size = 0;
150 from.data = nullptr;
151 }
153 void copy(const Raw &other) {
156
157 void destroy() { wasmtime_component_valrecord_delete(&raw); }
158
159public:
161 Record(std::vector<std::pair<std::string_view, Val>> entries);
162
164 size_t size() const { return raw.size; }
165
167 const RecordField *begin() const { return RecordField::from_capi(raw.data); }
168
170 const RecordField *end() const {
171 return RecordField::from_capi(raw.data + raw.size);
173};
174
176class List {
177 friend class Val;
180
181 static void transfer(Raw &&from, Raw &to) {
182 to = from;
183 from.size = 0;
184 from.data = nullptr;
185 }
186
187 void copy(const Raw &other) { wasmtime_component_vallist_copy(&raw, &other); }
188
189 void destroy() { wasmtime_component_vallist_delete(&raw); }
190
191public:
193 List(std::vector<Val> entries);
194
196 size_t size() const { return raw.size; }
197
199 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
200
202 const Val *end() const {
203 return reinterpret_cast<const Val *>(raw.data + raw.size);
204 }
205};
208class Tuple {
209 friend class Val;
210
213 static void transfer(Raw &&from, Raw &to) {
214 to = from;
215 from.size = 0;
216 from.data = nullptr;
217 }
219 void copy(const Raw &other) {
222
223 void destroy() { wasmtime_component_valtuple_delete(&raw); }
224
225public:
227 Tuple(std::vector<Val> entries);
228
230 size_t size() const { return raw.size; }
231
233 const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }
234
236 const Val *end() const {
237 return reinterpret_cast<const Val *>(raw.data + raw.size);
238 }
239};
240
242class Variant {
243 friend class Val;
244
246
247 static void transfer(wasmtime_component_valvariant_t &&from,
249 to = from;
250 from.discriminant.size = 0;
251 from.discriminant.data = nullptr;
252 from.val = nullptr;
253 }
254
255 void copy(const wasmtime_component_valvariant_t &other) {
257 if (other.val) {
259 wasmtime_component_val_clone(other.val, &clone);
260 raw.val = wasmtime_component_val_new(&clone);
261 } else {
262 raw.val = nullptr;
263 }
265
266 void destroy() {
269 }
270
271public:
273 Variant(std::string_view discriminant, std::optional<Val> x);
274
276 std::string_view discriminant() const {
277 return std::string_view(raw.discriminant.data, raw.discriminant.size);
278 }
279
281 const Val *value() const { return detail::val_from_capi(raw.val); }
282};
283
285class WitOption {
286 friend class Val;
287
289
290 static void transfer(Raw &&from, Raw &to) {
291 to = from;
292 from = nullptr;
293 }
294
295 void copy(const Raw &other) {
296 if (other) {
298 wasmtime_component_val_clone(other, &clone);
299 raw = wasmtime_component_val_new(&clone);
300 } else {
301 raw = nullptr;
302 }
303 }
304
305 void destroy() { wasmtime_component_val_free(raw); }
306
307public:
309 explicit WitOption(std::optional<Val> val);
310
312 const Val *value() const { return detail::val_from_capi(raw); }
313};
314
316class WitResult {
317 friend class Val;
318
320
321 static void transfer(Raw &&from, Raw &to) {
322 to = from;
323 from.val = nullptr;
324 }
325
326 void copy(const Raw &other) {
327 raw.is_ok = other.is_ok;
328 if (other.val) {
330 wasmtime_component_val_clone(other.val, &clone);
331 raw.val = wasmtime_component_val_new(&clone);
332 } else {
333 raw.val = nullptr;
334 }
335 }
337 void destroy() {
338 if (raw.val)
340 }
341
342public:
344 static WitResult ok(std::optional<Val> val);
345
347 static WitResult err(std::optional<Val> val);
348
350 bool is_ok() const { return raw.is_ok; }
351
353 const Val *payload() const { return detail::val_from_capi(raw.val); }
354};
355
357class Flag {
358 friend class Flags;
359
361
362 static void transfer(Raw &&from, Raw &to) {
363 to = from;
364 from.size = 0;
365 from.data = nullptr;
366 }
367
368 void copy(const Raw &other) { wasm_name_copy(&raw, &other); }
369
370 void destroy() { wasm_name_delete(&raw); }
371
372public:
374 Flag(std::string_view name) { wasm_name_new(&raw, name.size(), name.data()); }
375
377 std::string_view name() const { return std::string_view{raw.data, raw.size}; }
378};
379
381class Flags {
382 friend class Val;
383
385
386 static void transfer(Raw &&from, Raw &to) {
387 to = from;
388 from.size = 0;
389 from.data = nullptr;
391
392 void copy(const Raw &other) {
394 }
395
396 void destroy() { wasmtime_component_valflags_delete(&raw); }
398public:
400 Flags(std::vector<Flag> flags) {
402 auto dst = raw.data;
403 for (auto &&val : flags)
404 Flag::transfer(std::move(val.raw), *dst++);
405 }
406
408 size_t size() const { return raw.size; }
409
411 const Flag *begin() const { return reinterpret_cast<const Flag *>(raw.data); }
414 const Flag *end() const {
415 return reinterpret_cast<const Flag *>(raw.data + raw.size);
417};
418
421class ResourceType {
422 struct deleter {
423 void operator()(wasmtime_component_resource_type_t *p) const {
426 };
428 std::unique_ptr<wasmtime_component_resource_type_t, deleter> ptr;
429
430public:
432 explicit ResourceType(wasmtime_component_resource_type_t *raw) : ptr(raw) {}
433
436 explicit ResourceType(uint32_t ty)
438
441 : ptr(wasmtime_component_resource_type_clone(other.ptr.get())) {}
444 ptr.reset(wasmtime_component_resource_type_clone(other.ptr.get()));
445 return *this;
446 }
447 ~ResourceType() = default;
449 ResourceType(ResourceType &&other) = default;
451 ResourceType &operator=(ResourceType &&other) = default;
452
454 bool operator==(const ResourceType &b) const {
456 }
457
459 bool operator!=(const ResourceType &b) const {
460 return !wasmtime_component_resource_type_equal(capi(), b.capi());
462
464 const wasmtime_component_resource_type_t *capi() const { return ptr.get(); }
465
468};
469
470class ResourceHost;
471
474class ResourceAny {
475 struct deleter {
476 void operator()(wasmtime_component_resource_any_t *p) const {
479 };
480
481 std::unique_ptr<wasmtime_component_resource_any_t, deleter> ptr;
482
483public:
485 explicit ResourceAny(wasmtime_component_resource_any_t *raw) : ptr(raw) {}
486
488 ResourceAny(const ResourceAny &other)
489 : ptr(wasmtime_component_resource_any_clone(other.ptr.get())) {}
491 ResourceAny &operator=(const ResourceAny &other) {
492 ptr.reset(wasmtime_component_resource_any_clone(other.ptr.get()));
493 return *this;
494 }
495 ~ResourceAny() = default;
497 ResourceAny(ResourceAny &&other) = default;
499 ResourceAny &operator=(ResourceAny &&other) = default;
500
502 const wasmtime_component_resource_any_t *capi() const { return ptr.get(); }
503
505 wasmtime_component_resource_any_t *capi() { return ptr.get(); }
506
508 wasmtime_component_resource_any_t *capi_release() { return ptr.release(); }
509
511 bool owned() const { return wasmtime_component_resource_any_owned(capi()); }
512
517 return ResourceType(ty);
518 }
519
523 wasmtime_error_t *err =
525 if (err)
526 return Error(err);
527 return std::monostate();
528 }
529
537 static const ResourceAny *
539 return reinterpret_cast<const ResourceAny *>(capi);
540 }
541};
542
545class ResourceHost {
546 struct deleter {
547 void operator()(wasmtime_component_resource_host_t *p) const {
550 };
551
552 std::unique_ptr<wasmtime_component_resource_host_t, deleter> ptr;
553
554public:
557
559 ResourceHost(const ResourceHost &other)
560 : ptr(wasmtime_component_resource_host_clone(other.ptr.get())) {}
562 ResourceHost &operator=(const ResourceHost &other) {
563 ptr.reset(wasmtime_component_resource_host_clone(other.ptr.get()));
564 return *this;
565 }
566 ~ResourceHost() = default;
568 ResourceHost(ResourceHost &&other) = default;
570 ResourceHost &operator=(ResourceHost &&other) = default;
573 const wasmtime_component_resource_host_t *capi() const { return ptr.get(); }
574
576 wasmtime_component_resource_host_t *capi() { return ptr.get(); }
577
580 ResourceHost(bool owned, uint32_t rep, uint32_t ty)
582
584 bool owned() const { return wasmtime_component_resource_host_owned(capi()); }
585
587 uint32_t rep() const { return wasmtime_component_resource_host_rep(capi()); }
588
590 uint32_t type() const {
592 }
597 wasmtime_error_t *err =
599 if (err)
600 return Error(err);
601 return ResourceAny(out);
602 }
603};
604
607 wasmtime_error_t *err =
609 if (err)
610 return Error(err);
611 return ResourceHost(out);
613
617class Val {
618 friend class Variant;
619 friend class WitOption;
620 friend class WitResult;
621
623
624 static void transfer(Raw &&from, Raw &to) {
625 to = from;
627 from.of.boolean = false;
628 }
629
630 void copy(const Raw &other) { wasmtime_component_val_clone(&other, &raw); }
631
632 void destroy() { wasmtime_component_val_delete(&raw); }
633
634public:
636 Val(bool v) {
638 raw.of.boolean = v;
639 }
640
642 Val(uint8_t v) {
644 raw.of.u8 = v;
645 }
646
648 Val(int8_t v) {
650 raw.of.s8 = v;
651 }
652
654 Val(uint16_t v) {
656 raw.of.u16 = v;
657 }
658
660 Val(int16_t v) {
662 raw.of.s16 = v;
663 }
664
666 Val(uint32_t v) {
668 raw.of.u32 = v;
669 }
670
672 Val(int32_t v) {
674 raw.of.s32 = v;
675 }
676
678 Val(uint64_t v) {
680 raw.of.u64 = v;
681 }
682
684 Val(int64_t v) {
686 raw.of.s64 = v;
688
690 Val(float v) {
692 raw.of.f32 = v;
693 }
694
696 Val(double v) {
698 raw.of.f64 = v;
699 }
700
702 static Val char_(uint32_t v) {
705 .of = {.character = v},
706 };
707 return Val(std::move(raw));
708 }
709
711 static Val string(std::string_view v) {
714 wasm_byte_vec_new(&raw.of.string, v.size(), v.data());
715 return Val(std::move(raw));
716 }
717
721 List::transfer(std::move(v.raw), raw.of.list);
722 }
723
727 Record::transfer(std::move(r.raw), raw.of.record);
728 }
729
733 Tuple::transfer(std::move(v.raw), raw.of.tuple);
734 }
735
737 Val(Variant v) {
739 Variant::transfer(std::move(v.raw), raw.of.variant);
741
743 Val(WitOption v) {
745 WitOption::transfer(std::move(v.raw), raw.of.option);
747
749 Val(WitResult r) {
751 WitResult::transfer(std::move(r.raw), raw.of.result);
753
755 static Val enum_(std::string_view discriminant) {
758 wasm_byte_vec_new(&raw.of.enumeration, discriminant.size(),
759 discriminant.data());
760 return Val(std::move(raw));
762
766 Flags::transfer(std::move(f.raw), raw.of.flags);
767 }
768
772 raw.of.resource = r.capi_release();
774
776 bool is_bool() const { return raw.kind == WASMTIME_COMPONENT_BOOL; }
777
779 bool get_bool() const {
780 assert(is_bool());
781 return raw.of.boolean;
783
785 bool is_u8() const { return raw.kind == WASMTIME_COMPONENT_U8; }
786
788 uint8_t get_u8() const {
789 assert(is_u8());
790 return raw.of.u8;
792
794 bool is_s8() const { return raw.kind == WASMTIME_COMPONENT_S8; }
795
797 int8_t get_s8() const {
798 assert(is_s8());
799 return raw.of.s8;
801
803 bool is_u16() const { return raw.kind == WASMTIME_COMPONENT_U16; }
804
806 uint16_t get_u16() const {
807 assert(is_u16());
808 return raw.of.u16;
810
812 bool is_s16() const { return raw.kind == WASMTIME_COMPONENT_S16; }
813
815 int16_t get_s16() const {
816 assert(is_s16());
817 return raw.of.s16;
819
821 bool is_u32() const { return raw.kind == WASMTIME_COMPONENT_U32; }
822
824 uint32_t get_u32() const {
825 assert(is_u32());
826 return raw.of.u32;
828
830 bool is_s32() const { return raw.kind == WASMTIME_COMPONENT_S32; }
831
833 int32_t get_s32() const {
834 assert(is_s32());
835 return raw.of.s32;
837
839 bool is_u64() const { return raw.kind == WASMTIME_COMPONENT_U64; }
840
842 uint64_t get_u64() const {
843 assert(is_u64());
844 return raw.of.u64;
846
848 bool is_s64() const { return raw.kind == WASMTIME_COMPONENT_S64; }
849
851 int64_t get_s64() const {
852 assert(is_s64());
853 return raw.of.s64;
855
857 bool is_f32() const { return raw.kind == WASMTIME_COMPONENT_F32; }
858
860 float get_f32() const {
861 assert(is_f32());
862 return raw.of.f32;
864
866 bool is_f64() const { return raw.kind == WASMTIME_COMPONENT_F64; }
867
869 double get_f64() const {
870 assert(is_f64());
871 return raw.of.f64;
873
875 bool is_string() const { return raw.kind == WASMTIME_COMPONENT_STRING; }
876
878 std::string_view get_string() const {
879 assert(is_string());
880 return std::string_view(raw.of.string.data, raw.of.string.size);
882
884 bool is_list() const { return raw.kind == WASMTIME_COMPONENT_LIST; }
885
887 const List &get_list() const {
888 assert(is_list());
889 return *List::from_capi(&raw.of.list);
891
893 bool is_record() const { return raw.kind == WASMTIME_COMPONENT_RECORD; }
894
896 const Record &get_record() const {
897 assert(is_record());
898 return *Record::from_capi(&raw.of.record);
900
902 bool is_tuple() const { return raw.kind == WASMTIME_COMPONENT_TUPLE; }
903
905 const Tuple &get_tuple() const {
906 assert(is_tuple());
907 return *Tuple::from_capi(&raw.of.tuple);
909
911 bool is_variant() const { return raw.kind == WASMTIME_COMPONENT_VARIANT; }
912
914 const Variant &get_variant() const {
915 assert(is_variant());
916 return *Variant::from_capi(&raw.of.variant);
918
920 bool is_option() const { return raw.kind == WASMTIME_COMPONENT_OPTION; }
921
923 const WitOption &get_option() const {
924 assert(is_option());
925 return *WitOption::from_capi(&raw.of.option);
927
929 bool is_enum() const { return raw.kind == WASMTIME_COMPONENT_ENUM; }
930
932 std::string_view get_enum() const {
933 assert(is_enum());
934 return std::string_view(raw.of.enumeration.data, raw.of.enumeration.size);
936
938 bool is_result() const { return raw.kind == WASMTIME_COMPONENT_RESULT; }
939
941 const WitResult &get_result() const {
942 assert(is_result());
944 }
945
947 bool is_flags() const { return raw.kind == WASMTIME_COMPONENT_FLAGS; }
948
950 const Flags &get_flags() const {
951 assert(is_flags());
952 return *Flags::from_capi(&raw.of.flags);
954
956 bool is_resource() const { return raw.kind == WASMTIME_COMPONENT_RESOURCE; }
957
959 const ResourceAny &get_resource() const {
960 assert(is_resource());
961 return *ResourceAny::from_capi(&raw.of.resource);
962 }
963};
964
965#undef VAL_REPR
966
967inline Record::Record(std::vector<std::pair<std::string_view, Val>> entries) {
968 wasmtime_component_valrecord_new_uninit(&raw, entries.size());
969 auto dst = raw.data;
970 for (auto &&[name, val] : entries) {
971 wasm_byte_vec_new(&dst->name, name.size(), name.data());
972 new (&dst->val) Val(std::move(val));
973 dst++;
974 }
975}
977inline List::List(std::vector<Val> values) {
978 wasmtime_component_vallist_new_uninit(&raw, values.size());
979 auto dst = raw.data;
980 for (auto &&val : values)
981 new (dst++) Val(std::move(val));
982}
983
984inline Tuple::Tuple(std::vector<Val> values) {
985 wasmtime_component_valtuple_new_uninit(&raw, values.size());
986 auto dst = raw.data;
987 for (auto &&val : values)
988 new (dst++) Val(std::move(val));
989}
990
991inline Variant::Variant(std::string_view discriminant, std::optional<Val> x) {
992 wasm_name_new(&raw.discriminant, discriminant.size(), discriminant.data());
993 if (x) {
994 raw.val = wasmtime_component_val_new(&x->raw);
995 } else {
996 raw.val = nullptr;
997 }
998}
999
1000inline WitOption::WitOption(std::optional<Val> v) {
1001 if (v) {
1002 raw = wasmtime_component_val_new(&v->raw);
1003 } else {
1004 raw = nullptr;
1005 }
1006}
1007
1008inline WitResult WitResult::ok(std::optional<Val> v) {
1010 raw.is_ok = true;
1011 if (v) {
1012 raw.val = wasmtime_component_val_new(&v->raw);
1013 } else {
1014 raw.val = nullptr;
1015 }
1016 return WitResult(std::move(raw));
1017}
1018
1019inline WitResult WitResult::err(std::optional<Val> v) {
1021 raw.is_ok = false;
1022 if (v) {
1023 raw.val = wasmtime_component_val_new(&v->raw);
1024 } else {
1025 raw.val = nullptr;
1026 }
1027 return WitResult(std::move(raw));
1028}
1029
1030} // namespace component
1031} // namespace wasmtime
1032
1033#endif // WASMTIME_FEATURE_COMPONENT_MODEL
1034
1035#endif // WASMTIME_COMPONENT_VAL_H
Errors coming from Wasmtime.
Definition: error.hh:25
Fallible result type used for Wasmtime.
Definition: error.hh:82
An interior pointer into a Store.
Definition: store.hh:60
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:154
Representation of a generic WebAssembly value.
Definition: val.hh:204
Class representing a component model flags value.
Definition: component/val.hh:333
std::string_view name() const
Returns the name of this flag.
Definition: component/val.hh:353
Flag(Raw &&capi)
Definition: component/val.hh:336
Class representing a component model flags value.
Definition: component/val.hh:357
static const Flags * from_capi(const Raw *capi)
Definition: component/val.hh:360
size_t size() const
Returns the number of flags.
Definition: component/val.hh:384
const Flag * end() const
Returns an iterator to the end of the flags.
Definition: component/val.hh:390
Flags(Raw &&capi)
Definition: component/val.hh:360
const Flag * begin() const
Returns an iterator to the beginning of the flags.
Definition: component/val.hh:387
Class representing a component model list, a sequence of values.
Definition: component/val.hh:152
static const List * from_capi(const Raw *capi)
Definition: component/val.hh:155
size_t size() const
Returns the number of entries in the list.
Definition: component/val.hh:172
const Val * begin() const
Returns an iterator to the beginning of the list.
Definition: component/val.hh:175
List(Raw &&capi)
Definition: component/val.hh:155
const Val * end() const
Returns an iterator to the end of the list.
Definition: component/val.hh:178
Class representing a field in a record value.
Definition: component/val.hh:91
const Val & value() const
Returns the value of this record field.
Definition: component/val.hh:113
std::string_view name() const
Returns the name of this record field.
Definition: component/val.hh:108
Class representing a component model record, a list of name/value pairs.
Definition: component/val.hh:118
size_t size() const
Returns the number of entries in the record.
Definition: component/val.hh:140
static const Record * from_capi(const Raw *capi)
Definition: component/val.hh:121
const RecordField * end() const
Returns an iterator to the end of the record fields.
Definition: component/val.hh:146
const RecordField * begin() const
Returns an iterator to the beginning of the record fields.
Definition: component/val.hh:143
Record(Raw &&capi)
Definition: component/val.hh:121
Definition: component/val.hh:450
bool owned() const
Returns whether this resource is owned.
Definition: component/val.hh:487
static const ResourceAny * from_capi(wasmtime_component_resource_any_t *const *capi)
Definition: component/val.hh:514
ResourceAny(wasmtime_component_resource_any_t *raw)
Takes ownership of raw and wraps it with this class.
Definition: component/val.hh:461
wasmtime_component_resource_any_t * capi_release()
Gives up ownership of the underlying C pointer to the caller.
Definition: component/val.hh:484
ResourceAny & operator=(const ResourceAny &other)
Copies another resource into this one.
Definition: component/val.hh:467
const wasmtime_component_resource_any_t * capi() const
Returns the underlying C API pointer.
Definition: component/val.hh:478
ResourceType type() const
Returns the type of this resource.
Definition: component/val.hh:490
Result< ResourceHost > to_host(Store::Context cx) const
Attempts to convert this resource to a host-defined resource.
Definition: component/val.hh:581
Result< std::monostate > drop(Store::Context cx) const
Drops this resource in the component-model sense, cleaning up borrow state and executing the wasm des...
Definition: component/val.hh:498
Definition: component/val.hh:521
uint32_t rep() const
Returns the "rep" identifier associated with this resource.
Definition: component/val.hh:563
const wasmtime_component_resource_host_t * capi() const
Returns the underlying C API pointer.
Definition: component/val.hh:549
ResourceHost & operator=(const ResourceHost &other)
Copies another resource into this one.
Definition: component/val.hh:538
ResourceHost(wasmtime_component_resource_host_t *raw)
Takes ownership of raw and wraps it with this class.
Definition: component/val.hh:532
ResourceHost(bool owned, uint32_t rep, uint32_t ty)
Creates a new host-defined resource with the specified owned, rep, and ty identifiers.
Definition: component/val.hh:556
bool owned() const
Returns whether this resource is owned.
Definition: component/val.hh:560
Result< ResourceAny > to_any(Store::Context cx) const
Converts this host-defined resource into a generic resource-any.
Definition: component/val.hh:571
uint32_t type() const
Returns the "type" identifier associated with this resource.
Definition: component/val.hh:566
Definition: component/val.hh:397
const wasmtime_component_resource_type_t * capi() const
Returns the underlying C API pointer.
Definition: component/val.hh:440
ResourceType(wasmtime_component_resource_type_t *raw)
Takes ownership of raw and wraps it with this class.
Definition: component/val.hh:408
ResourceType & operator=(const ResourceType &other)
Copies another resource into this one.
Definition: component/val.hh:419
bool operator==(const ResourceType &b) const
Compares two resource types for equality.
Definition: component/val.hh:430
bool operator!=(const ResourceType &b) const
Compares two resource types for inequality.
Definition: component/val.hh:435
Class representing a component model tuple.
Definition: component/val.hh:184
const Val * begin() const
Returns an iterator to the beginning of the tuple.
Definition: component/val.hh:209
Tuple(Raw &&capi)
Definition: component/val.hh:187
size_t size() const
Returns the number of entries in the tuple.
Definition: component/val.hh:206
static const Tuple * from_capi(const Raw *capi)
Definition: component/val.hh:187
const Val * end() const
Returns an iterator to the end of the tuple.
Definition: component/val.hh:212
Class representing an instantiated WebAssembly component.
Definition: component/val.hh:593
const Variant & get_variant() const
Returns the variant value, only valid if is_variant().
Definition: component/val.hh:890
bool is_bool() const
Returns whether this value is a boolean.
Definition: component/val.hh:752
bool is_flags() const
Returns whether this value is flags.
Definition: component/val.hh:923
Val(Raw &&capi)
Definition: component/val.hh:598
bool get_bool() const
Returns the boolean value, only valid if is_bool().
Definition: component/val.hh:755
const Flags & get_flags() const
Returns the flags value, only valid if is_flags().
Definition: component/val.hh:926
std::string_view get_enum() const
Returns the enum discriminant, only valid if is_enum().
Definition: component/val.hh:908
bool is_string() const
Returns whether this value is a string.
Definition: component/val.hh:851
const WitResult & get_result() const
Returns the result value, only valid if is_result().
Definition: component/val.hh:917
const WitOption & get_option() const
Returns the option value, only valid if is_option().
Definition: component/val.hh:899
int32_t get_s32() const
Returns the s32 value, only valid if is_s32().
Definition: component/val.hh:809
static Val char_(uint32_t v)
Creates a new char value.
Definition: component/val.hh:678
bool is_s16() const
Returns whether this value is a s16.
Definition: component/val.hh:788
Val(WitOption v)
Creates a new option value.
Definition: component/val.hh:719
int64_t get_s64() const
Returns the s64 value, only valid if is_s64().
Definition: component/val.hh:827
double get_f64() const
Returns the f64 value, only valid if is_f64().
Definition: component/val.hh:845
bool is_record() const
Returns whether this value is a record.
Definition: component/val.hh:869
static Val enum_(std::string_view discriminant)
Creates a new enum value.
Definition: component/val.hh:731
Val(int32_t v)
Creates a new s32 value.
Definition: component/val.hh:648
bool is_f64() const
Returns whether this value is a f64.
Definition: component/val.hh:842
bool is_option() const
Returns whether this value is an option.
Definition: component/val.hh:896
Val(WitResult r)
Creates a new result value.
Definition: component/val.hh:725
uint8_t get_u8() const
Returns the u8 value, only valid if is_u8().
Definition: component/val.hh:764
std::string_view get_string() const
Returns the string value, only valid if is_string().
Definition: component/val.hh:854
Val(double v)
Creates a new f64 value.
Definition: component/val.hh:672
bool is_s32() const
Returns whether this value is a s32.
Definition: component/val.hh:806
Val(float v)
Creates a new f32 value.
Definition: component/val.hh:666
static Val string(std::string_view v)
Creates a new string value.
Definition: component/val.hh:687
float get_f32() const
Returns the f32 value, only valid if is_f32().
Definition: component/val.hh:836
const Record & get_record() const
Returns the record value, only valid if is_record().
Definition: component/val.hh:872
bool is_result() const
Returns whether this value is a result.
Definition: component/val.hh:914
int16_t get_s16() const
Returns the s16 value, only valid if is_s16().
Definition: component/val.hh:791
Val(uint64_t v)
Creates a new u64 value.
Definition: component/val.hh:654
bool is_u8() const
Returns whether this value is a u8.
Definition: component/val.hh:761
bool is_s64() const
Returns whether this value is a s64.
Definition: component/val.hh:824
const ResourceAny & get_resource() const
Returns the flags value, only valid if is_flags().
Definition: component/val.hh:935
Val(Tuple v)
Creates a new tuple value.
Definition: component/val.hh:707
Val(int16_t v)
Creates a new s16 value.
Definition: component/val.hh:636
bool is_u16() const
Returns whether this value is a u16.
Definition: component/val.hh:779
uint64_t get_u64() const
Returns the u64 value, only valid if is_u64().
Definition: component/val.hh:818
bool is_f32() const
Returns whether this value is a f32.
Definition: component/val.hh:833
bool is_list() const
Returns whether this value is a list.
Definition: component/val.hh:860
bool is_u64() const
Returns whether this value is a u64.
Definition: component/val.hh:815
bool is_s8() const
Returns whether this value is a s8.
Definition: component/val.hh:770
uint32_t get_u32() const
Returns the u32 value, only valid if is_u32().
Definition: component/val.hh:800
Val(int64_t v)
Creates a new s64 value.
Definition: component/val.hh:660
bool is_variant() const
Returns whether this value is a variant.
Definition: component/val.hh:887
int8_t get_s8() const
Returns the s8 value, only valid if is_s8().
Definition: component/val.hh:773
bool is_u32() const
Returns whether this value is a u32.
Definition: component/val.hh:797
bool is_enum() const
Returns whether this value is an enum.
Definition: component/val.hh:905
const List & get_list() const
Returns the list value, only valid if is_list().
Definition: component/val.hh:863
bool is_tuple() const
Returns whether this value is a tuple.
Definition: component/val.hh:878
uint16_t get_u16() const
Returns the u16 value, only valid if is_u16().
Definition: component/val.hh:782
bool is_resource() const
Returns whether this value is a resource.
Definition: component/val.hh:932
Val(uint32_t v)
Creates a new u32 value.
Definition: component/val.hh:642
const Tuple & get_tuple() const
Returns the tuple value, only valid if is_tuple().
Definition: component/val.hh:881
Class representing a component model variant value.
Definition: component/val.hh:218
Variant(Raw &&capi)
Definition: component/val.hh:221
const Val * value() const
Returns the optional payload value associated with this variant value.
Definition: component/val.hh:257
std::string_view discriminant() const
Returns the name of the discriminant of this value.
Definition: component/val.hh:252
static const Variant * from_capi(const Raw *capi)
Definition: component/val.hh:221
Class representing a component model option value.
Definition: component/val.hh:261
const Val * value() const
Returns the optional payload value associated with this option.
Definition: component/val.hh:288
static const WitOption * from_capi(const Raw *capi)
Definition: component/val.hh:264
WitOption(Raw &&capi)
Definition: component/val.hh:264
Class representing a component model result value.
Definition: component/val.hh:292
WitResult(Raw &&capi)
Definition: component/val.hh:295
const Val * payload() const
Returns the optional payload value associated with this result.
Definition: component/val.hh:329
bool is_ok() const
Returns whether this result is the ok variant.
Definition: component/val.hh:326
static WitResult ok(std::optional< Val > val)
Constructs a new result value with the ok variant.
Definition: component/val.hh:984
static const WitResult * from_capi(const Raw *capi)
Definition: component/val.hh:295
static WitResult err(std::optional< Val > val)
Constructs a new result value with the err variant.
Definition: component/val.hh:995
wasmtime_component_resource_any_t * wasmtime_component_resource_any_clone(const wasmtime_component_resource_any_t *resource)
Clones a component resource.
wasmtime_component_resource_type_t * wasmtime_component_resource_type_clone(const wasmtime_component_resource_type_t *ty)
Clones a resource type.
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.
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.
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_U8
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u8.
Definition: component/val.h:260
#define WASMTIME_COMPONENT_U16
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u16.
Definition: component/val.h:266
wasmtime_component_resource_host_t * wasmtime_component_resource_host_clone(const wasmtime_component_resource_host_t *resource)
Clones a host-defined component resource.
void wasmtime_component_valrecord_delete(wasmtime_component_valrecord_t *value)
Delete value.
#define WASMTIME_COMPONENT_ENUM
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a enum.
Definition: component/val.h:305
#define WASMTIME_COMPONENT_CHAR
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a char.
Definition: component/val.h:287
#define WASMTIME_COMPONENT_RESOURCE
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a resource.
Definition: component/val.h:317
void wasmtime_component_vallist_new_uninit(wasmtime_component_vallist_t *out, size_t size)
Create a vec with length size.
struct wasmtime_component_resource_host wasmtime_component_resource_host_t
Represents a host-defined component resource.
Definition: component/val.h:165
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.
void wasmtime_component_resource_host_delete(wasmtime_component_resource_host_t *resource)
Deallocates a host-defined component resource.
#define WASMTIME_COMPONENT_RESULT
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a result.
Definition: component/val.h:311
bool wasmtime_component_resource_type_equal(const wasmtime_component_resource_type_t *a, const wasmtime_component_resource_type_t *b)
Compares two resource types for equality.
#define WASMTIME_COMPONENT_S8
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s8.
Definition: component/val.h:257
void wasmtime_component_vallist_copy(wasmtime_component_vallist_t *dst, const wasmtime_component_vallist_t *src)
Copy src to dst.
struct wasmtime_component_resource_type wasmtime_component_resource_type_t
Represents the type of a component resource.
Definition: component/val.h:23
void wasmtime_component_valtuple_new_uninit(wasmtime_component_valtuple_t *out, size_t size)
Create a vec with length size.
#define WASMTIME_COMPONENT_BOOL
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a bool.
Definition: component/val.h:254
#define WASMTIME_COMPONENT_LIST
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a list.
Definition: component/val.h:293
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.
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_RECORD
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a record.
Definition: component/val.h:296
void wasmtime_component_resource_type_delete(wasmtime_component_resource_type_t *resource)
Deallocates a resource type.
#define WASMTIME_COMPONENT_U64
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u64.
Definition: component/val.h:278
void wasmtime_component_valrecord_new_uninit(wasmtime_component_valrecord_t *out, size_t size)
Create a vec with length size.
wasmtime_error_t * wasmtime_component_resource_any_drop(wasmtime_context_t *ctx, const wasmtime_component_resource_any_t *resource)
Drops a component resource.
void wasmtime_component_vallist_delete(wasmtime_component_vallist_t *value)
Delete value.
#define WASMTIME_COMPONENT_S64
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s64.
Definition: component/val.h:275
void wasmtime_component_valflags_copy(wasmtime_component_valflags_t *dst, const wasmtime_component_valflags_t *src)
Copy src to dst.
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:89
#define WASMTIME_COMPONENT_U32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a u32.
Definition: component/val.h:272
#define WASMTIME_COMPONENT_VARIANT
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a variant.
Definition: component/val.h:302
#define WASMTIME_COMPONENT_TUPLE
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a tuple.
Definition: component/val.h:299
#define WASMTIME_COMPONENT_S16
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s16.
Definition: component/val.h:263
#define WASMTIME_COMPONENT_OPTION
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a option.
Definition: component/val.h:308
void wasmtime_component_val_free(wasmtime_component_val_t *ptr)
Deallocates the heap-allocated value at ptr.
void wasmtime_component_valtuple_copy(wasmtime_component_valtuple_t *dst, const wasmtime_component_valtuple_t *src)
Copy src to dst.
#define WASMTIME_COMPONENT_FLAGS
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is flags.
Definition: component/val.h:314
void wasmtime_component_resource_any_delete(wasmtime_component_resource_any_t *resource)
Deallocates a component resource.
void wasmtime_component_val_delete(wasmtime_component_val_t *value)
Deallocates any memory owned by value.
#define WASMTIME_COMPONENT_S32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a s32.
Definition: component/val.h:269
#define WASMTIME_COMPONENT_F32
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a f32.
Definition: component/val.h:281
wasmtime_component_resource_type_t * wasmtime_component_resource_type_new_host(uint32_t ty)
Creates a new resource type representing a host-defined resource.
void wasmtime_component_valrecord_copy(wasmtime_component_valrecord_t *dst, const wasmtime_component_valrecord_t *src)
Copy src to dst.
uint32_t wasmtime_component_resource_host_rep(const wasmtime_component_resource_host_t *resource)
Gets the "rep" 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:284
#define WASMTIME_COMPONENT_STRING
Value of wasmtime_component_valkind_t meaning that wasmtime_component_val_t is a string.
Definition: component/val.h:290
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.
void wasmtime_component_valtuple_delete(wasmtime_component_valtuple_t *value)
Delete value.
wasmtime_component_resource_type_t * wasmtime_component_resource_any_type(const wasmtime_component_resource_any_t *resource)
Gets the type of a component resource.
void wasmtime_component_valflags_delete(wasmtime_component_valflags_t *value)
Delete value.
void wasmtime_component_valflags_new_uninit(wasmtime_component_valflags_t *out, size_t size)
Create a vec with length size.
#define VAL_REPR(name, raw_type)
Definition: component/val.hh:38
const Val * val_from_capi(const wasmtime_component_val_t *capi)
Definition: component/val.hh:29
Build-time defines for how the C API was built.
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:425
wasmtime_component_valunion_t of
Value of type kind.
Definition: component/val.h:429
wasmtime_component_valkind_t kind
The type discriminant.
Definition: component/val.h:427
A vec of a wasm_name_t
Definition: component/val.h:347
wasm_name_t * data
Definition: component/val.h:347
size_t size
Definition: component/val.h:347
A vec of a struct wasmtime_component_val
Definition: component/val.h:343
struct wasmtime_component_val * data
Definition: component/val.h:343
size_t size
Definition: component/val.h:343
A pair of a name and a value that represents one entry in a value with kind WASMTIME_COMPONENT_RECORD...
Definition: component/val.h:434
wasmtime_component_val_t val
The value of this entry.
Definition: component/val.h:438
wasm_name_t name
The name of this entry.
Definition: component/val.h:436
A vec of a struct wasmtime_component_valrecord_entry
Definition: component/val.h:345
struct wasmtime_component_valrecord_entry * data
Definition: component/val.h:345
size_t size
Definition: component/val.h:345
Represents a result type.
Definition: component/val.h:360
bool is_ok
The discriminant of the result.
Definition: component/val.h:362
struct wasmtime_component_val * val
Definition: component/val.h:365
A vec of a struct wasmtime_component_val
Definition: component/val.h:346
size_t size
Definition: component/val.h:346
struct wasmtime_component_val * data
Definition: component/val.h:346
Represents a variant type.
Definition: component/val.h:352
struct wasmtime_component_val * val
The payload of the variant.
Definition: component/val.h:356
wasm_name_t discriminant
The discriminant of the variant.
Definition: component/val.h:354
Errors generated by Wasmtime.
uint16_t u16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U16.
Definition: component/val.h:380
wasm_name_t string
Definition: component/val.h:397
int64_t s64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S64.
Definition: component/val.h:386
bool boolean
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_BOOL.
Definition: component/val.h:372
wasmtime_component_resource_any_t * resource
Definition: component/val.h:420
wasmtime_component_valvariant_t variant
Definition: component/val.h:407
uint32_t u32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U32.
Definition: component/val.h:384
wasmtime_component_vallist_t list
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_LIST.
Definition: component/val.h:399
int32_t s32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S32.
Definition: component/val.h:382
float32_t f32
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F32.
Definition: component/val.h:390
float64_t f64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_F64.
Definition: component/val.h:392
struct wasmtime_component_val * option
Definition: component/val.h:412
int16_t s16
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S16.
Definition: component/val.h:378
uint8_t u8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U8.
Definition: component/val.h:376
wasmtime_component_valrecord_t record
Definition: component/val.h:402
uint64_t u64
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_U64.
Definition: component/val.h:388
wasmtime_component_valresult_t result
Definition: component/val.h:415
wasmtime_component_valflags_t flags
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_FLAGS.
Definition: component/val.h:417
wasmtime_component_valtuple_t tuple
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_TUPLE.
Definition: component/val.h:404
int8_t s8
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_S8.
Definition: component/val.h:374
wasm_name_t enumeration
Field used if wasmtime_component_val_t::kind is WASMTIME_COMPONENT_ENUM.
Definition: component/val.h:409
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