wasmtime-c-api-impl 44.0.0

C API to expose the Wasmtime runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/// \file wasmtime/component/val.hh

#ifndef WASMTIME_COMPONENT_VAL_HH
#define WASMTIME_COMPONENT_VAL_HH

#include <wasmtime/conf.h>

#ifdef WASMTIME_FEATURE_COMPONENT_MODEL

#include <assert.h>
#include <memory>
#include <optional>
#include <string_view>
#include <utility>
#include <vector>
#include <wasmtime/component/types/val.hh>
#include <wasmtime/component/val.h>
#include <wasmtime/store.hh>

namespace wasmtime {
namespace component {

class Val;
class Record;

namespace detail {

/// Internal helper to convert from C to `Val`, sort of a forward-declaration
/// of `Val::from_capi` which I don't know how to otherwise forward-declare.
inline const Val *val_from_capi(const wasmtime_component_val_t *capi) {
  return reinterpret_cast<const Val *>(capi);
}

} // namespace detail

/// Internal helper macro to define ownership-semantics for C++ types based on
/// a C type as a single member where operations are defined in terms of
/// `transfer`, `copy`, and `destroy` functions.
#define VAL_REPR(name, raw_type)                                               \
private:                                                                       \
  using Raw = raw_type;                                                        \
  Raw raw;                                                                     \
                                                                               \
public:                                                                        \
  /**                                                                          \
   * Create a variant that takes ownership of the underlying C API variant.    \
   */                                                                          \
  explicit name(Raw &&capi) { name::transfer(std::move(capi), raw); }          \
                                                                               \
  /**                                                                          \
   * Converts the raw C API representation to this class without taking        \
   * ownership.                                                                \
   */                                                                          \
  static const name *from_capi(const Raw *capi) {                              \
    return reinterpret_cast<const name *>(capi);                               \
  }                                                                            \
                                                                               \
  /**                                                                          \
   * Converts the raw C API representation to this class without taking        \
   * ownership.                                                                \
   */                                                                          \
  static name *from_capi(Raw *capi) { return reinterpret_cast<name *>(capi); } \
                                                                               \
  /**                                                                          \
   * Converts to the raw C API representation to this class without taking     \
   * ownership.                                                                \
   */                                                                          \
  static const Raw *to_capi(const name *capi) {                                \
    return reinterpret_cast<const Raw *>(capi);                                \
  }                                                                            \
                                                                               \
  /**                                                                          \
   * Converts to the raw C API representation to this class without taking     \
   * ownership.                                                                \
   */                                                                          \
  static Raw *to_capi(name *capi) { return reinterpret_cast<Raw *>(capi); }    \
                                                                               \
  /**                                                                          \
   * \brief Copy constructor to clone `other`.                                 \
   */                                                                          \
  name(const name &other) { copy(other.raw); }                                 \
                                                                               \
  /**                                                                          \
   * \brief Copy assignment to clone from `other`.                             \
   */                                                                          \
  name &operator=(const name &other) {                                         \
    destroy();                                                                 \
    copy(other.raw);                                                           \
    return *this;                                                              \
  }                                                                            \
                                                                               \
  /**                                                                          \
   * \brief Move constructor to move the contents of `other`.                  \
   */                                                                          \
  name(name &&other) { name::transfer(std::move(other.raw), raw); }            \
                                                                               \
  /**                                                                          \
   * \brief Move assignment to move the contents of `other`.                   \
   */                                                                          \
  name &operator=(name &&other) {                                              \
    destroy();                                                                 \
    name::transfer(std::move(other.raw), raw);                                 \
    return *this;                                                              \
  }                                                                            \
                                                                               \
  ~name() { destroy(); }                                                       \
                                                                               \
  /**                                                                          \
   * \brief Returns a pointer to the underlying C API representation.          \
   */                                                                          \
  const Raw *capi() const { return &raw; }                                     \
                                                                               \
private:

/// \brief Class representing a field in a record value.
class RecordField {
  friend class Record;

  wasmtime_component_valrecord_entry_t entry;

  // This value can't be constructed or destructed, it's only used in iteration
  // of `Record`.
  RecordField() = delete;
  ~RecordField() = delete;

  static const RecordField *
  from_capi(const wasmtime_component_valrecord_entry_t *capi) {
    return reinterpret_cast<const RecordField *>(capi);
  }

public:
  /// \brief Returns the name of this record field.
  std::string_view name() const {
    return std::string_view{entry.name.data, entry.name.size};
  }

