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

Creates a normal, wasi, or wasmedge process import object.

Example

This example shows how to create a normal import object that contains a host function, a global variable, a memory and a table. The import object is named “extern”.

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

use wasmedge_sdk::{
    types::Val,
    Global, ImportObjectBuilder, Memory, Table,
    error::HostFuncError, WasmValue, GlobalType,
    MemoryType, Mutability, RefType, TableType,
    ValType, CallingFrame,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // a native function to be imported as host function
    fn real_add(_: &CallingFrame, inputs: Vec<WasmValue>) -> std::result::Result<Vec<WasmValue>, HostFuncError> {
        if inputs.len() != 2 {
            return Err(HostFuncError::User(1));
        }

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

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

        let c = a + b;

        Ok(vec![WasmValue::from_i32(c)])
    }

    // create a Const global instance to be imported
    let global_const = Global::new(
        GlobalType::new(ValType::F32, Mutability::Const),
        Val::F32(3.5),
    )?;

    // create a memory instance to be imported
    let memory = Memory::new(MemoryType::new(10, Some(20), false)?)?;

    // create a table instance to be imported
    let table = Table::new(TableType::new(RefType::FuncRef, 10, Some(20)))?;

    // create an import object
    let module_name = "extern";
    let _import = ImportObjectBuilder::new()
        // add a function
        .with_func::<(i32, i32), i32>("add", real_add)?
        // add a global
        .with_global("global", global_const)?
        // add a memory
        .with_memory("memory", memory)?
        // add a table
        .with_table("table", table)?
        .build(module_name)?;

    Ok(())
}

[Click for more examples]

Implementations

Creates a new ImportObjectBuilder.

Adds a host function to the ImportObject to create.

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

Arguments
  • name - The exported name of the host function to add.

  • real_func - The native function.

error

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

Adds a host function to the ImportObject to create.

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

Arguments
  • name - The exported name of the host function to add.

  • ty - The function type.

  • real_func - The native function.

error

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

Adds a host function to the ImportObject to create.

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
  • name - The exported name of the host function to add.

  • real_func - The native function.

error

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

Adds a global to the ImportObject to create.

Arguments
Error

If fail to create or add the global, then an error is returned.

Adds a memory to the ImportObject to create.

Arguments
Error

If fail to create or add the memory, then an error is returned.

Adds a table to the ImportObject to create.

Arguments
Error

If fail to create or add the table, then an error is returned.

Creates a new ImportObject.

Argument
Error

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

Creates a new wasi import object.

Arguments
  • args - The commandline arguments. The first argument is the program name.

  • envs - The environment variables in the format ENV_VAR_NAME=VALUE.

  • preopens - The directories to pre-open. The required format is DIR1:DIR2.

Error

If fail to create a wasi import module, then an error is returned.

Creates a new wasmedge process import object.

Arguments
  • allowed_cmds - A white list of commands.

  • allowed - Determines if wasmedge_process is allowed to execute all commands on the white list.

Error

If fail to create a wasmedge process import module, then an error is returned.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. 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.