wasm-component-trampoline 40.0.0

A library for linking WASM components together using host trampoline functions
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
use crate::path::{ForeignInterfacePath, InterfacePath, InterfacePathParseError};
use crate::{DynInterfaceTrampoline, DynPackageTrampoline, ImportFilter, ImportRule};
use derivative::Derivative;
use indexmap::{IndexMap, IndexSet};
use semver::Version;
use slab::Slab;
use snafu::{ResultExt, Snafu};
use std::collections::HashMap;
use std::ops::{Deref, Index};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::Arc;
use wac_types::{InterfaceId, ItemKind, Package};
use wasm_component_semver::VersionMap;
use wasmtime::component::{Component, Instance, LinkerInstance};
use wasmtime::{AsContextMut, component};

/// A graph for composing multiple WebAssembly components into a single linker, while allowing for
/// automatic insertion of "trampoline" functions between cross-component calls.
#[derive(Derivative)]
#[derivative(Debug)]
#[derivative(Default(bound = ""))]
pub struct CompositionGraph<D, C: Clone = ()> {
    nonce: usize,
    types: wac_types::Types,
    packages: Slab<PackageWrapper>,
    package_map: HashMap<String, VersionMap<PackageId>>,
    exported_interfaces: HashMap<ForeignInterfacePath, InterfaceExport<D, C>>,
    imported_interfaces: HashMap<PackageId, IndexSet<ForeignInterfacePath>>,
    #[derivative(Debug = "ignore")]
    import_filter: Box<dyn ImportFilter>,
}

impl<D, C: Clone> CompositionGraph<D, C> {
    /// Creates a new empty `CompositionGraph`.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters package imports for graph inclusion.
    /// The filter can be removed by using the default `ImportRule::default()` filter.
    pub fn set_import_filter<F>(&mut self, filter: F)
    where
        F: ImportFilter + 'static,
    {
        self.import_filter = Box::new(filter);
    }

    /// Adds a package (component) to the composition graph.
    ///
    /// Components can be added in any order, and dependencies will be resolved at instantiation time.
    pub fn add_package(
        &mut self,
        name: String,
        version: Version,
        bytes: impl Into<Vec<u8>>,
        trampoline: impl DynPackageTrampoline<D, C>,
    ) -> Result<PackageId, AddPackageError> {
        let package = Package::from_bytes(name.as_str(), Some(&version), bytes, &mut self.types)
            .context(add_package_error::PackageParseSnafu)?;

        let package_id = PackageId {
            id: self.packages.insert(PackageWrapper {
                package,
                nonce: self.nonce,
            }),
            nonce: self.nonce,
        };
        self.nonce += 1;

        let version_set = self.package_map.entry(name.to_string()).or_default();

        if let Err((version, _)) = version_set.try_insert(version, package_id) {
            return Err(AddPackageError::DuplicatePackage {
                name: name.to_string(),
                version: version.clone(),
            });
        }

        let package = self.packages.get_mut(package_id.id).unwrap();

        let package_prefix = format!("{}/", package.name());
        let version_suffix = package.version().map_or(String::new(), |v| format!("@{v}"));

        let exports = &self.types[package.ty()].exports;

        for (export_name, export_kind) in exports {
            let ItemKind::Instance(interface_id) = export_kind else {
                continue;
            };

            let interface_name = export_name
                .strip_prefix(&package_prefix)
                .and_then(|export_name| export_name.strip_suffix(&version_suffix));

            if let Some(interface_name) = interface_name {
                let path = ForeignInterfacePath::new(
                    package.name().to_string(),
                    interface_name.to_string(),
                    package.version().cloned(),
                );

                let interface_trampoline = InterfaceExport {
                    package: package_id,
                    interface: *interface_id,
                    trampoline: trampoline.interface_trampoline(interface_name),
                };

                if self
                    .exported_interfaces
                    .insert(path.clone(), interface_trampoline)
                    .is_some()
                {
                    // This would be a programming error, since the package name/version tuple is
                    // guaranteed to be unique.
                    panic!("duplicate exported interface key {path:?}");
                }
            }
        }

        let mut import = |package_id: PackageId, interface_id: InterfaceId, import_name: &str| {
            let import_interface_path = InterfacePath::from_str(import_name).context(
                add_package_error::ImportParseSnafu {
                    interface: import_name.to_string(),
                },
            )?;

            if let Some(import) = import_interface_path.into_foreign() {
                match self.import_filter.filter_rule(&import) {
                    ImportRule::Skip => return Ok(()),

                    ImportRule::Include => {
                        // If the interface defines no functions, skip it.
                        let interface = &self.types[interface_id];
                        let interface_has_func = interface
                            .exports
                            .iter()
                            .any(|(_item_name, item_kind)| matches!(item_kind, ItemKind::Func(_)));
                        if !interface_has_func {
                            return Ok(());
                        }
                    }

                    ImportRule::Force => { /* continue */ }
                }

                // Add the interface to the list of imports.
                self.imported_interfaces
                    .entry(package_id)
                    .or_default()
                    .insert(import);
            }

            Ok(())
        };

        for (package_id, package) in &self.packages {
            let package_id = PackageId {
                id: package_id,
                nonce: package.nonce,
            };
            let package_ty = &self.types[package.ty()];

            for (import_name, import_kind) in &package_ty.imports {
                let ItemKind::Instance(interface_id) = import_kind else {
                    continue;
                };

                import(package_id, *interface_id, import_name)?;
            }
        }

        Ok(package_id)
    }

