/build/cargo-vendor-dir/wasm-encoder-0.212.0/src/component/aliases.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::{COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, CORE_TYPE_SORT, TYPE_SORT}; |
2 | | use crate::{ |
3 | | encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, Encode, ExportKind, |
4 | | }; |
5 | | |
6 | | /// Represents the kinds of outer aliasable items in a component. |
7 | | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
8 | | pub enum ComponentOuterAliasKind { |
9 | | /// The alias is to a core module. |
10 | | CoreModule, |
11 | | /// The alias is to a core type. |
12 | | CoreType, |
13 | | /// The alias is to a type. |
14 | | Type, |
15 | | /// The alias is to a component. |
16 | | Component, |
17 | | } |
18 | | |
19 | | impl Encode for ComponentOuterAliasKind { |
20 | 0 | fn encode(&self, sink: &mut Vec<u8>) { |
21 | 0 | match self { |
22 | 0 | Self::CoreModule => { |
23 | 0 | sink.push(CORE_SORT); |
24 | 0 | sink.push(CORE_MODULE_SORT); |
25 | 0 | } |
26 | 0 | Self::CoreType => { |
27 | 0 | sink.push(CORE_SORT); |
28 | 0 | sink.push(CORE_TYPE_SORT); |
29 | 0 | } |
30 | 0 | Self::Type => sink.push(TYPE_SORT), |
31 | 0 | Self::Component => sink.push(COMPONENT_SORT), |
32 | | } |
33 | 0 | } |
34 | | } |
35 | | |
36 | | /// An encoder for the alias section of WebAssembly component. |
37 | | /// |
38 | | /// # Example |
39 | | /// |
40 | | /// ```rust |
41 | | /// use wasm_encoder::{Component, Alias, ComponentAliasSection, ComponentExportKind, ComponentOuterAliasKind}; |
42 | | /// |
43 | | /// let mut aliases = ComponentAliasSection::new(); |
44 | | /// aliases.alias(Alias::InstanceExport { instance: 0, kind: ComponentExportKind::Func, name: "f" }); |
45 | | /// aliases.alias(Alias::Outer { count: 0, kind: ComponentOuterAliasKind::Type, index: 1 }); |
46 | | /// |
47 | | /// let mut component = Component::new(); |
48 | | /// component.section(&aliases); |
49 | | /// |
50 | | /// let bytes = component.finish(); |
51 | | /// ``` |
52 | | #[derive(Clone, Debug, Default)] |
53 | | pub struct ComponentAliasSection { |
54 | | bytes: Vec<u8>, |
55 | | num_added: u32, |
56 | | } |
57 | | |
58 | | /// Different forms of aliases that can be inserted into a |
59 | | /// [`ComponentAliasSection`]. |
60 | | #[derive(Copy, Clone, Debug)] |
61 | | pub enum Alias<'a> { |
62 | | /// An alias of a component instance export. |
63 | | InstanceExport { |
64 | | /// The index of the component instance that's being aliased from. |
65 | | instance: u32, |
66 | | /// The kind of item that's being extracted from the component |
67 | | /// instance. |
68 | | kind: ComponentExportKind, |
69 | | /// The name of the export that's being aliased. |
70 | | name: &'a str, |
71 | | }, |
72 | | /// Same as `InstanceExport`, but for core instances. |
73 | | #[allow(missing_docs)] |
74 | | CoreInstanceExport { |
75 | | instance: u32, |
76 | | kind: ExportKind, |
77 | | name: &'a str, |
78 | | }, |
79 | | /// Aliasing an item from an outer component. |
80 | | Outer { |
81 | | /// The kind of item being aliased, either a type or a component. |
82 | | kind: ComponentOuterAliasKind, |
83 | | /// Number of levels "up" to go to lookup the index within. Level 0 is |
84 | | /// the current scope and level 1 is the enclosing scope, and so on. |
85 | | count: u32, |
86 | | /// The index of the item to alias within the scope referenced by |
87 | | /// `count`. |
88 | | index: u32, |
89 | | }, |
90 | | } |
91 | | |
92 | | impl ComponentAliasSection { |
93 | | /// Create a new alias section encoder. |
94 | 0 | pub fn new() -> Self { |
95 | 0 | Self::default() |
96 | 0 | } |
97 | | |
98 | | /// The number of aliases in the section. |
99 | 0 | pub fn len(&self) -> u32 { |
100 | 0 | self.num_added |
101 | 0 | } |
102 | | |
103 | | /// Determines if the section is empty. |
104 | 0 | pub fn is_empty(&self) -> bool { |
105 | 0 | self.num_added == 0 |
106 | 0 | } |
107 | | |
108 | | /// Define an alias to a component instance's export. |
109 | 0 | pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { |
110 | 0 | alias.encode(&mut self.bytes); |
111 | 0 | self.num_added += 1; |
112 | 0 | self |
113 | 0 | } |
114 | | } |
115 | | |
116 | | impl Encode for ComponentAliasSection { |
117 | 0 | fn encode(&self, sink: &mut Vec<u8>) { |
118 | 0 | encode_section(sink, self.num_added, &self.bytes); |
119 | 0 | } |
120 | | } |
121 | | |
122 | | impl ComponentSection for ComponentAliasSection { |
123 | 0 | fn id(&self) -> u8 { |
124 | 0 | ComponentSectionId::Alias.into() |
125 | 0 | } |
126 | | } |
127 | | |
128 | | impl Encode for Alias<'_> { |
129 | 0 | fn encode(&self, sink: &mut Vec<u8>) { |
130 | 0 | match self { |
131 | | Alias::InstanceExport { |
132 | 0 | instance, |
133 | 0 | kind, |
134 | 0 | name, |
135 | 0 | } => { |
136 | 0 | kind.encode(sink); |
137 | 0 | sink.push(0x00); |
138 | 0 | instance.encode(sink); |
139 | 0 | name.encode(sink); |
140 | 0 | } |
141 | | Alias::CoreInstanceExport { |
142 | 0 | instance, |
143 | 0 | kind, |
144 | 0 | name, |
145 | 0 | } => { |
146 | 0 | sink.push(CORE_SORT); |
147 | 0 | kind.encode(sink); |
148 | 0 | sink.push(0x01); |
149 | 0 | instance.encode(sink); |
150 | 0 | name.encode(sink); |
151 | 0 | } |
152 | 0 | Alias::Outer { kind, count, index } => { |
153 | 0 | kind.encode(sink); |
154 | 0 | sink.push(0x02); |
155 | 0 | count.encode(sink); |
156 | 0 | index.encode(sink); |
157 | 0 | } |
158 | | } |
159 | 0 | } |
160 | | } |