wirm 4.0.1

A lightweight WebAssembly Transformation Library for the Component Model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
#![allow(clippy::too_many_arguments)]
//! Intermediate Representation of a wasm component.

use crate::encode::component::encode;
use crate::error::Error;
use crate::error::Error::IO;
use crate::ir::component::alias::Aliases;
use crate::ir::component::canons::Canons;
use crate::ir::component::idx_spaces::{
    IndexSpaceOf, IndexStore, ScopeId, Space, SpaceSubtype, StoreHandle,
};
use crate::ir::component::refs::{GetItemRef, GetTypeRefs, IndexedRef};
use crate::ir::component::scopes::{IndexScopeRegistry, RegistryHandle};
use crate::ir::component::section::{
    get_sections_for_comp_ty, get_sections_for_core_ty_and_assign_top_level_ids,
    populate_space_for_comp_ty, populate_space_for_core_ty, ComponentSection,
};
use crate::ir::component::types::ComponentTypes;
use crate::ir::component::visitor::ResolvedItem;
use crate::ir::helpers::{
    print_alias, print_component_export, print_component_import, print_component_type,
    print_core_type,
};
use crate::ir::id::{
    AliasFuncId, AliasId, AliasMemId, CanonicalFuncId, ComponentExportId, ComponentId,
    ComponentTypeFuncId, ComponentTypeId, ComponentTypeInstanceId, CoreInstanceId, CustomSectionID,
    FunctionID, GlobalID, ModuleID,
};
use crate::ir::module::module_globals::Global;
use crate::ir::module::Module;
use crate::ir::types::{CustomSection, CustomSections};
use crate::ir::AppendOnlyVec;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
use wasmparser::{
    CanonicalFunction, ComponentAlias, ComponentExport, ComponentFuncType, ComponentImport,
    ComponentInstance, ComponentStartFunction, ComponentType, CoreType, Encoding, Instance,
    InstanceTypeDeclaration, NameMap, Parser, Payload,
};

mod alias;
mod canons;
pub mod concrete;
pub mod idx_spaces;
pub mod refs;
pub(crate) mod scopes;
pub(crate) mod section;
#[cfg(test)]
mod tests;
mod types;
pub mod visitor;

#[derive(Debug)]
/// Intermediate Representation of a wasm component.
pub struct Component<'a> {
    pub id: ComponentId,
    /// Nested Components
    // These have scopes, but the scopes are looked up by ComponentId
    pub components: AppendOnlyVec<Component<'a>>,
    /// Modules
    // These have scopes, but they aren't handled by component encoding logic
    pub modules: AppendOnlyVec<Module<'a>>,
    /// Component Types
    // These can have scopes and need to be looked up by a pointer to the IR node --> Box the value!
    pub component_types: ComponentTypes<'a>,
    /// Component Instances
    pub component_instance: AppendOnlyVec<ComponentInstance<'a>>,
    /// Canons
    pub canons: Canons,

    /// Alias
    pub alias: Aliases<'a>,
    /// Imports
    pub imports: AppendOnlyVec<ComponentImport<'a>>,
    /// Exports
    pub exports: AppendOnlyVec<ComponentExport<'a>>,

    /// Core Types
    // These can have scopes and need to be looked up by a pointer to the IR node --> Box the value!
    pub core_types: AppendOnlyVec<Box<CoreType<'a>>>,
    /// Core Instances
    pub instances: AppendOnlyVec<Instance<'a>>,

    // Tracks the index spaces of this component.
    pub(crate) space_id: ScopeId, // cached for quick lookup!
    pub(crate) scope_registry: RegistryHandle,
    pub(crate) index_store: StoreHandle,

    /// Custom sections
    pub custom_sections: CustomSections<'a>,
    /// Component Start Section
    pub start_section: AppendOnlyVec<ComponentStartFunction>,
    /// Sections of the Component. Represented as (#num of occurrences of a section, type of section)
    pub sections: Vec<(u32, ComponentSection)>,
    num_sections: usize,

    // Names
    pub(crate) component_name: Option<String>,
    pub(crate) core_func_names: Names,
    pub(crate) global_names: Names,
    pub(crate) memory_names: Names,
    pub(crate) tag_names: Names,
    pub(crate) table_names: Names,
    pub(crate) module_names: Names,
    pub(crate) core_instances_names: Names,
    pub(crate) core_type_names: Names,
    pub(crate) type_names: Names,
    pub(crate) instance_names: Names,
    pub(crate) components_names: Names,
    pub(crate) func_names: Names,
    pub(crate) value_names: Names,
}