  /// \brief Returns the value of this record field.
  const Val &value() const { return *detail::val_from_capi(&entry.val); }
};

/// \brief Class representing a component model record, a list of name/value
/// pairs.
class Record {
  friend class Val;

  VAL_REPR(Record, wasmtime_component_valrecord_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) {
    wasmtime_component_valrecord_copy(&raw, &other);
  }

  void destroy() { wasmtime_component_valrecord_delete(&raw); }

public:
  /// Creates a new record from the named field pairs provided.
  Record(std::vector<std::pair<std::string_view, Val>> entries);

  /// \brief Returns the number of entries in the record.
  size_t size() const { return raw.size; }

  /// \brief Returns an iterator to the beginning of the record fields.
  const RecordField *begin() const { return RecordField::from_capi(raw.data); }

  /// \brief Returns an iterator to the end of the record fields.
  const RecordField *end() const {
    return RecordField::from_capi(raw.data + raw.size);
  }
};

/// \brief Class representing a component model list, a sequence of values.
class List {
  friend class Val;

  VAL_REPR(List, wasmtime_component_vallist_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) { wasmtime_component_vallist_copy(&raw, &other); }

  void destroy() { wasmtime_component_vallist_delete(&raw); }

public:
  /// Creates a new list from the named field pairs provided.
  List(std::vector<Val> entries);

  /// \brief Returns the number of entries in the list.
  size_t size() const { return raw.size; }

  /// \brief Returns an iterator to the beginning of the list.
  const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }

  /// \brief Returns an iterator to the end of the list.
  const Val *end() const {
    return reinterpret_cast<const Val *>(raw.data + raw.size);
  }
};

/// \brief Class representing a component model tuple.
class Tuple {
  friend class Val;

  VAL_REPR(Tuple, wasmtime_component_valtuple_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) {
    wasmtime_component_valtuple_copy(&raw, &other);
  }

  void destroy() { wasmtime_component_valtuple_delete(&raw); }

public:
  /// Creates a new tuple from the named field pairs provided.
  Tuple(std::vector<Val> entries);

  /// \brief Returns the number of entries in the tuple.
  size_t size() const { return raw.size; }

  /// \brief Returns an iterator to the beginning of the tuple.
  const Val *begin() const { return reinterpret_cast<const Val *>(raw.data); }

  /// \brief Returns an iterator to the end of the tuple.
  const Val *end() const {
    return reinterpret_cast<const Val *>(raw.data + raw.size);
  }
};

/// Class representing a component model `variant` value.
class Variant {
  friend class Val;

  VAL_REPR(Variant, wasmtime_component_valvariant_t);

  static void transfer(wasmtime_component_valvariant_t &&from,
                       wasmtime_component_valvariant_t &to) {
    to = from;
    from.discriminant.size = 0;
    from.discriminant.data = nullptr;
    from.val = nullptr;
  }

  void copy(const wasmtime_component_valvariant_t &other) {
    wasm_name_copy(&raw.discriminant, &other.discriminant);
    if (other.val) {
      wasmtime_component_val_t clone;
      wasmtime_component_val_clone(other.val, &clone);
      raw.val = wasmtime_component_val_new(&clone);
    } else {
      raw.val = nullptr;
    }
  }

  void destroy() {
    wasm_name_delete(&raw.discriminant);
    wasmtime_component_val_free(raw.val);
  }

public:
  /// Constructs a new variant value with the provided discriminant and payload.
  Variant(std::string_view discriminant, std::optional<Val> x);

  /// Returns the name of the discriminant of this value.
  std::string_view discriminant() const {
    return std::string_view(raw.discriminant.data, raw.discriminant.size);
  }

  /// Returns the optional payload value associated with this variant value.
  const Val *value() const { return detail::val_from_capi(raw.val); }
};

/// Class representing a component model `option` value.
class WitOption {
  friend class Val;

  VAL_REPR(WitOption, wasmtime_component_val_t *);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from = nullptr;
  }

  void copy(const Raw &other) {
    if (other) {
      wasmtime_component_val_t clone;
      wasmtime_component_val_clone(other, &clone);
      raw = wasmtime_component_val_new(&clone);
    } else {
      raw = nullptr;
    }
  }

