lib.rs

 1pub mod wasi;
 2use pollster::FutureExt as _;
 3pub use wasi::*;
 4
 5#[cfg(test)]
 6mod tests {
 7    use super::*;
 8
 9    #[test]
10    pub fn test_plugin() {
11        pub struct TestPlugin {
12            noop: WasiFn<(), ()>,
13            constant: WasiFn<(), u32>,
14            identity: WasiFn<u32, u32>,
15            add: WasiFn<(u32, u32), u32>,
16            swap: WasiFn<(u32, u32), (u32, u32)>,
17            sort: WasiFn<Vec<u32>, Vec<u32>>,
18            print: WasiFn<String, ()>,
19            and_back: WasiFn<u32, u32>,
20            imports: WasiFn<u32, u32>,
21        }
22
23        async fn half(a: u32) -> u32 {
24            a / 2
25        }
26
27        let x = half;
28
29        async {
30            let mut runtime = WasiPluginBuilder::new_with_default_ctx()
31                .unwrap()
32                .host_function("mystery_number", |input: u32| input + 7)
33                .unwrap()
34                .host_function("import_noop", |_: ()| ())
35                .unwrap()
36                .host_function("import_identity", |input: u32| input)
37                .unwrap()
38                .host_function("import_swap", |(a, b): (u32, u32)| (b, a))
39                .unwrap()
40                // .host_function_async("import_half", half)
41                // .unwrap()
42                .init(include_bytes!("../../../plugins/bin/test_plugin.wasm"))
43                .await
44                .unwrap();
45
46            let plugin = TestPlugin {
47                noop: runtime.function("noop").unwrap(),
48                constant: runtime.function("constant").unwrap(),
49                identity: runtime.function("identity").unwrap(),
50                add: runtime.function("add").unwrap(),
51                swap: runtime.function("swap").unwrap(),
52                sort: runtime.function("sort").unwrap(),
53                print: runtime.function("print").unwrap(),
54                and_back: runtime.function("and_back").unwrap(),
55                imports: runtime.function("imports").unwrap(),
56            };
57
58            let unsorted = vec![1, 3, 4, 2, 5];
59            let sorted = vec![1, 2, 3, 4, 5];
60
61            assert_eq!(runtime.call(&plugin.noop, ()).await.unwrap(), ());
62            assert_eq!(runtime.call(&plugin.constant, ()).await.unwrap(), 27);
63            assert_eq!(runtime.call(&plugin.identity, 58).await.unwrap(), 58);
64            assert_eq!(runtime.call(&plugin.add, (3, 4)).await.unwrap(), 7);
65            assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
66            assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
67            assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
68            assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
69            assert_eq!(runtime.call(&plugin.imports, 1).await.unwrap(), 8);
70
71            // dbg!("{}", runtime.call(&plugin.and_back, 1).await.unwrap());
72        }
73        .block_on()
74    }
75}