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