wac-graph 0.10.0

A library for defining, encoding, and decoding WebAssembly composition graphs.
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
use crate::PackageId;
use indexmap::IndexMap;
use petgraph::graph::NodeIndex;
use std::collections::HashMap;
use wac_types::{
    CoreExtern, DefinedType, DefinedTypeId, Enum, Flags, FuncTypeId, InterfaceId, ItemKind,
    ModuleTypeId, PrimitiveType, Record, ResourceId, Type, Types, UsedType, ValueType, Variant,
    WorldId,
};
use wasm_encoder::{
    Alias, ComponentBuilder, ComponentCoreTypeEncoder, ComponentExportKind,
    ComponentOuterAliasKind, ComponentType, ComponentTypeEncoder, ComponentTypeRef,
    ComponentValType, EntityType, GlobalType, InstanceType, MemoryType, ModuleType, TableType,
    TagKind, TagType, TypeBounds,
};

/// A type used to abstract the API differences between a component builder,
/// component type, and instance type from `wasm-encoder`.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum Encodable {
    Builder(ComponentBuilder),
    Instance(InstanceType),
    Component(ComponentType),
}

impl Encodable {
    fn type_count(&self) -> u32 {
        match self {
            Encodable::Builder(t) => t.type_count(),
            Encodable::Component(t) => t.type_count(),
            Encodable::Instance(t) => t.type_count(),
        }
    }

    fn instance_count(&self) -> u32 {
        match self {
            Encodable::Builder(t) => t.instance_count(),
            Encodable::Component(t) => t.instance_count(),
            Encodable::Instance(t) => t.instance_count(),
        }
    }

    fn core_type_count(&self) -> u32 {
        match self {
            Encodable::Builder(t) => t.core_type_count(),
            Encodable::Component(t) => t.core_type_count(),
            Encodable::Instance(t) => t.core_type_count(),
        }
    }

    fn ty(&mut self) -> ComponentTypeEncoder<'_> {
        match self {
            Encodable::Builder(t) => t.ty(None).1,
            Encodable::Instance(t) => t.ty(),
            Encodable::Component(t) => t.ty(),
        }
    }

    fn core_type(&mut self) -> ComponentCoreTypeEncoder<'_> {
        match self {
            Encodable::Builder(t) => t.core_type(None).1,
            Encodable::Instance(t) => t.core_type(),
            Encodable::Component(t) => t.core_type(),
        }
    }

    fn import_type(&mut self, name: &str, ty: ComponentTypeRef) {
        match self {
            Encodable::Component(t) => {
                t.import(name, ty);
            }
            Encodable::Builder(b) => {
                b.import(name, ty);
            }
            _ => panic!("expected a component type"),
        }
    }

    fn alias(&mut self, alias: Alias) {
        match self {
            Encodable::Builder(t) => {
                t.alias(None, alias);
            }
            Encodable::Instance(t) => {
                t.alias(alias);
            }
            Encodable::Component(t) => {
                t.alias(alias);
            }
        }
    }
}

impl Default for Encodable {
    fn default() -> Self {
        Self::Builder(Default::default())
    }
}

#[derive(Debug, Default)]
pub struct Scope {
    /// The map from types to encoded type index.
    pub type_indexes: IndexMap<Type, u32>,
    /// The map from interface name (i.e. id) to encoded instance index.
    pub instances: IndexMap<String, u32>,
    /// The map of import/export name to their alias indexes.
    type_aliases: IndexMap<String, u32>,
    /// The map of resource names to their encoded indexes.
    resources: IndexMap<String, u32>,
    /// The encodable for this scope.
    encodable: Encodable,
}

#[derive(Debug, Default)]
pub struct State {
    /// The stack of encoding scopes.
    scopes: Vec<Scope>,
    /// The current encoding scope.
    pub current: Scope,
    /// A map of nodes in the graph to their encoded indexes.
    pub node_indexes: HashMap<NodeIndex, u32>,
    /// The map of package identifiers to encoded components (either imported or defined).
    pub packages: HashMap<PackageId, u32>,
    /// A map of instantiation nodes to a list of their encoded implicitly imported arguments.
    pub implicit_args: HashMap<NodeIndex, Vec<(String, ComponentExportKind, u32)>>,
}

impl State {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn builder(&mut self) -> &mut ComponentBuilder {
        assert!(self.scopes.is_empty(), "expected scopes to be empty");
        match &mut self.current.encodable {
            Encodable::Builder(builder) => builder,
            _ => panic!("expected a builder"),
        }
    }

