wirm 4.0.0-rc3

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
//! Internal traversal and resolution machinery.
//!
//! This module mirrors the encode traversal logic but operates in a
//! read-only mode. It maintains:
//!
//! - A stack of component identities
//! - A stack of active index scopes
//! - A reference to the scope registry
//!
//! It is intentionally not exposed publicly to avoid leaking implementation
//! details such as pointer identity or scope IDs.
//!
//! # Safety and Invariants
//!
//! This traversal logic relies on the following invariants:
//!
//! - Component IDs are stable for the lifetime of the IR.
//! - Scoped IR nodes are stored in stable allocations.
//! - The scope registry is fully populated before traversal begins.
//! - No mutation of the component occurs during traversal.
//!
//! These guarantees allow resolution to rely on structural identity
//! without exposing internal identity mechanisms publicly.

use crate::ir::component::idx_spaces::{IndexSpaceOf, ScopeId, Space, SpaceSubtype, StoreHandle};
use crate::ir::component::refs::{Depth, IndexedRef, RefKind};
use crate::ir::component::scopes::{
    build_component_store, ComponentStore, GetScopeKind, RegistryHandle, ScopeOwnerKind,
};
use crate::ir::component::section::ComponentSection;
use crate::ir::component::visitor::driver::VisitEvent;
use crate::ir::component::visitor::{ItemKind, ResolvedItem};
use crate::ir::id::ComponentId;
use crate::Component;
use wasmparser::{ComponentTypeDeclaration, InstanceTypeDeclaration, ModuleTypeDeclaration};

/// The declaration slice of a currently-active type body scope.
///
/// Pushed onto [`VisitCtxInner::type_body_stack`] when entering a
/// `ComponentType::Instance`, `ComponentType::Component`, or
/// `CoreType::Module` definition, and popped on exit.  Used by
/// [`VisitCtxInner::resolve`] to dispatch current-depth refs into the
/// right subvec rather than into the component's main index vectors.
#[derive(Clone)]
pub(crate) enum TypeBodyDecls<'a> {
    Inst(&'a [InstanceTypeDeclaration<'a>]),
    Comp(&'a [ComponentTypeDeclaration<'a>]),
    Module(&'a [ModuleTypeDeclaration<'a>]),
}

#[derive(Clone)]
pub struct VisitCtxInner<'a> {
    pub(crate) registry: RegistryHandle,
    pub(crate) component_stack: Vec<ComponentId>, // may not need
    pub(crate) scope_stack: ScopeStack,
    pub(crate) node_has_nested_scope: Vec<bool>,
    /// Counts non-component (type/instance-type) scope levels currently on `scope_stack`.
    /// `scope_stack` grows for both component scopes and type scopes, while `component_stack`
    /// only grows for component scopes.  This offset is used in `comp_at` to re-align the
    /// depth value (which is relative to `scope_stack`) with `component_stack`.
    pub(crate) type_scope_nesting: usize,
    /// Stack of active type-body decl slices.  When non-empty, current-depth refs in
    /// `resolve()` are dispatched into `type_body_stack.last()` rather than the component's
    /// main index vectors, because refs inside a type body use that body's own namespace.
    pub(crate) type_body_stack: Vec<TypeBodyDecls<'a>>,
    pub(crate) store: StoreHandle,
    pub(crate) comp_store: ComponentStore<'a>,
    section_tracker_stack: Vec<SectionTracker>,
    /// Top-level section ordinal of the event currently being driven, relative to the
    /// immediately-containing component. Set by `drive_event` from each event's recorded
    /// section_idx; readable by visitor callbacks via `VisitCtx::curr_section_idx()`.
    /// `None` while driving root-component enter/exit events, which are not associated
    /// with any section.
    pub(crate) current_section_idx: Option<usize>,
}

// =======================================
// =========== SCOPE INTERNALS ===========
// =======================================

impl<'a> VisitCtxInner<'a> {
    pub fn new(root: &'a Component<'a>) -> Self {
        let comp_store = build_component_store(root);
        Self {
            registry: root.scope_registry.clone(),
            component_stack: Vec::new(),
            section_tracker_stack: Vec::new(),
            scope_stack: ScopeStack::new(),
            node_has_nested_scope: Vec::new(),
            type_scope_nesting: 0,
            type_body_stack: Vec::new(),
            store: root.index_store.clone(),
            comp_store,
            current_section_idx: None,
        }
    }

