Struct wasmedge_sdk::Func

source ·
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, Caller, host_function};

// A native function to be wrapped as a host function
#[host_function]
fn real_add(_: Caller, 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.run(&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

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

Creates an asynchronous 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

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 a reference to the type of the host function.

Returns a reference to this function instance.

Runs this host function and returns the result.

Arguments
  • executor - The Executor instance.

  • args - The arguments passed to the host function.

Error

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

Asynchronously runs this host function and returns the result.

Arguments
  • executor - The Executor instance.

  • args - The arguments passed to the host function.

Error

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

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.