wasm_encoder/reencode/
component.rs

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