    pub fn visit_section(&mut self, section: &ComponentSection, num: usize) -> usize {
        self.section_tracker_stack
            .last_mut()
            .unwrap()
            .visit_section(section, num)
    }

    pub fn push_comp_section_tracker(&mut self) {
        self.section_tracker_stack.push(SectionTracker::default());
    }
    pub fn pop_comp_section_tracker(&mut self) {
        self.section_tracker_stack.pop();
    }

    pub fn push_component(&mut self, component: &Component) {
        let id = component.id;
        self.component_stack.push(id);
        self.push_comp_section_tracker();
        self.enter_comp_scope(id);
    }

    pub fn pop_component(&mut self) {
        let id = self.component_stack.pop().unwrap();
        self.pop_comp_section_tracker();
        self.exit_comp_scope(id);
    }
    pub fn curr_component(&self) -> &Component<'_> {
        let id = self.comp_at(Depth::default());
        self.comp_store.get(id)
    }

    pub fn maybe_enter_scope<T: GetScopeKind>(&mut self, node: &T) {
        let mut nested = false;
        if let Some(scope_entry) = self.registry.borrow().scope_entry(node) {
            nested = true;
            self.scope_stack.enter_scope(scope_entry.space);
            // Only ComponentType (Instance/Component) and CoreType (Module) reach here —
            // these are type scopes that push onto scope_stack but NOT onto component_stack.
            // Track the nesting depth so comp_at() can align the two stacks correctly.
            if matches!(
                scope_entry.kind,
                ScopeOwnerKind::ComponentTypeInstance
                    | ScopeOwnerKind::ComponentTypeComponent
                    | ScopeOwnerKind::CoreTypeModule
            ) {
                self.type_scope_nesting += 1;
            }
        }
        self.node_has_nested_scope.push(nested);
    }

    pub fn maybe_exit_scope<T: GetScopeKind>(&mut self, node: &T) {
        let nested = self.node_has_nested_scope.pop().unwrap();
        if let Some(scope_entry) = self.registry.borrow().scope_entry(node) {
            // Exit the nested index space...should be equivalent to the ID
            // of the scope that was entered by this node
            let exited_from = self.scope_stack.exit_scope();
            if matches!(
                scope_entry.kind,
                ScopeOwnerKind::ComponentTypeInstance
                    | ScopeOwnerKind::ComponentTypeComponent
                    | ScopeOwnerKind::CoreTypeModule
            ) {
                self.type_scope_nesting -= 1;
            }
            debug_assert!(nested);
            debug_assert_eq!(scope_entry.space, exited_from);
        } else {
            debug_assert!(!nested);
        }
    }

    pub(crate) fn enter_comp_scope(&mut self, comp_id: ComponentId) {
        let scope_id = self
            .registry
            .borrow()
            .scope_of_comp(comp_id)
            .expect("Internal error: no scope found for component");
        self.node_has_nested_scope
            .push(!self.scope_stack.stack.is_empty());
        self.scope_stack.enter_scope(scope_id);
    }

    pub(crate) fn exit_comp_scope(&mut self, comp_id: ComponentId) {
        let scope_id = self
            .registry
            .borrow()
            .scope_of_comp(comp_id)
            .unwrap_or_else(|| panic!("Internal error: no scope found for component {comp_id:?}"));
        let exited_from = self.scope_stack.exit_scope();
        debug_assert_eq!(scope_id, exited_from);
    }

    pub(crate) fn comp_at(&self, depth: Depth) -> &ComponentId {
        // `depth` is relative to the scope_stack (which includes both component scopes and
        // type scopes), but component_stack only tracks component scopes.  Subtract the number
        // of type-scope levels that sit above the current component on scope_stack so that the
        // index into component_stack is correct.
        let comp_depth = depth.val().saturating_sub(self.type_scope_nesting);
        let idx = self.component_stack.len() - comp_depth - 1;
        self.component_stack.get(idx).unwrap_or_else(|| {
            panic!(
                "Internal error: couldn't find component at depth {} \
                 (adjusted from scope depth {}, type_scope_nesting={}); stack: {:?}",
                comp_depth,
                depth.val(),
                self.type_scope_nesting,
                self.component_stack
            )
        })
    }

    pub(crate) fn push_type_body(&mut self, decls: TypeBodyDecls<'a>) {
        self.type_body_stack.push(decls);
    }

    pub(crate) fn pop_type_body(&mut self) {
        self.type_body_stack.pop();
    }
}