    fn push(&mut self, encodable: Encodable) {
        log::debug!("pushing new type scope");
        let prev = std::mem::replace(
            &mut self.current,
            Scope {
                encodable,
                ..Default::default()
            },
        );

        self.scopes.push(prev);
    }

    fn pop(&mut self) -> Encodable {
        log::debug!("popping type scope");
        let prev = std::mem::replace(
            &mut self.current,
            self.scopes.pop().expect("expected a scope to pop"),
        );
        prev.encodable
    }

    fn used_type_index(&mut self, name: &str) -> Option<u32> {
        if let Some(index) = self.current.type_aliases.get(name) {
            return Some(*index);
        }

        if let Some(parent) = self.scopes.last() {
            if let Some(outer) = parent.type_aliases.get(name) {
                let index = self.current.encodable.type_count();
                log::debug!("encoding outer alias for `{name}` to type index {index}");
                self.current.encodable.alias(Alias::Outer {
                    kind: ComponentOuterAliasKind::Type,
                    count: 1,
                    index: *outer,
                });
                return Some(index);
            }
        }

        None
    }
}

pub struct TypeEncoder<'a>(&'a Types);

impl<'a> TypeEncoder<'a> {
    pub fn new(types: &'a Types) -> Self {
        Self(types)
    }

    pub fn ty(&self, state: &mut State, ty: Type, name: Option<&str>) -> u32 {
        if let Some(index) = state.current.type_indexes.get(&ty) {
            return *index;
        }

        if let Some(name) = name {
            if let Some(index) = state.used_type_index(name) {
                state.current.type_indexes.insert(ty, index);
                return index;
            }
        }

        let index = match ty {
            Type::Resource(_) => panic!("cannot encode a resource"),
            Type::Func(id) => self.func_type(state, id),
            Type::Value(ValueType::Primitive(ty)) => Self::primitive(state, ty),
            Type::Value(ValueType::Borrow(id)) => self.borrow(state, id),
            Type::Value(ValueType::Own(id)) => self.own(state, id),
            Type::Value(ValueType::Defined(id)) => self.defined(state, id),
            Type::Interface(id) => self.instance(state, id, false),
            Type::World(id) => self.component(state, id),
            Type::Module(id) => self.module(state, id),
        };

        state.current.type_indexes.insert(ty, index);
        index
    }

    fn func_type(&self, state: &mut State, id: FuncTypeId) -> u32 {
        log::debug!("encoding function type");
        let ty = &self.0[id];

        let params = ty
            .params
            .iter()
            .map(|(n, ty)| (n.as_str(), self.value_type(state, *ty)))
            .collect::<Vec<_>>();

        let result = ty.result.map(|ty| self.value_type(state, ty));
        let index = state.current.encodable.type_count();
        let mut encoder = state.current.encodable.ty().function();
        encoder.async_(ty.is_async);
        encoder.params(params);
        encoder.result(result);

        log::debug!("function type encoded to type index {index}");
        index
    }

    fn defined(&self, state: &mut State, id: DefinedTypeId) -> u32 {
        log::debug!("encoding defined type");
        let ty = &self.0[id];
        let index = match ty {
            DefinedType::Tuple(types) => self.tuple(state, types),
            DefinedType::List(ty) => self.list(state, *ty),
            DefinedType::FixedSizeList(ty, elements) => self.fixed_size_list(state, *ty, *elements),
            DefinedType::Option(ty) => self.option(state, *ty),
            DefinedType::Result { ok, err } => self.result(state, *ok, *err),
            DefinedType::Variant(v) => self.variant(state, v),
            DefinedType::Record(r) => self.record(state, r),
            DefinedType::Flags(f) => self.flags(state, f),
            DefinedType::Enum(e) => self.enum_type(state, e),
            DefinedType::Alias(ValueType::Primitive(ty)) => Self::primitive(state, *ty),
            DefinedType::Alias(ValueType::Borrow(id)) => self.borrow(state, *id),
            DefinedType::Alias(ValueType::Own(id)) => self.own(state, *id),
            DefinedType::Alias(ValueType::Defined(id)) => self.defined(state, *id),
            DefinedType::Stream(ty) => self.stream(state, *ty),
            DefinedType::Future(ty) => self.future(state, *ty),
        };

        log::debug!("defined type encoded to type index {index}");
        index
    }