impl<'a> Component<'a> {
    /// Emit the Component into a wasm binary file.
    pub fn emit_wasm(&self, file_name: &str) -> crate::ir::types::Result<()> {
        let wasm = self.encode()?;
        std::fs::write(file_name, wasm).map_err(IO)?;
        Ok(())
    }

    fn add_section_and_get_id(
        &mut self,
        space: Space,
        sect: ComponentSection,
        idx: usize,
    ) -> usize {
        // get and save off the assumed id
        let assumed_id =
            self.index_store
                .borrow_mut()
                .assign_assumed_id(&self.space_id, &space, &sect, idx);

        self.add_section(sect);

        assumed_id.unwrap_or(idx)
    }

    fn add_section(&mut self, sect: ComponentSection) {
        // add to section order list
        if !self.sections.is_empty() && self.sections[self.num_sections - 1].1 == sect {
            self.sections[self.num_sections - 1].0 += 1;
        } else {
            self.sections.push((1, sect));
        }
    }

    /// Add a Module to this Component.
    pub fn add_module(&mut self, module: Module<'a>) -> ModuleID {
        let idx = self.modules.len();
        let id =
            self.add_section_and_get_id(module.index_space_of(), ComponentSection::Module, idx);
        self.modules.push(module);

        ModuleID(id as u32)
    }

    /// Add a Global to this Component.
    pub fn add_globals(&mut self, global: Global, module_idx: ModuleID) -> GlobalID {
        self.modules[*module_idx as usize].globals.add(global)
    }

    pub fn add_custom_section(&mut self, section: CustomSection<'a>) -> CustomSectionID {
        let id = self.custom_sections.add(section);
        self.add_section(ComponentSection::CustomSection);

        id
    }

    /// Add an Import to this Component.
    pub fn add_import(&mut self, import: ComponentImport<'a>) -> u32 {
        let idx = self.imports.len();
        let id = self.add_section_and_get_id(
            import.index_space_of(),
            ComponentSection::ComponentImport,
            idx,
        );
        self.imports.push(import);

        id as u32
    }

    /// Add an Aliased function to this Component.
    pub fn add_alias_func(&mut self, alias: ComponentAlias<'a>) -> (AliasFuncId, AliasId) {
        let space = alias.index_space_of();
        let (_item_id, alias_id) = self.alias.add(alias);
        let id = self.add_section_and_get_id(space, ComponentSection::Alias, *alias_id as usize);

        (AliasFuncId(id as u32), alias_id)
    }

    /// Add an Aliased core memory to this Component.
    pub fn add_alias_core_memory(&mut self, alias: ComponentAlias<'a>) -> (AliasMemId, AliasId) {
        let space = alias.index_space_of();
        let (_item_id, alias_id) = self.alias.add(alias);
        let id = self.add_section_and_get_id(space, ComponentSection::Alias, *alias_id as usize);

        (AliasMemId(id as u32), alias_id)
    }

    /// Add a Canonical Function to this Component.
    pub fn add_canon_func(&mut self, canon: CanonicalFunction) -> CanonicalFuncId {
        let space = canon.index_space_of();
        let idx = self.canons.add(canon).1;
        let id = self.add_section_and_get_id(space, ComponentSection::Canon, *idx as usize);

        CanonicalFuncId(id as u32)
    }