// ===============================================
// =========== ID RESOLUTION INTERNALS ===========
// ===============================================

impl<'a> VisitCtxInner<'a> {
    /// When looking up the ID of some node, we MUST consider whether the node we're assigning an ID for
    /// has a nested scope! If it does, this node's ID lives in its parent index space.
    pub(crate) fn lookup_id_for(
        &self,
        space: &Space,
        section: &ComponentSection,
        vec_idx: usize,
    ) -> u32 {
        let nested = self.node_has_nested_scope.last().unwrap_or(&false);
        let scope_id = if *nested {
            self.scope_stack.scope_at_depth(&Depth::parent())
        } else {
            self.scope_stack.curr_scope_id()
        };
        self.store
            .borrow()
            .scopes
            .get(&scope_id)
            .unwrap()
            .lookup_assumed_id(space, section, vec_idx) as u32
    }
    /// When looking up the ID of some node, we MUST consider whether the node we're assigning an ID for
    /// has a nested scope! If it does, this node's ID lives in its parent index space.
    pub(crate) fn lookup_id_with_subvec_for(
        &self,
        space: &Space,
        section: &ComponentSection,
        vec_idx: usize,
        subvec_idx: usize,
    ) -> u32 {
        let nested = self.node_has_nested_scope.last().unwrap_or(&false);
        let scope_id = if *nested {
            self.scope_stack.scope_at_depth(&Depth::parent())
        } else {
            self.scope_stack.curr_scope_id()
        };
        self.store
            .borrow()
            .scopes
            .get(&scope_id)
            .unwrap()
            .lookup_assumed_id_with_subvec(space, section, vec_idx, subvec_idx) as u32
    }

    /// Looking up a reference should always be relative to the scope of the node that
    /// contained the reference! No need to think about whether the node has a nested scope.
    pub(crate) fn index_from_assumed_id_no_cache(
        &self,
        r: &IndexedRef,
    ) -> (SpaceSubtype, usize, Option<usize>) {
        let scope_id = self.scope_stack.scope_at_depth(&r.depth);
        self.store
            .borrow()
            .scopes
            .get(&scope_id)
            .unwrap()
            .index_from_assumed_id_no_cache(r)
    }

    /// Looking up a reference should always be relative to the scope of the node that
    /// contained the reference! No need to think about whether the node has a nested scope.
    pub(crate) fn index_from_assumed_id(
        &mut self,
        r: &IndexedRef,
    ) -> (SpaceSubtype, usize, Option<usize>) {
        let scope_id = self.scope_stack.scope_at_depth(&r.depth);
        self.store
            .borrow_mut()
            .scopes
            .get_mut(&scope_id)
            .unwrap()
            .index_from_assumed_id(r)
    }
}

// =================================================
// =========== NODE RESOLUTION INTERNALS ===========
// =================================================

impl<'a> VisitCtxInner<'a> {
    pub fn lookup_root_comp_name(&self) -> Option<&str> {
        self.curr_component().component_name.as_deref()
    }
    pub fn lookup_comp_name(&self, id: u32) -> Option<&str> {
        self.curr_component().components_names.get(id)
    }
    pub fn lookup_comp_inst_name(&self, id: u32) -> Option<&str> {
        self.curr_component().instance_names.get(id)
    }
    pub fn lookup_comp_type_name(&self, id: u32) -> Option<&str> {
        self.curr_component().type_names.get(id)
    }
    pub fn lookup_comp_func_name(&self, id: u32) -> Option<&str> {
        self.curr_component().func_names.get(id)
    }
    pub fn lookup_module_name(&self, id: u32) -> Option<&str> {
        self.curr_component().module_names.get(id)
    }
    pub fn lookup_core_inst_name(&self, id: u32) -> Option<&str> {
        self.curr_component().core_instances_names.get(id)
    }
    pub fn lookup_core_type_name(&self, id: u32) -> Option<&str> {
        self.curr_component().core_type_names.get(id)
    }
    pub fn lookup_core_func_name(&self, id: u32) -> Option<&str> {
        self.curr_component().core_func_names.get(id)
    }
    pub fn lookup_global_name(&self, id: u32) -> Option<&str> {
        self.curr_component().global_names.get(id)
    }
    pub fn lookup_memory_name(&self, id: u32) -> Option<&str> {
        self.curr_component().memory_names.get(id)
    }
    pub fn lookup_tag_name(&self, id: u32) -> Option<&str> {
        self.curr_component().tag_names.get(id)
    }
    pub fn lookup_table_name(&self, id: u32) -> Option<&str> {
        self.curr_component().table_names.get(id)
    }
    pub fn lookup_value_name(&self, id: u32) -> Option<&str> {
        self.curr_component().value_names.get(id)
    }

