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::Tuple(t) => {
771                defined.tuple(t.iter().map(|t| reencoder.component_val_type(*t)));
772            }
773            wasmparser::ComponentDefinedType::Flags(t) => {
774                defined.flags(t.iter().copied());
775            }
776            wasmparser::ComponentDefinedType::Enum(t) => {
777                defined.enum_type(t.iter().copied());
778            }
779            wasmparser::ComponentDefinedType::Option(t) => {
780                defined.option(reencoder.component_val_type(t));
781            }
782            wasmparser::ComponentDefinedType::Result { ok, err } => {
783                let ok = ok.map(|t| reencoder.component_val_type(t));
784                let err = err.map(|t| reencoder.component_val_type(t));
785                defined.result(ok, err);
786            }
787            wasmparser::ComponentDefinedType::Own(i) => {
788                defined.own(reencoder.component_type_index(i));
789            }
790            wasmparser::ComponentDefinedType::Borrow(i) => {
791                defined.borrow(reencoder.component_type_index(i));
792            }
793            wasmparser::ComponentDefinedType::Future(t) => {
794                defined.future(t.map(|t| reencoder.component_val_type(t)));
795            }
796            wasmparser::ComponentDefinedType::Stream(t) => {
797                defined.stream(t.map(|t| reencoder.component_val_type(t)));
798            }
799            wasmparser::ComponentDefinedType::ErrorContext => defined.error_context(),
800        }
801        Ok(())
802    }
803
804    pub fn component_module_type<T: ?Sized + ReencodeComponent>(
805        reencoder: &mut T,
806        ty: Box<[wasmparser::ModuleTypeDeclaration<'_>]>,
807    ) -> Result<crate::ModuleType, Error<T::Error>> {
808        reencoder.push_depth();
809        let mut ret = crate::ModuleType::new();
810        for decl in Vec::from(ty) {
811            reencoder.parse_component_module_type_declaration(&mut ret, decl)?;
812        }
813        reencoder.pop_depth();
814        Ok(ret)
815    }
816
817    pub fn parse_component_module_type_declaration<T: ?Sized + ReencodeComponent>(
818        reencoder: &mut T,
819        module: &mut crate::ModuleType,
820        decl: wasmparser::ModuleTypeDeclaration<'_>,
821    ) -> Result<(), Error<T::Error>> {
822        match decl {
823            wasmparser::ModuleTypeDeclaration::Type(rec) => {
824                reencoder.parse_recursive_type_group(module.ty(), rec)?;
825            }
826            wasmparser::ModuleTypeDeclaration::Export { name, ty } => {
827                module.export(name, reencoder.entity_type(ty)?);
828            }
829            wasmparser::ModuleTypeDeclaration::OuterAlias {
830                kind: wasmparser::OuterAliasKind::Type,
831                count,
832                index,
833            } => {
834                let index = reencoder.outer_type_index(count, index);
835                module.alias_outer_core_type(count, index);
836            }
837            wasmparser::ModuleTypeDeclaration::Import(import) => {
838                module.import(
839                    import.module,
840                    import.name,
841                    reencoder.entity_type(import.ty)?,
842                );
843            }
844        }
845        Ok(())
846    }
847
848    pub fn component_alias<'a, T: ?Sized + ReencodeComponent>(
849        reencoder: &mut T,
850        alias: wasmparser::ComponentAlias<'a>,
851    ) -> Result<crate::Alias<'a>, Error<T::Error>> {
852        match alias {
853            wasmparser::ComponentAlias::InstanceExport {
854                kind,
855                instance_index,
856                name,
857            } => Ok(crate::Alias::InstanceExport {
858                instance: reencoder.component_instance_index(instance_index),
859                kind: kind.into(),
860                name,
861            }),
862            wasmparser::ComponentAlias::CoreInstanceExport {
863                kind,
864                instance_index,
865                name,
866            } => Ok(crate::Alias::CoreInstanceExport {
867                instance: reencoder.instance_index(instance_index),
868                kind: kind.into(),
869                name,
870            }),
871            wasmparser::ComponentAlias::Outer { kind, count, index } => Ok(crate::Alias::Outer {
872                kind: kind.into(),
873                count,
874                index: match kind {
875                    wasmparser::ComponentOuterAliasKind::CoreModule => {
876                        reencoder.outer_module_index(count, index)
877                    }
878                    wasmparser::ComponentOuterAliasKind::CoreType => {
879                        reencoder.outer_type_index(count, index)
880                    }
881                    wasmparser::ComponentOuterAliasKind::Type => {
882                        reencoder.outer_component_type_index(count, index)
883                    }
884                    wasmparser::ComponentOuterAliasKind::Component => {
885                        reencoder.outer_component_index(count, index)
886                    }
887                },
888            }),
889        }
890    }
891
892    pub fn parse_component_import_section<T: ?Sized + ReencodeComponent>(
893        reencoder: &mut T,
894        imports: &mut crate::ComponentImportSection,
895        section: wasmparser::ComponentImportSectionReader<'_>,
896    ) -> Result<(), Error<T::Error>> {
897        for import in section {
898            let import = import?;
899            imports.import(import.name.0, reencoder.component_type_ref(import.ty));
900        }
901        Ok(())
902    }
903
904    pub fn parse_component_canonical_section<T: ?Sized + ReencodeComponent>(
905        reencoder: &mut T,
906        canonical: &mut crate::CanonicalFunctionSection,
907        section: wasmparser::ComponentCanonicalSectionReader<'_>,
908    ) -> Result<(), Error<T::Error>> {
909        for c in section {
910            reencoder.parse_component_canonical(canonical, c?)?;
911        }
912        Ok(())
913    }
914
915    pub fn parse_component_canonical<T: ?Sized + ReencodeComponent>(
916        reencoder: &mut T,
917        section: &mut crate::CanonicalFunctionSection,
918        func: wasmparser::CanonicalFunction,
919    ) -> Result<(), Error<T::Error>> {
920        match func {
921            wasmparser::CanonicalFunction::Lift {
922                core_func_index,
923                type_index,
924                options,
925            } => {
926                let func = reencoder.function_index(core_func_index);
927                let ty = reencoder.component_type_index(type_index);
928                section.lift(
929                    func,
930                    ty,
931                    options.iter().map(|o| reencoder.canonical_option(*o)),
932                );
933            }
934            wasmparser::CanonicalFunction::Lower {
935                func_index,
936                options,
937            } => {
938                let func = reencoder.component_func_index(func_index);
939                section.lower(func, options.iter().map(|o| reencoder.canonical_option(*o)));
940            }
941            wasmparser::CanonicalFunction::ResourceNew { resource } => {
942                let resource = reencoder.component_type_index(resource);
943                section.resource_new(resource);
944            }
945            wasmparser::CanonicalFunction::ResourceDrop { resource } => {
946                let resource = reencoder.component_type_index(resource);
947                section.resource_drop(resource);
948            }
949            wasmparser::CanonicalFunction::ResourceRep { resource } => {
950                let resource = reencoder.component_type_index(resource);
951                section.resource_rep(resource);
952            }
953            wasmparser::CanonicalFunction::ThreadSpawn { func_ty_index } => {
954                let func_ty = reencoder.type_index(func_ty_index);
955                section.thread_spawn(func_ty);
956            }
957            wasmparser::CanonicalFunction::ThreadAvailableParallelism => {
958                section.thread_available_parallelism();
959            }
960            wasmparser::CanonicalFunction::TaskBackpressure => {
961                section.task_backpressure();
962            }
963            wasmparser::CanonicalFunction::TaskReturn { result } => {
964                section.task_return(result.map(|ty| reencoder.component_val_type(ty)));
965            }
966            wasmparser::CanonicalFunction::TaskWait { async_, memory } => {
967                section.task_wait(async_, reencoder.memory_index(memory));
968            }
969            wasmparser::CanonicalFunction::TaskPoll { async_, memory } => {
970                section.task_poll(async_, reencoder.memory_index(memory));
971            }
972            wasmparser::CanonicalFunction::TaskYield { async_ } => {
973                section.task_yield(async_);
974            }
975            wasmparser::CanonicalFunction::SubtaskDrop => {
976                section.subtask_drop();
977            }
978            wasmparser::CanonicalFunction::StreamNew { ty } => {
979                section.stream_new(reencoder.component_type_index(ty));
980            }
981            wasmparser::CanonicalFunction::StreamRead { ty, options } => {
982                section.stream_read(
983                    reencoder.component_type_index(ty),
984                    options.iter().map(|o| reencoder.canonical_option(*o)),
985                );
986            }
987            wasmparser::CanonicalFunction::StreamWrite { ty, options } => {
988                section.stream_write(
989                    reencoder.component_type_index(ty),
990                    options.iter().map(|o| reencoder.canonical_option(*o)),
991                );
992            }
993            wasmparser::CanonicalFunction::StreamCancelRead { ty, async_ } => {
994                section.stream_cancel_read(ty, async_);
995            }
996            wasmparser::CanonicalFunction::StreamCancelWrite { ty, async_ } => {
997                section.stream_cancel_write(ty, async_);
998            }
999            wasmparser::CanonicalFunction::StreamCloseReadable { ty } => {
1000                section.stream_close_readable(reencoder.component_type_index(ty));
1001            }
1002            wasmparser::CanonicalFunction::StreamCloseWritable { ty } => {
1003                section.stream_close_writable(reencoder.component_type_index(ty));
1004            }
1005            wasmparser::CanonicalFunction::FutureNew { ty } => {
1006                section.future_new(reencoder.component_type_index(ty));
1007            }
1008            wasmparser::CanonicalFunction::FutureRead { ty, options } => {
1009                section.future_read(
1010                    reencoder.component_type_index(ty),
1011                    options.iter().map(|o| reencoder.canonical_option(*o)),
1012                );
1013            }
1014            wasmparser::CanonicalFunction::FutureWrite { ty, options } => {
1015                section.future_write(
1016                    reencoder.component_type_index(ty),
1017                    options.iter().map(|o| reencoder.canonical_option(*o)),
1018                );
1019            }
1020            wasmparser::CanonicalFunction::FutureCancelRead { ty, async_ } => {
1021                section.future_cancel_read(ty, async_);
1022            }
1023            wasmparser::CanonicalFunction::FutureCancelWrite { ty, async_ } => {
1024                section.future_cancel_write(ty, async_);
1025            }
1026            wasmparser::CanonicalFunction::FutureCloseReadable { ty } => {
1027                section.future_close_readable(reencoder.component_type_index(ty));
1028            }
1029            wasmparser::CanonicalFunction::FutureCloseWritable { ty } => {
1030                section.future_close_writable(reencoder.component_type_index(ty));
1031            }
1032            wasmparser::CanonicalFunction::ErrorContextNew { options } => {
1033                section.error_context_new(options.iter().map(|o| reencoder.canonical_option(*o)));
1034            }
1035            wasmparser::CanonicalFunction::ErrorContextDebugMessage { options } => {
1036                section.error_context_debug_message(
1037                    options.iter().map(|o| reencoder.canonical_option(*o)),
1038                );
1039            }
1040            wasmparser::CanonicalFunction::ErrorContextDrop => {
1041                section.error_context_drop();
1042            }
1043        }
1044        Ok(())
1045    }
1046
1047    pub fn parse_component_alias_section<T: ?Sized + ReencodeComponent>(
1048        reencoder: &mut T,
1049        aliases: &mut crate::ComponentAliasSection,
1050        section: wasmparser::ComponentAliasSectionReader<'_>,
1051    ) -> Result<(), Error<T::Error>> {
1052        for a in section {
1053            aliases.alias(reencoder.component_alias(a?)?);
1054        }
1055        Ok(())
1056    }
1057
1058    pub fn parse_component_instance_section<T: ?Sized + ReencodeComponent>(
1059        reencoder: &mut T,
1060        instances: &mut crate::ComponentInstanceSection,
1061        section: wasmparser::ComponentInstanceSectionReader<'_>,
1062    ) -> Result<(), Error<T::Error>> {
1063        for i in section {
1064            reencoder.parse_component_instance(instances, i?)?;
1065        }
1066        Ok(())
1067    }
1068
1069    pub fn parse_component_instance<T: ?Sized + ReencodeComponent>(
1070        reencoder: &mut T,
1071        instances: &mut crate::ComponentInstanceSection,
1072        instance: wasmparser::ComponentInstance<'_>,
1073    ) -> Result<(), Error<T::Error>> {
1074        match instance {
1075            wasmparser::ComponentInstance::Instantiate {
1076                component_index,
1077                args,
1078            } => {
1079                instances.instantiate(
1080                    reencoder.component_index(component_index),
1081                    args.iter().map(|arg| {
1082                        (
1083                            arg.name,
1084                            arg.kind.into(),
1085                            reencoder.component_external_index(arg.kind, arg.index),
1086                        )
1087                    }),
1088                );
1089            }
1090            wasmparser::ComponentInstance::FromExports(exports) => {
1091                instances.export_items(exports.iter().map(|export| {
1092                    (
1093                        export.name.0,
1094                        export.kind.into(),
1095                        reencoder.component_external_index(export.kind, export.index),
1096                    )
1097                }));
1098            }
1099        }
1100        Ok(())
1101    }
1102
1103    pub fn parse_instance_section<T: ?Sized + ReencodeComponent>(
1104        reencoder: &mut T,
1105        instances: &mut crate::InstanceSection,
1106        section: wasmparser::InstanceSectionReader<'_>,
1107    ) -> Result<(), Error<T::Error>> {
1108        for i in section {
1109            reencoder.parse_instance(instances, i?)?;
1110        }
1111        Ok(())
1112    }
1113
1114    pub fn parse_instance<T: ?Sized + ReencodeComponent>(
1115        reencoder: &mut T,
1116        instances: &mut crate::InstanceSection,
1117        instance: wasmparser::Instance<'_>,
1118    ) -> Result<(), Error<T::Error>> {
1119        match instance {
1120            wasmparser::Instance::Instantiate { module_index, args } => {
1121                instances.instantiate(
1122                    reencoder.module_index(module_index),
1123                    args.iter().map(|arg| match arg.kind {
1124                        wasmparser::InstantiationArgKind::Instance => (
1125                            arg.name,
1126                            crate::ModuleArg::Instance(reencoder.instance_index(arg.index)),
1127                        ),
1128                    }),
1129                );
1130            }
1131            wasmparser::Instance::FromExports(exports) => {
1132                instances.export_items(exports.iter().map(|export| {
1133                    (
1134                        export.name,
1135                        reencoder.export_kind(export.kind),
1136                        reencoder.external_index(export.kind, export.index),
1137                    )
1138                }));
1139            }
1140        }
1141        Ok(())
1142    }
1143
1144    pub fn parse_core_type_section<T: ?Sized + ReencodeComponent>(
1145        reencoder: &mut T,
1146        types: &mut crate::CoreTypeSection,
1147        section: wasmparser::CoreTypeSectionReader<'_>,
1148    ) -> Result<(), Error<T::Error>> {
1149        for t in section {
1150            reencoder.parse_component_core_type(types.ty(), t?)?;
1151        }
1152        Ok(())
1153    }
1154
1155    pub fn parse_component_export_section<T: ?Sized + ReencodeComponent>(
1156        reencoder: &mut T,
1157        exports: &mut crate::ComponentExportSection,
1158        section: wasmparser::ComponentExportSectionReader<'_>,
1159    ) -> Result<(), Error<T::Error>> {
1160        for e in section {
1161            reencoder.parse_component_export(exports, e?)?;
1162        }
1163        Ok(())
1164    }
1165
1166    pub fn parse_component_export<T: ?Sized + ReencodeComponent>(
1167        reencoder: &mut T,
1168        exports: &mut crate::ComponentExportSection,
1169        export: wasmparser::ComponentExport<'_>,
1170    ) -> Result<(), Error<T::Error>> {
1171        exports.export(
1172            export.name.0,
1173            export.kind.into(),
1174            reencoder.component_external_index(export.kind, export.index),
1175            export.ty.map(|t| reencoder.component_type_ref(t)),
1176        );
1177        Ok(())
1178    }
1179
1180    pub fn parse_component_start_section<T: ?Sized + ReencodeComponent>(
1181        reencoder: &mut T,
1182        component: &mut crate::Component,
1183        func: wasmparser::ComponentStartFunction,
1184    ) -> Result<(), Error<T::Error>> {
1185        component.section(&crate::ComponentStartSection {
1186            function_index: reencoder.component_func_index(func.func_index),
1187            args: func
1188                .arguments
1189                .iter()
1190                .map(|i| reencoder.component_value_index(*i))
1191                .collect::<Vec<_>>(),
1192            results: func.results,
1193        });
1194        Ok(())
1195    }
1196
1197    pub fn component_type_ref<T: ?Sized + ReencodeComponent>(
1198        reencoder: &mut T,
1199        ty: wasmparser::ComponentTypeRef,
1200    ) -> crate::component::ComponentTypeRef {
1201        match ty {
1202            wasmparser::ComponentTypeRef::Module(u) => {
1203                crate::component::ComponentTypeRef::Module(reencoder.type_index(u))
1204            }
1205            wasmparser::ComponentTypeRef::Func(u) => {
1206                crate::component::ComponentTypeRef::Func(reencoder.component_type_index(u))
1207            }
1208            wasmparser::ComponentTypeRef::Value(valty) => {
1209                crate::component::ComponentTypeRef::Value(reencoder.component_val_type(valty))
1210            }
1211            wasmparser::ComponentTypeRef::Type(bounds) => {
1212                crate::component::ComponentTypeRef::Type(reencoder.type_bounds(bounds))
1213            }
1214            wasmparser::ComponentTypeRef::Instance(u) => {
1215                crate::component::ComponentTypeRef::Instance(reencoder.component_type_index(u))
1216            }
1217            wasmparser::ComponentTypeRef::Component(u) => {
1218                crate::component::ComponentTypeRef::Component(reencoder.component_type_index(u))
1219            }
1220        }
1221    }
1222
1223    pub fn component_primitive_val_type<T: ?Sized + ReencodeComponent>(
1224        _reencoder: &mut T,
1225        ty: wasmparser::PrimitiveValType,
1226    ) -> crate::component::PrimitiveValType {
1227        match ty {
1228            wasmparser::PrimitiveValType::Bool => crate::component::PrimitiveValType::Bool,
1229            wasmparser::PrimitiveValType::S8 => crate::component::PrimitiveValType::S8,
1230            wasmparser::PrimitiveValType::U8 => crate::component::PrimitiveValType::U8,
1231            wasmparser::PrimitiveValType::S16 => crate::component::PrimitiveValType::S16,
1232            wasmparser::PrimitiveValType::U16 => crate::component::PrimitiveValType::U16,
1233            wasmparser::PrimitiveValType::S32 => crate::component::PrimitiveValType::S32,
1234            wasmparser::PrimitiveValType::U32 => crate::component::PrimitiveValType::U32,
1235            wasmparser::PrimitiveValType::S64 => crate::component::PrimitiveValType::S64,
1236            wasmparser::PrimitiveValType::U64 => crate::component::PrimitiveValType::U64,
1237            wasmparser::PrimitiveValType::F32 => crate::component::PrimitiveValType::F32,
1238            wasmparser::PrimitiveValType::F64 => crate::component::PrimitiveValType::F64,
1239            wasmparser::PrimitiveValType::Char => crate::component::PrimitiveValType::Char,
1240            wasmparser::PrimitiveValType::String => crate::component::PrimitiveValType::String,
1241        }
1242    }
1243
1244    pub fn component_export_kind<T: ?Sized + ReencodeComponent>(
1245        _reencoder: &mut T,
1246        ty: wasmparser::ComponentExternalKind,
1247    ) -> crate::component::ComponentExportKind {
1248        match ty {
1249            wasmparser::ComponentExternalKind::Module => crate::ComponentExportKind::Module,
1250            wasmparser::ComponentExternalKind::Func => crate::ComponentExportKind::Func,
1251            wasmparser::ComponentExternalKind::Value => crate::ComponentExportKind::Value,
1252            wasmparser::ComponentExternalKind::Type => crate::ComponentExportKind::Type,
1253            wasmparser::ComponentExternalKind::Instance => crate::ComponentExportKind::Instance,
1254            wasmparser::ComponentExternalKind::Component => crate::ComponentExportKind::Component,
1255        }
1256    }
1257
1258    pub fn component_outer_alias_kind<T: ?Sized + ReencodeComponent>(
1259        _reencoder: &mut T,
1260        ty: wasmparser::ComponentOuterAliasKind,
1261    ) -> crate::component::ComponentOuterAliasKind {
1262        match ty {
1263            wasmparser::ComponentOuterAliasKind::CoreModule => {
1264                crate::component::ComponentOuterAliasKind::CoreModule
1265            }
1266            wasmparser::ComponentOuterAliasKind::CoreType => {
1267                crate::component::ComponentOuterAliasKind::CoreType
1268            }
1269            wasmparser::ComponentOuterAliasKind::Type => {
1270                crate::component::ComponentOuterAliasKind::Type
1271            }
1272            wasmparser::ComponentOuterAliasKind::Component => {
1273                crate::ComponentOuterAliasKind::Component
1274            }
1275        }
1276    }
1277
1278    pub fn component_val_type<T: ?Sized + ReencodeComponent>(
1279        reencoder: &mut T,
1280        ty: wasmparser::ComponentValType,
1281    ) -> crate::component::ComponentValType {
1282        match ty {
1283            wasmparser::ComponentValType::Type(u) => {
1284                crate::component::ComponentValType::Type(reencoder.component_type_index(u))
1285            }
1286            wasmparser::ComponentValType::Primitive(pty) => {
1287                crate::component::ComponentValType::Primitive(
1288                    crate::component::PrimitiveValType::from(pty),
1289                )
1290            }
1291        }
1292    }
1293
1294    pub fn type_bounds<T: ?Sized + ReencodeComponent>(
1295        reencoder: &mut T,
1296        ty: wasmparser::TypeBounds,
1297    ) -> crate::component::TypeBounds {
1298        match ty {
1299            wasmparser::TypeBounds::Eq(u) => {
1300                crate::component::TypeBounds::Eq(reencoder.component_type_index(u))
1301            }
1302            wasmparser::TypeBounds::SubResource => crate::component::TypeBounds::SubResource,
1303        }
1304    }
1305
1306    pub fn canonical_option<T: ?Sized + ReencodeComponent>(
1307        reencoder: &mut T,
1308        ty: wasmparser::CanonicalOption,
1309    ) -> crate::component::CanonicalOption {
1310        match ty {
1311            wasmparser::CanonicalOption::UTF8 => crate::component::CanonicalOption::UTF8,
1312            wasmparser::CanonicalOption::UTF16 => crate::component::CanonicalOption::UTF16,
1313            wasmparser::CanonicalOption::CompactUTF16 => {
1314                crate::component::CanonicalOption::CompactUTF16
1315            }
1316            wasmparser::CanonicalOption::Memory(u) => {
1317                crate::component::CanonicalOption::Memory(reencoder.memory_index(u))
1318            }
1319            wasmparser::CanonicalOption::Realloc(u) => {
1320                crate::component::CanonicalOption::Realloc(reencoder.function_index(u))
1321            }
1322            wasmparser::CanonicalOption::PostReturn(u) => {
1323                crate::component::CanonicalOption::PostReturn(reencoder.function_index(u))
1324            }
1325            wasmparser::CanonicalOption::Async => crate::component::CanonicalOption::Async,
1326            wasmparser::CanonicalOption::Callback(u) => {
1327                crate::component::CanonicalOption::Callback(reencoder.function_index(u))
1328            }
1329        }
1330    }
1331
1332    pub fn custom_component_name_section<T: ?Sized + ReencodeComponent>(
1333        reencoder: &mut T,
1334        section: wasmparser::ComponentNameSectionReader<'_>,
1335    ) -> Result<crate::ComponentNameSection, Error<T::Error>> {
1336        let mut ret = crate::ComponentNameSection::new();
1337        for subsection in section {
1338            reencoder.parse_custom_component_name_subsection(&mut ret, subsection?)?;
1339        }
1340        Ok(ret)
1341    }
1342
1343    pub fn parse_custom_component_name_subsection<T: ?Sized + ReencodeComponent>(
1344        reencoder: &mut T,
1345        names: &mut crate::ComponentNameSection,
1346        section: wasmparser::ComponentName<'_>,
1347    ) -> Result<(), Error<T::Error>> {
1348        match section {
1349            wasmparser::ComponentName::Component { name, .. } => {
1350                names.component(name);
1351            }
1352            wasmparser::ComponentName::CoreFuncs(map) => {
1353                names.core_funcs(&name_map(map, |i| reencoder.function_index(i))?);
1354            }
1355            wasmparser::ComponentName::CoreGlobals(map) => {
1356                names.core_globals(&name_map(map, |i| reencoder.global_index(i))?);
1357            }
1358            wasmparser::ComponentName::CoreMemories(map) => {
1359                names.core_memories(&name_map(map, |i| reencoder.memory_index(i))?);
1360            }
1361            wasmparser::ComponentName::CoreTables(map) => {
1362                names.core_tables(&name_map(map, |i| reencoder.table_index(i))?);
1363            }
1364            wasmparser::ComponentName::CoreModules(map) => {
1365                names.core_modules(&name_map(map, |i| reencoder.module_index(i))?);
1366            }
1367            wasmparser::ComponentName::CoreInstances(map) => {
1368                names.core_instances(&name_map(map, |i| reencoder.instance_index(i))?);
1369            }
1370            wasmparser::ComponentName::CoreTypes(map) => {
1371                names.core_types(&name_map(map, |i| reencoder.type_index(i))?);
1372            }
1373            wasmparser::ComponentName::Types(map) => {
1374                names.types(&name_map(map, |i| reencoder.component_type_index(i))?);
1375            }
1376            wasmparser::ComponentName::Instances(map) => {
1377                names.instances(&name_map(map, |i| reencoder.component_instance_index(i))?);
1378            }
1379            wasmparser::ComponentName::Components(map) => {
1380                names.components(&name_map(map, |i| reencoder.component_index(i))?);
1381            }
1382            wasmparser::ComponentName::Funcs(map) => {
1383                names.funcs(&name_map(map, |i| reencoder.component_func_index(i))?);
1384            }
1385            wasmparser::ComponentName::Values(map) => {
1386                names.values(&name_map(map, |i| reencoder.component_value_index(i))?);
1387            }
1388            wasmparser::ComponentName::Unknown { ty, data, .. } => {
1389                names.raw(ty, data);
1390            }
1391        }
1392        Ok(())
1393    }
1394}
1395
1396impl From<wasmparser::ComponentValType> for crate::ComponentValType {
1397    fn from(ty: wasmparser::ComponentValType) -> Self {
1398        RoundtripReencoder.component_val_type(ty)
1399    }
1400}
1401
1402impl From<wasmparser::TypeBounds> for crate::TypeBounds {
1403    fn from(ty: wasmparser::TypeBounds) -> Self {
1404        RoundtripReencoder.type_bounds(ty)
1405    }
1406}
1407
1408impl From<wasmparser::CanonicalOption> for crate::CanonicalOption {
1409    fn from(opt: wasmparser::CanonicalOption) -> Self {
1410        RoundtripReencoder.canonical_option(opt)
1411    }
1412}
1413
1414impl From<wasmparser::ComponentExternalKind> for crate::ComponentExportKind {
1415    fn from(kind: wasmparser::ComponentExternalKind) -> Self {
1416        RoundtripReencoder.component_export_kind(kind)
1417    }
1418}
1419
1420impl From<wasmparser::ComponentOuterAliasKind> for crate::ComponentOuterAliasKind {
1421    fn from(kind: wasmparser::ComponentOuterAliasKind) -> Self {
1422        RoundtripReencoder.component_outer_alias_kind(kind)
1423    }
1424}
1425
1426impl From<wasmparser::ComponentTypeRef> for crate::ComponentTypeRef {
1427    fn from(ty: wasmparser::ComponentTypeRef) -> Self {
1428        RoundtripReencoder.component_type_ref(ty)
1429    }
1430}
1431
1432impl From<wasmparser::PrimitiveValType> for crate::PrimitiveValType {
1433    fn from(ty: wasmparser::PrimitiveValType) -> Self {
1434        RoundtripReencoder.component_primitive_val_type(ty)
1435    }
1436}