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