    /// Instantiates a component from the composition graph, resolving all component dependencies.
    ///
    /// Host functions and other resources can be provided through the `linker` argument prior to
    /// instantiation.
    pub fn instantiate(
        &mut self,
        package_id: PackageId,
        linker: &mut component::Linker<D>,
        mut store: impl AsContextMut<Data = D>,
        engine: &wasmtime::Engine,
    ) -> Result<Instance, InstantiateError>
    where
        D: 'static,
        C: Send + Sync + 'static,
    {
        let mut interfaces = IndexMap::<PackageId, IndexSet<String>>::new();

        let load_order = self
            .package_load_order(package_id, &mut interfaces)
            .context(instantiate_error::LoadPackageSnafu)?;

        let package = self
            .packages
            .get(package_id.id)
            .ok_or(InstantiateError::PackageNotFound { id: package_id })?;

        let component = Component::new(engine, package.bytes())
            .context(instantiate_error::ComponentInstantiationSnafu)?;

        for shadow_package_id in load_order {
            if shadow_package_id == package_id {
                break;
            }

            let shadow_package = self.packages.get(shadow_package_id.id).ok_or(
                InstantiateError::PackageNotFound {
                    id: shadow_package_id,
                },
            )?;

            let empty_set = IndexSet::new();
            let shadow_interfaces = interfaces.get(&shadow_package_id).unwrap_or(&empty_set);

            self.instantiate_shadowed_package(
                shadow_package,
                linker,
                &mut store,
                engine,
                shadow_interfaces,
            )
            .with_context(|_err| {
                instantiate_error::InstantiatePackageDependencySnafu {
                    name: shadow_package.name().to_string(),
                    version: shadow_package.version().cloned(),
                }
            })?;
        }

        let instance = linker
            .instantiate(&mut store, &component)
            .context(instantiate_error::ComponentInstantiationSnafu)?;

        Ok(instance)
    }

