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