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