Coverage Report

Created: 2025-01-06 07:43

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