wasm_encoder/reencode/
component.rs

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