/src/wasm-tools/crates/wasm-encoder/src/component/instances.rs
Line | Count | Source |
1 | | use super::CORE_INSTANCE_SORT; |
2 | | use crate::{ |
3 | | ComponentExportKind, ComponentExternName, ComponentSection, ComponentSectionId, Encode, |
4 | | ExportKind, encode_section, |
5 | | }; |
6 | | use alloc::vec::Vec; |
7 | | |
8 | | /// Represents an argument to a module instantiation. |
9 | | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
10 | | pub enum ModuleArg { |
11 | | /// The argument is an instance. |
12 | | Instance(u32), |
13 | | } |
14 | | |
15 | | impl Encode for ModuleArg { |
16 | 5.59k | fn encode(&self, sink: &mut Vec<u8>) { |
17 | 5.59k | let (sort, idx) = match self { |
18 | 5.59k | Self::Instance(idx) => (CORE_INSTANCE_SORT, *idx), |
19 | 5.59k | }; |
20 | 5.59k | sink.push(sort); |
21 | 5.59k | idx.encode(sink); |
22 | 5.59k | } |
23 | | } |
24 | | |
25 | | /// An encoder for the core instance section of WebAssembly components. |
26 | | /// |
27 | | /// # Example |
28 | | /// |
29 | | /// ```rust |
30 | | /// use wasm_encoder::{Component, InstanceSection, ExportKind, ModuleArg}; |
31 | | /// |
32 | | /// let mut instances = InstanceSection::new(); |
33 | | /// instances.export_items([("foo", ExportKind::Func, 0)]); |
34 | | /// instances.instantiate(1, [("foo", ModuleArg::Instance(0))]); |
35 | | /// |
36 | | /// let mut component = Component::new(); |
37 | | /// component.section(&instances); |
38 | | /// |
39 | | /// let bytes = component.finish(); |
40 | | /// ``` |
41 | | #[derive(Clone, Debug, Default)] |
42 | | pub struct InstanceSection { |
43 | | bytes: Vec<u8>, |
44 | | num_added: u32, |
45 | | } |
46 | | |
47 | | impl InstanceSection { |
48 | | /// Create a new core instance section encoder. |
49 | 8.17k | pub fn new() -> Self { |
50 | 8.17k | Self::default() |
51 | 8.17k | } |
52 | | |
53 | | /// The number of instances in the section. |
54 | 0 | pub fn len(&self) -> u32 { |
55 | 0 | self.num_added |
56 | 0 | } |
57 | | |
58 | | /// Determines if the section is empty. |
59 | 0 | pub fn is_empty(&self) -> bool { |
60 | 0 | self.num_added == 0 |
61 | 0 | } |
62 | | |
63 | | /// Define an instance by instantiating a core module. |
64 | 7.24k | pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self |
65 | 7.24k | where |
66 | 7.24k | A: IntoIterator<Item = (S, ModuleArg)>, |
67 | 7.24k | A::IntoIter: ExactSizeIterator, |
68 | 7.24k | S: AsRef<str>, |
69 | | { |
70 | 7.24k | let args = args.into_iter(); |
71 | 7.24k | self.bytes.push(0x00); |
72 | 7.24k | module_index.encode(&mut self.bytes); |
73 | 7.24k | args.len().encode(&mut self.bytes); |
74 | 7.24k | for (name, arg) in args { |
75 | 5.59k | name.as_ref().encode(&mut self.bytes); |
76 | 5.59k | arg.encode(&mut self.bytes); |
77 | 5.59k | } |
78 | 7.24k | self.num_added += 1; |
79 | 7.24k | self |
80 | 7.24k | } <wasm_encoder::component::instances::InstanceSection>::instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 0], &str> Line | Count | Source | 64 | 677 | pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self | 65 | 677 | where | 66 | 677 | A: IntoIterator<Item = (S, ModuleArg)>, | 67 | 677 | A::IntoIter: ExactSizeIterator, | 68 | 677 | S: AsRef<str>, | 69 | | { | 70 | 677 | let args = args.into_iter(); | 71 | 677 | self.bytes.push(0x00); | 72 | 677 | module_index.encode(&mut self.bytes); | 73 | 677 | args.len().encode(&mut self.bytes); | 74 | 677 | for (name, arg) in args { | 75 | 0 | name.as_ref().encode(&mut self.bytes); | 76 | 0 | arg.encode(&mut self.bytes); | 77 | 0 | } | 78 | 677 | self.num_added += 1; | 79 | 677 | self | 80 | 677 | } |
<wasm_encoder::component::instances::InstanceSection>::instantiate::<[(&str, wasm_encoder::component::instances::ModuleArg); 1], &str> Line | Count | Source | 64 | 3.62k | pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self | 65 | 3.62k | where | 66 | 3.62k | A: IntoIterator<Item = (S, ModuleArg)>, | 67 | 3.62k | A::IntoIter: ExactSizeIterator, | 68 | 3.62k | S: AsRef<str>, | 69 | | { | 70 | 3.62k | let args = args.into_iter(); | 71 | 3.62k | self.bytes.push(0x00); | 72 | 3.62k | module_index.encode(&mut self.bytes); | 73 | 3.62k | args.len().encode(&mut self.bytes); | 74 | 3.62k | for (name, arg) in args { | 75 | 3.62k | name.as_ref().encode(&mut self.bytes); | 76 | 3.62k | arg.encode(&mut self.bytes); | 77 | 3.62k | } | 78 | 3.62k | self.num_added += 1; | 79 | 3.62k | self | 80 | 3.62k | } |
<wasm_encoder::component::instances::InstanceSection>::instantiate::<alloc::vec::Vec<(&str, wasm_encoder::component::instances::ModuleArg)>, &str> Line | Count | Source | 64 | 2.94k | pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self | 65 | 2.94k | where | 66 | 2.94k | A: IntoIterator<Item = (S, ModuleArg)>, | 67 | 2.94k | A::IntoIter: ExactSizeIterator, | 68 | 2.94k | S: AsRef<str>, | 69 | | { | 70 | 2.94k | let args = args.into_iter(); | 71 | 2.94k | self.bytes.push(0x00); | 72 | 2.94k | module_index.encode(&mut self.bytes); | 73 | 2.94k | args.len().encode(&mut self.bytes); | 74 | 2.94k | for (name, arg) in args { | 75 | 1.97k | name.as_ref().encode(&mut self.bytes); | 76 | 1.97k | arg.encode(&mut self.bytes); | 77 | 1.97k | } | 78 | 2.94k | self.num_added += 1; | 79 | 2.94k | self | 80 | 2.94k | } |
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::instantiate::<_, _> |
81 | | |
82 | | /// Define an instance by exporting core WebAssembly items. |
83 | 5.59k | pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self |
84 | 5.59k | where |
85 | 5.59k | E: IntoIterator<Item = (S, ExportKind, u32)>, |
86 | 5.59k | E::IntoIter: ExactSizeIterator, |
87 | 5.59k | S: AsRef<str>, |
88 | | { |
89 | 5.59k | let exports = exports.into_iter(); |
90 | 5.59k | self.bytes.push(0x01); |
91 | 5.59k | exports.len().encode(&mut self.bytes); |
92 | 16.9k | for (name, kind, index) in exports { |
93 | 16.9k | name.as_ref().encode(&mut self.bytes); |
94 | 16.9k | kind.encode(&mut self.bytes); |
95 | 16.9k | index.encode(&mut self.bytes); |
96 | 16.9k | } |
97 | 5.59k | self.num_added += 1; |
98 | 5.59k | self |
99 | 5.59k | } <wasm_encoder::component::instances::InstanceSection>::export_items::<[(&str, wasm_encoder::core::exports::ExportKind, u32); 1], &str> Line | Count | Source | 83 | 2.94k | pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self | 84 | 2.94k | where | 85 | 2.94k | E: IntoIterator<Item = (S, ExportKind, u32)>, | 86 | 2.94k | E::IntoIter: ExactSizeIterator, | 87 | 2.94k | S: AsRef<str>, | 88 | | { | 89 | 2.94k | let exports = exports.into_iter(); | 90 | 2.94k | self.bytes.push(0x01); | 91 | 2.94k | exports.len().encode(&mut self.bytes); | 92 | 2.94k | for (name, kind, index) in exports { | 93 | 2.94k | name.as_ref().encode(&mut self.bytes); | 94 | 2.94k | kind.encode(&mut self.bytes); | 95 | 2.94k | index.encode(&mut self.bytes); | 96 | 2.94k | } | 97 | 2.94k | self.num_added += 1; | 98 | 2.94k | self | 99 | 2.94k | } |
<wasm_encoder::component::instances::InstanceSection>::export_items::<alloc::vec::Vec<(&str, wasm_encoder::core::exports::ExportKind, u32)>, &str> Line | Count | Source | 83 | 2.64k | pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self | 84 | 2.64k | where | 85 | 2.64k | E: IntoIterator<Item = (S, ExportKind, u32)>, | 86 | 2.64k | E::IntoIter: ExactSizeIterator, | 87 | 2.64k | S: AsRef<str>, | 88 | | { | 89 | 2.64k | let exports = exports.into_iter(); | 90 | 2.64k | self.bytes.push(0x01); | 91 | 2.64k | exports.len().encode(&mut self.bytes); | 92 | 13.9k | for (name, kind, index) in exports { | 93 | 13.9k | name.as_ref().encode(&mut self.bytes); | 94 | 13.9k | kind.encode(&mut self.bytes); | 95 | 13.9k | index.encode(&mut self.bytes); | 96 | 13.9k | } | 97 | 2.64k | self.num_added += 1; | 98 | 2.64k | self | 99 | 2.64k | } |
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<core::iter::adapters::map::Map<core::slice::iter::Iter<(alloc::string::String, wasm_encoder::core::exports::ExportKind, u32)>, <wit_component::encoding::EncodingState>::create_export_task_initialization_wrappers::{closure#2}>, &str>Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<_, _> |
100 | | } |
101 | | |
102 | | impl Encode for InstanceSection { |
103 | 8.17k | fn encode(&self, sink: &mut Vec<u8>) { |
104 | 8.17k | encode_section(sink, self.num_added, &self.bytes); |
105 | 8.17k | } |
106 | | } |
107 | | |
108 | | impl ComponentSection for InstanceSection { |
109 | 8.17k | fn id(&self) -> u8 { |
110 | 8.17k | ComponentSectionId::CoreInstance.into() |
111 | 8.17k | } |
112 | | } |
113 | | |
114 | | /// An encoder for the instance section of WebAssembly components. |
115 | | /// |
116 | | /// # Example |
117 | | /// |
118 | | /// ```rust |
119 | | /// use wasm_encoder::{Component, ComponentInstanceSection, ComponentExportKind}; |
120 | | /// |
121 | | /// let mut instances = ComponentInstanceSection::new(); |
122 | | /// instances.export_items([("foo", ComponentExportKind::Func, 0)]); |
123 | | /// instances.instantiate(1, [("foo", ComponentExportKind::Instance, 0)]); |
124 | | /// |
125 | | /// let mut component = Component::new(); |
126 | | /// component.section(&instances); |
127 | | /// |
128 | | /// let bytes = component.finish(); |
129 | | /// ``` |
130 | | #[derive(Clone, Debug, Default)] |
131 | | pub struct ComponentInstanceSection { |
132 | | bytes: Vec<u8>, |
133 | | num_added: u32, |
134 | | } |
135 | | |
136 | | impl ComponentInstanceSection { |
137 | | /// Create a new instance section encoder. |
138 | 1.72k | pub fn new() -> Self { |
139 | 1.72k | Self::default() |
140 | 1.72k | } |
141 | | |
142 | | /// The number of instances in the section. |
143 | 0 | pub fn len(&self) -> u32 { |
144 | 0 | self.num_added |
145 | 0 | } |
146 | | |
147 | | /// Determines if the section is empty. |
148 | 0 | pub fn is_empty(&self) -> bool { |
149 | 0 | self.num_added == 0 |
150 | 0 | } |
151 | | |
152 | | /// Define an instance by instantiating a component. |
153 | 1.72k | pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self |
154 | 1.72k | where |
155 | 1.72k | A: IntoIterator<Item = (S, ComponentExportKind, u32)>, |
156 | 1.72k | A::IntoIter: ExactSizeIterator, |
157 | 1.72k | S: AsRef<str>, |
158 | | { |
159 | 1.72k | let args = args.into_iter(); |
160 | 1.72k | self.bytes.push(0x00); |
161 | 1.72k | component_index.encode(&mut self.bytes); |
162 | 1.72k | args.len().encode(&mut self.bytes); |
163 | 3.13k | for (name, kind, index) in args { |
164 | 3.13k | name.as_ref().encode(&mut self.bytes); |
165 | 3.13k | kind.encode(&mut self.bytes); |
166 | 3.13k | index.encode(&mut self.bytes); |
167 | 3.13k | } |
168 | 1.72k | self.num_added += 1; |
169 | 1.72k | self |
170 | 1.72k | } <wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<alloc::vec::Vec<(alloc::string::String, wasm_encoder::component::exports::ComponentExportKind, u32)>, alloc::string::String> Line | Count | Source | 153 | 1.72k | pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self | 154 | 1.72k | where | 155 | 1.72k | A: IntoIterator<Item = (S, ComponentExportKind, u32)>, | 156 | 1.72k | A::IntoIter: ExactSizeIterator, | 157 | 1.72k | S: AsRef<str>, | 158 | | { | 159 | 1.72k | let args = args.into_iter(); | 160 | 1.72k | self.bytes.push(0x00); | 161 | 1.72k | component_index.encode(&mut self.bytes); | 162 | 1.72k | args.len().encode(&mut self.bytes); | 163 | 3.13k | for (name, kind, index) in args { | 164 | 3.13k | name.as_ref().encode(&mut self.bytes); | 165 | 3.13k | kind.encode(&mut self.bytes); | 166 | 3.13k | index.encode(&mut self.bytes); | 167 | 3.13k | } | 168 | 1.72k | self.num_added += 1; | 169 | 1.72k | self | 170 | 1.72k | } |
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<_, _> |
171 | | |
172 | | /// Define an instance by exporting items. |
173 | 0 | pub fn export_items<'a, N, E>(&mut self, exports: E) -> &mut Self |
174 | 0 | where |
175 | 0 | E: IntoIterator<Item = (N, ComponentExportKind, u32)>, |
176 | 0 | E::IntoIter: ExactSizeIterator, |
177 | 0 | N: Into<ComponentExternName<'a>>, |
178 | | { |
179 | 0 | let exports = exports.into_iter(); |
180 | 0 | self.bytes.push(0x01); |
181 | 0 | exports.len().encode(&mut self.bytes); |
182 | 0 | for (name, kind, index) in exports { |
183 | 0 | name.into().encode(&mut self.bytes); |
184 | 0 | kind.encode(&mut self.bytes); |
185 | 0 | index.encode(&mut self.bytes); |
186 | 0 | } |
187 | 0 | self.num_added += 1; |
188 | 0 | self |
189 | 0 | } |
190 | | } |
191 | | |
192 | | impl Encode for ComponentInstanceSection { |
193 | 1.72k | fn encode(&self, sink: &mut Vec<u8>) { |
194 | 1.72k | encode_section(sink, self.num_added, &self.bytes); |
195 | 1.72k | } |
196 | | } |
197 | | |
198 | | impl ComponentSection for ComponentInstanceSection { |
199 | 1.72k | fn id(&self) -> u8 { |
200 | 1.72k | ComponentSectionId::Instance.into() |
201 | 1.72k | } |
202 | | } |