    pub fn resolve_all(&self, refs: &[RefKind]) -> Vec<ResolvedItem<'a, 'a>> {
        let mut items = vec![];
        for r in refs.iter() {
            items.push(self.resolve(&r.ref_));
        }

        items
    }

    /// All data in a `ResolvedItem` ultimately borrows from `ComponentStore`, which holds
    /// `&'a Component<'a>` references.  Both lifetime parameters are therefore `'a` — the
    /// result does **not** borrow from `self` and outlives any temporary `VisitCtxInner`.
    pub fn resolve(&self, r: &IndexedRef) -> ResolvedItem<'a, 'a> {
        // Inside a type-body scope, current-depth refs address the type body's own
        // declaration namespace, not the enclosing component's main index vectors.
        // The driver pushes the active decl slice onto type_body_stack on enter and
        // pops it on exit, so we dispatch here automatically into the right subvec.
        if r.depth.is_curr() {
            match self.type_body_stack.last() {
                Some(TypeBodyDecls::Inst(decls)) => {
                    return self.resolve_maybe_from_subvec(r, decls)
                }
                Some(TypeBodyDecls::Comp(decls)) => {
                    return self.resolve_maybe_from_subvec(r, decls)
                }
                Some(TypeBodyDecls::Module(decls)) => {
                    return self.resolve_maybe_from_subvec(r, decls)
                }
                None => {} // not inside a type body; fall through to normal resolution
            }
        }

        let (subtype, idx, subidx) = self.index_from_assumed_id_no_cache(r);
        if r.space != Space::CoreType {
            assert!(
                subidx.is_none(),
                "only core types (with rec groups) should ever have subvec indices!"
            );
        }

        let comp_id = self.comp_at(r.depth);
        let referenced_comp = self.comp_store.get(comp_id);
        referenced_comp.item_at(r, subtype, idx)
    }
    /// Resolve a ref whose depth is current against a type-body declaration subvec.
    ///
    /// `T` must implement [`AsResolvedItem`], which maps each declaration variant to
    /// the appropriate [`ResolvedItem`].  Out-of-scope refs fall through to normal
    /// resolution automatically.
    pub fn resolve_maybe_from_subvec<T>(
        &self,
        ref_: &IndexedRef,
        subvec: &'a [T],
    ) -> ResolvedItem<'a, 'a>
    where
        T: AsResolvedItem<'a>,
    {
        if !ref_.depth.is_curr() {
            return self.resolve(ref_);
        }

        let (vec, idx, ..) = self.index_from_assumed_id_no_cache(ref_);
        assert_eq!(vec, SpaceSubtype::Main);
        subvec[idx].as_resolved_item(ref_.index)
    }
}

// =======================================
// ======= SUBVEC RESOLUTION TRAIT =======
// =======================================

/// Maps a type-body declaration to the appropriate [`ResolvedItem`] variant.
///
/// Implemented for [`InstanceTypeDeclaration`], [`ComponentTypeDeclaration`], and
/// [`ModuleTypeDeclaration`] so that [`VisitCtxInner::resolve_maybe_from_subvec`] can
/// be generic over all three.
pub(crate) trait AsResolvedItem<'a> {
    fn as_resolved_item(&'a self, index: u32) -> ResolvedItem<'a, 'a>;
}

impl<'a> AsResolvedItem<'a> for InstanceTypeDeclaration<'a> {
    fn as_resolved_item(&'a self, index: u32) -> ResolvedItem<'a, 'a> {
        match self {
            InstanceTypeDeclaration::CoreType(ty) => ResolvedItem::CoreType(index, ty),
            InstanceTypeDeclaration::Type(ty) => ResolvedItem::CompType(index, ty),
            InstanceTypeDeclaration::Alias(alias) => ResolvedItem::Alias(index, alias),
            InstanceTypeDeclaration::Export { .. } => ResolvedItem::InstTyDeclExport(index, self),
        }
    }
}

