From 018fd46901f1cc9f42737403a6dc955a1b8e5e14 Mon Sep 17 00:00:00 2001 From: Isaac Clayton Date: Mon, 13 Jun 2022 12:57:30 +0200 Subject: [PATCH] Rename WasiPlugin -> Plugin, etc. --- crates/plugin_runtime/src/lib.rs | 16 +++--- .../plugin_runtime/src/{wasi.rs => plugin.rs} | 57 ++++++++----------- 2 files changed, 30 insertions(+), 43 deletions(-) rename crates/plugin_runtime/src/{wasi.rs => plugin.rs} (93%) diff --git a/crates/plugin_runtime/src/lib.rs b/crates/plugin_runtime/src/lib.rs index afbcfd9278..81d7ba7e84 100644 --- a/crates/plugin_runtime/src/lib.rs +++ b/crates/plugin_runtime/src/lib.rs @@ -1,10 +1,10 @@ -pub mod wasi; -use pollster::FutureExt as _; -pub use wasi::*; +pub mod plugin; +pub use plugin::*; #[cfg(test)] mod tests { use super::*; + use pollster::FutureExt as _; #[test] pub fn test_plugin() { @@ -20,14 +20,12 @@ mod tests { imports: WasiFn, } - async fn half(a: u32) -> u32 { - a / 2 - } - - let x = half; + // async fn half(a: u32) -> u32 { + // a / 2 + // } async { - let mut runtime = WasiPluginBuilder::new_with_default_ctx() + let mut runtime = PluginBuilder::new_with_default_ctx() .unwrap() .host_function("mystery_number", |input: u32| input + 7) .unwrap() diff --git a/crates/plugin_runtime/src/wasi.rs b/crates/plugin_runtime/src/plugin.rs similarity index 93% rename from crates/plugin_runtime/src/wasi.rs rename to crates/plugin_runtime/src/plugin.rs index 595162c7fd..98d9c2341b 100644 --- a/crates/plugin_runtime/src/wasi.rs +++ b/crates/plugin_runtime/src/plugin.rs @@ -1,19 +1,17 @@ -use std::{ - collections::HashMap, fs::File, future::Future, marker::PhantomData, path::Path, pin::Pin, -}; +use std::{fs::File, marker::PhantomData, path::Path}; use anyhow::{anyhow, Error}; use serde::{de::DeserializeOwned, Serialize}; use wasi_common::{dir, file}; +use wasmtime::Memory; use wasmtime::{ - AsContext, AsContextMut, Caller, Config, Engine, Extern, Instance, Linker, Module, Store, - StoreContext, StoreContextMut, Trap, TypedFunc, WasmParams, + AsContext, AsContextMut, Caller, Config, Engine, Extern, Instance, Linker, Module, Store, Trap, + TypedFunc, }; -use wasmtime::{IntoFunc, Memory}; use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder}; -pub struct WasiResource(u32); +pub struct PluginResource(u32); #[repr(C)] struct WasiBuffer { @@ -50,20 +48,20 @@ impl Clone for WasiFn { } } -pub struct WasiPluginBuilder { +pub struct PluginBuilder { wasi_ctx: WasiCtx, engine: Engine, linker: Linker, } -impl WasiPluginBuilder { +impl PluginBuilder { pub fn new(wasi_ctx: WasiCtx) -> Result { let mut config = Config::default(); config.async_support(true); let engine = Engine::new(&config)?; let linker = Linker::new(&engine); - Ok(WasiPluginBuilder { + Ok(PluginBuilder { // host_functions: HashMap::new(), wasi_ctx, engine, @@ -241,13 +239,13 @@ impl WasiPluginBuilder { let buffer = WasiBuffer::from_u64(packed_buffer); // get the args passed from Guest - let args = Wasi::buffer_to_type(&mut plugin_memory, &mut caller, &buffer)?; + let args = Plugin::buffer_to_type(&mut plugin_memory, &mut caller, &buffer)?; // Call the Host-side function let result: R = function(args); // Serialize the result back to guest - let result = Wasi::serialize_to_bytes(result).map_err(|_| { + let result = Plugin::serialize_to_bytes(result).map_err(|_| { Trap::new("Could not serialize value returned from function") })?; @@ -257,9 +255,10 @@ impl WasiPluginBuilder { Box::new(async move { let (buffer, mut plugin_memory, result) = result?; - Wasi::buffer_to_free(caller.data().free_buffer(), &mut caller, buffer).await?; + Plugin::buffer_to_free(caller.data().free_buffer(), &mut caller, buffer) + .await?; - let buffer = Wasi::bytes_to_buffer( + let buffer = Plugin::bytes_to_buffer( caller.data().alloc_buffer(), &mut plugin_memory, &mut caller, @@ -274,21 +273,11 @@ impl WasiPluginBuilder { Ok(self) } - pub async fn init>(self, module: T) -> Result { - Wasi::init(module.as_ref().to_vec(), self).await + pub async fn init>(self, module: T) -> Result { + Plugin::init(module.as_ref().to_vec(), self).await } } -// // TODO: remove -// /// Represents a to-be-initialized plugin. -// /// Please use [`WasiPluginBuilder`], don't use this directly. -// pub struct WasiPlugin { -// pub module: Vec, -// pub wasi_ctx: WasiCtx, -// pub host_functions: -// HashMap) -> Result<(), Error>>>, -// } - #[derive(Copy, Clone)] struct WasiAlloc { alloc_buffer: TypedFunc, @@ -318,14 +307,14 @@ impl WasiCtxAlloc { } } -pub struct Wasi { +pub struct Plugin { engine: Engine, module: Module, store: Store, instance: Instance, } -impl Wasi { +impl Plugin { pub fn dump_memory(data: &[u8]) { for (i, byte) in data.iter().enumerate() { if i % 32 == 0 { @@ -344,8 +333,8 @@ impl Wasi { } } -impl Wasi { - async fn init(module: Vec, plugin: WasiPluginBuilder) -> Result { +impl Plugin { + async fn init(module: Vec, plugin: PluginBuilder) -> Result { // initialize the WebAssembly System Interface context let engine = plugin.engine; let mut linker = plugin.linker; @@ -375,7 +364,7 @@ impl Wasi { free_buffer, }); - Ok(Wasi { + Ok(Plugin { engine, module, store, @@ -385,7 +374,7 @@ impl Wasi { /// Attaches a file or directory the the given system path to the runtime. /// Note that the resource must be freed by calling `remove_resource` afterwards. - pub fn attach_path>(&mut self, path: T) -> Result { + pub fn attach_path>(&mut self, path: T) -> Result { // grab the WASI context let ctx = self.store.data_mut(); @@ -404,11 +393,11 @@ impl Wasi { // return a handle to the resource ctx.wasi_ctx .insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf()); - Ok(WasiResource(fd)) + Ok(PluginResource(fd)) } /// Returns `true` if the resource existed and was removed. - pub fn remove_resource(&mut self, resource: WasiResource) -> Result<(), Error> { + pub fn remove_resource(&mut self, resource: PluginResource) -> Result<(), Error> { self.store .data_mut() .wasi_ctx