mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-01 23:41:03 +00:00
Merge pull request #1225 from Hywan/feature/polymorphic-v2.1
feat(runtime-core) Allow dynamic signature for polymorphic host functions
This commit is contained in:
commit
644755faec
@ -154,7 +154,7 @@ fn imported_functions_forms(test: &dyn Fn(&Instance)) {
|
|||||||
Ok(n + 1)
|
Ok(n + 1)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
"callback_closure_polymorphic" => Func::<i32, i32, _>::new_polymorphic(
|
"callback_closure_polymorphic" => Func::new_polymorphic(
|
||||||
Arc::new(FuncSig::new(vec![Type::I32], vec![Type::I32])),
|
Arc::new(FuncSig::new(vec![Type::I32], vec![Type::I32])),
|
||||||
|_, params| -> Vec<Value> {
|
|_, params| -> Vec<Value> {
|
||||||
match params[0] {
|
match params[0] {
|
||||||
|
@ -189,9 +189,21 @@ where
|
|||||||
/// Represents a function that can be used by WebAssembly.
|
/// Represents a function that can be used by WebAssembly.
|
||||||
pub struct Func<'a, Args = (), Rets = (), Inner: Kind = Wasm> {
|
pub struct Func<'a, Args = (), Rets = (), Inner: Kind = Wasm> {
|
||||||
inner: Inner,
|
inner: Inner,
|
||||||
|
|
||||||
|
/// The function pointer.
|
||||||
func: NonNull<vm::Func>,
|
func: NonNull<vm::Func>,
|
||||||
|
|
||||||
|
/// The function environment.
|
||||||
func_env: Option<NonNull<vm::FuncEnv>>,
|
func_env: Option<NonNull<vm::FuncEnv>>,
|
||||||
|
|
||||||
|
/// The famous `vm::Ctx`.
|
||||||
vmctx: *mut vm::Ctx,
|
vmctx: *mut vm::Ctx,
|
||||||
|
|
||||||
|
/// The signature is usually infered from `Args` and `Rets`. In
|
||||||
|
/// case of polymorphic function, the signature is only known at
|
||||||
|
/// runtime.
|
||||||
|
signature: Arc<FuncSig>,
|
||||||
|
|
||||||
_phantom: PhantomData<(&'a (), Args, Rets)>,
|
_phantom: PhantomData<(&'a (), Args, Rets)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,6 +226,7 @@ where
|
|||||||
func,
|
func,
|
||||||
func_env,
|
func_env,
|
||||||
vmctx,
|
vmctx,
|
||||||
|
signature: Arc::new(FuncSig::new(Args::types(), Rets::types())),
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,7 +238,7 @@ where
|
|||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
{
|
{
|
||||||
/// Creates a new `Func`.
|
/// Creates a new `Func`.
|
||||||
pub fn new<F, Kind>(func: F) -> Func<'a, Args, Rets, Host>
|
pub fn new<F, Kind>(func: F) -> Self
|
||||||
where
|
where
|
||||||
Kind: ExternalFunctionKind,
|
Kind: ExternalFunctionKind,
|
||||||
F: ExternalFunction<Kind, Args, Rets>,
|
F: ExternalFunction<Kind, Args, Rets>,
|
||||||
@ -237,14 +250,17 @@ where
|
|||||||
func,
|
func,
|
||||||
func_env,
|
func_env,
|
||||||
vmctx: ptr::null_mut(),
|
vmctx: ptr::null_mut(),
|
||||||
|
signature: Arc::new(FuncSig::new(Args::types(), Rets::types())),
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Func<'a, (), (), Host> {
|
||||||
/// Creates a polymorphic function.
|
/// Creates a polymorphic function.
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
#[cfg(all(unix, target_arch = "x86_64"))]
|
#[cfg(all(unix, target_arch = "x86_64"))]
|
||||||
pub fn new_polymorphic<F>(signature: Arc<FuncSig>, func: F) -> Func<'a, Args, Rets, Host>
|
pub fn new_polymorphic<F>(signature: Arc<FuncSig>, func: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut vm::Ctx, &[crate::types::Value]) -> Vec<crate::types::Value> + 'static,
|
F: Fn(&mut vm::Ctx, &[crate::types::Value]) -> Vec<crate::types::Value> + 'static,
|
||||||
{
|
{
|
||||||
@ -254,7 +270,7 @@ where
|
|||||||
|
|
||||||
struct PolymorphicContext {
|
struct PolymorphicContext {
|
||||||
arg_types: Vec<Type>,
|
arg_types: Vec<Type>,
|
||||||
func: Box<Fn(&mut vm::Ctx, &[Value]) -> Vec<Value>>,
|
func: Box<dyn Fn(&mut vm::Ctx, &[Value]) -> Vec<Value>>,
|
||||||
}
|
}
|
||||||
unsafe extern "C" fn enter_host_polymorphic(
|
unsafe extern "C" fn enter_host_polymorphic(
|
||||||
ctx: *const CallContext,
|
ctx: *const CallContext,
|
||||||
@ -305,11 +321,13 @@ where
|
|||||||
let ptr = builder
|
let ptr = builder
|
||||||
.append_global()
|
.append_global()
|
||||||
.expect("cannot bump-allocate global trampoline memory");
|
.expect("cannot bump-allocate global trampoline memory");
|
||||||
|
|
||||||
Func {
|
Func {
|
||||||
inner: Host(()),
|
inner: Host(()),
|
||||||
func: ptr.cast::<vm::Func>(),
|
func: ptr.cast::<vm::Func>(),
|
||||||
func_env: None,
|
func_env: None,
|
||||||
vmctx: ptr::null_mut(),
|
vmctx: ptr::null_mut(),
|
||||||
|
signature,
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -751,12 +769,11 @@ where
|
|||||||
func_env @ Some(_) => Context::ExternalWithEnv(self.vmctx, func_env),
|
func_env @ Some(_) => Context::ExternalWithEnv(self.vmctx, func_env),
|
||||||
None => Context::Internal,
|
None => Context::Internal,
|
||||||
};
|
};
|
||||||
let signature = Arc::new(FuncSig::new(Args::types(), Rets::types()));
|
|
||||||
|
|
||||||
Export::Function {
|
Export::Function {
|
||||||
func,
|
func,
|
||||||
ctx,
|
ctx,
|
||||||
signature,
|
signature: self.signature.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user