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