    fn use_aliases(&self, state: &mut State, uses: &'a IndexMap<String, UsedType>) {
        state.current.type_aliases.clear();

        for (name, used) in uses {
            let interface = &self.0[used.interface];
            let iid = interface.id.as_ref().expect("interface should have an id");
            let instance = state.current.instances[iid];
            let index = state.current.encodable.type_count();
            let export: &String = used.name.as_ref().unwrap_or(name);
            let kind = interface.exports.get(export).unwrap();
            state.current.encodable.alias(Alias::InstanceExport {
                instance,
                kind: ComponentExportKind::Type,
                name: export,
            });

            log::debug!(
                "aliased {kind} export `{export}` of instance index {instance} ({iid}) to type index {index}",
                kind = kind.desc(self.0)
            );

            state.current.type_aliases.insert(name.clone(), index);
        }
    }

    fn instance(&self, state: &mut State, id: InterfaceId, types_only: bool) -> u32 {
        log::debug!("encoding instance type");
        let interface = &self.0[id];
        for used in interface.uses.values() {
            self.import_deps(state, used.interface);
        }

        // Encode any required aliases
        self.use_aliases(state, &interface.uses);
        state.push(Encodable::Instance(InstanceType::default()));

        // Otherwise, export all exports
        for (name, kind) in &interface.exports {
            match kind {
                ItemKind::Type(_) => {
                    self.export(state, name, *kind);
                }
                _ => {
                    if !types_only {
                        self.export(state, name, *kind);
                    }
                }
            }
        }

        match state.pop() {
            Encodable::Instance(ty) => {
                let index = state.current.encodable.type_count();
                state.current.encodable.ty().instance(&ty);
                log::debug!("instance type encoded to type index {index}");
                index
            }
            _ => panic!("expected the pushed encodable to be an instance type"),
        }
    }

    pub fn component(&self, state: &mut State, id: WorldId) -> u32 {
        log::debug!("encoding component type");
        let world = &self.0[id];

        state.push(Encodable::Component(ComponentType::default()));

        for used in world.uses.values() {
            self.import_deps(state, used.interface);
        }

        self.use_aliases(state, &world.uses);

        for (name, kind) in &world.imports {
            self.import(state, name, *kind);
        }

        for (name, kind) in &world.exports {
            self.export(state, name, *kind);
        }

        match state.pop() {
            Encodable::Component(ty) => {
                let index = state.current.encodable.type_count();
                state.current.encodable.ty().component(&ty);
                log::debug!("component type encoded to type index {index}");
                index
            }
            _ => panic!("expected the pushed encodable to be a component type"),
        }
    }

    fn import_deps(&self, state: &mut State, id: InterfaceId) {
        let iid = self.0[id].id.as_ref().expect("interface should have an id");
        if state.current.instances.contains_key(iid) {
            return;
        }

        let interface = &self.0[id];

        // Depth-first recurse on the dependencies of this interface
        for used in interface.uses.values() {
            self.import_deps(state, used.interface);
        }

        log::debug!("encoding dependency on interface `{iid}`");

        let index = self.instance(state, id, !state.scopes.is_empty());
        let import_index = state.current.encodable.instance_count();

        state
            .current
            .encodable
            .import_type(iid, ComponentTypeRef::Instance(index));

        log::debug!(
            "interface `{iid}` is available for aliasing from instance index {import_index}"
        );
        state.current.instances.insert(iid.clone(), import_index);
    }

    pub fn interface(&self, state: &mut State, id: InterfaceId) -> u32 {
        let interface = &self.0[id];
        let iid = interface.id.as_deref().expect("interface must have an id");
        log::debug!("encoding interface definition of `{iid}`");
        assert!(state.scopes.is_empty());
        state.push(Encodable::Component(ComponentType::default()));

        for used in interface.uses.values() {
            self.import_deps(state, used.interface);
        }

        let index = self.instance(state, id, false);
        Self::export_type(state, iid, ComponentTypeRef::Instance(index));

        match state.pop() {
            Encodable::Component(ty) => {
                let (index, encoder) = state.builder().ty(None);
                encoder.component(&ty);
                log::debug!("encoded interface definition of `{iid}` to type index {index}",);
                index
            }
            _ => panic!("expected the pushed encodable to be a component type"),
        }
    }

