Coverage Report

Created: 2026-07-19 07:55

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