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