Clean up impl a bit

This commit is contained in:
Isaac Clayton 2022-06-03 11:18:13 +02:00
parent f6a9558c5c
commit 0cf64d6fba
4 changed files with 51 additions and 210 deletions

View file

@ -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,
// struct SumLengths {
// sum_lengths: Handle,
// }
// impl Interface for SumLengths {
// fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
// Some(SumLengths {
// sum_lengths: runtime.handle_for("sum_lengths")?,
// })
// }
// }
// impl SumLengths {
// fn sum_lengths<T: Runtime>(&self, runtime: &mut T, strings: Vec<String>) -> Option<usize> {
// runtime.call(&self.sum_lengths, strings).ok()
// }
// }
// #[plugin::interface]
trait SumLengths {
fn sum_lengths(&mut self, strings: Vec<String>) -> usize;
}
impl Interface for Banana {
fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
let banana = runtime.handle_for("banana")?;
Some(Banana { banana })
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;
}
}
impl Banana {
fn banana<T: Runtime>(&self, runtime: &mut T, number: f64) -> Option<f64> {
runtime.call(&self.banana, number).ok()
}
}
#[allow(dead_code)]
#[derive(Debug)]
struct TestRunner {
pub query: String,
run_test: Handle,
}
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 })
}
}
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()
}
}
#[test]
pub fn it_works() {
panic!("huh, that was surprising...");
}

View file

@ -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);