  void destroy() { wasmtime_component_val_free(raw); }

public:
  /// Constructs a new option value with the provided value.
  explicit WitOption(std::optional<Val> val);

  /// Returns the optional payload value associated with this option.
  const Val *value() const { return detail::val_from_capi(raw); }
};

/// Class representing a component model `result` value.
class WitResult {
  friend class Val;

  VAL_REPR(WitResult, wasmtime_component_valresult_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.val = nullptr;
  }

  void copy(const Raw &other) {
    raw.is_ok = other.is_ok;
    if (other.val) {
      wasmtime_component_val_t clone;
      wasmtime_component_val_clone(other.val, &clone);
      raw.val = wasmtime_component_val_new(&clone);
    } else {
      raw.val = nullptr;
    }
  }

  void destroy() {
    if (raw.val)
      wasmtime_component_val_free(raw.val);
  }

public:
  /// Constructs a new result value with the `ok` variant.
  static WitResult ok(std::optional<Val> val);

  /// Constructs a new result value with the `err` variant.
  static WitResult err(std::optional<Val> val);

  /// \brief Returns whether this result is the `ok` variant.
  bool is_ok() const { return raw.is_ok; }

  /// Returns the optional payload value associated with this result.
  const Val *payload() const { return detail::val_from_capi(raw.val); }
};

/// Class representing a component model `flags` value.
class Flag {
  friend class Flags;

  VAL_REPR(Flag, wasm_name_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) { wasm_name_copy(&raw, &other); }

  void destroy() { wasm_name_delete(&raw); }

public:
  /// Creates a new flag from the provided string.
  Flag(std::string_view name) { wasm_name_new(&raw, name.size(), name.data()); }

  /// \brief Returns the name of this flag.
  std::string_view name() const { return std::string_view{raw.data, raw.size}; }
};

/// Class representing a component model `flags` value.
class Flags {
  friend class Val;

  VAL_REPR(Flags, wasmtime_component_valflags_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) {
    wasmtime_component_valflags_copy(&raw, &other);
  }

  void destroy() { wasmtime_component_valflags_delete(&raw); }

public:
  /// Creates a new flags value from the provided flags.
  Flags(std::vector<Flag> flags) {
    wasmtime_component_valflags_new_uninit(&raw, flags.size());
    auto dst = raw.data;
    for (auto &&val : flags)
      Flag::transfer(std::move(val.raw), *dst++);
  }

  /// \brief Returns the number of flags.
  size_t size() const { return raw.size; }

  /// \brief Returns an iterator to the beginning of the flags.
  const Flag *begin() const { return reinterpret_cast<const Flag *>(raw.data); }

  /// \brief Returns an iterator to the end of the flags.
  const Flag *end() const {
    return reinterpret_cast<const Flag *>(raw.data + raw.size);
  }
};

/// \brief Class representing an entry in a map value.
class MapEntry {
  friend class Map;

  wasmtime_component_valmap_entry_t entry;

  // This value can't be constructed or destructed, it's only used in iteration
  // of `Map`.
  MapEntry() = delete;
  ~MapEntry() = delete;

  static const MapEntry *
  from_capi(const wasmtime_component_valmap_entry_t *capi) {
    return reinterpret_cast<const MapEntry *>(capi);
  }

public:
  /// \brief Returns the key of this map entry.
  const Val &key() const { return *detail::val_from_capi(&entry.key); }

  /// \brief Returns the value of this map entry.
  const Val &value() const { return *detail::val_from_capi(&entry.value); }
};

/// \brief Class representing a component model map, a collection of key/value
/// pairs.
class Map {
  friend class Val;

  VAL_REPR(Map, wasmtime_component_valmap_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.size = 0;
    from.data = nullptr;
  }

  void copy(const Raw &other) { wasmtime_component_valmap_copy(&raw, &other); }

  void destroy() { wasmtime_component_valmap_delete(&raw); }

public:
  /// Creates a new map from the key/value pairs provided.
  Map(std::vector<std::pair<Val, Val>> entries);

  /// \brief Returns the number of entries in the map.
  size_t size() const { return raw.size; }

  /// \brief Returns an iterator to the beginning of the map entries.
  const MapEntry *begin() const { return MapEntry::from_capi(raw.data); }

