pub struct Func(/* private fields */);
Expand description
Newtype wrapper around wasmer::Function
.
Implementations§
Source§impl Func
impl Func
Sourcepub fn new(inner: Function) -> Self
pub fn new(inner: Function) -> Self
Create a wasm_runtime_layer::Func
-compatible Func
from a wasmer::Function
.
Sourcepub fn into_inner(self) -> Function
pub fn into_inner(self) -> Function
Consume a Func
to obtain the inner wasmer::Function
.
Methods from Deref<Target = Function>§
Sourcepub fn ty(&self, store: &impl AsStoreRef) -> FunctionType
pub fn ty(&self, store: &impl AsStoreRef) -> FunctionType
Returns the FunctionType
of the Function
.
§Example
fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 {
a + b
}
let f = Function::new_typed_with_env(&mut store, &env, sum);
assert_eq!(f.ty(&mut store).params(), vec![Type::I32, Type::I32]);
assert_eq!(f.ty(&mut store).results(), vec![Type::I32]);
Sourcepub fn param_arity(&self, store: &impl AsStoreRef) -> usize
pub fn param_arity(&self, store: &impl AsStoreRef) -> usize
Returns the number of parameters that this function takes.
§Example
fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 {
a + b
}
let f = Function::new_typed_with_env(&mut store, &env, sum);
assert_eq!(f.param_arity(&mut store), 2);
Sourcepub fn result_arity(&self, store: &impl AsStoreRef) -> usize
pub fn result_arity(&self, store: &impl AsStoreRef) -> usize
Returns the number of results this function produces.
§Example
fn sum(_env: FunctionEnvMut<()>, a: i32, b: i32) -> i32 {
a + b
}
let f = Function::new_typed_with_env(&mut store, &env, sum);
assert_eq!(f.result_arity(&mut store), 1);
Sourcepub fn call(
&self,
store: &mut impl AsStoreMut,
params: &[Value],
) -> Result<Box<[Value]>, RuntimeError>
pub fn call( &self, store: &mut impl AsStoreMut, params: &[Value], ) -> Result<Box<[Value]>, RuntimeError>
Call the 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.
§Examples
let sum = instance.exports.get_function("sum").unwrap();
assert_eq!(sum.call(&mut store, &[Value::I32(1), Value::I32(2)]).unwrap().to_vec(), vec![Value::I32(3)]);
Sourcepub fn typed<Args, Rets>(
&self,
store: &impl AsStoreRef,
) -> Result<TypedFunction<Args, Rets>, RuntimeError>where
Args: WasmTypeList,
Rets: WasmTypeList,
pub fn typed<Args, Rets>(
&self,
store: &impl AsStoreRef,
) -> Result<TypedFunction<Args, Rets>, RuntimeError>where
Args: WasmTypeList,
Rets: WasmTypeList,
Transform this WebAssembly function into a typed function.
See TypedFunction
to learn more.
§Examples
let sum = instance.exports.get_function("sum").unwrap();
let sum_typed: TypedFunction<(i32, i32), i32> = sum.typed(&mut store).unwrap();
assert_eq!(sum_typed.call(&mut store, 1, 2).unwrap(), 3);
§Errors
If the Args
generic parameter does not match the exported function
an error will be raised:
let sum = instance.exports.get_function("sum").unwrap();
// This results in an error: `RuntimeError`
let sum_typed : TypedFunction<(i64, i64), i32> = sum.typed(&mut store).unwrap();
If the Rets
generic parameter does not match the exported function
an error will be raised:
let sum = instance.exports.get_function("sum").unwrap();
// This results in an error: `RuntimeError`
let sum_typed: TypedFunction<(i32, i32), i64> = sum.typed(&mut store).unwrap();
Sourcepub fn is_from_store(&self, store: &impl AsStoreRef) -> bool
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool
Checks whether this Function
can be used with the given store.
Sourcepub fn as_sys(&self) -> &Function
pub fn as_sys(&self) -> &Function
Convert a reference to self
into a reference to crate::backend::sys::function::Function
.
Sourcepub fn as_sys_mut(&mut self) -> &mut Function
pub fn as_sys_mut(&mut self) -> &mut Function
Convert a mutable reference to self
into a mutable reference crate::backend::sys::function::Function
.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Func
impl RefUnwindSafe for Func
impl Send for Func
impl Sync for Func
impl Unpin for Func
impl UnwindSafe for Func
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.