    pub fn world(&self, state: &mut State, id: WorldId) -> u32 {
        let world = &self.0[id];
        let world_id = world.id.as_deref().expect("world must have an id");
        log::debug!("encoding world definition of `{world_id}`");

        assert!(state.scopes.is_empty());
        state.push(Encodable::Component(ComponentType::default()));
        let index = self.component(state, id);
        Self::export_type(state, world_id, ComponentTypeRef::Component(index));

        match state.pop() {
            Encodable::Component(ty) => {
                let (index, encoder) = state.builder().ty(None);
                encoder.component(&ty);
                log::debug!("encoded world definition of `{world_id}` to type index {index}");
                index
            }
            _ => panic!("expected the push encodable to be a component type"),
        }
    }

    fn module(&self, state: &mut State, id: ModuleTypeId) -> u32 {
        log::debug!("encoding module definition");
        let ty = &self.0[id];
        let mut encoded = ModuleType::new();

        for ((module, name), ext) in &ty.imports {
            let ty = self.entity_type(&mut encoded, ext);
            encoded.import(module, name, ty);
        }

        for (name, ext) in &ty.exports {
            let ty = self.entity_type(&mut encoded, ext);
            encoded.export(name, ty);
        }

        let index = state.current.encodable.core_type_count();
        state.current.encodable.core_type().module(&encoded);
        log::debug!("encoded module definition to type index {index}");
        index
    }

    fn entity_type(&self, encodable: &mut ModuleType, ext: &CoreExtern) -> EntityType {
        match ext {
            CoreExtern::Func(func) => {
                let index = encodable.type_count();
                encodable.ty().function(
                    func.params.iter().copied().map(Into::into),
                    func.results.iter().copied().map(Into::into),
                );
                EntityType::Function(index)
            }
            CoreExtern::Table {
                element_type,
                initial,
                maximum,
                table64,
                shared,
            } => EntityType::Table(TableType {
                element_type: (*element_type).into(),
                minimum: *initial,
                maximum: *maximum,
                table64: *table64,
                shared: *shared,
            }),
            CoreExtern::Memory {
                memory64,
                shared,
                initial,
                maximum,
                page_size_log2,
            } => EntityType::Memory(MemoryType {
                minimum: *initial,
                maximum: *maximum,
                memory64: *memory64,
                shared: *shared,
                page_size_log2: *page_size_log2,
            }),
            CoreExtern::Global {
                val_type,
                mutable,
                shared,
            } => EntityType::Global(GlobalType {
                val_type: (*val_type).into(),
                mutable: *mutable,
                shared: *shared,
            }),
            CoreExtern::Tag(func) => {
                let index = encodable.type_count();
                encodable.ty().function(
                    func.params.iter().copied().map(Into::into),
                    func.results.iter().copied().map(Into::into),
                );
                EntityType::Tag(TagType {
                    kind: TagKind::Exception,
                    func_type_idx: index,
                })
            }
        }
    }

    pub fn value_type(&self, state: &mut State, ty: ValueType) -> ComponentValType {
        if let Some(index) = state.current.type_indexes.get(&Type::Value(ty)) {
            return ComponentValType::Type(*index);
        }

        let index = match ty {
            ValueType::Primitive(ty) => return ComponentValType::Primitive(ty.into()),
            ValueType::Borrow(id) => self.borrow(state, id),
            ValueType::Own(id) => self.own(state, id),
            ValueType::Defined(id) => self.defined(state, id),
        };

        state.current.type_indexes.insert(Type::Value(ty), index);
        ComponentValType::Type(index)
    }

    fn primitive(state: &mut State, ty: PrimitiveType) -> u32 {
        let index = state.current.encodable.type_count();
        state
            .current
            .encodable
            .ty()
            .defined_type()
            .primitive(ty.into());
        index
    }

    fn tuple(&self, state: &mut State, types: &[ValueType]) -> u32 {
        let types = types
            .iter()
            .map(|ty| self.value_type(state, *ty))
            .collect::<Vec<_>>();
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().tuple(types);
        index
    }

    fn list(&self, state: &mut State, ty: ValueType) -> u32 {
        let ty = self.value_type(state, ty);
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().list(ty);
        index
    }

    fn fixed_size_list(&self, state: &mut State, ty: ValueType, elements: u32) -> u32 {
        let ty = self.value_type(state, ty);
        let index = state.current.encodable.type_count();
        state
            .current
            .encodable
            .ty()
            .defined_type()
            .fixed_length_list(ty, elements);
        index
    }

