[−][src]Struct wasmer::Function
A WebAssembly function
instance.
A function instance is the runtime representation of a function.
It effectively is a closure of the original function (defined in either
the host or the WebAssembly module) over the runtime Instance
of its
originating Module
.
The module instance is used to resolve references to other definitions during execution of the function.
Spec: https://webassembly.github.io/spec/core/exec/runtime.html#function-instances
Implementations
impl Function
[src]
pub fn new<F>(store: &Store, ty: &FunctionType, func: F) -> Self where
F: Fn(&[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
[src]
F: Fn(&[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Creates a new host Function
(dynamic) with the provided signature.
Example
let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); let f = Function::new(&store, &signature, |args| { let sum = args[0].unwrap_i32() + args[1].unwrap_i32(); Ok(vec![Value::I32(sum)]) });
pub fn new_with_env<F, Env>(
store: &Store,
ty: &FunctionType,
env: Env,
func: F
) -> Self where
F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Env: Sized + 'static,
[src]
store: &Store,
ty: &FunctionType,
env: Env,
func: F
) -> Self where
F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Env: Sized + 'static,
Creates a new host Function
(dynamic) with the provided signature and environment.
Example
struct Env { multiplier: i32, }; let env = Env { multiplier: 2 }; let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); let f = Function::new_with_env(&store, &signature, env, |env, args| { let result = env.multiplier * (args[0].unwrap_i32() + args[1].unwrap_i32()); Ok(vec![Value::I32(result)]) });
pub fn new_native<F, Args, Rets, Env>(store: &Store, func: F) -> Self where
F: HostFunction<Args, Rets, WithoutEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + 'static,
[src]
F: HostFunction<Args, Rets, WithoutEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + 'static,
Creates a new host Function
from a native function.
The function signature is automatically retrieved using the Rust typing system.
Example
fn sum(a: i32, b: i32) -> i32 { a + b } let f = Function::new_native(&store, sum);
pub fn new_native_with_env<F, Args, Rets, Env>(
store: &Store,
env: Env,
func: F
) -> Self where
F: HostFunction<Args, Rets, WithEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + 'static,
[src]
store: &Store,
env: Env,
func: F
) -> Self where
F: HostFunction<Args, Rets, WithEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + 'static,
Creates a new host Function
from a native function and a provided environment.
The function signature is automatically retrieved using the Rust typing system.
Example
struct Env { multiplier: i32, }; let env = Env { multiplier: 2 }; fn sum_and_multiply(env: &mut Env, a: i32, b: i32) -> i32 { (a + b) * env.multiplier } let f = Function::new_native_with_env(&store, env, sum_and_multiply);
pub fn ty(&self) -> &FunctionType
[src]
Returns the FunctionType
of the Function
.
pub fn store(&self) -> &Store
[src]
Returns the Store
where the Function
belongs.
pub fn param_arity(&self) -> usize
[src]
Returns the number of parameters that this function takes.
pub fn result_arity(&self) -> usize
[src]
Returns the number of results this function produces.
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>, RuntimeError>
[src]
Call the Function
function.
Depending on where the Function is defined, it will call it.
- If the function is defined inside a WebAssembly, it will call the trampoline for the function signature.
- If the function is defined in the host (in a native way), it will call the trampoline.
pub fn native<'a, Args, Rets>(
&self
) -> Result<NativeFunc<'a, Args, Rets>, RuntimeError> where
Args: WasmTypeList,
Rets: WasmTypeList,
[src]
&self
) -> Result<NativeFunc<'a, Args, Rets>, RuntimeError> where
Args: WasmTypeList,
Rets: WasmTypeList,
Transform this WebAssembly function into a function with the
native ABI. See NativeFunc
to learn more.
Trait Implementations
impl Clone for Function
[src]
impl Debug for Function
[src]
impl<'a> Exportable<'a> for Function
[src]
fn to_export(&self) -> Export
[src]
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError>
[src]
impl From<Function> for Extern
[src]
impl From<Function> for Val
[src]
impl<'a, Args, Rets> From<NativeFunc<'a, Args, Rets>> for Function where
Args: WasmTypeList,
Rets: WasmTypeList,
[src]
Args: WasmTypeList,
Rets: WasmTypeList,
fn from(other: NativeFunc<'a, Args, Rets>) -> Self
[src]
impl PartialEq<Function> for Function
[src]
impl StructuralPartialEq for Function
[src]
Auto Trait Implementations
impl !RefUnwindSafe for Function
impl Send for Function
impl Sync for Function
impl Unpin for Function
impl !UnwindSafe for Function
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,