    /// Like `instantiate`, but for asynchronous contexts.
    pub async fn instantiate_async(
        &mut self,
        package_id: PackageId,
        linker: &mut component::Linker<D>,
        mut store: impl AsContextMut<Data = D>,
        engine: &wasmtime::Engine,
    ) -> Result<Instance, InstantiateError>
    where
        D: Send + 'static,
        C: Send + Sync + 'static,
    {
        let mut interfaces = IndexMap::<PackageId, IndexSet<String>>::new();

        let load_order = self
            .package_load_order(package_id, &mut interfaces)
            .context(instantiate_error::LoadPackageSnafu)?;

        let package = self
            .packages
            .get(package_id.id)
            .ok_or(InstantiateError::PackageNotFound { id: package_id })?;

        let component = Component::new(engine, package.bytes())
            .context(instantiate_error::ComponentInstantiationSnafu)?;

        for shadow_package_id in load_order {
            if shadow_package_id == package_id {
                break;
            }

            let shadow_package = self.packages.get(shadow_package_id.id).ok_or(
                InstantiateError::PackageNotFound {
                    id: shadow_package_id,
                },
            )?;

            let empty_set = IndexSet::new();
            let shadow_interfaces = interfaces.get(&shadow_package_id).unwrap_or(&empty_set);

            self.instantiate_shadowed_package_async(
                shadow_package,
                linker,
                &mut store,
                engine,
                shadow_interfaces,
            )
            .await
            .with_context(|_err| {
                instantiate_error::InstantiatePackageDependencySnafu {
                    name: shadow_package.name().to_string(),
                    version: shadow_package.version().cloned(),
                }
            })?;
        }

        let instance = linker
            .instantiate_async(&mut store, &component)
            .await
            .context(instantiate_error::ComponentInstantiationSnafu)?;

        Ok(instance)
    }

    /// Gets a reference to the type collection of the graph.
    #[must_use]
    pub fn types(&self) -> &wac_types::Types {
        &self.types
    }

    /// Gets a mutable reference to the type collection of the graph.
    ///
    /// This type collection is used to define types directly in the graph.
    pub fn types_mut(&mut self) -> &mut wac_types::Types {
        &mut self.types
    }

    fn package_load_order(
        &self,
        origin: PackageId,
        interfaces: &mut IndexMap<PackageId, IndexSet<String>>,
    ) -> Result<impl IntoIterator<Item = PackageId> + 'static, LoadPackageError> {
        let mut package_stack = vec![(origin, 0)];

        let mut load_order = IndexSet::<PackageId>::new();
        let mut load_stack = IndexSet::<PackageId>::new();

        while let Some((package_id, offset)) = package_stack.pop() {
            load_order.extend(load_stack.drain(offset..).rev());

            if let Some(cycle_start) = load_stack.get_index_of(&package_id) {
                let self_import = (cycle_start == load_stack.len() - 1)
                    && load_stack.index(cycle_start) == &package_id;

                if self_import {
                    continue;
                }

                let mut cycle = load_stack
                    .iter()
                    .skip(cycle_start)
                    .copied()
                    .collect::<Vec<_>>();

                cycle.push(package_id);

                return Err(LoadPackageError::PackageCycle {
                    cycle: cycle
                        .into_iter()
                        .map(|package| {
                            self.packages
                                .get(package.id)
                                .map_or("{{UNKNOWN_PACKAGE}}".to_string(), |package| {
                                    package.name().to_string()
                                })
                        })
                        .collect(),
                });
            }

            if load_order.contains(&package_id) {
                continue;
            }

            load_stack.insert(package_id);

            let imports = self
                .imported_interfaces
                .get(&package_id)
                .map(IndexSet::as_slice)
                .unwrap_or_default();

            for import in imports {
                let version_map = self.package_map.get(import.package_name()).ok_or_else(|| {
                    LoadPackageError::MissingPackageDependency {
                        package_name: import.package_name().to_string(),
                    }
                })?;

                let import_package =
                    version_map.get_or_latest(import.version()).ok_or_else(|| {
                        LoadPackageError::CannotResolvePackageVersion {
                            name: import.package_name().to_string(),
                            version: import.version().cloned(),
                        }
                    })?;

                package_stack.push((*import_package, load_stack.len()));

                interfaces
                    .entry(*import_package)
                    .or_default()
                    .insert(import.interface_name().to_string());
            }
        }

