lib.rs

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