  /// \brief Returns an iterator to the end of the map entries.
  const MapEntry *end() const {
    return MapEntry::from_capi(raw.data + raw.size);
  }
};

class ResourceHost;

/// Class representing a component model `resource` value which is either a
/// guest or host-defined resource.
class ResourceAny {
  WASMTIME_CLONE_WRAPPER(ResourceAny, wasmtime_component_resource_any);

  /// \brief Returns whether this resource is owned.
  bool owned() const { return wasmtime_component_resource_any_owned(capi()); }

  /// \brief Returns the type of this resource.
  ResourceType type() const {
    wasmtime_component_resource_type_t *ty =
        wasmtime_component_resource_any_type(capi());
    return ResourceType(ty);
  }

  /// \brief Drops this resource in the component-model sense, cleaning up
  /// borrow state and executing the wasm destructor, if any.
  Result<std::monostate> drop(Store::Context cx) const {
    wasmtime_error_t *err =
        wasmtime_component_resource_any_drop(cx.capi(), capi());
    if (err)
      return Error(err);
    return std::monostate();
  }

  /// \brief Attempts to convert this resource to a host-defined resource.
  Result<ResourceHost> to_host(Store::Context cx) const;
};

/// Class representing a component model `resource` value which is a host-owned
/// resource.
class ResourceHost {
  WASMTIME_CLONE_WRAPPER(ResourceHost, wasmtime_component_resource_host);

  /// \brief Creates a new host-defined resource with the specified `owned`,
  /// `rep`, and `ty` identifiers.
  ResourceHost(bool owned, uint32_t rep, uint32_t ty)
      : ptr(wasmtime_component_resource_host_new(owned, rep, ty)) {}

  /// \brief Returns whether this resource is owned.
  bool owned() const { return wasmtime_component_resource_host_owned(capi()); }

  /// \brief Returns the "rep" identifier associated with this resource.
  uint32_t rep() const { return wasmtime_component_resource_host_rep(capi()); }

  /// \brief Returns the "type" identifier associated with this resource.
  uint32_t type() const {
    return wasmtime_component_resource_host_type(capi());
  }

  /// \brief Converts this host-defined resource into a generic resource-any.
  Result<ResourceAny> to_any(Store::Context cx) const {
    wasmtime_component_resource_any_t *out;
    wasmtime_error_t *err =
        wasmtime_component_resource_host_to_any(cx.capi(), capi(), &out);
    if (err)
      return Error(err);
    return ResourceAny(out);
  }
};

inline Result<ResourceHost> ResourceAny::to_host(Store::Context cx) const {
  wasmtime_component_resource_host_t *out;
  wasmtime_error_t *err =
      wasmtime_component_resource_any_to_host(cx.capi(), capi(), &out);
  if (err)
    return Error(err);
  return ResourceHost(out);
}

/**
 * \brief Class representing an instantiated WebAssembly component.
 */
class Val {
  friend class Variant;
  friend class WitOption;
  friend class WitResult;

  VAL_REPR(Val, wasmtime_component_val_t);

  static void transfer(Raw &&from, Raw &to) {
    to = from;
    from.kind = WASMTIME_COMPONENT_BOOL;
    from.of.boolean = false;
  }

  void copy(const Raw &other) { wasmtime_component_val_clone(&other, &raw); }

  void destroy() { wasmtime_component_val_delete(&raw); }

public:
  /// Creates a new boolean value.
  Val(bool v) {
    raw.kind = WASMTIME_COMPONENT_BOOL;
    raw.of.boolean = v;
  }

  /// Creates a new u8 value.
  Val(uint8_t v) {
    raw.kind = WASMTIME_COMPONENT_U8;
    raw.of.u8 = v;
  }

  /// Creates a new s8 value.
  Val(int8_t v) {
    raw.kind = WASMTIME_COMPONENT_S8;
    raw.of.s8 = v;
  }

  /// Creates a new u16 value.
  Val(uint16_t v) {
    raw.kind = WASMTIME_COMPONENT_U16;
    raw.of.u16 = v;
  }

  /// Creates a new s16 value.
  Val(int16_t v) {
    raw.kind = WASMTIME_COMPONENT_S16;
    raw.of.s16 = v;
  }

  /// Creates a new u32 value.
  Val(uint32_t v) {
    raw.kind = WASMTIME_COMPONENT_U32;
    raw.of.u32 = v;
  }

