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