        Ok(load_order.into_iter().chain(load_stack.into_iter().rev()))
    }

    fn instantiate_shadowed_package(
        &self,
        package: &Package,
        linker: &mut component::Linker<D>,
        mut store: impl AsContextMut<Data = D>,
        engine: &wasmtime::Engine,
        interfaces: &IndexSet<String>,
    ) -> Result<(), InstantiatePackageError>
    where
        D: 'static,
        C: Send + Sync + 'static,
    {
        let component = Component::new(engine, package.bytes())
            .context(instantiate_package_error::ComponentInstantiationSnafu)?;

        let shadow_instance = linker
            .instantiate(&mut store, &component)
            .context(instantiate_package_error::ComponentInstantiationSnafu)?;

        self.shadow_package(
            package,
            Rc::new(shadow_instance),
            linker,
            store,
            interfaces,
            SyncInstanceShadower,
        )
    }

    async fn instantiate_shadowed_package_async(
        &self,
        package: &Package,
        linker: &mut component::Linker<D>,
        mut store: impl AsContextMut<Data = D>,
        engine: &wasmtime::Engine,
        interfaces: &IndexSet<String>,
    ) -> Result<(), InstantiatePackageError>
    where
        D: Send + 'static,
        C: Send + Sync + 'static,
    {
        let component = Component::new(engine, package.bytes())
            .context(instantiate_package_error::ComponentInstantiationSnafu)?;

        let shadow_instance = linker
            .instantiate_async(&mut store, &component)
            .await
            .context(instantiate_package_error::ComponentInstantiationSnafu)?;

        self.shadow_package(
            package,
            Rc::new(shadow_instance),
            linker,
            store,
            interfaces,
            AsyncInstanceShadower,
        )
    }

    fn shadow_package(
        &self,
        package: &Package,
        shadow_instance: Rc<Instance>,
        linker: &mut component::Linker<D>,
        mut store: impl AsContextMut<Data = D>,
        interfaces: &IndexSet<String>,
        shadower: impl InstanceShadower<D, C>,
    ) -> Result<(), InstantiatePackageError> {
        for interface_name in interfaces {
            let interface_path = ForeignInterfacePath::new(
                package.name().to_string(),
                interface_name.to_string(),
                package.version().cloned(),
            );

            let interface_full_name = interface_path.to_string();

            let (_, shadow_interface_export_id) = shadow_instance
                .get_export(&mut store, None, &interface_full_name)
                .ok_or_else(|| InstantiatePackageError::InstanceMissingInterfaceExport {
                    interface_name: interface_full_name.to_string(),
                })?;

            let interface_export =
                self.exported_interfaces
                    .get(&interface_path)
                    .ok_or_else(|| InstantiatePackageError::MissingInterfaceExport {
                        path: interface_path.clone(),
                    })?;

            let mut front_instance = linker
                .instance(interface_full_name.as_str())
                .context(instantiate_package_error::LinkerInstanceSnafu)?;

            let interface = &self.types[interface_export.interface];

            for (export_name, export_kind) in &interface.exports {
                let ItemKind::Func(func_id) = export_kind else {
                    continue;
                };

                let (_, shadow_func_export_id) = shadow_instance
                    .get_export(&mut store, Some(&shadow_interface_export_id), export_name)
                    .ok_or_else(
                        || InstantiatePackageError::InstanceMissingInterfaceFuncExport {
                            interface_name: interface_full_name.to_string(),
                            func_name: export_name.to_string(),
                        },
                    )?;

                let shadow_func = shadow_instance
                    .get_func(&mut store, shadow_func_export_id)
                    .ok_or_else(|| InstantiatePackageError::ComponentFuncRetrievalError {
                        interface_name: interface_full_name.to_string(),
                        func_name: export_name.to_string(),
                    })?;

                shadower.shadow_func(
                    &mut front_instance,
                    export_name,
                    shadow_func,
                    interface_path.clone(),
                    self.types[*func_id].clone(),
                    &interface_export.trampoline,
                )?;
            }
        }

        Ok(())
    }
}