  /// Creates a new s32 value.
  Val(int32_t v) {
    raw.kind = WASMTIME_COMPONENT_S32;
    raw.of.s32 = v;
  }

  /// Creates a new u64 value.
  Val(uint64_t v) {
    raw.kind = WASMTIME_COMPONENT_U64;
    raw.of.u64 = v;
  }

  /// Creates a new s64 value.
  Val(int64_t v) {
    raw.kind = WASMTIME_COMPONENT_S64;
    raw.of.s64 = v;
  }

  /// Creates a new f32 value.
  Val(float v) {
    raw.kind = WASMTIME_COMPONENT_F32;
    raw.of.f32 = v;
  }

  /// Creates a new f64 value.
  Val(double v) {
    raw.kind = WASMTIME_COMPONENT_F64;
    raw.of.f64 = v;
  }

  /// Creates a new char value.
  static Val char_(uint32_t v) {
    wasmtime_component_val_t raw = {
        .kind = WASMTIME_COMPONENT_CHAR,
        .of = {.character = v},
    };
    return Val(std::move(raw));
  }

  /// Creates a new string value.
  static Val string(std::string_view v) {
    wasmtime_component_val_t raw;
    raw.kind = WASMTIME_COMPONENT_STRING;
    wasm_byte_vec_new(&raw.of.string, v.size(), v.data());
    return Val(std::move(raw));
  }

  /// Creates a new list value.
  Val(List v) {
    raw.kind = WASMTIME_COMPONENT_LIST;
    List::transfer(std::move(v.raw), raw.of.list);
  }

  /// Creates a new record value.
  Val(Record r) {
    raw.kind = WASMTIME_COMPONENT_RECORD;
    Record::transfer(std::move(r.raw), raw.of.record);
  }

  /// Creates a new tuple value.
  Val(Tuple v) {
    raw.kind = WASMTIME_COMPONENT_TUPLE;
    Tuple::transfer(std::move(v.raw), raw.of.tuple);
  }

  /// Creates a new variant value.
  Val(Variant v) {
    raw.kind = WASMTIME_COMPONENT_VARIANT;
    Variant::transfer(std::move(v.raw), raw.of.variant);
  }

  /// Creates a new option value.
  Val(WitOption v) {
    raw.kind = WASMTIME_COMPONENT_OPTION;
    WitOption::transfer(std::move(v.raw), raw.of.option);
  }

  /// Creates a new result value.
  Val(WitResult r) {
    raw.kind = WASMTIME_COMPONENT_RESULT;
    WitResult::transfer(std::move(r.raw), raw.of.result);
  }

  /// Creates a new enum value.
  static Val enum_(std::string_view discriminant) {
    wasmtime_component_val_t raw;
    raw.kind = WASMTIME_COMPONENT_ENUM;
    wasm_byte_vec_new(&raw.of.enumeration, discriminant.size(),
                      discriminant.data());
    return Val(std::move(raw));
  }

  /// Creates a new flags value.
  Val(Flags f) {
    raw.kind = WASMTIME_COMPONENT_FLAGS;
    Flags::transfer(std::move(f.raw), raw.of.flags);
  }

  /// Creates a new map value.
  Val(Map m) {
    raw.kind = WASMTIME_COMPONENT_MAP;
    Map::transfer(std::move(m.raw), raw.of.map);
  }

  /// Creates a new resource value.
  Val(ResourceAny r) {
    raw.kind = WASMTIME_COMPONENT_RESOURCE;
    raw.of.resource = r.capi_release();
  }

  /// \brief Returns whether this value is a boolean.
  bool is_bool() const { return raw.kind == WASMTIME_COMPONENT_BOOL; }

  /// \brief Returns the boolean value, only valid if `is_bool()`.
  bool get_bool() const {
    assert(is_bool());
    return raw.of.boolean;
  }

  /// \brief Returns whether this value is a u8.
  bool is_u8() const { return raw.kind == WASMTIME_COMPONENT_U8; }

  /// \brief Returns the u8 value, only valid if `is_u8()`.
  uint8_t get_u8() const {
    assert(is_u8());
    return raw.of.u8;
  }

  /// \brief Returns whether this value is a s8.
  bool is_s8() const { return raw.kind == WASMTIME_COMPONENT_S8; }

  /// \brief Returns the s8 value, only valid if `is_s8()`.
  int8_t get_s8() const {
    assert(is_s8());
    return raw.of.s8;
  }

