Clean up impl a bit

Isaac Clayton created

Change summary

crates/plugin_runtime/plugin/Cargo.lock             | 128 +-------------
crates/plugin_runtime/plugin/cargo_test/src/main.rs |  20 --
crates/plugin_runtime/src/main.rs                   |  91 +++-------
crates/plugin_runtime/src/wasm.rs                   |  12 
4 files changed, 46 insertions(+), 205 deletions(-)

Detailed changes

crates/plugin_runtime/plugin/Cargo.lock 🔗

@@ -11,47 +11,33 @@ dependencies = [
  "serde",
 ]
 
-[[package]]
-name = "bumpalo"
-version = "3.10.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3"
-
 [[package]]
 name = "cargo_test"
 version = "0.1.0"
 dependencies = [
  "bincode",
- "rust_plugin_macros",
+ "plugin",
  "serde",
- "wasm-bindgen",
 ]
 
 [[package]]
-name = "cfg-if"
-version = "1.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
-
-[[package]]
-name = "itoa"
-version = "1.0.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
-
-[[package]]
-name = "lazy_static"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+name = "plugin"
+version = "0.1.0"
+dependencies = [
+ "bincode",
+ "plugin_macros",
+ "serde",
+]
 
 [[package]]
-name = "log"
-version = "0.4.17"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
+name = "plugin_macros"
+version = "0.1.0"
 dependencies = [
- "cfg-if",
+ "bincode",
+ "proc-macro2",
+ "quote",
+ "serde",
+ "syn",
 ]
 
 [[package]]
@@ -72,40 +58,12 @@ dependencies = [
  "proc-macro2",
 ]
 
-[[package]]
-name = "rust_plugin_macros"
-version = "0.1.0"
-dependencies = [
- "bincode",
- "proc-macro2",
- "quote",
- "serde",
- "syn",
-]
-
-[[package]]
-name = "ryu"
-version = "1.0.10"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
-
 [[package]]
 name = "serde"
 version = "1.0.137"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
 
-[[package]]
-name = "serde_json"
-version = "1.0.81"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
-dependencies = [
- "itoa",
- "ryu",
- "serde",
-]
-
 [[package]]
 name = "syn"
 version = "1.0.96"
@@ -122,59 +80,3 @@ name = "unicode-ident"
 version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
-
-[[package]]
-name = "wasm-bindgen"
-version = "0.2.80"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad"
-dependencies = [
- "cfg-if",
- "serde",
- "serde_json",
- "wasm-bindgen-macro",
-]
-
-[[package]]
-name = "wasm-bindgen-backend"
-version = "0.2.80"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4"
-dependencies = [
- "bumpalo",
- "lazy_static",
- "log",
- "proc-macro2",
- "quote",
- "syn",
- "wasm-bindgen-shared",
-]
-
-[[package]]
-name = "wasm-bindgen-macro"
-version = "0.2.80"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5"
-dependencies = [
- "quote",
- "wasm-bindgen-macro-support",
-]
-
-[[package]]
-name = "wasm-bindgen-macro-support"
-version = "0.2.80"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
- "wasm-bindgen-backend",
- "wasm-bindgen-shared",
-]
-
-[[package]]
-name = "wasm-bindgen-shared"
-version = "0.2.80"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744"

crates/plugin_runtime/plugin/cargo_test/src/main.rs 🔗

@@ -1,25 +1,5 @@
-use core::slice;
 use plugin::prelude::*;
 
