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            half_async: WasiFn<u32, u32>,
 22            echo_async: WasiFn<String, String>,
 23        }
 24
 25        async {
 26            let mut runtime = PluginBuilder::new_with_default_ctx()
 27                .unwrap()
 28                .host_function("mystery_number", |input: u32| input + 7)
 29                .unwrap()
 30                .host_function("import_noop", |_: ()| ())
 31                .unwrap()
 32                .host_function("import_identity", |input: u32| input)
 33                .unwrap()
 34                .host_function("import_swap", |(a, b): (u32, u32)| (b, a))
 35                .unwrap()
 36                .host_function_async("import_half", |a: u32| async move { a / 2 })
 37                .unwrap()
 38                .host_function_async("command_async", |command: String| async move {
 39                    // TODO: actual thing
 40                    dbg!(&command);
 41                    let mut args = command.split(' ');
 42                    let command = args.next().unwrap();
 43                    smol::process::Command::new(command)
 44                        .args(args)
 45                        .output()
 46                        .await
 47                        .ok()
 48                        .map(|output| {
 49                            dbg!("Did run command!");
 50                            output.stdout
 51                        })
 52                })
 53                .unwrap()
 54                .init(
 55                    false,
 56                    include_bytes!("../../../plugins/bin/test_plugin.wasm"),
 57                )
 58                .await
 59                .unwrap();
 60
 61            let plugin = TestPlugin {
 62                noop: runtime.function("noop").unwrap(),
 63                constant: runtime.function("constant").unwrap(),
 64                identity: runtime.function("identity").unwrap(),
 65                add: runtime.function("add").unwrap(),
 66                swap: runtime.function("swap").unwrap(),
 67                sort: runtime.function("sort").unwrap(),
 68                print: runtime.function("print").unwrap(),
 69                and_back: runtime.function("and_back").unwrap(),
 70                imports: runtime.function("imports").unwrap(),
 71                half_async: runtime.function("half_async").unwrap(),
 72                echo_async: runtime.function("echo_async").unwrap(),
 73            };
 74
 75            let unsorted = vec![1, 3, 4, 2, 5];
 76            let sorted = vec![1, 2, 3, 4, 5];
 77
 78            assert_eq!(runtime.call(&plugin.noop, ()).await.unwrap(), ());
 79            assert_eq!(runtime.call(&plugin.constant, ()).await.unwrap(), 27);
 80            assert_eq!(runtime.call(&plugin.identity, 58).await.unwrap(), 58);
 81            assert_eq!(runtime.call(&plugin.add, (3, 4)).await.unwrap(), 7);
 82            assert_eq!(runtime.call(&plugin.swap, (1, 2)).await.unwrap(), (2, 1));
 83            assert_eq!(runtime.call(&plugin.sort, unsorted).await.unwrap(), sorted);
 84            assert_eq!(runtime.call(&plugin.print, "Hi!".into()).await.unwrap(), ());
 85            assert_eq!(runtime.call(&plugin.and_back, 1).await.unwrap(), 8);
 86            assert_eq!(runtime.call(&plugin.imports, 1).await.unwrap(), 8);
 87            assert_eq!(runtime.call(&plugin.half_async, 4).await.unwrap(), 2);
 88            assert_eq!(
 89                runtime
 90                    .call(&plugin.echo_async, "eko".into())
 91                    .await
 92                    .unwrap(),
 93                "eko\n"
 94            );
 95
 96            // dbg!("{}", runtime.call(&plugin.and_back, 1).await.unwrap());
 97        }
 98        .block_on()
 99    }
100}