main.rs

 1use mlua::Lua;
 2
 3use runner::*;
 4
 5pub fn main() -> anyhow::Result<()> {
 6    let plugin = WasmPlugin {
 7        source_bytes: include_bytes!(
 8            "../plugin/target/wasm32-unknown-unknown/release/cargo_test.wasm"
 9        )
10        .to_vec(),
11        store_data: (),
12    };
13
14    let mut sum = Wasm::init(plugin)?;
15    let strings = "I hope you have a nice day".split(" ").iter().collect();
16    let result = sum.sum_lengths(strings);
17
18    dbg!(result);
19
20    Ok(())
21}
22
23// struct SumLengths {
24//     sum_lengths: Handle,
25// }
26
27// impl Interface for SumLengths {
28//     fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
29//         Some(SumLengths {
30//             sum_lengths: runtime.handle_for("sum_lengths")?,
31//         })
32//     }
33// }
34
35// impl SumLengths {
36//     fn sum_lengths<T: Runtime>(&self, runtime: &mut T, strings: Vec<String>) -> Option<usize> {
37//         runtime.call(&self.sum_lengths, strings).ok()
38//     }
39// }
40
41// #[plugin::interface]
42trait SumLengths {
43    fn sum_lengths(&mut self, strings: Vec<String>) -> usize;
44}
45
46impl<T: Runtime> SumLengths for T {
47    fn sum_lengths(&mut self, strings: Vec<String>) -> usize {
48        let handle = self.handle_for("sum_lengths").unwrap();
49        let result = self.call(&handle, strings).ok().unwrap();
50        return result;
51    }
52}