impl<D, C: Clone> Index<PackageId> for CompositionGraph<D, C> {
    type Output = Package;

    fn index(&self, index: PackageId) -> &Self::Output {
        let package = self
            .packages
            .get(index.id)
            .expect("package id out of bounds");

        assert_eq!(
            package.nonce, index.nonce,
            "package nonce mismatch for id {index:?}"
        );

        &package.package
    }
}

#[derive(Debug)]
struct PackageWrapper {
    package: Package,
    nonce: usize,
}

impl Deref for PackageWrapper {
    type Target = Package;

    fn deref(&self) -> &Self::Target {
        &self.package
    }
}

trait InstanceShadower<D, C: Clone> {
    fn shadow_func(
        &self,
        instance: &mut LinkerInstance<D>,
        export_name: &str,
        shadow_func: component::Func,
        interface_path: ForeignInterfacePath,
        func_ty: wac_types::FuncType,
        trampoline: &DynInterfaceTrampoline<D, C>,
    ) -> Result<(), InstantiatePackageError>;
}

#[derive(Copy, Clone, Default, Debug)]
struct SyncInstanceShadower;

impl<D: 'static, C: Clone + Send + Sync + 'static> InstanceShadower<D, C> for SyncInstanceShadower {
    fn shadow_func(
        &self,
        instance: &mut LinkerInstance<D>,
        export_name: &str,
        shadow_func: component::Func,
        interface_path: ForeignInterfacePath,
        func_ty: wac_types::FuncType,
        trampoline: &DynInterfaceTrampoline<D, C>,
    ) -> Result<(), InstantiatePackageError> {
        let fn_export_name = Arc::new(export_name.to_string());
        let fn_interface_path = Arc::new(interface_path);
        let fn_ty = Arc::new(func_ty);

        match &trampoline {
            DynInterfaceTrampoline::Sync(trampoline) => {
                let fn_trampoline = trampoline.clone();

                instance
                    .func_new(export_name, move |store, _ty, arguments, result| {
                        let mut result = fn_trampoline.bounce(
                            &shadow_func,
                            store,
                            fn_interface_path.as_ref(),
                            fn_export_name.as_str(),
                            fn_ty.as_ref(),
                            arguments,
                            result,
                        )?;

                        result.post_return()?;

                        Ok(())
                    })
                    .context(instantiate_package_error::LinkFuncInstantiationSnafu)
            }

            DynInterfaceTrampoline::Async(_trampoline) => {
                Err(InstantiatePackageError::InvalidTrampolineSynchronicity)
            }
        }
    }
}

#[derive(Copy, Clone, Default, Debug)]
struct AsyncInstanceShadower;

impl<D: Send + 'static, C: Clone + Send + Sync + 'static> InstanceShadower<D, C>
    for AsyncInstanceShadower
{
    fn shadow_func(
        &self,
        instance: &mut LinkerInstance<D>,
        export_name: &str,
        shadow_func: component::Func,
        interface_path: ForeignInterfacePath,
        func_ty: wac_types::FuncType,
        trampoline: &DynInterfaceTrampoline<D, C>,
    ) -> Result<(), InstantiatePackageError> {
        let fn_export_name = Arc::new(export_name.to_string());
        let fn_interface_path = Arc::new(interface_path);
        let fn_ty = Arc::new(func_ty);

        match &trampoline {
            DynInterfaceTrampoline::Sync(trampoline) => {
                let fn_trampoline = trampoline.clone();

                instance
                    .func_new(export_name, move |store, _ty, arguments, result| {
                        let mut result = fn_trampoline.bounce(
                            &shadow_func,
                            store,
                            fn_interface_path.as_ref(),
                            fn_export_name.as_str(),
                            fn_ty.as_ref(),
                            arguments,
                            result,
                        )?;

                        result.post_return()?;

                        Ok(())
                    })
                    .context(instantiate_package_error::LinkFuncInstantiationSnafu)
            }

            #[cfg(feature = "async")]
            DynInterfaceTrampoline::Async(trampoline) => {
                let fn_trampoline = trampoline.clone();

                instance
                    .func_new_async(export_name, move |store, _ty, arguments, result| {
                        let export_name = fn_export_name.clone();
                        let trampoline = fn_trampoline.clone();
                        let interface_path = fn_interface_path.clone();
                        let ty = fn_ty.clone();

                        Box::new(async move {
                            let mut result = trampoline
                                .bounce_async(
                                    &shadow_func,
                                    store,
                                    interface_path.as_ref(),
                                    export_name.as_str(),
                                    ty.as_ref(),
                                    arguments,
                                    result,
                                )
                                .await?;

                            result.post_return_async().await?;

                            Ok(())
                        })
                    })
                    .context(instantiate_package_error::LinkFuncInstantiationSnafu)
            }
        }
    }
}

