Skip to content

Commit

Permalink
Cleanup code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Nov 21, 2022
1 parent 14c6674 commit 2548221
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 66 deletions.
30 changes: 8 additions & 22 deletions utils/ayaka-plugin-wasmer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl WasmerModule {
impl RawModule for WasmerModule {
type Linker = WasmerStoreLinker;

type Func = WasmerFunc;
type Func = Function;

fn call<T>(&self, name: &str, data: &[u8], f: impl FnOnce(&[u8]) -> Result<T>) -> Result<T> {
let memory = &self.memory;
Expand Down Expand Up @@ -145,45 +145,31 @@ impl StoreLinker<WasmerModule> for WasmerStoreLinker {
Ok(host)
}

fn import(&mut self, ns: impl Into<String>, funcs: HashMap<String, WasmerFunc>) -> Result<()> {
fn import(&mut self, ns: impl Into<String>, funcs: HashMap<String, Function>) -> Result<()> {
let mut import_object = ImportObject::new();
let mut namespace = Exports::new();
for (name, func) in funcs {
namespace.insert(name, func.into_raw());
namespace.insert(name, func);
}
import_object.register(ns, namespace);
self.imports.push(import_object);
Ok(())
}

fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> WasmerFunc {
WasmerFunc::new(Function::new_native(&self.store, f))
fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> Function {
Function::new_native(&self.store, f)
}

fn wrap_with_args_raw(
&self,
f: impl (Fn(*const [u8]) -> Result<()>) + Send + Sync + 'static,
) -> WasmerFunc {
WasmerFunc::new(Function::new_native_with_env(
) -> Function {
Function::new_native_with_env(
&self.store,
RuntimeInstanceData::default(),
move |env_data: &RuntimeInstanceData, len: i32, data: i32| unsafe {
env_data.import(len, data, &f)
},
))
}
}

pub struct WasmerFunc {
func: Function,
}

impl WasmerFunc {
pub(crate) fn new(func: Function) -> Self {
Self { func }
}

pub fn into_raw(self) -> Function {
self.func
)
}
}
30 changes: 8 additions & 22 deletions utils/ayaka-plugin-wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl WasmiModule {
impl RawModule for WasmiModule {
type Linker = WasmiStoreLinker;

type Func = WasmiFunc;
type Func = Func;

fn call<T>(&self, name: &str, data: &[u8], f: impl FnOnce(&[u8]) -> Result<T>) -> Result<T> {
let memory = &self.memory;
Expand Down Expand Up @@ -200,45 +200,31 @@ impl StoreLinker<WasmiModule> for WasmiStoreLinker {
fn import(
&mut self,
ns: impl Into<String>,
funcs: std::collections::HashMap<String, WasmiFunc>,
funcs: std::collections::HashMap<String, Func>,
) -> Result<()> {
let ns = ns.into();
for (name, func) in funcs {
self.linker.define(&ns, &name, func.into_raw())?;
self.linker.define(&ns, &name, func)?;
}
Ok(())
}

fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> WasmiFunc {
WasmiFunc::new(Func::wrap(self.store.lock().unwrap().as_context_mut(), f))
fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> Func {
Func::wrap(self.store.lock().unwrap().as_context_mut(), f)
}

fn wrap_with_args_raw(
&self,
f: impl (Fn(*const [u8]) -> Result<()>) + Send + Sync + 'static,
) -> WasmiFunc {
WasmiFunc::new(Func::wrap(
) -> Func {
Func::wrap(
self.store.lock().unwrap().as_context_mut(),
move |store: Caller<WasiCtx>, len: i32, data: i32| unsafe {
let memory = store.get_export("memory").unwrap().into_memory().unwrap();
let data = mem_slice(store.as_context(), &memory, data, len);
f(data).map_err(|e| Trap::new(e.to_string()))?;
Ok(())
},
))
}
}

pub struct WasmiFunc {
func: Func,
}

impl WasmiFunc {
pub(crate) fn new(func: Func) -> Self {
Self { func }
}

pub fn into_raw(self) -> Func {
self.func
)
}
}
30 changes: 8 additions & 22 deletions utils/ayaka-plugin-wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl WasmtimeModule {
impl RawModule for WasmtimeModule {
type Linker = WasmtimeStoreLinker;

type Func = WasmtimeFunc;
type Func = Func;

fn call<T>(&self, name: &str, data: &[u8], f: impl FnOnce(&[u8]) -> Result<T>) -> Result<T> {
let memory = &self.memory;
Expand Down Expand Up @@ -188,45 +188,31 @@ impl StoreLinker<WasmtimeModule> for WasmtimeStoreLinker {
fn import(
&mut self,
ns: impl Into<String>,
funcs: std::collections::HashMap<String, WasmtimeFunc>,
funcs: std::collections::HashMap<String, Func>,
) -> Result<()> {
let ns = ns.into();
for (name, func) in funcs {
self.linker.define(&ns, &name, func.into_raw())?;
self.linker.define(&ns, &name, func)?;
}
Ok(())
}

fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> WasmtimeFunc {
WasmtimeFunc::new(Func::wrap(self.store.lock().unwrap().as_context_mut(), f))
fn wrap(&self, f: impl Fn() + Send + Sync + 'static) -> Func {
Func::wrap(self.store.lock().unwrap().as_context_mut(), f)
}

fn wrap_with_args_raw(
&self,
f: impl (Fn(*const [u8]) -> Result<()>) + Send + Sync + 'static,
) -> WasmtimeFunc {
WasmtimeFunc::new(Func::wrap(
) -> Func {
Func::wrap(
self.store.lock().unwrap().as_context_mut(),
move |mut store: Caller<WasiCtx>, len: i32, data: i32| unsafe {
let memory = store.get_export("memory").unwrap().into_memory().unwrap();
let data = mem_slice(store.as_context(), &memory, data, len);
f(data).map_err(|e| Trap::new(e.to_string()))?;
Ok(())
},
))
}
}

pub struct WasmtimeFunc {
func: Func,
}

impl WasmtimeFunc {
pub(crate) fn new(func: Func) -> Self {
Self { func }
}

pub fn into_raw(self) -> Func {
self.func
)
}
}

0 comments on commit 2548221

Please sign in to comment.