impl<'a> AsResolvedItem<'a> for ComponentTypeDeclaration<'a> {
    fn as_resolved_item(&'a self, index: u32) -> ResolvedItem<'a, 'a> {
        match self {
            ComponentTypeDeclaration::CoreType(ty) => ResolvedItem::CoreType(index, ty),
            ComponentTypeDeclaration::Type(ty) => ResolvedItem::CompType(index, ty),
            ComponentTypeDeclaration::Alias(alias) => ResolvedItem::Alias(index, alias),
            ComponentTypeDeclaration::Import(imp) => ResolvedItem::Import(index, imp),
            ComponentTypeDeclaration::Export { .. } => ResolvedItem::CompTyDeclExport(index, self),
        }
    }
}

impl<'a> AsResolvedItem<'a> for ModuleTypeDeclaration<'a> {
    fn as_resolved_item(&'a self, index: u32) -> ResolvedItem<'a, 'a> {
        ResolvedItem::ModuleTyDecl(index, self)
    }
}

#[derive(Clone)]
pub(crate) struct ScopeStack {
    pub(crate) stack: Vec<ScopeId>,
}
impl ScopeStack {
    fn new() -> Self {
        Self { stack: vec![] }
    }
    pub(crate) fn curr_scope_id(&self) -> ScopeId {
        self.stack.last().cloned().unwrap()
    }
    pub(crate) fn scope_at_depth(&self, depth: &Depth) -> ScopeId {
        *self
            .stack
            .get(self.stack.len() - depth.val() - 1)
            .unwrap_or_else(|| {
                panic!(
                    "couldn't find scope at depth {}; this is the current scope stack: {:?}",
                    depth.val(),
                    self.stack
                )
            })
    }
    pub fn enter_scope(&mut self, id: ScopeId) {
        self.stack.push(id)
    }
    pub fn exit_scope(&mut self) -> ScopeId {
        self.stack.pop().unwrap()
    }
}

// General trackers for indices of item vectors (used to track where i've been during visitation)
#[derive(Clone, Default)]
struct SectionTracker {
    last_processed_module: usize,
    last_processed_alias: usize,
    last_processed_core_ty: usize,
    last_processed_comp_ty: usize,
    last_processed_imp: usize,
    last_processed_exp: usize,
    last_processed_core_inst: usize,
    last_processed_comp_inst: usize,
    last_processed_canon: usize,
    last_processed_component: usize,
    last_processed_start: usize,
    last_processed_custom: usize,
}
impl SectionTracker {
    /// This function is used while traversing the component. This means that we
    /// should already know the space ID associated with the component section
    /// (if in visiting this next session we enter some inner index space).
    ///
    /// So, we use the associated space ID to return the inner index space. The
    /// calling function should use this return value to then context switch into
    /// this new index space. When we've finished visiting the section, swap back
    /// to the returned index space's `parent` (a field on the space).
    pub fn visit_section(&mut self, section: &ComponentSection, num: usize) -> usize {
        let tracker = match section {
            ComponentSection::Component => &mut self.last_processed_component,
            ComponentSection::Module => &mut self.last_processed_module,
            ComponentSection::Alias => &mut self.last_processed_alias,
            ComponentSection::CoreType => &mut self.last_processed_core_ty,
            ComponentSection::ComponentType => &mut self.last_processed_comp_ty,
            ComponentSection::ComponentImport => &mut self.last_processed_imp,
            ComponentSection::ComponentExport => &mut self.last_processed_exp,
            ComponentSection::CoreInstance => &mut self.last_processed_core_inst,
            ComponentSection::ComponentInstance => &mut self.last_processed_comp_inst,
            ComponentSection::Canon => &mut self.last_processed_canon,
            ComponentSection::CustomSection => &mut self.last_processed_custom,
            ComponentSection::ComponentStartSection => &mut self.last_processed_start,
        };

        let curr = *tracker;
        *tracker += num;
        curr
    }
}

pub fn for_each_indexed<'ir, T>(
    slice: &'ir [T],
    start: usize,
    count: usize,
    mut f: impl FnMut(usize, &'ir T),
) {
    debug_assert!(start + count <= slice.len());

    for i in 0..count {
        let idx = start + i;
        f(idx, &slice[idx]);
    }
}

pub fn emit_indexed<'ir, T: IndexSpaceOf>(
    out: &mut Vec<VisitEvent<'ir>>,
    section_idx: usize,
    idx: usize,
    item: &'ir T,
    make: fn(usize, ItemKind, usize, &'ir T) -> VisitEvent<'ir>,
) {
    out.push(make(section_idx, item.index_space_of().into(), idx, item));
}