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