-#[no_mangle]
-pub extern "C" fn banana(ptr: *const u8, len: usize) -> *const Buffer {
-    // setup
-    let buffer = Buffer { ptr, len };
-    let data = unsafe { buffer.to_vec() };
-    // operation
-    // let reversed: Vec<u8> = data.into_iter().rev().collect();
-    let number: f64 = bincode::deserialize(&data).unwrap();
-    let new_number = number * 2.0;
-    let new_data = bincode::serialize(&new_number).unwrap();
-    // teardown
-    let new_buffer = unsafe { Buffer::from_vec(new_data) };
-    return new_buffer.leak_to_heap();
-}
-
-pub fn banana2(number: f64) -> f64 {
-    number * 2.0
-}
-
 #[bind]
 pub fn sum_lengths(strings: Vec<String>) -> usize {
     let mut total = 0;

crates/plugin_runtime/src/main.rs 🔗

@@ -2,30 +2,6 @@ use mlua::Lua;
 
 use runner::*;
 
-// pub fn main() -> Result<(), mlua::Error> {
-//     let source = include_str!("../plugin/cargo_test.lua").to_string();
-
-//     let module = LuaPlugin::new(source);
-//     let mut lua: Lua = Runtime::init(module)?;
-//     let runner: TestRunner = lua.as_interface::<TestRunner>().unwrap();
-
-//     println!("extracted interface: {:#?}", &runner);
-
-//     let contents = runner.run_test(&mut lua, "it_works".into());
-
-//     println!("test results:{}", contents.unwrap());
-
-//     Ok(())
-// }
-
-// pub fn main() -> mlua::Result<()> {
-//     let module = LuaPlugin::new(include_str!("../plugin/cargo_test.lua").to_string());
-//     let mut lua: Lua = Runtime::init(module)?;
-//     let runner = lua.as_interface::<TestRunner>().unwrap();
-//     let test_results = runner.run_test(&mut lua, "it_works".into());
-//     Ok(())
-// }
-
 pub fn main() -> anyhow::Result<()> {
     let plugin = WasmPlugin {
         source_bytes: include_bytes!(
@@ -35,55 +11,42 @@ pub fn main() -> anyhow::Result<()> {
         store_data: (),
     };
 
-    let mut wasm: Wasm<()> = Runtime::init(plugin)?;
-    let banana = wasm.as_interface::<Banana>().unwrap();
-    let result = banana.banana(&mut wasm, 420.69);
+    let mut sum = Wasm::init(plugin)?;
+    let strings = "I hope you have a nice day".split(" ").iter().collect();
+    let result = sum.sum_lengths(strings);
 
-    dbg!("{}", result);
+    dbg!(result);
 
     Ok(())
 }
 
-struct Banana {
-    banana: Handle,
-}
-
-impl Interface for Banana {
-    fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
-        let banana = runtime.handle_for("banana")?;
-        Some(Banana { banana })
-    }
-}
+// struct SumLengths {
+//     sum_lengths: Handle,
+// }
 
-impl Banana {
-    fn banana<T: Runtime>(&self, runtime: &mut T, number: f64) -> Option<f64> {
-        runtime.call(&self.banana, number).ok()
-    }
-}
+// impl Interface for SumLengths {
+//     fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
+//         Some(SumLengths {
+//             sum_lengths: runtime.handle_for("sum_lengths")?,
+//         })
+//     }
+// }
 
-#[allow(dead_code)]
-#[derive(Debug)]
-struct TestRunner {
-    pub query: String,
-    run_test: Handle,
-}
+// impl SumLengths {
+//     fn sum_lengths<T: Runtime>(&self, runtime: &mut T, strings: Vec<String>) -> Option<usize> {
+//         runtime.call(&self.sum_lengths, strings).ok()
+//     }
+// }
 
-impl Interface for TestRunner {
-    fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
-        let run_test = runtime.handle_for("run_test")?;
-        let query = runtime.handle_for("query")?;
-        let query: String = runtime.constant(&query).ok()?;
-        Some(TestRunner { query, run_test })
-    }
+// #[plugin::interface]
+trait SumLengths {
+    fn sum_lengths(&mut self, strings: Vec<String>) -> usize;
 }
 
-impl TestRunner {
-    pub fn run_test<T: Runtime>(&self, runtime: &mut T, test_name: String) -> Option<String> {
-        runtime.call(&self.run_test, test_name).ok()
+impl<T: Runtime> SumLengths for T {
+    fn sum_lengths(&mut self, strings: Vec<String>) -> usize {
+        let handle = self.handle_for("sum_lengths").unwrap();
+        let result = self.call(&handle, strings).ok().unwrap();
+        return result;
     }
 }
-
-#[test]
-pub fn it_works() {
-    panic!("huh, that was surprising...");
-}

crates/plugin_runtime/src/wasm.rs 🔗

@@ -135,18 +135,16 @@ impl<S> Runtime for Wasm<S> {
         plugin_memory.write(&mut self.store, arg_buffer_ptr as usize, &arg)?;
 
         // get the webassembly function we want to actually call
+        // TODO: precompute handle
+        let fun_name = format!("__{}", handle.inner());
         let fun = self
             .instance
-            .get_typed_func::<(i32, i32), i32, _>(&mut self.store, handle.inner())?;
+            .get_typed_func::<(i32, i32), i32, _>(&mut self.store, &fun_name)?;
 
         // call the function, passing in the buffer and its length
         // this should return a pointer to a (ptr, lentgh) pair
         let arg_buffer = (arg_buffer_ptr, arg_buffer_len as i32);
         let result_buffer = fun.call(&mut self.store, arg_buffer)?;
-        dbg!(result_buffer);
-
-        // panic!();
-        // dbg!()
 
         // create a buffer to read the (ptr, length) pair into
         // this is a total of 4 + 4 = 8 bytes.
@@ -160,14 +158,12 @@ impl<S> Runtime for Wasm<S> {
         let result_buffer_len = u32::from_le_bytes([b[4], b[5], b[6], b[7]]) as usize;
         let result_buffer_end = result_buffer_ptr + result_buffer_len;
 
-        dbg!(result_buffer_ptr);
-        dbg!(result_buffer_len);
-
         // read the buffer at this point into a byte array
         // deserialize the byte array into the provided serde type
         let result = &plugin_memory.data(&mut self.store)[result_buffer_ptr..result_buffer_end];
         let result = bincode::deserialize(result)?;
 
+        // TODO: this is handled wasm-side, but I'd like to double-check
         // // deallocate the argument buffer
         // self.free_buffer.call(&mut self.store, arg_buffer);