diff --git a/lib/api/src/externals.rs b/lib/api/src/externals.rs index 18629f329..df962d6f6 100644 --- a/lib/api/src/externals.rs +++ b/lib/api/src/externals.rs @@ -6,12 +6,15 @@ use crate::Mutability; use crate::RuntimeError; use crate::{ExternType, FuncType, GlobalType, MemoryType, TableType, ValType}; use std::cmp::max; +use std::marker::PhantomData; use std::slice; -use wasm_common::{Bytes, HostFunction, Pages, ValueType, WasmTypeList, WithEnv, WithoutEnv}; +use wasm_common::{ + Bytes, HostFunction, Pages, ValueType, WasmExternType, WasmTypeList, WithEnv, WithoutEnv, +}; use wasmer_runtime::{ - wasmer_call_trampoline, Export, ExportFunction, ExportGlobal, ExportMemory, ExportTable, - LinearMemory, Table as RuntimeTable, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, - VMGlobalDefinition, VMMemoryDefinition, VMTrampoline, + catch_traps, wasmer_call_trampoline, Export, ExportFunction, ExportGlobal, ExportMemory, + ExportTable, LinearMemory, Table as RuntimeTable, VMCallerCheckedAnyfunc, VMContext, + VMFunctionBody, VMGlobalDefinition, VMMemoryDefinition, VMTrampoline, }; #[derive(Clone)] @@ -662,6 +665,10 @@ impl Func { Ok(results.into_boxed_slice()) } + // pub fn native(&self) -> NativeFunc { + // NativeFunc::from_export(self.exported.clone()) + // } + pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Func { let trampoline = store.engine().trampoline(wasmer_export.signature).unwrap(); Func { diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 048e8913c..46601340d 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -1,4 +1,5 @@ //! Wasmer API +#![feature(trace_macros)] #![deny(intra_doc_link_resolution_failure)] mod exports; @@ -7,6 +8,7 @@ mod import_object; mod instance; mod memory_view; mod module; +mod native; mod ptr; mod store; mod tunables; diff --git a/lib/api/src/native.rs b/lib/api/src/native.rs new file mode 100644 index 000000000..e2e37fc39 --- /dev/null +++ b/lib/api/src/native.rs @@ -0,0 +1,114 @@ +// Native Funcs +// use wasmer_runtime::ExportFunction; +use std::marker::PhantomData; +use wasm_common::{WasmExternType, WasmTypeList}; +// pub trait NativeHostFunction +// where +// Args: WasmTypeList, +// Rets: WasmTypeList, +// { +// } + +#[derive(Clone)] +pub struct UnprovidedArgs; +#[derive(Clone)] +pub struct UnprovidedRets; + +pub struct NativeFunc<'a, Args = UnprovidedArgs, Rets = UnprovidedRets> { + // exported: ExportFunction, + _phantom: PhantomData<(&'a (), Args, Rets)>, +} + +unsafe impl<'a, Args, Rets> Send for NativeFunc<'a, Args, Rets> {} + +impl<'a, Args, Rets> NativeFunc<'a, Args, Rets> { + fn from_export() -> Self { + Self { + // exported, + _phantom: PhantomData, + } + } +} + +// #[allow (unused_parens)] +// impl <'a, A1, Rets > NativeFunc <'a, (A1,), Rets > +// where +// A1 : WasmExternType, +// Rets : WasmTypeList, +// { +// /// Call the typed func and return results. +// pub fn calla(&self, A1:A1,) -> Result { +// unimplemented!(""); +// } +// } + +// #[allow (unused_parens)] +// impl <'a, A1, A2, Rets > NativeFunc <'a, (A1, A2), Rets > +// where +// A1 : WasmExternType, +// A2 : WasmExternType, +// Rets : WasmTypeList, +// { +// /// Call the typed func and return results. +// pub fn calla(&self, A1:A1, A2: A2,) -> Result { +// unimplemented!(""); +// } +// } + +macro_rules! impl_native_traits { + ( $( $x:ident ),* ) => { + #[allow(unused_parens)] + impl<'a $( , $x )*, Rets> NativeFunc<'a, ( $( $x ),* ), Rets> + where + $( $x: WasmExternType, )* + Rets: WasmTypeList, + { + /// Call the typed func and return results. + pub fn call(&self, $( $x: $x, )* ) -> Result { + trace_macros!(false); + unimplemented!(""); + trace_macros!(true); + } + } + // impl<'a $( , $x )*, Rets> NativeFunc<'a, ( $( $x ),* ), Rets> + // where + // $( $x: WasmExternType, )* + // Rets: WasmTypeList, + // { + // /// Call the typed func and return results. + // #[allow(non_snake_case, clippy::too_many_arguments)] + // pub fn call(&self, $( $x: $x, )* ) -> Result { + // unimplemented!(""); + // // use std::mem; + // // use std::cell::RefCell; + // // let vmctx = self.exported.vmctx; + // // let callee_address = self.exported.address; + // // #[allow(unused_parens)] + // // unsafe { + // // let result: RefCell> = RefCell::new(None); + // // catch_traps(vmctx, || { + // // let func_result = mem::transmute::< + // // *const VMFunctionBody, + // // unsafe extern "C" fn(*mut VMContext, *mut VMContext, $( $x ),* ) -> Rets::CStruct, + // // >(callee_address)(vmctx, std::ptr::null_mut() as *mut VMContext, $( $x ),* ); + + // // *result.borrow_mut() = Some(func_result); + // // }); + // // Ok(result.into_inner().unwrap()) + // // } + // } + // } + + + }; +} + +trace_macros!(true); + +// impl_native_traits!(); +impl_native_traits!(A1); +impl_native_traits!(A1, A2); + +trace_macros!(false); + +// impl_native_traits!(A1, A2, A3); diff --git a/lib/wasm-common/src/lib.rs b/lib/wasm-common/src/lib.rs index 1c660aa8f..8ff6ec90e 100644 --- a/lib/wasm-common/src/lib.rs +++ b/lib/wasm-common/src/lib.rs @@ -4,9 +4,9 @@ //! This crate provides common structures such as `Type` or `Value`, type indexes //! and native function wrappers with `Func`. +#![feature(trace_macros)] #![deny(missing_docs, unused_extern_crates)] #![warn(unused_import_braces)] -#![cfg_attr(feature = "std", deny(unstable_features))] #![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))] #![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))] #![cfg_attr( diff --git a/lib/wasm-common/src/native.rs b/lib/wasm-common/src/native.rs index 325057033..be668853e 100644 --- a/lib/wasm-common/src/native.rs +++ b/lib/wasm-common/src/native.rs @@ -705,3 +705,107 @@ mod test_func { // assert_eq!(result.is_err(), true); } } + +#[derive(Clone)] +pub struct UnprovidedArgs; +#[derive(Clone)] +pub struct UnprovidedRets; + +pub struct NativeFunc<'a, Args = UnprovidedArgs, Rets = UnprovidedRets> { + // exported: ExportFunction, + _phantom: PhantomData<(&'a (), Args, Rets)>, +} + +unsafe impl<'a, Args, Rets> Send for NativeFunc<'a, Args, Rets> {} + +impl<'a, Args, Rets> NativeFunc<'a, Args, Rets> { + fn from_export() -> Self { + Self { + // exported, + _phantom: PhantomData, + } + } +} + +// #[allow (unused_parens)] +// impl <'a, A1, Rets > NativeFunc <'a, (A1,), Rets > +// where +// A1 : WasmExternType, +// Rets : WasmTypeList, +// { +// /// Call the typed func and return results. +// pub fn calla(&self, A1:A1,) -> Result { +// unimplemented!(""); +// } +// } + +// #[allow (unused_parens)] +// impl <'a, A1, A2, Rets > NativeFunc <'a, (A1, A2), Rets > +// where +// A1 : WasmExternType, +// A2 : WasmExternType, +// Rets : WasmTypeList, +// { +// /// Call the typed func and return results. +// pub fn calla(&self, A1:A1, A2: A2,) -> Result { +// unimplemented!(""); +// } +// } + +macro_rules! impl_native_traits { + ( $( $x:ident ),* ) => { + #[allow(unused_parens)] + impl<'a $( , $x )*, Rets> NativeFunc<'a, ( $( $x ),* ), Rets> + where + $( $x: WasmExternType, )* + Rets: WasmTypeList, + { + /// Call the typed func and return results. + pub fn call(&self, $( $x: $x, )* ) -> Result { + trace_macros!(false); + unimplemented!(""); + trace_macros!(true); + } + } + // impl<'a $( , $x )*, Rets> NativeFunc<'a, ( $( $x ),* ), Rets> + // where + // $( $x: WasmExternType, )* + // Rets: WasmTypeList, + // { + // /// Call the typed func and return results. + // #[allow(non_snake_case, clippy::too_many_arguments)] + // pub fn call(&self, $( $x: $x, )* ) -> Result { + // unimplemented!(""); + // // use std::mem; + // // use std::cell::RefCell; + // // let vmctx = self.exported.vmctx; + // // let callee_address = self.exported.address; + // // #[allow(unused_parens)] + // // unsafe { + // // let result: RefCell> = RefCell::new(None); + // // catch_traps(vmctx, || { + // // let func_result = mem::transmute::< + // // *const VMFunctionBody, + // // unsafe extern "C" fn(*mut VMContext, *mut VMContext, $( $x ),* ) -> Rets::CStruct, + // // >(callee_address)(vmctx, std::ptr::null_mut() as *mut VMContext, $( $x ),* ); + + // // *result.borrow_mut() = Some(func_result); + // // }); + // // Ok(result.into_inner().unwrap()) + // // } + // } + // } + + + }; +} + +trace_macros!(true); + +// impl_native_traits!(); +impl_native_traits!(A1); +impl_native_traits!(A1, A2); + +trace_macros!(false); + +// impl_native_traits!(A1, A2, A3);