/src/WasmEdge/include/common/component_variant.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/common/component_variant.h - Component value variant -----===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the host-side value model of the Component Model. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include <cstdint> |
17 | | #include <memory> |
18 | | #include <optional> |
19 | | #include <string> |
20 | | #include <utility> |
21 | | #include <variant> |
22 | | #include <vector> |
23 | | |
24 | | namespace WasmEdge { |
25 | | |
26 | | /// Canonical ABI string encoding (CanonicalABI.md `string-encoding` canon |
27 | | /// option). The host always represents component strings as a UTF-8 |
28 | | /// std::string; this selects how the *guest* lays them out in linear memory, so |
29 | | /// lift decodes from it and lower encodes to it. |
30 | | enum class StringEncoding : uint8_t { UTF8, UTF16, Latin1UTF16 }; |
31 | | |
32 | | /// Forward declaration of the aggregate component value container. |
33 | | /// Defined after ComponentValVariant so members can hold ComponentValVariant. |
34 | | struct ValComp; |
35 | | |
36 | | using ComponentValVariant = std::variant< |
37 | | // Primitive types in the Component Model value model. Held in typed arms |
38 | | // directly (no ValVariant wrapping) so that aggregate-internal primitives |
39 | | // and top-level primitives share a single representation. See |
40 | | // CanonicalABI.md L2920+ — component values are the spec's typed Python |
41 | | // values; core wasm ValVariants belong to the orthogonal Core layer and |
42 | | // never appear inside a component value. |
43 | | uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, |
44 | | float, double, bool, std::string, |
45 | | // Aggregate types (record/variant/list/tuple/option/result/flags/enum/own/ |
46 | | // borrow). Held via shared_ptr because the variant is frequently copied and |
47 | | // the contained ValComp recursively holds further ComponentValVariants. |
48 | | std::shared_ptr<ValComp>>; |
49 | | |
50 | | // Per-aggregate value structs. Labels for records/variants/flags/enums are |
51 | | // retained on the value side for easier round-trip debugging — the canonical |
52 | | // source of truth remains the DefValType. |
53 | | struct RecordVal { |
54 | | std::vector<std::pair<std::string, ComponentValVariant>> Fields; |
55 | | }; |
56 | | struct TupleVal { |
57 | | std::vector<ComponentValVariant> Values; |
58 | | }; |
59 | | struct VariantVal { |
60 | | uint32_t Case = 0; |
61 | | std::optional<ComponentValVariant> Payload; |
62 | | // Case label recorded by the value decoder. TODO: resolve host-supplied |
63 | | // values by label against the declared type at the lowering site. |
64 | | std::string Label{}; |
65 | | }; |
66 | | struct ListVal { |
67 | | std::vector<ComponentValVariant> Elements; |
68 | | }; |
69 | | struct OptionVal { |
70 | | std::optional<ComponentValVariant> Value; |
71 | | }; |
72 | | struct ResultVal { |
73 | | bool IsOk; |
74 | | std::optional<ComponentValVariant> Payload; |
75 | | }; |
76 | | struct FlagsVal { |
77 | | std::vector<bool> Bits; |
78 | | // Set-label form recorded by the value decoder. TODO: resolve the bits |
79 | | // against the declared flag labels at the lowering site. |
80 | | std::vector<std::string> SetLabels{}; |
81 | | }; |
82 | | struct EnumVal { |
83 | | uint32_t Case = 0; |
84 | | // Case label recorded like VariantVal::Label. |
85 | | std::string Label{}; |
86 | | }; |
87 | | struct OwnVal { |
88 | | uint32_t Handle; |
89 | | }; |
90 | | struct BorrowVal { |
91 | | uint32_t Handle; |
92 | | }; |
93 | | |
94 | | struct ValComp { |
95 | | std::variant<RecordVal, TupleVal, VariantVal, ListVal, OptionVal, ResultVal, |
96 | | FlagsVal, EnumVal, OwnVal, BorrowVal> |
97 | | V; |
98 | | }; |
99 | | |
100 | | /// Wrap an aggregate value in a heap-allocated ValComp and lift it into a |
101 | | /// ComponentValVariant. Saves the make_shared + V= + variant-wrap dance at the |
102 | | /// many lift/load call sites. |
103 | 19.4k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { |
104 | 19.4k | auto VC = std::make_shared<ValComp>(); |
105 | 19.4k | VC->V = std::forward<T>(Inner); |
106 | 19.4k | return ComponentValVariant{std::move(VC)}; |
107 | 19.4k | } std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::RecordVal>(WasmEdge::RecordVal&&) Line | Count | Source | 103 | 1.34k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 1.34k | auto VC = std::make_shared<ValComp>(); | 105 | 1.34k | VC->V = std::forward<T>(Inner); | 106 | 1.34k | return ComponentValVariant{std::move(VC)}; | 107 | 1.34k | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::VariantVal>(WasmEdge::VariantVal&&) Line | Count | Source | 103 | 1.55k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 1.55k | auto VC = std::make_shared<ValComp>(); | 105 | 1.55k | VC->V = std::forward<T>(Inner); | 106 | 1.55k | return ComponentValVariant{std::move(VC)}; | 107 | 1.55k | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::ListVal>(WasmEdge::ListVal&&) Line | Count | Source | 103 | 5.00k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 5.00k | auto VC = std::make_shared<ValComp>(); | 105 | 5.00k | VC->V = std::forward<T>(Inner); | 106 | 5.00k | return ComponentValVariant{std::move(VC)}; | 107 | 5.00k | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::TupleVal>(WasmEdge::TupleVal&&) Line | Count | Source | 103 | 7.15k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 7.15k | auto VC = std::make_shared<ValComp>(); | 105 | 7.15k | VC->V = std::forward<T>(Inner); | 106 | 7.15k | return ComponentValVariant{std::move(VC)}; | 107 | 7.15k | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::FlagsVal>(WasmEdge::FlagsVal&&) Line | Count | Source | 103 | 834 | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 834 | auto VC = std::make_shared<ValComp>(); | 105 | 834 | VC->V = std::forward<T>(Inner); | 106 | 834 | return ComponentValVariant{std::move(VC)}; | 107 | 834 | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::EnumVal>(WasmEdge::EnumVal&&) Line | Count | Source | 103 | 290 | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 290 | auto VC = std::make_shared<ValComp>(); | 105 | 290 | VC->V = std::forward<T>(Inner); | 106 | 290 | return ComponentValVariant{std::move(VC)}; | 107 | 290 | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::OptionVal>(WasmEdge::OptionVal&&) Line | Count | Source | 103 | 2.60k | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 2.60k | auto VC = std::make_shared<ValComp>(); | 105 | 2.60k | VC->V = std::forward<T>(Inner); | 106 | 2.60k | return ComponentValVariant{std::move(VC)}; | 107 | 2.60k | } |
std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::ResultVal>(WasmEdge::ResultVal&&) Line | Count | Source | 103 | 709 | template <typename T> inline ComponentValVariant makeComponentVal(T &&Inner) { | 104 | 709 | auto VC = std::make_shared<ValComp>(); | 105 | 709 | VC->V = std::forward<T>(Inner); | 106 | 709 | return ComponentValVariant{std::move(VC)}; | 107 | 709 | } |
Unexecuted instantiation: std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::OwnVal>(WasmEdge::OwnVal&&) Unexecuted instantiation: std::__1::variant<unsigned char, unsigned short, unsigned int, unsigned long, signed char, short, int, long, float, double, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<WasmEdge::ValComp> > WasmEdge::makeComponentVal<WasmEdge::BorrowVal>(WasmEdge::BorrowVal&&) |
108 | | |
109 | | } // namespace WasmEdge |