  /// \brief Returns whether this value is a u16.
  bool is_u16() const { return raw.kind == WASMTIME_COMPONENT_U16; }

  /// \brief Returns the u16 value, only valid if `is_u16()`.
  uint16_t get_u16() const {
    assert(is_u16());
    return raw.of.u16;
  }

  /// \brief Returns whether this value is a s16.
  bool is_s16() const { return raw.kind == WASMTIME_COMPONENT_S16; }

  /// \brief Returns the s16 value, only valid if `is_s16()`.
  int16_t get_s16() const {
    assert(is_s16());
    return raw.of.s16;
  }

  /// \brief Returns whether this value is a u32.
  bool is_u32() const { return raw.kind == WASMTIME_COMPONENT_U32; }

  /// \brief Returns the u32 value, only valid if `is_u32()`.
  uint32_t get_u32() const {
    assert(is_u32());
    return raw.of.u32;
  }

  /// \brief Returns whether this value is a s32.
  bool is_s32() const { return raw.kind == WASMTIME_COMPONENT_S32; }

  /// \brief Returns the s32 value, only valid if `is_s32()`.
  int32_t get_s32() const {
    assert(is_s32());
    return raw.of.s32;
  }

  /// \brief Returns whether this value is a u64.
  bool is_u64() const { return raw.kind == WASMTIME_COMPONENT_U64; }

  /// \brief Returns the u64 value, only valid if `is_u64()`.
  uint64_t get_u64() const {
    assert(is_u64());
    return raw.of.u64;
  }

  /// \brief Returns whether this value is a s64.
  bool is_s64() const { return raw.kind == WASMTIME_COMPONENT_S64; }

  /// \brief Returns the s64 value, only valid if `is_s64()`.
  int64_t get_s64() const {
    assert(is_s64());
    return raw.of.s64;
  }

  /// \brief Returns whether this value is a f32.
  bool is_f32() const { return raw.kind == WASMTIME_COMPONENT_F32; }

  /// \brief Returns the f32 value, only valid if `is_f32()`.
  float get_f32() const {
    assert(is_f32());
    return raw.of.f32;
  }

  /// \brief Returns whether this value is a f64.
  bool is_f64() const { return raw.kind == WASMTIME_COMPONENT_F64; }

  /// \brief Returns the f64 value, only valid if `is_f64()`.
  double get_f64() const {
    assert(is_f64());
    return raw.of.f64;
  }

  /// \brief Returns whether this value is a string.
  bool is_string() const { return raw.kind == WASMTIME_COMPONENT_STRING; }

  /// \brief Returns the string value, only valid if `is_string()`.
  std::string_view get_string() const {
    assert(is_string());
    return std::string_view(raw.of.string.data, raw.of.string.size);
  }

  /// \brief Returns whether this value is a list.
  bool is_list() const { return raw.kind == WASMTIME_COMPONENT_LIST; }

  /// \brief Returns the list value, only valid if `is_list()`.
  const List &get_list() const {
    assert(is_list());
    return *List::from_capi(&raw.of.list);
  }

  /// \brief Returns whether this value is a record.
  bool is_record() const { return raw.kind == WASMTIME_COMPONENT_RECORD; }

  /// \brief Returns the record value, only valid if `is_record()`.
  const Record &get_record() const {
    assert(is_record());
    return *Record::from_capi(&raw.of.record);
  }

  /// \brief Returns whether this value is a tuple.
  bool is_tuple() const { return raw.kind == WASMTIME_COMPONENT_TUPLE; }

  /// \brief Returns the tuple value, only valid if `is_tuple()`.
  const Tuple &get_tuple() const {
    assert(is_tuple());
    return *Tuple::from_capi(&raw.of.tuple);
  }

  /// \brief Returns whether this value is a variant.
  bool is_variant() const { return raw.kind == WASMTIME_COMPONENT_VARIANT; }

  /// \brief Returns the variant value, only valid if `is_variant()`.
  const Variant &get_variant() const {
    assert(is_variant());
    return *Variant::from_capi(&raw.of.variant);
  }

  /// \brief Returns whether this value is an option.
  bool is_option() const { return raw.kind == WASMTIME_COMPONENT_OPTION; }

