/src/wasm-tools/crates/wasm-encoder/src/reencode/component.rs
Line | Count | Source |
1 | | use crate::reencode::{Error, Reencode, RoundtripReencoder}; |
2 | | use alloc::boxed::Box; |
3 | | use core::convert::Infallible; |
4 | | |
5 | | #[allow(missing_docs)] // FIXME |
6 | | pub trait ReencodeComponent: Reencode { |
7 | 0 | fn component_type_index(&mut self, ty: u32) -> u32 { |
8 | 0 | ty |
9 | 0 | } |
10 | | |
11 | 0 | fn component_instance_index(&mut self, ty: u32) -> u32 { |
12 | 0 | ty |
13 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::component_instance_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::component_instance_index |
14 | | |
15 | 0 | fn component_func_index(&mut self, ty: u32) -> u32 { |
16 | 0 | ty |
17 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::component_func_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::component_func_index |
18 | | |
19 | 0 | fn component_index(&mut self, ty: u32) -> u32 { |
20 | 0 | ty |
21 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::component_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::component_index |
22 | | |
23 | 0 | fn module_index(&mut self, ty: u32) -> u32 { |
24 | 0 | ty |
25 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::module_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::module_index |
26 | | |
27 | 0 | fn instance_index(&mut self, ty: u32) -> u32 { |
28 | 0 | ty |
29 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::instance_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::instance_index |
30 | | |
31 | 0 | fn component_value_index(&mut self, ty: u32) -> u32 { |
32 | 0 | ty |
33 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::component_value_index Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::component_value_index |
34 | | |
35 | 0 | fn outer_type_index(&mut self, count: u32, ty: u32) -> Result<u32, Error<Self::Error>> { |
36 | 0 | let _ = count; |
37 | 0 | self.type_index(ty) |
38 | 0 | } |
39 | | |
40 | 0 | fn outer_component_type_index(&mut self, count: u32, ty: u32) -> u32 { |
41 | 0 | let _ = count; |
42 | 0 | self.component_type_index(ty) |
43 | 0 | } |
44 | | |
45 | 0 | fn outer_component_index(&mut self, count: u32, component: u32) -> u32 { |
46 | 0 | let _ = count; |
47 | 0 | self.component_index(component) |
48 | 0 | } |
49 | | |
50 | 0 | fn outer_module_index(&mut self, count: u32, module: u32) -> u32 { |
51 | 0 | let _ = count; |
52 | 0 | self.module_index(module) |
53 | 0 | } |
54 | | |
55 | 0 | fn push_depth(&mut self) {} |
56 | | |
57 | 0 | fn pop_depth(&mut self) {} |
58 | | |
59 | 0 | fn component_external_index( |
60 | 0 | &mut self, |
61 | 0 | kind: wasmparser::ComponentExternalKind, |
62 | 0 | index: u32, |
63 | 0 | ) -> u32 { |
64 | 0 | match kind { |
65 | 0 | wasmparser::ComponentExternalKind::Func => self.component_func_index(index), |
66 | 0 | wasmparser::ComponentExternalKind::Module => self.module_index(index), |
67 | 0 | wasmparser::ComponentExternalKind::Component => self.component_index(index), |
68 | 0 | wasmparser::ComponentExternalKind::Type => self.component_type_index(index), |
69 | 0 | wasmparser::ComponentExternalKind::Instance => self.component_instance_index(index), |
70 | 0 | wasmparser::ComponentExternalKind::Value => self.component_value_index(index), |
71 | | } |
72 | 0 | } |
73 | | |
74 | 0 | fn parse_component( |
75 | 0 | &mut self, |
76 | 0 | component: &mut crate::Component, |
77 | 0 | parser: wasmparser::Parser, |
78 | 0 | data: &[u8], |
79 | 0 | ) -> Result<(), Error<Self::Error>> { |
80 | | // so we can slice into the data with the offsets from the parser |
81 | 0 | assert_eq!(parser.offset(), 0, "data must be parsed at offset 0"); |
82 | 0 | component_utils::parse_component(self, component, parser, data, data) |
83 | 0 | } |
84 | | |
85 | 0 | fn parse_component_payload( |
86 | 0 | &mut self, |
87 | 0 | component: &mut crate::Component, |
88 | 0 | payload: wasmparser::Payload<'_>, |
89 | 0 | whole_component: &[u8], |
90 | 0 | ) -> Result<(), Error<Self::Error>> { |
91 | 0 | component_utils::parse_component_payload(self, component, payload, whole_component) |
92 | 0 | } |
93 | | |
94 | 0 | fn parse_component_submodule( |
95 | 0 | &mut self, |
96 | 0 | component: &mut crate::Component, |
97 | 0 | parser: wasmparser::Parser, |
98 | 0 | module: &[u8], |
99 | 0 | ) -> Result<(), Error<Self::Error>> { |
100 | 0 | component_utils::parse_component_submodule(self, component, parser, module) |
101 | 0 | } |
102 | | |
103 | 0 | fn parse_component_subcomponent( |
104 | 0 | &mut self, |
105 | 0 | component: &mut crate::Component, |
106 | 0 | parser: wasmparser::Parser, |
107 | 0 | subcomponent: &[u8], |
108 | 0 | whole_component: &[u8], |
109 | 0 | ) -> Result<(), Error<Self::Error>> { |
110 | 0 | component_utils::parse_component_subcomponent( |
111 | 0 | self, |
112 | 0 | component, |
113 | 0 | parser, |
114 | 0 | subcomponent, |
115 | 0 | whole_component, |
116 | | ) |
117 | 0 | } |
118 | | |
119 | 0 | fn parse_unknown_component_section( |
120 | 0 | &mut self, |
121 | 0 | component: &mut crate::Component, |
122 | 0 | id: u8, |
123 | 0 | contents: &[u8], |
124 | 0 | ) -> Result<(), Error<Self::Error>> { |
125 | 0 | component_utils::parse_unknown_component_section(self, component, id, contents) |
126 | 0 | } |
127 | | |
128 | 0 | fn parse_component_custom_section( |
129 | 0 | &mut self, |
130 | 0 | component: &mut crate::Component, |
131 | 0 | section: wasmparser::CustomSectionReader<'_>, |
132 | 0 | ) -> Result<(), Error<Self::Error>> { |
133 | 0 | component_utils::parse_component_custom_section(self, component, section) |
134 | 0 | } |
135 | | |
136 | 0 | fn parse_component_type_section( |
137 | 0 | &mut self, |
138 | 0 | types: &mut crate::ComponentTypeSection, |
139 | 0 | section: wasmparser::ComponentTypeSectionReader<'_>, |
140 | 0 | ) -> Result<(), Error<Self::Error>> { |
141 | 0 | component_utils::parse_component_type_section(self, types, section) |
142 | 0 | } |
143 | | |
144 | 0 | fn parse_component_type( |
145 | 0 | &mut self, |
146 | 0 | dst: crate::ComponentTypeEncoder<'_>, |
147 | 0 | ty: wasmparser::ComponentType<'_>, |
148 | 0 | ) -> Result<(), Error<Self::Error>> { |
149 | 0 | component_utils::parse_component_type(self, dst, ty) |
150 | 0 | } |
151 | | |
152 | 0 | fn component_instance_type( |
153 | 0 | &mut self, |
154 | 0 | ty: Box<[wasmparser::InstanceTypeDeclaration<'_>]>, |
155 | 0 | ) -> Result<crate::InstanceType, Error<Self::Error>> { |
156 | 0 | component_utils::component_instance_type(self, ty) |
157 | 0 | } |
158 | | |
159 | 0 | fn parse_component_instance_type_declaration( |
160 | 0 | &mut self, |
161 | 0 | ty: &mut crate::InstanceType, |
162 | 0 | decl: wasmparser::InstanceTypeDeclaration<'_>, |
163 | 0 | ) -> Result<(), Error<Self::Error>> { |
164 | 0 | component_utils::parse_component_instance_type_declaration(self, ty, decl) |
165 | 0 | } |
166 | | |
167 | 0 | fn parse_component_core_type( |
168 | 0 | &mut self, |
169 | 0 | ty: crate::ComponentCoreTypeEncoder<'_>, |
170 | 0 | core: wasmparser::CoreType<'_>, |
171 | 0 | ) -> Result<(), Error<Self::Error>> { |
172 | 0 | component_utils::parse_component_core_type(self, ty, core) |
173 | 0 | } |
174 | | |
175 | 0 | fn component_type( |
176 | 0 | &mut self, |
177 | 0 | ty: Box<[wasmparser::ComponentTypeDeclaration<'_>]>, |
178 | 0 | ) -> Result<crate::ComponentType, Error<Self::Error>> { |
179 | 0 | component_utils::component_type(self, ty) |
180 | 0 | } |
181 | | |
182 | 0 | fn parse_component_type_declaration( |
183 | 0 | &mut self, |
184 | 0 | component: &mut crate::ComponentType, |
185 | 0 | decl: wasmparser::ComponentTypeDeclaration<'_>, |
186 | 0 | ) -> Result<(), Error<Self::Error>> { |
187 | 0 | component_utils::parse_component_type_declaration(self, component, decl) |
188 | 0 | } |
189 | | |
190 | 0 | fn parse_component_func_type( |
191 | 0 | &mut self, |
192 | 0 | func: crate::ComponentFuncTypeEncoder<'_>, |
193 | 0 | ty: wasmparser::ComponentFuncType<'_>, |
194 | 0 | ) -> Result<(), Error<Self::Error>> { |
195 | 0 | component_utils::parse_component_func_type(self, func, ty) |
196 | 0 | } |
197 | | |
198 | 0 | fn parse_component_defined_type( |
199 | 0 | &mut self, |
200 | 0 | defined: crate::ComponentDefinedTypeEncoder<'_>, |
201 | 0 | ty: wasmparser::ComponentDefinedType<'_>, |
202 | 0 | ) -> Result<(), Error<Self::Error>> { |
203 | 0 | component_utils::parse_component_defined_type(self, defined, ty) |
204 | 0 | } |
205 | | |
206 | 0 | fn component_module_type( |
207 | 0 | &mut self, |
208 | 0 | ty: Box<[wasmparser::ModuleTypeDeclaration<'_>]>, |
209 | 0 | ) -> Result<crate::ModuleType, Error<Self::Error>> { |
210 | 0 | component_utils::component_module_type(self, ty) |
211 | 0 | } |
212 | | |
213 | 0 | fn parse_component_module_type_declaration( |
214 | 0 | &mut self, |
215 | 0 | module: &mut crate::ModuleType, |
216 | 0 | decl: wasmparser::ModuleTypeDeclaration<'_>, |
217 | 0 | ) -> Result<(), Error<Self::Error>> { |
218 | 0 | component_utils::parse_component_module_type_declaration(self, module, decl) |
219 | 0 | } |
220 | | |
221 | 0 | fn component_alias<'a>( |
222 | 0 | &mut self, |
223 | 0 | alias: wasmparser::ComponentAlias<'a>, |
224 | 0 | ) -> Result<crate::Alias<'a>, Error<Self::Error>> { |
225 | 0 | component_utils::component_alias(self, alias) |
226 | 0 | } |
227 | | |
228 | 0 | fn parse_component_import_section( |
229 | 0 | &mut self, |
230 | 0 | imports: &mut crate::ComponentImportSection, |
231 | 0 | section: wasmparser::ComponentImportSectionReader<'_>, |
232 | 0 | ) -> Result<(), Error<Self::Error>> { |
233 | 0 | component_utils::parse_component_import_section(self, imports, section) |
234 | 0 | } |
235 | | |
236 | 0 | fn parse_component_canonical_section( |
237 | 0 | &mut self, |
238 | 0 | canonical: &mut crate::CanonicalFunctionSection, |
239 | 0 | section: wasmparser::ComponentCanonicalSectionReader<'_>, |
240 | 0 | ) -> Result<(), Error<Self::Error>> { |
241 | 0 | component_utils::parse_component_canonical_section(self, canonical, section) |
242 | 0 | } |
243 | | |
244 | 0 | fn parse_component_canonical( |
245 | 0 | &mut self, |
246 | 0 | section: &mut crate::CanonicalFunctionSection, |
247 | 0 | func: wasmparser::CanonicalFunction, |
248 | 0 | ) -> Result<(), Error<Self::Error>> { |
249 | 0 | component_utils::parse_component_canonical(self, section, func) |
250 | 0 | } |
251 | | |
252 | 0 | fn parse_component_alias_section( |
253 | 0 | &mut self, |
254 | 0 | aliases: &mut crate::ComponentAliasSection, |
255 | 0 | section: wasmparser::ComponentAliasSectionReader<'_>, |
256 | 0 | ) -> Result<(), Error<Self::Error>> { |
257 | 0 | component_utils::parse_component_alias_section(self, aliases, section) |
258 | 0 | } |
259 | | |
260 | 0 | fn parse_component_instance_section( |
261 | 0 | &mut self, |
262 | 0 | instances: &mut crate::ComponentInstanceSection, |
263 | 0 | section: wasmparser::ComponentInstanceSectionReader<'_>, |
264 | 0 | ) -> Result<(), Error<Self::Error>> { |
265 | 0 | component_utils::parse_component_instance_section(self, instances, section) |
266 | 0 | } |
267 | | |
268 | 0 | fn parse_component_instance( |
269 | 0 | &mut self, |
270 | 0 | instances: &mut crate::ComponentInstanceSection, |
271 | 0 | instance: wasmparser::ComponentInstance<'_>, |
272 | 0 | ) -> Result<(), Error<Self::Error>> { |
273 | 0 | component_utils::parse_component_instance(self, instances, instance) |
274 | 0 | } |
275 | | |
276 | 0 | fn parse_instance_section( |
277 | 0 | &mut self, |
278 | 0 | instances: &mut crate::InstanceSection, |
279 | 0 | section: wasmparser::InstanceSectionReader<'_>, |
280 | 0 | ) -> Result<(), Error<Self::Error>> { |
281 | 0 | component_utils::parse_instance_section(self, instances, section) |
282 | 0 | } |
283 | | |
284 | 0 | fn parse_instance( |
285 | 0 | &mut self, |
286 | 0 | instances: &mut crate::InstanceSection, |
287 | 0 | instance: wasmparser::Instance<'_>, |
288 | 0 | ) -> Result<(), Error<Self::Error>> { |
289 | 0 | component_utils::parse_instance(self, instances, instance) |
290 | 0 | } |
291 | | |
292 | 0 | fn parse_core_type_section( |
293 | 0 | &mut self, |
294 | 0 | types: &mut crate::CoreTypeSection, |
295 | 0 | section: wasmparser::CoreTypeSectionReader<'_>, |
296 | 0 | ) -> Result<(), Error<Self::Error>> { |
297 | 0 | component_utils::parse_core_type_section(self, types, section) |
298 | 0 | } |
299 | | |
300 | 0 | fn parse_component_export_section( |
301 | 0 | &mut self, |
302 | 0 | exports: &mut crate::ComponentExportSection, |
303 | 0 | section: wasmparser::ComponentExportSectionReader<'_>, |
304 | 0 | ) -> Result<(), Error<Self::Error>> { |
305 | 0 | component_utils::parse_component_export_section(self, exports, section) |
306 | 0 | } |
307 | | |
308 | 0 | fn parse_component_export( |
309 | 0 | &mut self, |
310 | 0 | exports: &mut crate::ComponentExportSection, |
311 | 0 | export: wasmparser::ComponentExport<'_>, |
312 | 0 | ) -> Result<(), Error<Self::Error>> { |
313 | 0 | component_utils::parse_component_export(self, exports, export) |
314 | 0 | } |
315 | | |
316 | 0 | fn parse_component_start_section( |
317 | 0 | &mut self, |
318 | 0 | component: &mut crate::Component, |
319 | 0 | func: wasmparser::ComponentStartFunction, |
320 | 0 | ) -> Result<(), Error<Self::Error>> { |
321 | 0 | component_utils::parse_component_start_section(self, component, func) |
322 | 0 | } |
323 | | |
324 | 0 | fn component_type_ref( |
325 | 0 | &mut self, |
326 | 0 | ty: wasmparser::ComponentTypeRef, |
327 | 0 | ) -> Result<crate::component::ComponentTypeRef, Error<Self::Error>> { |
328 | 0 | component_utils::component_type_ref(self, ty) |
329 | 0 | } |
330 | | |
331 | 0 | fn component_primitive_val_type( |
332 | 0 | &mut self, |
333 | 0 | ty: wasmparser::PrimitiveValType, |
334 | 0 | ) -> crate::component::PrimitiveValType { |
335 | 0 | component_utils::component_primitive_val_type(self, ty) |
336 | 0 | } |
337 | | |
338 | 0 | fn component_export_kind( |
339 | 0 | &mut self, |
340 | 0 | ty: wasmparser::ComponentExternalKind, |
341 | 0 | ) -> crate::component::ComponentExportKind { |
342 | 0 | component_utils::component_export_kind(self, ty) |
343 | 0 | } |
344 | | |
345 | 0 | fn component_outer_alias_kind( |
346 | 0 | &mut self, |
347 | 0 | kind: wasmparser::ComponentOuterAliasKind, |
348 | 0 | ) -> crate::component::ComponentOuterAliasKind { |
349 | 0 | component_utils::component_outer_alias_kind(self, kind) |
350 | 0 | } |
351 | | |
352 | 0 | fn component_val_type( |
353 | 0 | &mut self, |
354 | 0 | ty: wasmparser::ComponentValType, |
355 | 0 | ) -> crate::component::ComponentValType { |
356 | 0 | component_utils::component_val_type(self, ty) |
357 | 0 | } |
358 | | |
359 | 0 | fn type_bounds(&mut self, ty: wasmparser::TypeBounds) -> crate::component::TypeBounds { |
360 | 0 | component_utils::type_bounds(self, ty) |
361 | 0 | } |
362 | | |
363 | 0 | fn canonical_option( |
364 | 0 | &mut self, |
365 | 0 | ty: wasmparser::CanonicalOption, |
366 | 0 | ) -> Result<crate::component::CanonicalOption, Error<Self::Error>> { |
367 | 0 | component_utils::canonical_option(self, ty) |
368 | 0 | } |
369 | | |
370 | 0 | fn custom_component_name_section( |
371 | 0 | &mut self, |
372 | 0 | section: wasmparser::ComponentNameSectionReader<'_>, |
373 | 0 | ) -> Result<crate::ComponentNameSection, Error<Self::Error>> { |
374 | 0 | component_utils::custom_component_name_section(self, section) |
375 | 0 | } |
376 | | |
377 | 0 | fn parse_custom_component_name_subsection( |
378 | 0 | &mut self, |
379 | 0 | names: &mut crate::ComponentNameSection, |
380 | 0 | section: wasmparser::ComponentName<'_>, |
381 | 0 | ) -> Result<(), Error<Self::Error>> { |
382 | 0 | component_utils::parse_custom_component_name_subsection(self, names, section) |
383 | 0 | } Unexecuted instantiation: <wasm_encoder::reencode::RoundtripReencoder as wasm_encoder::reencode::component::ReencodeComponent>::parse_custom_component_name_subsection Unexecuted instantiation: <_ as wasm_encoder::reencode::component::ReencodeComponent>::parse_custom_component_name_subsection |
384 | | } |
385 | | |
386 | | impl ReencodeComponent for RoundtripReencoder {} |
387 | | |
388 | | #[allow(missing_docs)] // FIXME |
389 | | pub mod component_utils { |
390 | | use core::ops::Range; |
391 | | |
392 | | use super::super::utils::name_map; |
393 | | use super::ReencodeComponent; |
394 | | use crate::reencode::Error; |
395 | | use alloc::boxed::Box; |
396 | | use alloc::vec::Vec; |
397 | | |
398 | 0 | pub fn parse_component<T: ?Sized + ReencodeComponent>( |
399 | 0 | reencoder: &mut T, |
400 | 0 | component: &mut crate::Component, |
401 | 0 | mut parser: wasmparser::Parser, |
402 | 0 | data: &[u8], |
403 | 0 | whole_component: &[u8], |
404 | 0 | ) -> Result<(), Error<T::Error>> { |
405 | 0 | let mut remaining = data; |
406 | 0 | while !remaining.is_empty() { |
407 | 0 | let section = match parser.parse(remaining, true)? { |
408 | 0 | wasmparser::Chunk::Parsed { consumed, payload } => { |
409 | 0 | remaining = &remaining[consumed..]; |
410 | 0 | payload |
411 | | } |
412 | 0 | wasmparser::Chunk::NeedMoreData(_) => unreachable!(), |
413 | | }; |
414 | 0 | match §ion { |
415 | | wasmparser::Payload::ComponentSection { |
416 | 0 | unchecked_range, .. |
417 | | } |
418 | | | wasmparser::Payload::ModuleSection { |
419 | 0 | unchecked_range, .. |
420 | 0 | } => { |
421 | 0 | let skipped_len = (unchecked_range.end - unchecked_range.start) as usize; |
422 | 0 | remaining = &remaining[skipped_len..]; |
423 | 0 | } |
424 | 0 | _ => {} |
425 | | } |
426 | 0 | reencoder.parse_component_payload(component, section, whole_component)?; |
427 | | } |
428 | | |
429 | 0 | Ok(()) |
430 | 0 | } |
431 | | |
432 | 0 | pub fn parse_component_payload<T: ?Sized + ReencodeComponent>( |
433 | 0 | reencoder: &mut T, |
434 | 0 | component: &mut crate::Component, |
435 | 0 | payload: wasmparser::Payload<'_>, |
436 | 0 | whole_component: &[u8], |
437 | 0 | ) -> Result<(), Error<T::Error>> { |
438 | 0 | let convert_range = |file_range: &Range<u64>| { |
439 | | // By assumption that `whole_component` is at parser offset 0 |
440 | 0 | file_range.start as usize..file_range.end as usize |
441 | 0 | }; |
442 | 0 | match payload { |
443 | | wasmparser::Payload::Version { |
444 | | encoding: wasmparser::Encoding::Component, |
445 | | .. |
446 | 0 | } => (), |
447 | | wasmparser::Payload::Version { .. } => { |
448 | 0 | return Err(Error::UnexpectedNonComponentSection); |
449 | | } |
450 | | wasmparser::Payload::TypeSection(_) |
451 | | | wasmparser::Payload::ImportSection(_) |
452 | | | wasmparser::Payload::FunctionSection(_) |
453 | | | wasmparser::Payload::TableSection(_) |
454 | | | wasmparser::Payload::MemorySection(_) |
455 | | | wasmparser::Payload::TagSection(_) |
456 | | | wasmparser::Payload::GlobalSection(_) |
457 | | | wasmparser::Payload::ExportSection(_) |
458 | | | wasmparser::Payload::StartSection { .. } |
459 | | | wasmparser::Payload::ElementSection(_) |
460 | | | wasmparser::Payload::DataCountSection { .. } |
461 | | | wasmparser::Payload::DataSection(_) |
462 | | | wasmparser::Payload::CodeSectionStart { .. } |
463 | | | wasmparser::Payload::CodeSectionEntry(_) => { |
464 | 0 | return Err(Error::UnexpectedNonComponentSection); |
465 | | } |
466 | 0 | wasmparser::Payload::ComponentTypeSection(section) => { |
467 | 0 | let mut types = crate::ComponentTypeSection::new(); |
468 | 0 | reencoder.parse_component_type_section(&mut types, section)?; |
469 | 0 | component.section(&types); |
470 | | } |
471 | 0 | wasmparser::Payload::ComponentImportSection(section) => { |
472 | 0 | let mut imports = crate::ComponentImportSection::new(); |
473 | 0 | reencoder.parse_component_import_section(&mut imports, section)?; |
474 | 0 | component.section(&imports); |
475 | | } |
476 | 0 | wasmparser::Payload::ComponentCanonicalSection(section) => { |
477 | 0 | let mut canonical = crate::CanonicalFunctionSection::new(); |
478 | 0 | reencoder.parse_component_canonical_section(&mut canonical, section)?; |
479 | 0 | component.section(&canonical); |
480 | | } |
481 | 0 | wasmparser::Payload::ComponentAliasSection(section) => { |
482 | 0 | let mut aliases = crate::ComponentAliasSection::new(); |
483 | 0 | reencoder.parse_component_alias_section(&mut aliases, section)?; |
484 | 0 | component.section(&aliases); |
485 | | } |
486 | 0 | wasmparser::Payload::ComponentInstanceSection(section) => { |
487 | 0 | let mut instances = crate::ComponentInstanceSection::new(); |
488 | 0 | reencoder.parse_component_instance_section(&mut instances, section)?; |
489 | 0 | component.section(&instances); |
490 | | } |
491 | 0 | wasmparser::Payload::InstanceSection(section) => { |
492 | 0 | let mut instances = crate::InstanceSection::new(); |
493 | 0 | reencoder.parse_instance_section(&mut instances, section)?; |
494 | 0 | component.section(&instances); |
495 | | } |
496 | 0 | wasmparser::Payload::CoreTypeSection(section) => { |
497 | 0 | let mut types = crate::CoreTypeSection::new(); |
498 | 0 | reencoder.parse_core_type_section(&mut types, section)?; |
499 | 0 | component.section(&types); |
500 | | } |
501 | 0 | wasmparser::Payload::ComponentExportSection(section) => { |
502 | 0 | let mut exports = crate::ComponentExportSection::new(); |
503 | 0 | reencoder.parse_component_export_section(&mut exports, section)?; |
504 | 0 | component.section(&exports); |
505 | | } |
506 | 0 | wasmparser::Payload::CustomSection(section) => { |
507 | 0 | reencoder.parse_component_custom_section(component, section)?; |
508 | | } |
509 | | wasmparser::Payload::ModuleSection { |
510 | 0 | parser, |
511 | 0 | unchecked_range, |
512 | | } => { |
513 | 0 | reencoder.parse_component_submodule( |
514 | 0 | component, |
515 | 0 | parser, |
516 | 0 | &whole_component[convert_range(&unchecked_range)], |
517 | 0 | )?; |
518 | | } |
519 | | wasmparser::Payload::ComponentSection { |
520 | 0 | parser, |
521 | 0 | unchecked_range, |
522 | | } => { |
523 | 0 | reencoder.parse_component_subcomponent( |
524 | 0 | component, |
525 | 0 | parser, |
526 | 0 | &whole_component[convert_range(&unchecked_range)], |
527 | 0 | whole_component, |
528 | 0 | )?; |
529 | | } |
530 | 0 | wasmparser::Payload::ComponentStartSection { start, range: _ } => { |
531 | 0 | reencoder.parse_component_start_section(component, start)?; |
532 | | } |
533 | 0 | wasmparser::Payload::End(_) => {} |
534 | | |
535 | 0 | other => match other.as_section() { |
536 | 0 | Some((id, range)) => { |
537 | 0 | let section = &whole_component[convert_range(&range)]; |
538 | 0 | reencoder.parse_unknown_component_section(component, id, section)?; |
539 | | } |
540 | 0 | None => unreachable!(), |
541 | | }, |
542 | | } |
543 | 0 | Ok(()) |
544 | 0 | } |
545 | | |
546 | 0 | pub fn parse_component_submodule<T: ?Sized + ReencodeComponent>( |
547 | 0 | reencoder: &mut T, |
548 | 0 | component: &mut crate::Component, |
549 | 0 | parser: wasmparser::Parser, |
550 | 0 | submodule: &[u8], |
551 | 0 | ) -> Result<(), Error<T::Error>> { |
552 | 0 | reencoder.push_depth(); |
553 | 0 | let mut module = crate::Module::new(); |
554 | 0 | crate::reencode::utils::parse_core_module(reencoder, &mut module, parser, submodule)?; |
555 | 0 | component.section(&crate::ModuleSection(&module)); |
556 | 0 | reencoder.pop_depth(); |
557 | 0 | Ok(()) |
558 | 0 | } |
559 | | |
560 | 0 | pub fn parse_component_subcomponent<T: ?Sized + ReencodeComponent>( |
561 | 0 | reencoder: &mut T, |
562 | 0 | component: &mut crate::Component, |
563 | 0 | parser: wasmparser::Parser, |
564 | 0 | data: &[u8], |
565 | 0 | whole_component: &[u8], |
566 | 0 | ) -> Result<(), Error<T::Error>> { |
567 | 0 | reencoder.push_depth(); |
568 | 0 | let mut subcomponent = crate::Component::new(); |
569 | 0 | parse_component(reencoder, &mut subcomponent, parser, data, whole_component)?; |
570 | 0 | component.section(&crate::NestedComponentSection(&subcomponent)); |
571 | 0 | reencoder.pop_depth(); |
572 | 0 | Ok(()) |
573 | 0 | } |
574 | | |
575 | 0 | pub fn parse_unknown_component_section<T: ?Sized + ReencodeComponent>( |
576 | 0 | _reencoder: &mut T, |
577 | 0 | component: &mut crate::Component, |
578 | 0 | id: u8, |
579 | 0 | contents: &[u8], |
580 | 0 | ) -> Result<(), Error<T::Error>> { |
581 | 0 | component.section(&crate::RawSection { id, data: contents }); |
582 | 0 | Ok(()) |
583 | 0 | } |
584 | | |
585 | 0 | pub fn parse_component_custom_section<T: ?Sized + ReencodeComponent>( |
586 | 0 | reencoder: &mut T, |
587 | 0 | component: &mut crate::Component, |
588 | 0 | section: wasmparser::CustomSectionReader<'_>, |
589 | 0 | ) -> Result<(), Error<T::Error>> { |
590 | 0 | match section.as_known() { |
591 | 0 | wasmparser::KnownCustom::ComponentName(name) => { |
592 | 0 | component.section(&reencoder.custom_component_name_section(name)?); |
593 | | } |
594 | | _ => { |
595 | 0 | component.section(&reencoder.custom_section(section)?); |
596 | | } |
597 | | } |
598 | 0 | Ok(()) |
599 | 0 | } |
600 | | |
601 | 0 | pub fn parse_component_type_section<T: ?Sized + ReencodeComponent>( |
602 | 0 | reencoder: &mut T, |
603 | 0 | types: &mut crate::ComponentTypeSection, |
604 | 0 | section: wasmparser::ComponentTypeSectionReader<'_>, |
605 | 0 | ) -> Result<(), Error<T::Error>> { |
606 | 0 | for ty in section { |
607 | 0 | reencoder.parse_component_type(types.ty(), ty?)?; |
608 | | } |
609 | 0 | Ok(()) |
610 | 0 | } |
611 | | |
612 | 0 | pub fn parse_component_type<T: ?Sized + ReencodeComponent>( |
613 | 0 | reencoder: &mut T, |
614 | 0 | dst: crate::ComponentTypeEncoder, |
615 | 0 | ty: wasmparser::ComponentType<'_>, |
616 | 0 | ) -> Result<(), Error<T::Error>> { |
617 | 0 | match ty { |
618 | 0 | wasmparser::ComponentType::Defined(ty) => { |
619 | 0 | reencoder.parse_component_defined_type(dst.defined_type(), ty)?; |
620 | | } |
621 | 0 | wasmparser::ComponentType::Func(func) => { |
622 | 0 | reencoder.parse_component_func_type(dst.function(), func)?; |
623 | | } |
624 | 0 | wasmparser::ComponentType::Component(component) => { |
625 | 0 | let ty = reencoder.component_type(component)?; |
626 | 0 | dst.component(&ty); |
627 | | } |
628 | 0 | wasmparser::ComponentType::Instance(instance) => { |
629 | 0 | let ty = reencoder.component_instance_type(instance)?; |
630 | 0 | dst.instance(&ty); |
631 | | } |
632 | 0 | wasmparser::ComponentType::Resource { rep, dtor } => { |
633 | 0 | let rep = reencoder.val_type(rep)?; |
634 | 0 | let dtor = dtor.map(|i| reencoder.function_index(i)).transpose()?; |
635 | 0 | dst.resource(rep, dtor); |
636 | | } |
637 | | } |
638 | 0 | Ok(()) |
639 | 0 | } |
640 | | |
641 | 0 | pub fn component_instance_type<T: ?Sized + ReencodeComponent>( |
642 | 0 | reencoder: &mut T, |
643 | 0 | ty: Box<[wasmparser::InstanceTypeDeclaration<'_>]>, |
644 | 0 | ) -> Result<crate::InstanceType, Error<T::Error>> { |
645 | 0 | reencoder.push_depth(); |
646 | 0 | let mut ret = crate::InstanceType::new(); |
647 | 0 | for decl in Vec::from(ty) { |
648 | 0 | reencoder.parse_component_instance_type_declaration(&mut ret, decl)?; |
649 | | } |
650 | 0 | reencoder.pop_depth(); |
651 | 0 | Ok(ret) |
652 | 0 | } |
653 | | |
654 | 0 | pub fn parse_component_instance_type_declaration<T: ?Sized + ReencodeComponent>( |
655 | 0 | reencoder: &mut T, |
656 | 0 | instance: &mut crate::InstanceType, |
657 | 0 | decl: wasmparser::InstanceTypeDeclaration<'_>, |
658 | 0 | ) -> Result<(), Error<T::Error>> { |
659 | 0 | match decl { |
660 | 0 | wasmparser::InstanceTypeDeclaration::CoreType(core) => { |
661 | 0 | reencoder.parse_component_core_type(instance.core_type(), core) |
662 | | } |
663 | 0 | wasmparser::InstanceTypeDeclaration::Type(t) => { |
664 | 0 | reencoder.parse_component_type(instance.ty(), t) |
665 | | } |
666 | 0 | wasmparser::InstanceTypeDeclaration::Alias(a) => { |
667 | 0 | let a = reencoder.component_alias(a)?; |
668 | 0 | instance.alias(a); |
669 | 0 | Ok(()) |
670 | | } |
671 | 0 | wasmparser::InstanceTypeDeclaration::Export { name, ty } => { |
672 | 0 | let ty = reencoder.component_type_ref(ty)?; |
673 | 0 | instance.export(name, ty); |
674 | 0 | Ok(()) |
675 | | } |
676 | | } |
677 | 0 | } |
678 | | |
679 | 0 | pub fn parse_component_core_type<T: ?Sized + ReencodeComponent>( |
680 | 0 | reencoder: &mut T, |
681 | 0 | ty: crate::ComponentCoreTypeEncoder<'_>, |
682 | 0 | decl: wasmparser::CoreType<'_>, |
683 | 0 | ) -> Result<(), Error<T::Error>> { |
684 | 0 | match decl { |
685 | 0 | wasmparser::CoreType::Rec(rec) => { |
686 | 0 | reencoder.parse_recursive_type_group(ty.core(), rec)?; |
687 | | } |
688 | 0 | wasmparser::CoreType::Module(decls) => { |
689 | 0 | ty.module(&reencoder.component_module_type(decls)?); |
690 | | } |
691 | | } |
692 | 0 | Ok(()) |
693 | 0 | } |
694 | | |
695 | 0 | pub fn component_type<T: ?Sized + ReencodeComponent>( |
696 | 0 | reencoder: &mut T, |
697 | 0 | ty: Box<[wasmparser::ComponentTypeDeclaration<'_>]>, |
698 | 0 | ) -> Result<crate::ComponentType, Error<T::Error>> { |
699 | 0 | reencoder.push_depth(); |
700 | 0 | let mut ret = crate::ComponentType::new(); |
701 | 0 | for decl in Vec::from(ty) { |
702 | 0 | reencoder.parse_component_type_declaration(&mut ret, decl)?; |
703 | | } |
704 | 0 | reencoder.pop_depth(); |
705 | 0 | Ok(ret) |
706 | 0 | } |
707 | | |
708 | 0 | pub fn parse_component_type_declaration<T: ?Sized + ReencodeComponent>( |
709 | 0 | reencoder: &mut T, |
710 | 0 | component: &mut crate::ComponentType, |
711 | 0 | decl: wasmparser::ComponentTypeDeclaration<'_>, |
712 | 0 | ) -> Result<(), Error<T::Error>> { |
713 | 0 | match decl { |
714 | 0 | wasmparser::ComponentTypeDeclaration::CoreType(ty) => { |
715 | 0 | reencoder.parse_component_core_type(component.core_type(), ty) |
716 | | } |
717 | 0 | wasmparser::ComponentTypeDeclaration::Type(ty) => { |
718 | 0 | reencoder.parse_component_type(component.ty(), ty) |
719 | | } |
720 | 0 | wasmparser::ComponentTypeDeclaration::Alias(a) => { |
721 | 0 | let a = reencoder.component_alias(a)?; |
722 | 0 | component.alias(a); |
723 | 0 | Ok(()) |
724 | | } |
725 | 0 | wasmparser::ComponentTypeDeclaration::Export { name, ty } => { |
726 | 0 | let ty = reencoder.component_type_ref(ty)?; |
727 | 0 | component.export(name, ty); |
728 | 0 | Ok(()) |
729 | | } |
730 | 0 | wasmparser::ComponentTypeDeclaration::Import(import) => { |
731 | 0 | let ty = reencoder.component_type_ref(import.ty)?; |
732 | 0 | component.import(import.name, ty); |
733 | 0 | Ok(()) |
734 | | } |
735 | | } |
736 | 0 | } |
737 | | |
738 | 0 | pub fn parse_component_func_type<T: ?Sized + ReencodeComponent>( |
739 | 0 | reencoder: &mut T, |
740 | 0 | mut func: crate::ComponentFuncTypeEncoder<'_>, |
741 | 0 | ty: wasmparser::ComponentFuncType<'_>, |
742 | 0 | ) -> Result<(), Error<T::Error>> { |
743 | 0 | func.async_(ty.async_); |
744 | 0 | func.params( |
745 | 0 | Vec::from(ty.params) |
746 | 0 | .into_iter() |
747 | 0 | .map(|(name, ty)| (name, reencoder.component_val_type(ty))), |
748 | | ); |
749 | 0 | let result = ty.result.map(|ty| reencoder.component_val_type(ty)); |
750 | 0 | func.result(result); |
751 | 0 | Ok(()) |
752 | 0 | } |
753 | | |
754 | 0 | pub fn parse_component_defined_type<T: ?Sized + ReencodeComponent>( |
755 | 0 | reencoder: &mut T, |
756 | 0 | defined: crate::ComponentDefinedTypeEncoder<'_>, |
757 | 0 | ty: wasmparser::ComponentDefinedType<'_>, |
758 | 0 | ) -> Result<(), Error<T::Error>> { |
759 | 0 | match ty { |
760 | 0 | wasmparser::ComponentDefinedType::Primitive(p) => { |
761 | 0 | defined.primitive(reencoder.component_primitive_val_type(p)); |
762 | 0 | } |
763 | 0 | wasmparser::ComponentDefinedType::Record(r) => { |
764 | 0 | defined.record( |
765 | 0 | r.iter() |
766 | 0 | .map(|(name, ty)| (*name, reencoder.component_val_type(*ty))), |
767 | | ); |
768 | | } |
769 | 0 | wasmparser::ComponentDefinedType::Variant(v) => { |
770 | 0 | defined.variant( |
771 | 0 | v.iter() |
772 | 0 | .map(|case| (case.name, case.ty.map(|t| reencoder.component_val_type(t)))), |
773 | | ); |
774 | | } |
775 | 0 | wasmparser::ComponentDefinedType::List(t) => { |
776 | 0 | defined.list(reencoder.component_val_type(t)); |
777 | 0 | } |
778 | 0 | wasmparser::ComponentDefinedType::Map(k, v) => { |
779 | 0 | defined.map( |
780 | 0 | reencoder.component_val_type(k), |
781 | 0 | reencoder.component_val_type(v), |
782 | 0 | ); |
783 | 0 | } |
784 | 0 | wasmparser::ComponentDefinedType::FixedLengthList(t, elements) => { |
785 | 0 | defined.fixed_length_list(reencoder.component_val_type(t), elements); |
786 | 0 | } |
787 | 0 | wasmparser::ComponentDefinedType::Tuple(t) => { |
788 | 0 | defined.tuple(t.iter().map(|t| reencoder.component_val_type(*t))); |
789 | | } |
790 | 0 | wasmparser::ComponentDefinedType::Flags(t) => { |
791 | 0 | defined.flags(t.iter().copied()); |
792 | 0 | } |
793 | 0 | wasmparser::ComponentDefinedType::Enum(t) => { |
794 | 0 | defined.enum_type(t.iter().copied()); |
795 | 0 | } |
796 | 0 | wasmparser::ComponentDefinedType::Option(t) => { |
797 | 0 | defined.option(reencoder.component_val_type(t)); |
798 | 0 | } |
799 | 0 | wasmparser::ComponentDefinedType::Result { ok, err } => { |
800 | 0 | let ok = ok.map(|t| reencoder.component_val_type(t)); |
801 | 0 | let err = err.map(|t| reencoder.component_val_type(t)); |
802 | 0 | defined.result(ok, err); |
803 | | } |
804 | 0 | wasmparser::ComponentDefinedType::Own(i) => { |
805 | 0 | defined.own(reencoder.component_type_index(i)); |
806 | 0 | } |
807 | 0 | wasmparser::ComponentDefinedType::Borrow(i) => { |
808 | 0 | defined.borrow(reencoder.component_type_index(i)); |
809 | 0 | } |
810 | 0 | wasmparser::ComponentDefinedType::Future(t) => { |
811 | 0 | defined.future(t.map(|t| reencoder.component_val_type(t))); |
812 | | } |
813 | 0 | wasmparser::ComponentDefinedType::Stream(t) => { |
814 | 0 | defined.stream(t.map(|t| reencoder.component_val_type(t))); |
815 | | } |
816 | | } |
817 | 0 | Ok(()) |
818 | 0 | } |
819 | | |
820 | 0 | pub fn component_module_type<T: ?Sized + ReencodeComponent>( |
821 | 0 | reencoder: &mut T, |
822 | 0 | ty: Box<[wasmparser::ModuleTypeDeclaration<'_>]>, |
823 | 0 | ) -> Result<crate::ModuleType, Error<T::Error>> { |
824 | 0 | reencoder.push_depth(); |
825 | 0 | let mut ret = crate::ModuleType::new(); |
826 | 0 | for decl in Vec::from(ty) { |
827 | 0 | reencoder.parse_component_module_type_declaration(&mut ret, decl)?; |
828 | | } |
829 | 0 | reencoder.pop_depth(); |
830 | 0 | Ok(ret) |
831 | 0 | } |
832 | | |
833 | 0 | pub fn parse_component_module_type_declaration<T: ?Sized + ReencodeComponent>( |
834 | 0 | reencoder: &mut T, |
835 | 0 | module: &mut crate::ModuleType, |
836 | 0 | decl: wasmparser::ModuleTypeDeclaration<'_>, |
837 | 0 | ) -> Result<(), Error<T::Error>> { |
838 | 0 | match decl { |
839 | 0 | wasmparser::ModuleTypeDeclaration::Type(rec) => { |
840 | 0 | reencoder.parse_recursive_type_group(module.ty(), rec)?; |
841 | | } |
842 | 0 | wasmparser::ModuleTypeDeclaration::Export { name, ty } => { |
843 | 0 | module.export(name, reencoder.entity_type(ty)?); |
844 | | } |
845 | | wasmparser::ModuleTypeDeclaration::OuterAlias { |
846 | | kind: wasmparser::OuterAliasKind::Type, |
847 | 0 | count, |
848 | 0 | index, |
849 | | } => { |
850 | 0 | let index = reencoder.outer_type_index(count, index)?; |
851 | 0 | module.alias_outer_core_type(count, index); |
852 | | } |
853 | 0 | wasmparser::ModuleTypeDeclaration::Import(import) => { |
854 | 0 | module.import( |
855 | 0 | import.module, |
856 | 0 | import.name, |
857 | 0 | reencoder.entity_type(import.ty)?, |
858 | | ); |
859 | | } |
860 | | } |
861 | 0 | Ok(()) |
862 | 0 | } |
863 | | |
864 | 0 | pub fn component_alias<'a, T: ?Sized + ReencodeComponent>( |
865 | 0 | reencoder: &mut T, |
866 | 0 | alias: wasmparser::ComponentAlias<'a>, |
867 | 0 | ) -> Result<crate::Alias<'a>, Error<T::Error>> { |
868 | 0 | match alias { |
869 | | wasmparser::ComponentAlias::InstanceExport { |
870 | 0 | kind, |
871 | 0 | instance_index, |
872 | 0 | name, |
873 | 0 | } => Ok(crate::Alias::InstanceExport { |
874 | 0 | instance: reencoder.component_instance_index(instance_index), |
875 | 0 | kind: kind.into(), |
876 | 0 | name, |
877 | 0 | }), |
878 | | wasmparser::ComponentAlias::CoreInstanceExport { |
879 | 0 | kind, |
880 | 0 | instance_index, |
881 | 0 | name, |
882 | 0 | } => Ok(crate::Alias::CoreInstanceExport { |
883 | 0 | instance: reencoder.instance_index(instance_index), |
884 | 0 | kind: kind.into(), |
885 | 0 | name, |
886 | 0 | }), |
887 | 0 | wasmparser::ComponentAlias::Outer { kind, count, index } => Ok(crate::Alias::Outer { |
888 | 0 | kind: kind.into(), |
889 | 0 | count, |
890 | 0 | index: match kind { |
891 | | wasmparser::ComponentOuterAliasKind::CoreModule => { |
892 | 0 | reencoder.outer_module_index(count, index) |
893 | | } |
894 | | wasmparser::ComponentOuterAliasKind::CoreType => { |
895 | 0 | reencoder.outer_type_index(count, index)? |
896 | | } |
897 | | wasmparser::ComponentOuterAliasKind::Type => { |
898 | 0 | reencoder.outer_component_type_index(count, index) |
899 | | } |
900 | | wasmparser::ComponentOuterAliasKind::Component => { |
901 | 0 | reencoder.outer_component_index(count, index) |
902 | | } |
903 | | }, |
904 | | }), |
905 | | } |
906 | 0 | } |
907 | | |
908 | 0 | pub fn parse_component_import_section<T: ?Sized + ReencodeComponent>( |
909 | 0 | reencoder: &mut T, |
910 | 0 | imports: &mut crate::ComponentImportSection, |
911 | 0 | section: wasmparser::ComponentImportSectionReader<'_>, |
912 | 0 | ) -> Result<(), Error<T::Error>> { |
913 | 0 | for import in section { |
914 | 0 | let import = import?; |
915 | 0 | imports.import(import.name, reencoder.component_type_ref(import.ty)?); |
916 | | } |
917 | 0 | Ok(()) |
918 | 0 | } |
919 | | |
920 | 0 | pub fn parse_component_canonical_section<T: ?Sized + ReencodeComponent>( |
921 | 0 | reencoder: &mut T, |
922 | 0 | canonical: &mut crate::CanonicalFunctionSection, |
923 | 0 | section: wasmparser::ComponentCanonicalSectionReader<'_>, |
924 | 0 | ) -> Result<(), Error<T::Error>> { |
925 | 0 | for c in section { |
926 | 0 | reencoder.parse_component_canonical(canonical, c?)?; |
927 | | } |
928 | 0 | Ok(()) |
929 | 0 | } |
930 | | |
931 | 0 | pub fn parse_component_canonical<T: ?Sized + ReencodeComponent>( |
932 | 0 | reencoder: &mut T, |
933 | 0 | section: &mut crate::CanonicalFunctionSection, |
934 | 0 | func: wasmparser::CanonicalFunction, |
935 | 0 | ) -> Result<(), Error<T::Error>> { |
936 | 0 | match func { |
937 | | wasmparser::CanonicalFunction::Lift { |
938 | 0 | core_func_index, |
939 | 0 | type_index, |
940 | 0 | options, |
941 | | } => { |
942 | 0 | let func = reencoder.function_index(core_func_index)?; |
943 | 0 | let ty = reencoder.component_type_index(type_index); |
944 | 0 | let options = options |
945 | 0 | .iter() |
946 | 0 | .map(|o| reencoder.canonical_option(*o)) |
947 | 0 | .collect::<Result<Vec<_>, _>>()?; |
948 | 0 | section.lift(func, ty, options); |
949 | | } |
950 | | wasmparser::CanonicalFunction::Lower { |
951 | 0 | func_index, |
952 | 0 | options, |
953 | | } => { |
954 | 0 | let func = reencoder.component_func_index(func_index); |
955 | 0 | let options = options |
956 | 0 | .iter() |
957 | 0 | .map(|o| reencoder.canonical_option(*o)) |
958 | 0 | .collect::<Result<Vec<_>, _>>()?; |
959 | 0 | section.lower(func, options); |
960 | | } |
961 | 0 | wasmparser::CanonicalFunction::ResourceNew { resource } => { |
962 | 0 | let resource = reencoder.component_type_index(resource); |
963 | 0 | section.resource_new(resource); |
964 | 0 | } |
965 | 0 | wasmparser::CanonicalFunction::ResourceDrop { resource } => { |
966 | 0 | let resource = reencoder.component_type_index(resource); |
967 | 0 | section.resource_drop(resource); |
968 | 0 | } |
969 | 0 | wasmparser::CanonicalFunction::ResourceRep { resource } => { |
970 | 0 | let resource = reencoder.component_type_index(resource); |
971 | 0 | section.resource_rep(resource); |
972 | 0 | } |
973 | 0 | wasmparser::CanonicalFunction::ThreadSpawnRef { func_ty_index } => { |
974 | 0 | let func_ty = reencoder.type_index(func_ty_index)?; |
975 | 0 | section.thread_spawn_ref(func_ty); |
976 | | } |
977 | | wasmparser::CanonicalFunction::ThreadSpawnIndirect { |
978 | 0 | func_ty_index, |
979 | 0 | table_index, |
980 | | } => { |
981 | 0 | let func_ty = reencoder.type_index(func_ty_index)?; |
982 | 0 | let table_index = reencoder.table_index(table_index)?; |
983 | 0 | section.thread_spawn_indirect(func_ty, table_index); |
984 | | } |
985 | 0 | wasmparser::CanonicalFunction::ThreadAvailableParallelism => { |
986 | 0 | section.thread_available_parallelism(); |
987 | 0 | } |
988 | 0 | wasmparser::CanonicalFunction::BackpressureInc => { |
989 | 0 | section.backpressure_inc(); |
990 | 0 | } |
991 | 0 | wasmparser::CanonicalFunction::BackpressureDec => { |
992 | 0 | section.backpressure_dec(); |
993 | 0 | } |
994 | 0 | wasmparser::CanonicalFunction::TaskReturn { result, options } => { |
995 | 0 | let options = options |
996 | 0 | .iter() |
997 | 0 | .map(|o| reencoder.canonical_option(*o)) |
998 | 0 | .collect::<Result<Vec<_>, _>>()?; |
999 | 0 | section.task_return(result.map(|ty| reencoder.component_val_type(ty)), options); |
1000 | | } |
1001 | 0 | wasmparser::CanonicalFunction::TaskCancel => { |
1002 | 0 | section.task_cancel(); |
1003 | 0 | } |
1004 | 0 | wasmparser::CanonicalFunction::ContextGet { ty, slot } => { |
1005 | 0 | section.context_get(reencoder.val_type(ty)?, slot); |
1006 | | } |
1007 | 0 | wasmparser::CanonicalFunction::ContextSet { ty, slot } => { |
1008 | 0 | section.context_set(reencoder.val_type(ty)?, slot); |
1009 | | } |
1010 | 0 | wasmparser::CanonicalFunction::SubtaskDrop => { |
1011 | 0 | section.subtask_drop(); |
1012 | 0 | } |
1013 | 0 | wasmparser::CanonicalFunction::SubtaskCancel { async_ } => { |
1014 | 0 | section.subtask_cancel(async_); |
1015 | 0 | } |
1016 | 0 | wasmparser::CanonicalFunction::StreamNew { ty } => { |
1017 | 0 | section.stream_new(reencoder.component_type_index(ty)); |
1018 | 0 | } |
1019 | 0 | wasmparser::CanonicalFunction::StreamRead { ty, options } => { |
1020 | 0 | let options = options |
1021 | 0 | .iter() |
1022 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1023 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1024 | 0 | section.stream_read(reencoder.component_type_index(ty), options); |
1025 | | } |
1026 | 0 | wasmparser::CanonicalFunction::StreamWrite { ty, options } => { |
1027 | 0 | let options = options |
1028 | 0 | .iter() |
1029 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1030 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1031 | 0 | section.stream_write(reencoder.component_type_index(ty), options); |
1032 | | } |
1033 | 0 | wasmparser::CanonicalFunction::StreamForward { ty } => { |
1034 | 0 | section.stream_forward(reencoder.component_type_index(ty)); |
1035 | 0 | } |
1036 | 0 | wasmparser::CanonicalFunction::StreamCancelRead { ty, async_ } => { |
1037 | 0 | section.stream_cancel_read(reencoder.component_type_index(ty), async_); |
1038 | 0 | } |
1039 | 0 | wasmparser::CanonicalFunction::StreamCancelWrite { ty, async_ } => { |
1040 | 0 | section.stream_cancel_write(reencoder.component_type_index(ty), async_); |
1041 | 0 | } |
1042 | 0 | wasmparser::CanonicalFunction::StreamDropReadable { ty } => { |
1043 | 0 | section.stream_drop_readable(reencoder.component_type_index(ty)); |
1044 | 0 | } |
1045 | 0 | wasmparser::CanonicalFunction::StreamDropWritable { ty } => { |
1046 | 0 | section.stream_drop_writable(reencoder.component_type_index(ty)); |
1047 | 0 | } |
1048 | 0 | wasmparser::CanonicalFunction::FutureNew { ty } => { |
1049 | 0 | section.future_new(reencoder.component_type_index(ty)); |
1050 | 0 | } |
1051 | 0 | wasmparser::CanonicalFunction::FutureRead { ty, options } => { |
1052 | 0 | let options = options |
1053 | 0 | .iter() |
1054 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1055 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1056 | 0 | section.future_read(reencoder.component_type_index(ty), options); |
1057 | | } |
1058 | 0 | wasmparser::CanonicalFunction::FutureWrite { ty, options } => { |
1059 | 0 | let options = options |
1060 | 0 | .iter() |
1061 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1062 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1063 | 0 | section.future_write(reencoder.component_type_index(ty), options); |
1064 | | } |
1065 | 0 | wasmparser::CanonicalFunction::FutureForward { ty } => { |
1066 | 0 | section.future_forward(reencoder.component_type_index(ty)); |
1067 | 0 | } |
1068 | 0 | wasmparser::CanonicalFunction::FutureCancelRead { ty, async_ } => { |
1069 | 0 | section.future_cancel_read(reencoder.component_type_index(ty), async_); |
1070 | 0 | } |
1071 | 0 | wasmparser::CanonicalFunction::FutureCancelWrite { ty, async_ } => { |
1072 | 0 | section.future_cancel_write(reencoder.component_type_index(ty), async_); |
1073 | 0 | } |
1074 | 0 | wasmparser::CanonicalFunction::FutureDropReadable { ty } => { |
1075 | 0 | section.future_drop_readable(reencoder.component_type_index(ty)); |
1076 | 0 | } |
1077 | 0 | wasmparser::CanonicalFunction::FutureDropWritable { ty } => { |
1078 | 0 | section.future_drop_writable(reencoder.component_type_index(ty)); |
1079 | 0 | } |
1080 | 0 | wasmparser::CanonicalFunction::ErrorContextNew { options } => { |
1081 | 0 | let options = options |
1082 | 0 | .iter() |
1083 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1084 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1085 | 0 | section.error_context_new(options); |
1086 | | } |
1087 | 0 | wasmparser::CanonicalFunction::ErrorContextDebugMessage { options } => { |
1088 | 0 | let options = options |
1089 | 0 | .iter() |
1090 | 0 | .map(|o| reencoder.canonical_option(*o)) |
1091 | 0 | .collect::<Result<Vec<_>, _>>()?; |
1092 | 0 | section.error_context_debug_message(options); |
1093 | | } |
1094 | 0 | wasmparser::CanonicalFunction::ErrorContextDrop => { |
1095 | 0 | section.error_context_drop(); |
1096 | 0 | } |
1097 | 0 | wasmparser::CanonicalFunction::WaitableSetNew => { |
1098 | 0 | section.waitable_set_new(); |
1099 | 0 | } |
1100 | 0 | wasmparser::CanonicalFunction::WaitableSetWait { memory } => { |
1101 | 0 | section.waitable_set_wait(reencoder.memory_index(memory)?); |
1102 | | } |
1103 | 0 | wasmparser::CanonicalFunction::WaitableSetPoll { memory } => { |
1104 | 0 | section.waitable_set_poll(reencoder.memory_index(memory)?); |
1105 | | } |
1106 | 0 | wasmparser::CanonicalFunction::WaitableSetDrop => { |
1107 | 0 | section.waitable_set_drop(); |
1108 | 0 | } |
1109 | 0 | wasmparser::CanonicalFunction::WaitableJoin => { |
1110 | 0 | section.waitable_join(); |
1111 | 0 | } |
1112 | 0 | wasmparser::CanonicalFunction::ThreadIndex => { |
1113 | 0 | section.thread_index(); |
1114 | 0 | } |
1115 | | wasmparser::CanonicalFunction::ThreadNewIndirect { |
1116 | 0 | func_ty_index, |
1117 | 0 | table_index, |
1118 | | } => { |
1119 | 0 | let func_ty = reencoder.type_index(func_ty_index)?; |
1120 | 0 | let table_index = reencoder.table_index(table_index)?; |
1121 | 0 | section.thread_new_indirect(func_ty, table_index); |
1122 | | } |
1123 | 0 | wasmparser::CanonicalFunction::ThreadResumeLater => { |
1124 | 0 | section.thread_resume_later(); |
1125 | 0 | } |
1126 | 0 | wasmparser::CanonicalFunction::ThreadSuspend => { |
1127 | 0 | section.thread_suspend(); |
1128 | 0 | } |
1129 | 0 | wasmparser::CanonicalFunction::ThreadYield => { |
1130 | 0 | section.thread_yield(); |
1131 | 0 | } |
1132 | 0 | wasmparser::CanonicalFunction::ThreadSuspendThenResume => { |
1133 | 0 | section.thread_suspend_then_resume(); |
1134 | 0 | } |
1135 | 0 | wasmparser::CanonicalFunction::ThreadYieldThenResume => { |
1136 | 0 | section.thread_yield_then_resume(); |
1137 | 0 | } |
1138 | 0 | wasmparser::CanonicalFunction::ThreadSuspendThenPromote => { |
1139 | 0 | section.thread_suspend_then_promote(); |
1140 | 0 | } |
1141 | 0 | wasmparser::CanonicalFunction::ThreadYieldThenPromote => { |
1142 | 0 | section.thread_yield_then_promote(); |
1143 | 0 | } |
1144 | | } |
1145 | 0 | Ok(()) |
1146 | 0 | } |
1147 | | |
1148 | 0 | pub fn parse_component_alias_section<T: ?Sized + ReencodeComponent>( |
1149 | 0 | reencoder: &mut T, |
1150 | 0 | aliases: &mut crate::ComponentAliasSection, |
1151 | 0 | section: wasmparser::ComponentAliasSectionReader<'_>, |
1152 | 0 | ) -> Result<(), Error<T::Error>> { |
1153 | 0 | for a in section { |
1154 | 0 | aliases.alias(reencoder.component_alias(a?)?); |
1155 | | } |
1156 | 0 | Ok(()) |
1157 | 0 | } |
1158 | | |
1159 | 0 | pub fn parse_component_instance_section<T: ?Sized + ReencodeComponent>( |
1160 | 0 | reencoder: &mut T, |
1161 | 0 | instances: &mut crate::ComponentInstanceSection, |
1162 | 0 | section: wasmparser::ComponentInstanceSectionReader<'_>, |
1163 | 0 | ) -> Result<(), Error<T::Error>> { |
1164 | 0 | for i in section { |
1165 | 0 | reencoder.parse_component_instance(instances, i?)?; |
1166 | | } |
1167 | 0 | Ok(()) |
1168 | 0 | } |
1169 | | |
1170 | 0 | pub fn parse_component_instance<T: ?Sized + ReencodeComponent>( |
1171 | 0 | reencoder: &mut T, |
1172 | 0 | instances: &mut crate::ComponentInstanceSection, |
1173 | 0 | instance: wasmparser::ComponentInstance<'_>, |
1174 | 0 | ) -> Result<(), Error<T::Error>> { |
1175 | 0 | match instance { |
1176 | | wasmparser::ComponentInstance::Instantiate { |
1177 | 0 | component_index, |
1178 | 0 | args, |
1179 | | } => { |
1180 | 0 | instances.instantiate( |
1181 | 0 | reencoder.component_index(component_index), |
1182 | 0 | args.iter().map(|arg| { |
1183 | 0 | ( |
1184 | 0 | arg.name, |
1185 | 0 | arg.kind.into(), |
1186 | 0 | reencoder.component_external_index(arg.kind, arg.index), |
1187 | 0 | ) |
1188 | 0 | }), |
1189 | | ); |
1190 | | } |
1191 | 0 | wasmparser::ComponentInstance::FromExports(exports) => { |
1192 | 0 | instances.export_items(exports.iter().map(|export| { |
1193 | 0 | ( |
1194 | 0 | export.name, |
1195 | 0 | export.kind.into(), |
1196 | 0 | reencoder.component_external_index(export.kind, export.index), |
1197 | 0 | ) |
1198 | 0 | })); |
1199 | | } |
1200 | | } |
1201 | 0 | Ok(()) |
1202 | 0 | } |
1203 | | |
1204 | 0 | pub fn parse_instance_section<T: ?Sized + ReencodeComponent>( |
1205 | 0 | reencoder: &mut T, |
1206 | 0 | instances: &mut crate::InstanceSection, |
1207 | 0 | section: wasmparser::InstanceSectionReader<'_>, |
1208 | 0 | ) -> Result<(), Error<T::Error>> { |
1209 | 0 | for i in section { |
1210 | 0 | reencoder.parse_instance(instances, i?)?; |
1211 | | } |
1212 | 0 | Ok(()) |
1213 | 0 | } |
1214 | | |
1215 | 0 | pub fn parse_instance<T: ?Sized + ReencodeComponent>( |
1216 | 0 | reencoder: &mut T, |
1217 | 0 | instances: &mut crate::InstanceSection, |
1218 | 0 | instance: wasmparser::Instance<'_>, |
1219 | 0 | ) -> Result<(), Error<T::Error>> { |
1220 | 0 | match instance { |
1221 | 0 | wasmparser::Instance::Instantiate { module_index, args } => { |
1222 | 0 | instances.instantiate( |
1223 | 0 | reencoder.module_index(module_index), |
1224 | 0 | args.iter().map(|arg| match arg.kind { |
1225 | 0 | wasmparser::InstantiationArgKind::Instance => ( |
1226 | 0 | arg.name, |
1227 | 0 | crate::ModuleArg::Instance(reencoder.instance_index(arg.index)), |
1228 | 0 | ), |
1229 | 0 | }), |
1230 | | ); |
1231 | | } |
1232 | 0 | wasmparser::Instance::FromExports(exports) => { |
1233 | 0 | let exports = exports |
1234 | 0 | .iter() |
1235 | 0 | .map(|export| { |
1236 | | Ok(( |
1237 | 0 | export.name, |
1238 | 0 | reencoder.export_kind(export.kind)?, |
1239 | 0 | reencoder.external_index(export.kind, export.index)?, |
1240 | | )) |
1241 | 0 | }) |
1242 | 0 | .collect::<Result<Vec<_>, Error<T::Error>>>()?; |
1243 | 0 | instances.export_items(exports); |
1244 | | } |
1245 | | } |
1246 | 0 | Ok(()) |
1247 | 0 | } |
1248 | | |
1249 | 0 | pub fn parse_core_type_section<T: ?Sized + ReencodeComponent>( |
1250 | 0 | reencoder: &mut T, |
1251 | 0 | types: &mut crate::CoreTypeSection, |
1252 | 0 | section: wasmparser::CoreTypeSectionReader<'_>, |
1253 | 0 | ) -> Result<(), Error<T::Error>> { |
1254 | 0 | for t in section { |
1255 | 0 | reencoder.parse_component_core_type(types.ty(), t?)?; |
1256 | | } |
1257 | 0 | Ok(()) |
1258 | 0 | } |
1259 | | |
1260 | 0 | pub fn parse_component_export_section<T: ?Sized + ReencodeComponent>( |
1261 | 0 | reencoder: &mut T, |
1262 | 0 | exports: &mut crate::ComponentExportSection, |
1263 | 0 | section: wasmparser::ComponentExportSectionReader<'_>, |
1264 | 0 | ) -> Result<(), Error<T::Error>> { |
1265 | 0 | for e in section { |
1266 | 0 | reencoder.parse_component_export(exports, e?)?; |
1267 | | } |
1268 | 0 | Ok(()) |
1269 | 0 | } |
1270 | | |
1271 | 0 | pub fn parse_component_export<T: ?Sized + ReencodeComponent>( |
1272 | 0 | reencoder: &mut T, |
1273 | 0 | exports: &mut crate::ComponentExportSection, |
1274 | 0 | export: wasmparser::ComponentExport<'_>, |
1275 | 0 | ) -> Result<(), Error<T::Error>> { |
1276 | 0 | exports.export( |
1277 | 0 | export.name, |
1278 | 0 | export.kind.into(), |
1279 | 0 | reencoder.component_external_index(export.kind, export.index), |
1280 | 0 | export |
1281 | 0 | .ty |
1282 | 0 | .map(|t| reencoder.component_type_ref(t)) |
1283 | 0 | .transpose()?, |
1284 | | ); |
1285 | 0 | Ok(()) |
1286 | 0 | } |
1287 | | |
1288 | 0 | pub fn parse_component_start_section<T: ?Sized + ReencodeComponent>( |
1289 | 0 | reencoder: &mut T, |
1290 | 0 | component: &mut crate::Component, |
1291 | 0 | func: wasmparser::ComponentStartFunction, |
1292 | 0 | ) -> Result<(), Error<T::Error>> { |
1293 | 0 | component.section(&crate::ComponentStartSection { |
1294 | 0 | function_index: reencoder.component_func_index(func.func_index), |
1295 | 0 | args: func |
1296 | 0 | .arguments |
1297 | 0 | .iter() |
1298 | 0 | .map(|i| reencoder.component_value_index(*i)) |
1299 | 0 | .collect::<Vec<_>>(), |
1300 | 0 | results: func.results, |
1301 | | }); |
1302 | 0 | Ok(()) |
1303 | 0 | } |
1304 | | |
1305 | 0 | pub fn component_type_ref<T: ?Sized + ReencodeComponent>( |
1306 | 0 | reencoder: &mut T, |
1307 | 0 | ty: wasmparser::ComponentTypeRef, |
1308 | 0 | ) -> Result<crate::component::ComponentTypeRef, Error<T::Error>> { |
1309 | 0 | Ok(match ty { |
1310 | 0 | wasmparser::ComponentTypeRef::Module(u) => { |
1311 | 0 | crate::component::ComponentTypeRef::Module(reencoder.type_index(u)?) |
1312 | | } |
1313 | 0 | wasmparser::ComponentTypeRef::Func(u) => { |
1314 | 0 | crate::component::ComponentTypeRef::Func(reencoder.component_type_index(u)) |
1315 | | } |
1316 | 0 | wasmparser::ComponentTypeRef::Value(valty) => { |
1317 | 0 | crate::component::ComponentTypeRef::Value(reencoder.component_val_type(valty)) |
1318 | | } |
1319 | 0 | wasmparser::ComponentTypeRef::Type(bounds) => { |
1320 | 0 | crate::component::ComponentTypeRef::Type(reencoder.type_bounds(bounds)) |
1321 | | } |
1322 | 0 | wasmparser::ComponentTypeRef::Instance(u) => { |
1323 | 0 | crate::component::ComponentTypeRef::Instance(reencoder.component_type_index(u)) |
1324 | | } |
1325 | 0 | wasmparser::ComponentTypeRef::Component(u) => { |
1326 | 0 | crate::component::ComponentTypeRef::Component(reencoder.component_type_index(u)) |
1327 | | } |
1328 | | }) |
1329 | 0 | } |
1330 | | |
1331 | 0 | pub fn component_primitive_val_type<T: ?Sized + ReencodeComponent>( |
1332 | 0 | _reencoder: &mut T, |
1333 | 0 | ty: wasmparser::PrimitiveValType, |
1334 | 0 | ) -> crate::component::PrimitiveValType { |
1335 | 0 | match ty { |
1336 | 0 | wasmparser::PrimitiveValType::Bool => crate::component::PrimitiveValType::Bool, |
1337 | 0 | wasmparser::PrimitiveValType::S8 => crate::component::PrimitiveValType::S8, |
1338 | 0 | wasmparser::PrimitiveValType::U8 => crate::component::PrimitiveValType::U8, |
1339 | 0 | wasmparser::PrimitiveValType::S16 => crate::component::PrimitiveValType::S16, |
1340 | 0 | wasmparser::PrimitiveValType::U16 => crate::component::PrimitiveValType::U16, |
1341 | 0 | wasmparser::PrimitiveValType::S32 => crate::component::PrimitiveValType::S32, |
1342 | 0 | wasmparser::PrimitiveValType::U32 => crate::component::PrimitiveValType::U32, |
1343 | 0 | wasmparser::PrimitiveValType::S64 => crate::component::PrimitiveValType::S64, |
1344 | 0 | wasmparser::PrimitiveValType::U64 => crate::component::PrimitiveValType::U64, |
1345 | 0 | wasmparser::PrimitiveValType::F32 => crate::component::PrimitiveValType::F32, |
1346 | 0 | wasmparser::PrimitiveValType::F64 => crate::component::PrimitiveValType::F64, |
1347 | 0 | wasmparser::PrimitiveValType::Char => crate::component::PrimitiveValType::Char, |
1348 | 0 | wasmparser::PrimitiveValType::String => crate::component::PrimitiveValType::String, |
1349 | | wasmparser::PrimitiveValType::ErrorContext => { |
1350 | 0 | crate::component::PrimitiveValType::ErrorContext |
1351 | | } |
1352 | | } |
1353 | 0 | } |
1354 | | |
1355 | 0 | pub fn component_export_kind<T: ?Sized + ReencodeComponent>( |
1356 | 0 | _reencoder: &mut T, |
1357 | 0 | ty: wasmparser::ComponentExternalKind, |
1358 | 0 | ) -> crate::component::ComponentExportKind { |
1359 | 0 | match ty { |
1360 | 0 | wasmparser::ComponentExternalKind::Module => crate::ComponentExportKind::Module, |
1361 | 0 | wasmparser::ComponentExternalKind::Func => crate::ComponentExportKind::Func, |
1362 | 0 | wasmparser::ComponentExternalKind::Value => crate::ComponentExportKind::Value, |
1363 | 0 | wasmparser::ComponentExternalKind::Type => crate::ComponentExportKind::Type, |
1364 | 0 | wasmparser::ComponentExternalKind::Instance => crate::ComponentExportKind::Instance, |
1365 | 0 | wasmparser::ComponentExternalKind::Component => crate::ComponentExportKind::Component, |
1366 | | } |
1367 | 0 | } |
1368 | | |
1369 | 0 | pub fn component_outer_alias_kind<T: ?Sized + ReencodeComponent>( |
1370 | 0 | _reencoder: &mut T, |
1371 | 0 | ty: wasmparser::ComponentOuterAliasKind, |
1372 | 0 | ) -> crate::component::ComponentOuterAliasKind { |
1373 | 0 | match ty { |
1374 | | wasmparser::ComponentOuterAliasKind::CoreModule => { |
1375 | 0 | crate::component::ComponentOuterAliasKind::CoreModule |
1376 | | } |
1377 | | wasmparser::ComponentOuterAliasKind::CoreType => { |
1378 | 0 | crate::component::ComponentOuterAliasKind::CoreType |
1379 | | } |
1380 | | wasmparser::ComponentOuterAliasKind::Type => { |
1381 | 0 | crate::component::ComponentOuterAliasKind::Type |
1382 | | } |
1383 | | wasmparser::ComponentOuterAliasKind::Component => { |
1384 | 0 | crate::ComponentOuterAliasKind::Component |
1385 | | } |
1386 | | } |
1387 | 0 | } |
1388 | | |
1389 | 0 | pub fn component_val_type<T: ?Sized + ReencodeComponent>( |
1390 | 0 | reencoder: &mut T, |
1391 | 0 | ty: wasmparser::ComponentValType, |
1392 | 0 | ) -> crate::component::ComponentValType { |
1393 | 0 | match ty { |
1394 | 0 | wasmparser::ComponentValType::Type(u) => { |
1395 | 0 | crate::component::ComponentValType::Type(reencoder.component_type_index(u)) |
1396 | | } |
1397 | 0 | wasmparser::ComponentValType::Primitive(pty) => { |
1398 | 0 | crate::component::ComponentValType::Primitive( |
1399 | 0 | crate::component::PrimitiveValType::from(pty), |
1400 | 0 | ) |
1401 | | } |
1402 | | } |
1403 | 0 | } |
1404 | | |
1405 | 0 | pub fn type_bounds<T: ?Sized + ReencodeComponent>( |
1406 | 0 | reencoder: &mut T, |
1407 | 0 | ty: wasmparser::TypeBounds, |
1408 | 0 | ) -> crate::component::TypeBounds { |
1409 | 0 | match ty { |
1410 | 0 | wasmparser::TypeBounds::Eq(u) => { |
1411 | 0 | crate::component::TypeBounds::Eq(reencoder.component_type_index(u)) |
1412 | | } |
1413 | 0 | wasmparser::TypeBounds::SubResource => crate::component::TypeBounds::SubResource, |
1414 | | } |
1415 | 0 | } |
1416 | | |
1417 | 0 | pub fn canonical_option<T: ?Sized + ReencodeComponent>( |
1418 | 0 | reencoder: &mut T, |
1419 | 0 | ty: wasmparser::CanonicalOption, |
1420 | 0 | ) -> Result<crate::component::CanonicalOption, Error<T::Error>> { |
1421 | 0 | Ok(match ty { |
1422 | 0 | wasmparser::CanonicalOption::UTF8 => crate::component::CanonicalOption::UTF8, |
1423 | 0 | wasmparser::CanonicalOption::UTF16 => crate::component::CanonicalOption::UTF16, |
1424 | | wasmparser::CanonicalOption::CompactUTF16 => { |
1425 | 0 | crate::component::CanonicalOption::CompactUTF16 |
1426 | | } |
1427 | 0 | wasmparser::CanonicalOption::Memory(u) => { |
1428 | 0 | crate::component::CanonicalOption::Memory(reencoder.memory_index(u)?) |
1429 | | } |
1430 | 0 | wasmparser::CanonicalOption::Realloc(u) => { |
1431 | 0 | crate::component::CanonicalOption::Realloc(reencoder.function_index(u)?) |
1432 | | } |
1433 | 0 | wasmparser::CanonicalOption::PostReturn(u) => { |
1434 | 0 | crate::component::CanonicalOption::PostReturn(reencoder.function_index(u)?) |
1435 | | } |
1436 | 0 | wasmparser::CanonicalOption::Async => crate::component::CanonicalOption::Async, |
1437 | 0 | wasmparser::CanonicalOption::Callback(u) => { |
1438 | 0 | crate::component::CanonicalOption::Callback(reencoder.function_index(u)?) |
1439 | | } |
1440 | 0 | wasmparser::CanonicalOption::CoreType(u) => { |
1441 | 0 | crate::component::CanonicalOption::CoreType(reencoder.type_index(u)?) |
1442 | | } |
1443 | 0 | wasmparser::CanonicalOption::Gc => crate::component::CanonicalOption::Gc, |
1444 | | }) |
1445 | 0 | } |
1446 | | |
1447 | 0 | pub fn custom_component_name_section<T: ?Sized + ReencodeComponent>( |
1448 | 0 | reencoder: &mut T, |
1449 | 0 | section: wasmparser::ComponentNameSectionReader<'_>, |
1450 | 0 | ) -> Result<crate::ComponentNameSection, Error<T::Error>> { |
1451 | 0 | let mut ret = crate::ComponentNameSection::new(); |
1452 | 0 | for subsection in section { |
1453 | 0 | reencoder.parse_custom_component_name_subsection(&mut ret, subsection?)?; |
1454 | | } |
1455 | 0 | Ok(ret) |
1456 | 0 | } |
1457 | | |
1458 | 0 | pub fn parse_custom_component_name_subsection<T: ?Sized + ReencodeComponent>( |
1459 | 0 | reencoder: &mut T, |
1460 | 0 | names: &mut crate::ComponentNameSection, |
1461 | 0 | section: wasmparser::ComponentName<'_>, |
1462 | 0 | ) -> Result<(), Error<T::Error>> { |
1463 | 0 | match section { |
1464 | 0 | wasmparser::ComponentName::Component { name, .. } => { |
1465 | 0 | names.component(name); |
1466 | 0 | } |
1467 | 0 | wasmparser::ComponentName::CoreFuncs(map) => { |
1468 | 0 | names.core_funcs(&name_map(map, |i| reencoder.function_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#0}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#0} |
1469 | | } |
1470 | 0 | wasmparser::ComponentName::CoreGlobals(map) => { |
1471 | 0 | names.core_globals(&name_map(map, |i| reencoder.global_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#1}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#1} |
1472 | | } |
1473 | 0 | wasmparser::ComponentName::CoreMemories(map) => { |
1474 | 0 | names.core_memories(&name_map(map, |i| reencoder.memory_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#2}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#2} |
1475 | | } |
1476 | 0 | wasmparser::ComponentName::CoreTables(map) => { |
1477 | 0 | names.core_tables(&name_map(map, |i| reencoder.table_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#3}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#3} |
1478 | | } |
1479 | 0 | wasmparser::ComponentName::CoreTags(map) => { |
1480 | 0 | names.core_tags(&name_map(map, |i| reencoder.tag_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#4}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#4} |
1481 | | } |
1482 | 0 | wasmparser::ComponentName::CoreModules(map) => { |
1483 | 0 | names.core_modules(&name_map(map, |i| Ok(reencoder.module_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#5}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#5} |
1484 | | } |
1485 | 0 | wasmparser::ComponentName::CoreInstances(map) => { |
1486 | 0 | names.core_instances(&name_map(map, |i| Ok(reencoder.instance_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#6}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#6} |
1487 | | } |
1488 | 0 | wasmparser::ComponentName::CoreTypes(map) => { |
1489 | 0 | names.core_types(&name_map(map, |i| reencoder.type_index(i))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#7}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#7} |
1490 | | } |
1491 | 0 | wasmparser::ComponentName::Types(map) => { |
1492 | 0 | names.types(&name_map(map, |i| Ok(reencoder.component_type_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#8}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#8} |
1493 | | } |
1494 | 0 | wasmparser::ComponentName::Instances(map) => { |
1495 | 0 | names.instances(&name_map(map, |i| { |
1496 | 0 | Ok(reencoder.component_instance_index(i)) |
1497 | 0 | })?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#9}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#9} |
1498 | | } |
1499 | 0 | wasmparser::ComponentName::Components(map) => { |
1500 | 0 | names.components(&name_map(map, |i| Ok(reencoder.component_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#10}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#10} |
1501 | | } |
1502 | 0 | wasmparser::ComponentName::Funcs(map) => { |
1503 | 0 | names.funcs(&name_map(map, |i| Ok(reencoder.component_func_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#11}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#11} |
1504 | | } |
1505 | 0 | wasmparser::ComponentName::Values(map) => { |
1506 | 0 | names.values(&name_map(map, |i| Ok(reencoder.component_value_index(i)))?); Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder>::{closure#12}Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_>::{closure#12} |
1507 | | } |
1508 | 0 | wasmparser::ComponentName::Unknown { ty, data, .. } => { |
1509 | 0 | names.raw(ty, data); |
1510 | 0 | } |
1511 | | } |
1512 | 0 | Ok(()) |
1513 | 0 | } Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<wasm_encoder::reencode::RoundtripReencoder> Unexecuted instantiation: wasm_encoder::reencode::component::component_utils::parse_custom_component_name_subsection::<_> |
1514 | | } |
1515 | | |
1516 | | impl From<wasmparser::ComponentValType> for crate::ComponentValType { |
1517 | 0 | fn from(ty: wasmparser::ComponentValType) -> Self { |
1518 | 0 | RoundtripReencoder.component_val_type(ty) |
1519 | 0 | } |
1520 | | } |
1521 | | |
1522 | | impl From<wasmparser::TypeBounds> for crate::TypeBounds { |
1523 | 0 | fn from(ty: wasmparser::TypeBounds) -> Self { |
1524 | 0 | RoundtripReencoder.type_bounds(ty) |
1525 | 0 | } |
1526 | | } |
1527 | | |
1528 | | impl From<wasmparser::CanonicalOption> for crate::CanonicalOption { |
1529 | 0 | fn from(opt: wasmparser::CanonicalOption) -> Self { |
1530 | 0 | Result::<_, Error<Infallible>>::unwrap(RoundtripReencoder.canonical_option(opt)) |
1531 | 0 | } |
1532 | | } |
1533 | | |
1534 | | impl From<wasmparser::ComponentExternalKind> for crate::ComponentExportKind { |
1535 | 0 | fn from(kind: wasmparser::ComponentExternalKind) -> Self { |
1536 | 0 | RoundtripReencoder.component_export_kind(kind) |
1537 | 0 | } |
1538 | | } |
1539 | | |
1540 | | impl From<wasmparser::ComponentOuterAliasKind> for crate::ComponentOuterAliasKind { |
1541 | 0 | fn from(kind: wasmparser::ComponentOuterAliasKind) -> Self { |
1542 | 0 | RoundtripReencoder.component_outer_alias_kind(kind) |
1543 | 0 | } |
1544 | | } |
1545 | | |
1546 | | impl From<wasmparser::ComponentTypeRef> for crate::ComponentTypeRef { |
1547 | 0 | fn from(ty: wasmparser::ComponentTypeRef) -> Self { |
1548 | 0 | Result::<_, Error<Infallible>>::unwrap(RoundtripReencoder.component_type_ref(ty)) |
1549 | 0 | } |
1550 | | } |
1551 | | |
1552 | | impl From<wasmparser::PrimitiveValType> for crate::PrimitiveValType { |
1553 | 0 | fn from(ty: wasmparser::PrimitiveValType) -> Self { |
1554 | 0 | RoundtripReencoder.component_primitive_val_type(ty) |
1555 | 0 | } |
1556 | | } |