    /// Add a Component Type to this Component.
    pub(crate) fn add_component_type(
        &mut self,
        component_ty: ComponentType<'a>,
    ) -> (u32, ComponentTypeId) {
        let space = component_ty.index_space_of();
        let ids = self.component_types.add(component_ty);
        let id =
            self.add_section_and_get_id(space, ComponentSection::ComponentType, *ids.1 as usize);

        // Handle the index space of this node
        populate_space_for_comp_ty(
            self.component_types.items.last().unwrap(),
            self.scope_registry.clone(),
            self.index_store.clone(),
        );

        (id as u32, ids.1)
    }

    /// Add a Component Type that is an Instance to this component.
    pub fn add_type_instance(
        &mut self,
        decls: Vec<InstanceTypeDeclaration<'a>>,
    ) -> (ComponentTypeInstanceId, ComponentTypeId) {
        let (ty_inst_id, ty_id) =
            self.add_component_type(ComponentType::Instance(decls.into_boxed_slice()));

        // almost account for aliased types!
        (ComponentTypeInstanceId(ty_inst_id), ty_id)
    }

    /// Add a Component Type that is a Function to this component.
    pub fn add_type_func(
        &mut self,
        ty: ComponentFuncType<'a>,
    ) -> (ComponentTypeFuncId, ComponentTypeId) {
        let (ty_inst_id, ty_id) = self.add_component_type(ComponentType::Func(ty));

        // almost account for aliased types!
        (ComponentTypeFuncId(ty_inst_id), ty_id)
    }

    /// Add a new core instance to this component.
    pub fn add_core_instance(&mut self, instance: Instance<'a>) -> CoreInstanceId {
        let idx = self.instances.len();
        let id = self.add_section_and_get_id(
            instance.index_space_of(),
            ComponentSection::CoreInstance,
            idx,
        );
        self.instances.push(instance);

        CoreInstanceId(id as u32)
    }

    fn add_to_sections(
        sections: &mut Vec<(u32, ComponentSection)>,
        new_sections: &[ComponentSection],
        num_sections: &mut usize,
        payload_kind: ComponentSection,
    ) {
        // Empty payload (e.g. an import section with count=0): still push a
        // `(0, kind)` entry so `section_idx` stays 1:1 with the binary's
        // section positions. Skipping would desync wasmparser-based consumers
        // on inputs that contain empty sections.
        if new_sections.is_empty() {
            sections.push((0, payload_kind));
            *num_sections += 1;
            return;
        }
        debug_assert!(
            new_sections.iter().all(|k| *k == payload_kind),
            "non-empty new_sections must all equal payload_kind"
        );
        // Group consecutive same-kind items within this call into one
        // `(count, kind)` entry. Do NOT fold across calls: one call == one
        // binary section, and collapsing would desync section ordinals from
        // wasmparser-based consumers.
        let mut i = 0usize;
        while i < new_sections.len() {
            let kind = new_sections[i].clone();
            let mut count = 1u32;
            while i + (count as usize) < new_sections.len()
                && new_sections[i + count as usize] == kind
            {
                count += 1;
            }
            sections.push((count, kind));
            *num_sections += 1;
            i += count as usize;
        }
    }

    /// Parse a `Component` from a wasm binary.
    ///
    /// Set enable_multi_memory to `true` to support parsing modules using multiple memories.
    /// Set with_offsets to `true` to save opcode pc offset metadata during parsing
    /// (can be used to determine the static pc offset inside a function body of the start of any opcode).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use wirm::Component;
    ///
    /// let file = "path_to_file";
    /// let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
    /// let comp = Component::parse(&buff, false, false).unwrap();
    /// ```
    pub fn parse(
        wasm: &'_ [u8],
        enable_multi_memory: bool,
        with_offsets: bool,
    ) -> Result<Component<'_>, Error> {
        let parser = Parser::new(0);