  /// \brief Returns the option value, only valid if `is_option()`.
  const WitOption &get_option() const {
    assert(is_option());
    return *WitOption::from_capi(&raw.of.option);
  }

  /// \brief Returns whether this value is an enum.
  bool is_enum() const { return raw.kind == WASMTIME_COMPONENT_ENUM; }

  /// \brief Returns the enum discriminant, only valid if `is_enum()`.
  std::string_view get_enum() const {
    assert(is_enum());
    return std::string_view(raw.of.enumeration.data, raw.of.enumeration.size);
  }

  /// \brief Returns whether this value is a result.
  bool is_result() const { return raw.kind == WASMTIME_COMPONENT_RESULT; }

  /// \brief Returns the result value, only valid if `is_result()`.
  const WitResult &get_result() const {
    assert(is_result());
    return *WitResult::from_capi(&raw.of.result);
  }

  /// \brief Returns whether this value is flags.
  bool is_flags() const { return raw.kind == WASMTIME_COMPONENT_FLAGS; }

  /// \brief Returns the flags value, only valid if `is_flags()`.
  const Flags &get_flags() const {
    assert(is_flags());
    return *Flags::from_capi(&raw.of.flags);
  }

  /// \brief Returns whether this value is a resource.
  bool is_resource() const { return raw.kind == WASMTIME_COMPONENT_RESOURCE; }

  /// \brief Returns the resource value, only valid if `is_resource()`.
  const ResourceAny &get_resource() const {
    assert(is_resource());
    return *ResourceAny::from_capi(&raw.of.resource);
  }

  /// \brief Returns whether this value is a map.
  bool is_map() const { return raw.kind == WASMTIME_COMPONENT_MAP; }

  /// \brief Returns the map value, only valid if `is_map()`.
  const Map &get_map() const {
    assert(is_map());
    return *Map::from_capi(&raw.of.map);
  }
};

#undef VAL_REPR

inline Record::Record(std::vector<std::pair<std::string_view, Val>> entries) {
  wasmtime_component_valrecord_new_uninit(&raw, entries.size());
  auto dst = raw.data;
  for (auto &&[name, val] : entries) {
    wasm_byte_vec_new(&dst->name, name.size(), name.data());
    new (&dst->val) Val(std::move(val));
    dst++;
  }
}

inline Map::Map(std::vector<std::pair<Val, Val>> entries) {
  wasmtime_component_valmap_new_uninit(&raw, entries.size());
  auto dst = raw.data;
  for (auto &&[key, val] : entries) {
    new (&dst->key) Val(std::move(key));
    new (&dst->value) Val(std::move(val));
    dst++;
  }
}

inline List::List(std::vector<Val> values) {
  wasmtime_component_vallist_new_uninit(&raw, values.size());
  auto dst = raw.data;
  for (auto &&val : values)
    new (dst++) Val(std::move(val));
}

inline Tuple::Tuple(std::vector<Val> values) {
  wasmtime_component_valtuple_new_uninit(&raw, values.size());
  auto dst = raw.data;
  for (auto &&val : values)
    new (dst++) Val(std::move(val));
}

inline Variant::Variant(std::string_view discriminant, std::optional<Val> x) {
  wasm_name_new(&raw.discriminant, discriminant.size(), discriminant.data());
  if (x) {
    raw.val = wasmtime_component_val_new(&x->raw);
  } else {
    raw.val = nullptr;
  }
}

inline WitOption::WitOption(std::optional<Val> v) {
  if (v) {
    raw = wasmtime_component_val_new(&v->raw);
  } else {
    raw = nullptr;
  }
}

inline WitResult WitResult::ok(std::optional<Val> v) {
  wasmtime_component_valresult_t raw;
  raw.is_ok = true;
  if (v) {
    raw.val = wasmtime_component_val_new(&v->raw);
  } else {
    raw.val = nullptr;
  }
  return WitResult(std::move(raw));
}

inline WitResult WitResult::err(std::optional<Val> v) {
  wasmtime_component_valresult_t raw;
  raw.is_ok = false;
  if (v) {
    raw.val = wasmtime_component_val_new(&v->raw);
  } else {
    raw.val = nullptr;
  }
  return WitResult(std::move(raw));
}

} // namespace component
} // namespace wasmtime

#endif // WASMTIME_FEATURE_COMPONENT_MODEL

#endif // WASMTIME_COMPONENT_VAL_H