Skip to main content

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