/// Represents a unique identifier for a package within the composition graph.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct PackageId {
    id: usize,
    nonce: usize,
}

#[derive(Derivative)]
#[derivative(Debug(bound = ""))]
struct InterfaceExport<D, C: Clone> {
    package: PackageId,
    interface: InterfaceId,

    #[derivative(Debug = "ignore")]
    trampoline: DynInterfaceTrampoline<D, C>,
}

#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum AddPackageError {
    #[snafu(display("Duplicate package: {name}@{version:?}"))]
    DuplicatePackage { name: String, version: Version },

    #[snafu(display("Failed to parse package"))]
    PackageParseError { source: anyhow::Error },

    #[snafu(display("Failed to parse import '{interface}'"))]
    ImportParseError {
        interface: String,
        source: InterfacePathParseError,
    },
}

#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum InstantiateError {
    #[snafu(display("Package id '{id:?}' not found"))]
    PackageNotFound { id: PackageId },

    #[snafu(display("Failed to load package"))]
    LoadPackageError { source: LoadPackageError },

    #[snafu(display("Failed to instantiate package dependency '{name}@{version:?}'"))]
    InstantiatePackageDependencyError {
        name: String,
        version: Option<Version>,
        source: InstantiatePackageError,
    },

    #[snafu(display("Failed to instantiate wasm component"))]
    ComponentInstantiationError { source: anyhow::Error },
}

#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum LoadPackageError {
    #[snafu(display("Package import cycle detected: {cycle:?}"))]
    PackageCycle { cycle: Vec<String> },

    #[snafu(display("Package dependency {package_name} not found"))]
    MissingPackageDependency { package_name: String },

    #[snafu(display("Cannot resolve package version for {name}@{version:?}"))]
    CannotResolvePackageVersion {
        name: String,
        version: Option<Version>,
    },
}

#[derive(Snafu, Debug)]
#[snafu(module)]
pub enum InstantiatePackageError {
    #[snafu(display("Failed to instantiate wasm component"))]
    ComponentInstantiationError { source: anyhow::Error },

    #[snafu(display("Failed to create linker instance"))]
    LinkerInstanceError { source: anyhow::Error },

    #[snafu(display("Instance is missing interface export with name '{interface_name}'"))]
    InstanceMissingInterfaceExport { interface_name: String },

    #[snafu(display(
        "Instance is missing interface func export with name '{interface_name}/{func_name}'",
    ))]
    InstanceMissingInterfaceFuncExport {
        interface_name: String,
        func_name: String,
    },

    #[snafu(display("Failed to retrieve component function '{interface_name}/{func_name}'"))]
    ComponentFuncRetrievalError {
        interface_name: String,
        func_name: String,
    },

    #[snafu(display("Failed to instantiate function"))]
    LinkFuncInstantiationError { source: anyhow::Error },

    #[snafu(display("Invalid trampoline sync/async call match"))]
    InvalidTrampolineSynchronicity,

    #[snafu(display("Missing interface export {path}"))]
    MissingInterfaceExport { path: ForeignInterfacePath },
}