    fn stream(&self, state: &mut State, ty: Option<ValueType>) -> u32 {
        let ty = ty.map(|ty| self.value_type(state, ty));
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().stream(ty);
        index
    }

    fn future(&self, state: &mut State, ty: Option<ValueType>) -> u32 {
        let ty = ty.map(|ty| self.value_type(state, ty));
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().future(ty);
        index
    }

    fn option(&self, state: &mut State, ty: ValueType) -> u32 {
        let ty = self.value_type(state, ty);
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().option(ty);
        index
    }

    fn result(&self, state: &mut State, ok: Option<ValueType>, err: Option<ValueType>) -> u32 {
        let ok = ok.map(|ty| self.value_type(state, ty));
        let err = err.map(|ty| self.value_type(state, ty));
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().result(ok, err);
        index
    }

    fn borrow(&self, state: &mut State, res: ResourceId) -> u32 {
        assert!(!state.scopes.is_empty());
        let res = state.current.resources[self.0[res].name.as_str()];
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().borrow(res);
        index
    }

    fn own(&self, state: &mut State, res: ResourceId) -> u32 {
        let res = state.current.resources[self.0[res].name.as_str()];
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().own(res);
        index
    }

    fn variant(&self, state: &mut State, variant: &Variant) -> u32 {
        let cases = variant
            .cases
            .iter()
            .map(|(n, ty)| (n.as_str(), ty.map(|ty| self.value_type(state, ty))))
            .collect::<Vec<_>>();

        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().variant(cases);
        index
    }

    fn record(&self, state: &mut State, record: &Record) -> u32 {
        let fields = record
            .fields
            .iter()
            .map(|(n, ty)| (n.as_str(), self.value_type(state, *ty)))
            .collect::<Vec<_>>();
        let index = state.current.encodable.type_count();
        state.current.encodable.ty().defined_type().record(fields);
        index
    }

    fn flags(&self, state: &mut State, flags: &Flags) -> u32 {
        let index = state.current.encodable.type_count();
        state
            .current
            .encodable
            .ty()
            .defined_type()
            .flags(flags.0.iter().map(String::as_str));
        index
    }

    fn enum_type(&self, state: &mut State, e: &Enum) -> u32 {
        let index = state.current.encodable.type_count();
        state
            .current
            .encodable
            .ty()
            .defined_type()
            .enum_type(e.0.iter().map(String::as_str));
        index
    }

    fn import(&self, state: &mut State, name: &str, kind: ItemKind) {
        if let ItemKind::Type(Type::Resource(id)) = kind {
            self.import_resource(state, name, id);
            return;
        }

        log::debug!("encoding {kind} import `{name}`", kind = kind.desc(self.0));
        let ty = kind.ty();
        let index = self.ty(state, ty, Some(name));

        match kind {
            ItemKind::Type(_) => {
                let import_index = state.current.encodable.type_count();
                state
                    .current
                    .encodable
                    .import_type(name, ComponentTypeRef::Type(TypeBounds::Eq(index)));

                // Remap the type to the index of the imported item
                state.current.type_indexes.insert(ty, import_index);
            }
            ItemKind::Func(_) => {
                state
                    .current
                    .encodable
                    .import_type(name, ComponentTypeRef::Func(index));
            }
            ItemKind::Instance(id) => {
                let import_index = state.current.encodable.instance_count();
                state
                    .current
                    .encodable
                    .import_type(name, ComponentTypeRef::Instance(index));
                if let Some(iid) = &self.0[id].id {
                    log::debug!("instance index {import_index} ({iid}) is available for aliasing");
                    state.current.instances.insert(iid.clone(), import_index);
                }
            }
            _ => panic!("expected only types, functions, and instance types"),
        }
    }

