[][src]Struct wasmtime::Func

pub struct Func { /* fields omitted */ }

A WebAssembly function which can be called.

This type can represent a number of callable items, such as:

  • An exported function from a WebAssembly module.
  • A user-defined function used to satisfy an import.

These types of callable items are all wrapped up in this Func and can be used to both instantiate an Instance as well as be extracted from an Instance.

Func and Clone

Functions are internally reference counted so you can clone a Func. The cloning process only performs a shallow clone, so two cloned Func instances are equivalent in their functionality.

Methods

impl Func[src]

pub fn new(
    store: &Store,
    ty: FuncType,
    callable: Rc<dyn Callable + 'static>
) -> Self
[src]

Creates a new Func with the given arguments, typically to create a user-defined function to pass as an import to a module.

  • store - a cache of data where information is stored, typically shared with a Module.

  • ty - the signature of this function, used to indicate what the inputs and outputs are, which must be WebAssembly types.

  • callable - a type implementing the Callable trait which is the implementation of this Func value.

Note that the implementation of callable must adhere to the ty signature given, error or traps may occur if it does not respect the ty signature.

pub fn wrap0<F, R>(store: &Store, func: F) -> Func where
    F: Fn() -> R + 'static,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 0 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap1<F, A1, R>(store: &Store, func: F) -> Func where
    F: Fn(A1) -> R + 'static,
    A1: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 1 argument.

This function will create a new Func which, when called, will execute the given Rust closure. Unlike Func::new the target function being called is known statically so the type signature can be inferred. Rust types will map to WebAssembly types as follows:

Rust Argument TypeWebAssembly Type
i32i32
i64i64
f32f32
f64f64
(not supported)v128
(not supported)anyref

Any of the Rust types can be returned from the closure as well, in addition to some extra types

Rust Return TypeWebAssembly Return TypeMeaning
()nothingno return value
Result<T, Trap>Tfunction may trap

Note that when using this API (and the related wrap* family of functions), the intention is to create as thin of a layer as possible for when WebAssembly calls the function provided. With sufficient inlining and optimization the WebAssembly will call straight into func provided, with no extra fluff entailed.

pub fn wrap2<F, A1, A2, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 2 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap3<F, A1, A2, A3, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2, A3) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 3 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap4<F, A1, A2, A3, A4, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2, A3, A4) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 4 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap5<F, A1, A2, A3, A4, A5, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2, A3, A4, A5) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 5 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap6<F, A1, A2, A3, A4, A5, A6, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2, A3, A4, A5, A6) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 6 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap7<F, A1, A2, A3, A4, A5, A6, A7, R>(store: &Store, func: F) -> Func where
    F: Fn(A1, A2, A3, A4, A5, A6, A7) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 7 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap8<F, A1, A2, A3, A4, A5, A6, A7, A8, R>(
    store: &Store,
    func: F
) -> Func where
    F: Fn(A1, A2, A3, A4, A5, A6, A7, A8) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 8 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap9<F, A1, A2, A3, A4, A5, A6, A7, A8, A9, R>(
    store: &Store,
    func: F
) -> Func where
    F: Fn(A1, A2, A3, A4, A5, A6, A7, A8, A9) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    A9: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 9 arguments.

For more information about this function, see Func::wrap1.

pub fn wrap10<F, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>(
    store: &Store,
    func: F
) -> Func where
    F: Fn(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) -> R + 'static,
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    A9: WasmTy,
    A10: WasmTy,
    R: WasmRet
[src]

Creates a new Func from the given Rust closure, which takes 10 arguments.

For more information about this function, see Func::wrap1.

pub fn ty(&self) -> &FuncType[src]

Returns the underlying wasm type that this Func has.

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]>, Trap>[src]

Invokes this function with the params given, returning the results and any trap, if one occurs.

The params here must match the type signature of this Func, or a trap will occur. If a trap occurs while executing this function, then a trap will also be returned.

This function should not panic unless the underlying function itself initiates a panic.

pub fn get0<R>(&self) -> Result<impl Fn() -> Result<R, Trap>> where
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get1<A1, R>(&self) -> Result<impl Fn(A1) -> Result<R, Trap>> where
    A1: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

This function serves as an optimized version of the Func::call method if the type signature of a function is statically known to the program. This method is faster than call on a few metrics:

  • Runtime type-checking only happens once, when this method is called.
  • The result values, if any, aren't boxed into a vector.
  • Arguments and return values don't go through boxing and unboxing.
  • No trampolines are used to transfer control flow to/from JIT code, instead this function jumps directly into JIT code.

For more information about which Rust types match up to which wasm types, see the documentation on Func::wrap1.

Return

This function will return None if the type signature asserted statically does not match the runtime type signature. Some, however, will be returned if the underlying function takes one parameter of type A and returns the parameter R. Currently R can either be () (no return values) or one wasm type. At this time a multi-value return isn't supported.

The returned closure will always return a Result<R, Trap> and an Err is returned if a trap happens while the wasm is executing.

pub fn get2<A1, A2, R>(&self) -> Result<impl Fn(A1, A2) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get3<A1, A2, A3, R>(
    &self
) -> Result<impl Fn(A1, A2, A3) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get4<A1, A2, A3, A4, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get5<A1, A2, A3, A4, A5, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get6<A1, A2, A3, A4, A5, A6, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5, A6) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get7<A1, A2, A3, A4, A5, A6, A7, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5, A6, A7) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get8<A1, A2, A3, A4, A5, A6, A7, A8, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5, A6, A7, A8) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5, A6, A7, A8, A9) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    A9: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

pub fn get10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>(
    &self
) -> Result<impl Fn(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) -> Result<R, Trap>> where
    A1: WasmTy,
    A2: WasmTy,
    A3: WasmTy,
    A4: WasmTy,
    A5: WasmTy,
    A6: WasmTy,
    A7: WasmTy,
    A8: WasmTy,
    A9: WasmTy,
    A10: WasmTy,
    R: WasmTy
[src]

Extracts a natively-callable object from this Func, if the signature matches.

See the Func::get1 method for more documentation.

Trait Implementations

impl Clone for Func[src]

impl Debug for Func[src]

impl From<Func> for Extern[src]

impl From<Func> for Val[src]

Auto Trait Implementations

impl !RefUnwindSafe for Func

impl !Send for Func

impl !Sync for Func

impl Unpin for Func

impl !UnwindSafe for Func

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.