        let registry = IndexScopeRegistry::default();
        let mut store = IndexStore::default();
        let space_id = store.new_scope();
        let mut next_comp_id = 0;
        let res = Component::parse_comp(
            wasm,
            enable_multi_memory,
            with_offsets,
            parser,
            0,
            space_id,
            Rc::new(RefCell::new(registry)),
            Rc::new(RefCell::new(store)),
            &mut next_comp_id,
        );
        res
    }

    fn parse_comp(
        wasm: &'a [u8],
        enable_multi_memory: bool,
        with_offsets: bool,
        parser: Parser,
        start: usize,
        space_id: ScopeId,
        registry_handle: RegistryHandle,
        store_handle: StoreHandle,
        next_comp_id: &mut u32,
    ) -> Result<Component<'a>, Error> {
        let my_comp_id = ComponentId(*next_comp_id);
        *next_comp_id += 1;

        let mut modules = AppendOnlyVec::default();
        let mut core_types = AppendOnlyVec::default();
        let mut component_types = AppendOnlyVec::default();
        let mut imports = AppendOnlyVec::default();
        let mut exports = AppendOnlyVec::default();
        let mut instances = AppendOnlyVec::default();
        let mut canons = AppendOnlyVec::default();
        let mut alias = AppendOnlyVec::default();
        let mut component_instance = AppendOnlyVec::default();
        let mut components = AppendOnlyVec::default();
        let mut start_section = AppendOnlyVec::default();
        let mut custom_sections = vec![];

        let mut sections = vec![];
        let mut num_sections: usize = 0;
        let mut stack = vec![];

        // Names
        let mut component_name: Option<String> = None;
        let mut core_func_names = Names::default();
        let mut global_names = Names::default();
        let mut tag_names = Names::default();
        let mut memory_names = Names::default();
        let mut table_names = Names::default();
        let mut module_names = Names::default();
        let mut core_instances_names = Names::default();
        let mut instance_names = Names::default();
        let mut components_names = Names::default();
        let mut func_names = Names::default();
        let mut value_names = Names::default();
        let mut core_type_names = Names::default();
        let mut type_names = Names::default();

        for payload in parser.parse_all(wasm) {
            let payload = payload?;

            // `wasmparser::Parser::parse_all` yields payloads from every
            // nesting level via its own internal recursion. For subcomponents
            // and submodules we've already handed off to a separate recursive
            // `parse_comp` / `parse_internal` call, so we need to skip those
            // nested payloads here. The `stack` counter tracks how deep we
            // are inside a nested-but-already-processed section: every nested
            // `ComponentSection`/`ModuleSection` bumps it, every `End`
            // unbumps. Top-level `End` (stack empty) and the nested section
            // that we entered ourselves both fall through to the match.
            match &payload {
                Payload::End(_) if !stack.is_empty() => {
                    stack.pop();
                    continue;
                }
                Payload::ComponentSection { .. } | Payload::ModuleSection { .. }
                    if !stack.is_empty() =>
                {
                    stack.push(Encoding::Component); // marker; variant unused
                    continue;
                }
                _ if !stack.is_empty() => continue,
                _ => {}
            }

            match payload {
                Payload::ComponentImportSection(import_section_reader) => {
                    let mut temp: Vec<ComponentImport> = import_section_reader
                        .into_iter()
                        .collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::ComponentImport; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        imports.len(),
                        &new_sections,
                    );
                    imports.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::ComponentImport,
                    );
                }
                Payload::ComponentExportSection(export_section_reader) => {
                    let mut temp: Vec<ComponentExport> = export_section_reader
                        .into_iter()
                        .collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::ComponentExport; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        exports.len(),
                        &new_sections,
                    );
                    exports.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::ComponentExport,
                    );
                }
                Payload::InstanceSection(instance_section_reader) => {
                    let mut temp: Vec<Instance> = instance_section_reader
                        .into_iter()
                        .collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::CoreInstance; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        instances.len(),
                        &new_sections,
                    );
                    instances.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::CoreInstance,
                    );
                }
                Payload::CoreTypeSection(core_type_reader) => {
                    let mut temp: Vec<Box<CoreType>> = core_type_reader
                        .into_iter()
                        .map(|res| res.map(Box::new))
                        .collect::<Result<_, _>>()?;

                    let old_len = core_types.len();
                    core_types.append(&mut temp);

                    let mut new_sects = vec![];
                    for (idx, ty) in core_types[old_len..].iter().enumerate() {
                        let (new_sect, _) = get_sections_for_core_ty_and_assign_top_level_ids(
                            ty,
                            old_len + idx,
                            &space_id,
                            store_handle.clone(),
                        );
                        new_sects.push(new_sect);
                    }

                    Self::add_to_sections(
                        &mut sections,
                        &new_sects,
                        &mut num_sections,
                        ComponentSection::CoreType,
                    );
                }
                Payload::ComponentTypeSection(component_type_reader) => {
                    let mut temp: Vec<Box<ComponentType>> = component_type_reader
                        .into_iter()
                        .map(|res| res.map(Box::new))
                        .collect::<Result<_, _>>()?;

                    let old_len = component_types.len();
                    component_types.append(&mut temp);

                    let mut new_sects = vec![];
                    for ty in &component_types[old_len..] {
                        let (new_sect, _) = get_sections_for_comp_ty(ty);
                        new_sects.push(new_sect);
                    }

                    store_handle.borrow_mut().assign_assumed_id_for_boxed(
                        &space_id,
                        &component_types[old_len..],
                        old_len,
                        &new_sects,
                    );
                    Self::add_to_sections(
                        &mut sections,
                        &new_sects,
                        &mut num_sections,
                        ComponentSection::ComponentType,
                    );
                }
                Payload::ComponentInstanceSection(component_instances) => {
                    let mut temp: Vec<ComponentInstance> =
                        component_instances.into_iter().collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::ComponentInstance; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        component_instance.len(),
                        &new_sections,
                    );
                    component_instance.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::ComponentInstance,
                    );
                }
                Payload::ComponentAliasSection(alias_reader) => {
                    let mut temp: Vec<ComponentAlias> =
                        alias_reader.into_iter().collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::Alias; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        alias.len(),
                        &new_sections,
                    );
                    alias.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::Alias,
                    );
                }
                Payload::ComponentCanonicalSection(canon_reader) => {
                    let mut temp: Vec<CanonicalFunction> =
                        canon_reader.into_iter().collect::<Result<_, _>>()?;
                    let l = temp.len();
                    let new_sections = vec![ComponentSection::Canon; l];
                    store_handle.borrow_mut().assign_assumed_id_for(
                        &space_id,
                        &temp,
                        canons.len(),
                        &new_sections,
                    );
                    canons.append(&mut temp);
                    Self::add_to_sections(
                        &mut sections,
                        &new_sections,
                        &mut num_sections,
                        ComponentSection::Canon,
                    );
                }
                Payload::ModuleSection {
                    parser,
                    unchecked_range,
                } => {
                    // Indicating the start of a new module
                    stack.push(Encoding::Module);
                    let m = Module::parse_internal(
                        &wasm[unchecked_range.start - start..unchecked_range.end - start],
                        enable_multi_memory,
                        with_offsets,
                        parser,
                    )?;
                    store_handle.borrow_mut().assign_assumed_id(
                        &space_id,
                        &m.index_space_of(),
                        &ComponentSection::Module,
                        modules.len(),
                    );
                    modules.push(m);
                    Self::add_to_sections(
                        &mut sections,
                        &[ComponentSection::Module],
                        &mut num_sections,
                        ComponentSection::Module,
                    );
                }
                Payload::ComponentSection {
                    parser,
                    unchecked_range,
                } => {
                    let sub_space_id = store_handle.borrow_mut().new_scope();
                    let sect = ComponentSection::Component;

                    stack.push(Encoding::Component);
                    let cmp = Component::parse_comp(
                        &wasm[unchecked_range.start - start..unchecked_range.end - start],
                        enable_multi_memory,
                        with_offsets,
                        parser,
                        unchecked_range.start,
                        sub_space_id,
                        Rc::clone(&registry_handle),
                        Rc::clone(&store_handle),
                        next_comp_id,
                    )?;
                    store_handle.borrow_mut().assign_assumed_id(
                        &space_id,
                        &cmp.index_space_of(),
                        &sect,
                        components.len(),
                    );
                    components.push(cmp);

                    Self::add_to_sections(
                        &mut sections,
                        &[sect],
                        &mut num_sections,
                        ComponentSection::Component,
                    );
                }
                Payload::ComponentStartSection { start, range: _ } => {
                    start_section.push(start);
                    Self::add_to_sections(
                        &mut sections,
                        &[ComponentSection::ComponentStartSection],
                        &mut num_sections,
                        ComponentSection::ComponentStartSection,
                    );
                }
                Payload::CustomSection(custom_section_reader) => {
                    match custom_section_reader.as_known() {
                        wasmparser::KnownCustom::ComponentName(name_section_reader) => {
                            for subsection in name_section_reader {
                                #[allow(clippy::single_match)]
                                match subsection? {
                                    wasmparser::ComponentName::Component { name, .. } => {
                                        component_name = Some(name.parse().unwrap())
                                    }
                                    wasmparser::ComponentName::CoreFuncs(names) => {
                                        core_func_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreGlobals(names) => {
                                        global_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreTables(names) => {
                                        table_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreModules(names) => {
                                        module_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreInstances(names) => {
                                        core_instances_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreTypes(names) => {
                                        core_type_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Types(names) => {
                                        type_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Instances(names) => {
                                        instance_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Components(names) => {
                                        components_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Funcs(names) => {
                                        func_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Values(names) => {
                                        value_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreMemories(names) => {
                                        memory_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::CoreTags(names) => {
                                        tag_names.add_all(names);
                                    }
                                    wasmparser::ComponentName::Unknown { .. } => {}
                                }
                            }
                        }
                        _ => {
                            custom_sections
                                .push((custom_section_reader.name(), custom_section_reader.data()));
                            Self::add_to_sections(
                                &mut sections,
                                &[ComponentSection::CustomSection],
                                &mut num_sections,
                                ComponentSection::CustomSection,
                            );
                        }
                    }
                }
                Payload::UnknownSection {
                    id,
                    contents: _,
                    range: _,
                } => return Err(Error::UnknownSection { section_id: id }),
                Payload::Version { .. } | Payload::End { .. } => {} // nothing to do
                other => return Err(Error::UnhandledPayload(format!("{other:?}"))),
            }
        }

        // Scope discovery
        for comp in components.iter() {
            let comp_id = comp.id;
            let sub_space_id = comp.space_id;
            registry_handle
                .borrow_mut()
                .register_comp(comp_id, sub_space_id);
        }
        for ty in core_types.iter() {
            populate_space_for_core_ty(ty, registry_handle.clone(), store_handle.clone());
        }
        for ty in component_types.iter() {
            populate_space_for_comp_ty(ty, registry_handle.clone(), store_handle.clone());
        }

        let comp = Component {
            id: my_comp_id,
            modules,
            alias: Aliases::new(alias),
            core_types,
            component_types: ComponentTypes::new(component_types),
            imports,
            exports,
            instances,
            component_instance,
            canons: Canons::new(canons),
            space_id,
            scope_registry: registry_handle,
            index_store: store_handle,
            custom_sections: CustomSections::new(custom_sections),
            sections,
            start_section,
            num_sections,
            component_name,
            core_func_names,
            global_names,
            memory_names,
            tag_names,
            table_names,
            module_names,
            core_instances_names,
            core_type_names,
            type_names,
            instance_names,
            components_names,
            func_names,
            components,
            value_names,
        };

        comp.scope_registry
            .borrow_mut()
            .register_comp(my_comp_id, space_id);

        Ok(comp)
    }

    /// Encode this component into a WebAssembly binary.
    ///
    /// This method performs encoding in three high-level phases:
    ///
    /// 1. **Collect** – Walks the component IR and records all items that will be
    ///    encoded, along with any index references that need to be rewritten.
    /// 2. **Assign** – Resolves all index references after instrumentation so that
    ///    every reference points to its final concrete index.
    /// 3. **Encode** – Emits the final WebAssembly binary using the resolved indices.
    ///
    /// # Nested Components
    ///
    /// Components may be arbitrarily nested. During traversal, the encoder maintains
    /// a stack of component IDs that tracks which component is currently being
    /// visited. A registry maps component IDs to their corresponding components,
    /// allowing the encoder to correctly resolve cross-component references.
    ///
    /// # Scoped Definitions
    ///
    /// Many IR nodes introduce scopes (such as types, instances, or component-local
    /// definitions). To support correct index resolution in the presence of deep
    /// nesting and instrumentation, the encoder tracks scopes explicitly:
    ///
    /// - A scope stack is maintained during traversal.
    /// - Each scoped IR node is registered by identity in a scope registry.
    /// - Index lookups can recover the correct scope for any IR node in O(1) time.
    ///
    /// This design allows instrumentation to safely insert, reorder, or modify
    /// nodes without breaking encoding invariants.
    ///
    /// # Panics
    ///
    /// Panics if encoding encounters an internal inconsistency, such as:
    ///
    /// - An index reference that cannot be resolved to a registered scope
    /// - A malformed or structurally invalid component
    /// - Violations of encoding invariants introduced by instrumentation
    ///
    /// These panics indicate a bug in the encoder or in transformations applied
    /// to the component prior to encoding, rather than a recoverable runtime error.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use wirm::Component;
    ///
    /// let file = "path/to/file.wasm";
    /// let buff = wat::parse_file(file).expect("couldn't convert the input wat to Wasm");
    /// let mut comp = Component::parse(&buff, false, false).unwrap();
    /// let result = comp.encode();
    /// ```
    pub fn encode(&self) -> crate::ir::types::Result<Vec<u8>> {
        encode(self)
    }

    /// Map an already-resolved `(subtype, idx)` pair to a [`ResolvedItem`].
    ///
    /// This is the shared core of both [`Component::resolve`] and
    /// [`VisitCtxInner::resolve`].  Both callers look up the `(SpaceSubtype, idx)` from
    /// the appropriate [`IndexStore`] scope, then delegate the actual IR-field lookup here.
    pub(crate) fn item_at<'s>(
        &'s self,
        ref_: &IndexedRef,
        subtype: SpaceSubtype,
        idx: usize,
    ) -> ResolvedItem<'s, 'a> {
        match subtype {
            SpaceSubtype::Main => match ref_.space {
                Space::Comp => ResolvedItem::Component(ref_.index, &self.components[idx]),
                Space::CompType => {
                    ResolvedItem::CompType(ref_.index, &self.component_types.items[idx])
                }
                Space::CompInst => {
                    ResolvedItem::CompInst(ref_.index, &self.component_instance[idx])
                }
                Space::CoreInst => ResolvedItem::CoreInst(ref_.index, &self.instances[idx]),
                Space::CoreModule => ResolvedItem::Module(ref_.index, &self.modules[idx]),
                Space::CoreType => ResolvedItem::CoreType(ref_.index, &self.core_types[idx]),
                Space::CompFunc | Space::CoreFunc => {
                    ResolvedItem::Func(ref_.index, &self.canons.items[idx])
                }
                Space::CompVal
                | Space::CoreMemory
                | Space::CoreTable
                | Space::CoreGlobal
                | Space::CoreTag
                | Space::NA => unreachable!(
                    "space {:?} has no main vector on the component IR",
                    ref_.space
                ),
            },
            SpaceSubtype::Export => ResolvedItem::Export(ref_.index, &self.exports[idx]),
            SpaceSubtype::Import => ResolvedItem::Import(ref_.index, &self.imports[idx]),
            SpaceSubtype::Alias => ResolvedItem::Alias(ref_.index, &self.alias.items[idx]),
        }
    }

    /// Resolve a reference that is local to this component's own index space.
    ///
    /// The `ref_` must have `depth == 0` (i.e. it refers to an item defined directly
    /// within this component, not to an outer scope).  This is the typical case for
    /// refs found in a component's own exports, imports, types, etc.
    ///
    /// This uses the index space that was built at parse time — no walk is required.
    /// Because all nested components share the same index store and scope registry
    /// (via `Rc`), this method works correctly on any component in the tree, not just the root.
    pub fn resolve<'s>(&'s self, ref_: &IndexedRef) -> ResolvedItem<'s, 'a> {
        let store = self.index_store.borrow();
        let (subtype, idx, _subvec_idx) =
            store.index_from_assumed_id_no_cache(&self.space_id, ref_);
        drop(store);
        self.item_at(ref_, subtype, idx)
    }

    /// Resolve the item referred to by the named import, using this component's own index space.
    ///
    /// Finds the first import whose name matches `name`, then resolves the type ref it carries
    /// (e.g. `ComponentTypeRef::Instance(idx)`) into a [`ResolvedItem`].
    ///
    /// Returns `None` if no import with that name exists or if the import carries no type ref.
    pub fn resolve_named_import<'s>(&'s self, name: &str) -> Option<ResolvedItem<'s, 'a>> {
        let import = self.imports.iter().find(|i| i.name.0 == name)?;
        let type_ref = import.get_type_refs().into_iter().next()?;
        Some(self.resolve(&type_ref.ref_))
    }

    /// Resolve the item referred to by the named export, using this component's own index space.
    ///
    /// Finds the first export whose name matches `name`, then resolves the item ref it carries
    /// into a [`ResolvedItem`].
    ///
    /// Returns `None` if no export with that name exists.
    pub fn resolve_named_export<'s>(&'s self, name: &str) -> Option<ResolvedItem<'s, 'a>> {
        let export = self.exports.iter().find(|e| e.name.0 == name)?;
        Some(self.resolve(&export.get_item_ref().ref_))
    }

    /// Lookup the type for an exported `lift` canonical function.
    pub fn get_type_of_exported_lift_func(
        &self,
        export_id: ComponentExportId,
    ) -> Option<&ComponentType<'a>> {
        let export = self.exports.get(*export_id as usize)?;
        let type_ref = match self.resolve(&export.get_item_ref().ref_) {
            ResolvedItem::Func(_, canon) => *canon.get_type_refs().first().unwrap(),
            ResolvedItem::Alias(_, alias) => alias.get_item_ref(),
            _ => unreachable!("exported lift func should resolve to a Func or Alias"),
        };
        match self.resolve(&type_ref.ref_) {
            ResolvedItem::CompType(_, ty) => Some(ty),
            _ => None,
        }
    }

    /// Print a rudimentary textual representation of a `Component`
    pub fn print(&self) {
        // Print Alias
        if !self.alias.items.is_empty() {
            eprintln!("Alias Section:");
            for alias in self.alias.items.iter() {
                print_alias(alias);
            }
            eprintln!();
        }

        // Print CoreType
        if !self.core_types.is_empty() {
            eprintln!("Core Type Section:");
            for cty in self.core_types.iter() {
                print_core_type(cty);
            }
            eprintln!();
        }

        // Print ComponentType
        if !self.component_types.items.is_empty() {
            eprintln!("Component Type Section:");
            for cty in self.component_types.items.iter() {
                print_component_type(cty);
            }
            eprintln!();
        }

        // Print Imports
        if !self.imports.is_empty() {
            eprintln!("Imports Section:");
            for imp in self.imports.iter() {
                print_component_import(imp);
            }
            eprintln!();
        }

        // Print Exports
        if !self.imports.is_empty() {
            eprintln!("Exports Section:");
            for exp in self.exports.iter() {
                print_component_export(exp);
            }
            eprintln!();
        }
    }

    /// Get Local Function ID by name
    // Note: returned absolute id here
    pub fn get_fid_by_name(&self, name: &str, module_idx: ModuleID) -> Option<FunctionID> {
        self.modules[*module_idx as usize]
            .functions
            .get_local_fid_by_name(name)
    }
}

#[derive(Debug, Default)]
pub struct Names {
    // Maintains keys in sorted order (need to encode in order of the index)
    pub(crate) names: BTreeMap<u32, String>,
}
impl Names {
    pub fn get(&self, id: u32) -> Option<&str> {
        self.names.get(&id).map(|s| s.as_str())
    }
    pub(crate) fn add_all(&mut self, names: NameMap) {
        for name in names {
            let naming = name.unwrap();
            self.add(naming.index, naming.name);
        }
    }
    fn add(&mut self, id: u32, name: &str) {
        self.names.insert(id, name.to_string());
    }
}