-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.rs
100 lines (89 loc) · 3.44 KB
/
resolve.rs
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
#![allow(warnings)]
use modularity::*;
use std::collections::*;
use std::mem::*;
use wasm_component_layer::*;
const WASM_A: &[u8] = include_bytes!("a/component.wasm");
const WASM_B: &[u8] = include_bytes!("b/component.wasm");
const WASM_C: &[u8] = include_bytes!("c/component.wasm");
const WASM_D: &[u8] = include_bytes!("d/component.wasm");
pub fn main() {
let engine = Engine::new(wasmi::Engine::default());
let mut store = Store::new(&engine, ());
let a = Component::new(&engine, WASM_A).unwrap();
let b = Component::new(&engine, WASM_B).unwrap();
let c = Component::new(&engine, WASM_C).unwrap();
let d = Component::new(&engine, WASM_D).unwrap();
let package_names = [
"test:guest@0.1.0",
"test:guest@0.1.1",
"test:guest2",
"test:guest3",
"test:guest4",
]
.into_iter()
.map(PackageIdentifier::try_from)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let mut resolver = Some(PackageResolver::new(
[package_names[3].clone(), package_names[4].clone()],
&Linker::default(),
));
let packages = package_names
.into_iter()
.zip([a.clone(), a, b, c, d].into_iter())
.collect::<Vec<_>>();
while let Some(r) = take(&mut resolver) {
match r.resolve() {
Ok(x) => {
println!("The image was {x:?}");
let mut ctx = PackageContext::new();
let transition = PackageContextTransitionBuilder::new(&x, &ctx)
.build(&mut store)
.unwrap();
assert!(
transition.apply(&mut store, &mut ctx).is_empty(),
"Errors occurred during transition."
);
println!("The context was {ctx:?}");
let pkg = ctx.package(&"test:guest4".try_into().unwrap()).unwrap();
let select_nth = pkg
.exports()
.instance(&"test:guest4/bar".try_into().unwrap())
.unwrap()
.func("select-nth")
.unwrap()
.typed::<(Vec<String>, u32), (String,)>()
.unwrap();
let example = ["a", "b", "c"]
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>();
println!(
"Calling select-nth({example:?}, 1) == {}",
select_nth.call(&mut store, (example.clone(), 1)).unwrap().0
);
}
Err(PackageResolverError::MissingPackages(x)) => {
let r = resolver.insert(x);
for u in r.unresolved() {
let (id, component) = packages
.iter()
.find(|(id, _)| {
id.name() == u.id().name()
&& u.id()
.version()
.zip(id.version())
.map(|(a, b)| {
a <= b && a.major == b.major && a.minor == b.minor
})
.unwrap_or(id.version().is_none())
})
.unwrap();
u.resolve(id.clone(), component.clone());
}
}
x => panic!("{x:?}"),
}
}
}