    pub fn import_resource(&self, state: &mut State, name: &str, id: ResourceId) -> u32 {
        if let Some(index) = state.current.resources.get(name) {
            return *index;
        }

        log::debug!("encoding import of resource `{name}`");

        let resource = &self.0[id];
        let index = if let Some(outer) = state.used_type_index(name) {
            // This is an alias to an outer resource type
            let index = state.current.encodable.type_count();
            state
                .current
                .encodable
                .import_type(name, ComponentTypeRef::Type(TypeBounds::Eq(outer)));

            log::debug!("encoded outer alias for resource `{name}` to type index {index}");
            index
        } else if let Some(alias) = resource.alias {
            let source = self.0.resolve_resource(alias.source);
            let source_index = if let Some(index) =
                state.current.resources.get(self.0[source].name.as_str())
            {
                // The source resource was previously imported
                *index
            } else if let Some(index) = state.current.type_indexes.get(&Type::Resource(source)) {
                // The source resource isn't directly imported, but was previously aliased
                *index
            } else {
                // Otherwise, we need to alias the source resource
                // This should only occur for resources owned by interfaces
                let source_index = state.current.encodable.type_count();
                let iid = self.0[alias.owner.expect("should have owner")]
                    .id
                    .as_deref()
                    .expect("expected an interface with an id");
                state.current.encodable.alias(Alias::InstanceExport {
                    instance: state.current.instances[iid],
                    kind: ComponentExportKind::Type,
                    name: self.0[source].name.as_str(),
                });
                state
                    .current
                    .type_indexes
                    .insert(Type::Resource(source), source_index);
                source_index
            };

            let index = state.current.encodable.type_count();
            state
                .current
                .encodable
                .import_type(name, ComponentTypeRef::Type(TypeBounds::Eq(source_index)));

            log::debug!("encoded import for resource `{name}` as type index {index} (alias of type index {source_index})");
            index
        } else {
            // Otherwise, this is a new resource type, import with a subtype bounds
            let index = state.current.encodable.type_count();
            state
                .current
                .encodable
                .import_type(name, ComponentTypeRef::Type(TypeBounds::SubResource));

            log::debug!("encoded import for resource `{name}` to type index {index}");
            index
        };

        state.current.resources.insert(resource.name.clone(), index);
        index
    }

    fn export(&self, state: &mut State, name: &str, kind: ItemKind) -> u32 {
        if let ItemKind::Type(Type::Resource(id)) = kind {
            return self.export_resource(state, name, id);
        }

        log::debug!(
            "encoding {kind} export of `{name}`",
            kind = kind.desc(self.0)
        );

        let ty = kind.ty();
        let index = self.ty(state, ty, Some(name));
        let index = Self::export_type(
            state,
            name,
            match kind {
                ItemKind::Type(_) => ComponentTypeRef::Type(TypeBounds::Eq(index)),
                ItemKind::Func(_) => ComponentTypeRef::Func(index),
                ItemKind::Instance(_) => ComponentTypeRef::Instance(index),
                _ => panic!("expected only types, functions, and instance types"),
            },
        );

        // For types, remap to the index of the exported item
        if let ItemKind::Type(ty) = kind {
            state.current.type_indexes.insert(ty, index);
        }

        index
    }

    fn export_resource(&self, state: &mut State, name: &str, id: ResourceId) -> u32 {
        log::debug!("encoding export of resource `{name}`");

        if let Some(existing) = state.current.resources.get(name) {
            return *existing;
        }

        let resource = &self.0[id];
        let index = if let Some(outer) = state.used_type_index(name) {
            // This is an alias to an outer resource type
            let index =
                Self::export_type(state, name, ComponentTypeRef::Type(TypeBounds::Eq(outer)));
            log::debug!("encoded outer alias for resource `{name}` as type index {index}");
            index
        } else if let Some(alias) = resource.alias {
            // This is an alias to another resource at the same scope
            let index = state.current.resources
                [self.0[self.0.resolve_resource(alias.source)].name.as_str()];
            let index =
                Self::export_type(state, name, ComponentTypeRef::Type(TypeBounds::Eq(index)));
            log::debug!("encoded alias for resource `{name}` as type index {index}");
            index
        } else {
            // Otherwise, this is a new resource type, export with a subtype bounds
            let index =
                Self::export_type(state, name, ComponentTypeRef::Type(TypeBounds::SubResource));
            log::debug!("encoded export of resource `{name}` as type index {index}");
            index
        };

        state.current.resources.insert(resource.name.clone(), index);
        index
    }

    fn export_type(state: &mut State, name: &str, ty: ComponentTypeRef) -> u32 {
        match &mut state.current.encodable {
            Encodable::Component(t) => {
                let index = t.type_count();
                t.export(name, ty);
                index
            }
            Encodable::Instance(t) => {
                let index = t.type_count();
                t.export(name, ty);
                index
            }
            Encodable::Builder(_) => panic!("expected a component or instance type"),
        }
    }
}