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        }
21
22        async {
23            let mut runtime = WasiPluginBuilder::new_with_default_ctx()
24                .unwrap()
25                .host_function("mystery_number", |input: u32| input + 7)
26                .unwrap()
27                .init(include_bytes!("../../../plugins/bin/test_plugin.wasm"))
28                .await
29                .unwrap();
30
31            let plugin = TestPlugin {
32                noop: runtime.function("noop").unwrap(),
33                constant: runtime.function("constant").unwrap(),
34                identity: runtime.function("identity").unwrap(),
35                add: runtime.function("add").unwrap(),
36                swap: runtime.function("swap").unwrap(),
37                sort: runtime.function("sort").unwrap(),
38                print: runtime.function("print").unwrap(),
39                // and_back: runtime.function("and_back").unwrap(),
40            };
41
42            let unsorted = vec![1, 3, 4, 2, 5];
43            let sorted = vec![1, 2, 3, 4, 5];
44
45            assert_eq!(runtime.call(&plugin.noop, ()).await.unwrap(), ());
46            assert_eq!(runtime.call(&plugin.constant, ()).await.unwrap(), 27);
47            assert_eq!(runtime.call(&plugin.identity, 58).await.unwrap(), 58);
48            assert_eq!(runtime.call(&plugin.add, (3, 4)).await.unwrap(), 7);
49            assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
50            assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
51            assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
52            // assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
53        }
54        .block_on()
55    }
56}