logo
pub struct Func { /* private fields */ }
Expand description

Defines a host function instance.

A WasmEdge Func is a wasm function instance, which is a “wrapper” of the original function (defined in either the host or the WebAssembly module) over the runtime module instance of its originating module.

Example

The following example shows how to create a host function, and invoke it with a given executor.

// If the version of rust used is less than v1.63,
// #![feature(explicit_generic_args_with_impl_trait)]

use wasmedge_sdk::{Func, Executor, params, WasmVal, error::HostFuncError, WasmValue, ValType, CallingFrame};

// A native function to be wrapped as a host function
fn real_add(_: &CallingFrame, input: Vec<WasmValue>) -> Result<Vec<WasmValue>, HostFuncError> {
    if input.len() != 2 {
        return Err(HostFuncError::User(1));
    }

    let a = if input[0].ty() == ValType::I32 {
        input[0].to_i32()
    } else {
        return Err(HostFuncError::User(2));
    };

    let b = if input[1].ty() == ValType::I32 {
        input[1].to_i32()
    } else {
        return Err(HostFuncError::User(3));
    };

    let c = a + b;
    Ok(vec![WasmValue::from_i32(c)])
}

// create a host function
let result = Func::wrap::<(i32, i32), i32>(real_add);
assert!(result.is_ok());
let func = result.unwrap();

// create an executor
let mut executor = Executor::new(None, None).unwrap();

// call the host function
let result = func.call(&mut executor, params!(2, 3));
assert!(result.is_ok());
let returns = result.unwrap();
assert_eq!(returns[0].to_i32(), 5);

[Click for more examples]

Implementations

Creates a host function of the given func type.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • ty - The function type.

  • real_func - The native function that will be wrapped as a host function.

Error

If fail to create the host function, then an error is returned.

Creates a host function by wrapping a native function.

N.B. that this function can be used in thread-safe scenarios.

Arguments
  • real_func - The native function to be wrapped.
Error

If fail to create the host function, then an error is returned.

Creates a host function by wrapping a native function.

N.B. that this function is used for single-threaded scenarios. If you would like to use hostfunc call chaining design, you should use this method to create a Func instance.

Arguments
  • real_func - The native function to be wrapped.
Error

If fail to create the host function, then an error is returned.

Returns the exported name of this function.

Notice that this field is meaningful only if this host function is used as an exported instance.

Returns the name of the module instance from which this function exports.

Notice that this field is meaningful only if this host function is used as an exported instance.

Returns the type of the host function.

If fail to get the signature, then an error is returned.

Returns a reference to this function instance.

Runs this host function and returns the result.

Arguments
  • engine - The object implementing the Engine trait.

  • args - The arguments passed to the host function.

Error

If fail to run the host function, then an error is returned.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.