Rename WasiPlugin -> Plugin, etc.
This commit is contained in:
parent
31e3a4d208
commit
018fd46901
2 changed files with 30 additions and 43 deletions
|
@ -1,10 +1,10 @@
|
||||||
pub mod wasi;
|
pub mod plugin;
|
||||||
use pollster::FutureExt as _;
|
pub use plugin::*;
|
||||||
pub use wasi::*;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use pollster::FutureExt as _;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_plugin() {
|
pub fn test_plugin() {
|
||||||
|
@ -20,14 +20,12 @@ mod tests {
|
||||||
imports: WasiFn<u32, u32>,
|
imports: WasiFn<u32, u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn half(a: u32) -> u32 {
|
// async fn half(a: u32) -> u32 {
|
||||||
a / 2
|
// a / 2
|
||||||
}
|
// }
|
||||||
|
|
||||||
let x = half;
|
|
||||||
|
|
||||||
async {
|
async {
|
||||||
let mut runtime = WasiPluginBuilder::new_with_default_ctx()
|
let mut runtime = PluginBuilder::new_with_default_ctx()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.host_function("mystery_number", |input: u32| input + 7)
|
.host_function("mystery_number", |input: u32| input + 7)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -1,19 +1,17 @@
|
||||||
use std::{
|
use std::{fs::File, marker::PhantomData, path::Path};
|
||||||
collections::HashMap, fs::File, future::Future, marker::PhantomData, path::Path, pin::Pin,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
use wasi_common::{dir, file};
|
use wasi_common::{dir, file};
|
||||||
|
use wasmtime::Memory;
|
||||||
use wasmtime::{
|
use wasmtime::{
|
||||||
AsContext, AsContextMut, Caller, Config, Engine, Extern, Instance, Linker, Module, Store,
|
AsContext, AsContextMut, Caller, Config, Engine, Extern, Instance, Linker, Module, Store, Trap,
|
||||||
StoreContext, StoreContextMut, Trap, TypedFunc, WasmParams,
|
TypedFunc,
|
||||||
};
|
};
|
||||||
use wasmtime::{IntoFunc, Memory};
|
|
||||||
use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
|
use wasmtime_wasi::{Dir, WasiCtx, WasiCtxBuilder};
|
||||||
|
|
||||||
pub struct WasiResource(u32);
|
pub struct PluginResource(u32);
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct WasiBuffer {
|
struct WasiBuffer {
|
||||||
|
@ -50,20 +48,20 @@ impl<A: Serialize, R: DeserializeOwned> Clone for WasiFn<A, R> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WasiPluginBuilder {
|
pub struct PluginBuilder {
|
||||||
wasi_ctx: WasiCtx,
|
wasi_ctx: WasiCtx,
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
linker: Linker<WasiCtxAlloc>,
|
linker: Linker<WasiCtxAlloc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasiPluginBuilder {
|
impl PluginBuilder {
|
||||||
pub fn new(wasi_ctx: WasiCtx) -> Result<Self, Error> {
|
pub fn new(wasi_ctx: WasiCtx) -> Result<Self, Error> {
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
config.async_support(true);
|
config.async_support(true);
|
||||||
let engine = Engine::new(&config)?;
|
let engine = Engine::new(&config)?;
|
||||||
let linker = Linker::new(&engine);
|
let linker = Linker::new(&engine);
|
||||||
|
|
||||||
Ok(WasiPluginBuilder {
|
Ok(PluginBuilder {
|
||||||
// host_functions: HashMap::new(),
|
// host_functions: HashMap::new(),
|
||||||
wasi_ctx,
|
wasi_ctx,
|
||||||
engine,
|
engine,
|
||||||
|
@ -241,13 +239,13 @@ impl WasiPluginBuilder {
|
||||||
let buffer = WasiBuffer::from_u64(packed_buffer);
|
let buffer = WasiBuffer::from_u64(packed_buffer);
|
||||||
|
|
||||||
// get the args passed from Guest
|
// 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
|
// Call the Host-side function
|
||||||
let result: R = function(args);
|
let result: R = function(args);
|
||||||
|
|
||||||
// Serialize the result back to guest
|
// 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")
|
Trap::new("Could not serialize value returned from function")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
@ -257,9 +255,10 @@ impl WasiPluginBuilder {
|
||||||
Box::new(async move {
|
Box::new(async move {
|
||||||
let (buffer, mut plugin_memory, result) = result?;
|
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(),
|
caller.data().alloc_buffer(),
|
||||||
&mut plugin_memory,
|
&mut plugin_memory,
|
||||||
&mut caller,
|
&mut caller,
|
||||||
|
@ -274,21 +273,11 @@ impl WasiPluginBuilder {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init<T: AsRef<[u8]>>(self, module: T) -> Result<Wasi, Error> {
|
pub async fn init<T: AsRef<[u8]>>(self, module: T) -> Result<Plugin, Error> {
|
||||||
Wasi::init(module.as_ref().to_vec(), self).await
|
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<u8>,
|
|
||||||
// pub wasi_ctx: WasiCtx,
|
|
||||||
// pub host_functions:
|
|
||||||
// HashMap<String, Box<dyn Fn(&str, &mut Linker<WasiCtx>) -> Result<(), Error>>>,
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
struct WasiAlloc {
|
struct WasiAlloc {
|
||||||
alloc_buffer: TypedFunc<u32, u32>,
|
alloc_buffer: TypedFunc<u32, u32>,
|
||||||
|
@ -318,14 +307,14 @@ impl WasiCtxAlloc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Wasi {
|
pub struct Plugin {
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
module: Module,
|
module: Module,
|
||||||
store: Store<WasiCtxAlloc>,
|
store: Store<WasiCtxAlloc>,
|
||||||
instance: Instance,
|
instance: Instance,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wasi {
|
impl Plugin {
|
||||||
pub fn dump_memory(data: &[u8]) {
|
pub fn dump_memory(data: &[u8]) {
|
||||||
for (i, byte) in data.iter().enumerate() {
|
for (i, byte) in data.iter().enumerate() {
|
||||||
if i % 32 == 0 {
|
if i % 32 == 0 {
|
||||||
|
@ -344,8 +333,8 @@ impl Wasi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wasi {
|
impl Plugin {
|
||||||
async fn init(module: Vec<u8>, plugin: WasiPluginBuilder) -> Result<Self, Error> {
|
async fn init(module: Vec<u8>, plugin: PluginBuilder) -> Result<Self, Error> {
|
||||||
// initialize the WebAssembly System Interface context
|
// initialize the WebAssembly System Interface context
|
||||||
let engine = plugin.engine;
|
let engine = plugin.engine;
|
||||||
let mut linker = plugin.linker;
|
let mut linker = plugin.linker;
|
||||||
|
@ -375,7 +364,7 @@ impl Wasi {
|
||||||
free_buffer,
|
free_buffer,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Wasi {
|
Ok(Plugin {
|
||||||
engine,
|
engine,
|
||||||
module,
|
module,
|
||||||
store,
|
store,
|
||||||
|
@ -385,7 +374,7 @@ impl Wasi {
|
||||||
|
|
||||||
/// Attaches a file or directory the the given system path to the runtime.
|
/// 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.
|
/// Note that the resource must be freed by calling `remove_resource` afterwards.
|
||||||
pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<WasiResource, Error> {
|
pub fn attach_path<T: AsRef<Path>>(&mut self, path: T) -> Result<PluginResource, Error> {
|
||||||
// grab the WASI context
|
// grab the WASI context
|
||||||
let ctx = self.store.data_mut();
|
let ctx = self.store.data_mut();
|
||||||
|
|
||||||
|
@ -404,11 +393,11 @@ impl Wasi {
|
||||||
// return a handle to the resource
|
// return a handle to the resource
|
||||||
ctx.wasi_ctx
|
ctx.wasi_ctx
|
||||||
.insert_dir(fd, dir, caps, file_caps, path.as_ref().to_path_buf());
|
.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.
|
/// 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
|
self.store
|
||||||
.data_mut()
|
.data_mut()
|
||||||
.wasi_ctx
|
.wasi_ctx
|
Loading…
Add table
Add a link
Reference in a new issue