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

A Vm defines a virtual environment for managing WebAssembly programs.

Example

The example below presents how to register a module as named module in a Vm instance and run a target wasm function.

// 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::{params, Vm, WasmVal};
use wasmedge_types::{wat2wasm, ValType};

#[cfg_attr(test, test)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // create a Vm context
    let vm = Vm::new(None)?;

    // register a wasm module from the given in-memory wasm bytes
    let wasm_bytes = wat2wasm(
        br#"(module
        (export "fib" (func $fib))
        (func $fib (param $n i32) (result i32)
         (if
          (i32.lt_s
           (get_local $n)
           (i32.const 2)
          )
          (return
           (i32.const 1)
          )
         )
         (return
          (i32.add
           (call $fib
            (i32.sub
             (get_local $n)
             (i32.const 2)
            )
           )
           (call $fib
            (i32.sub
             (get_local $n)
             (i32.const 1)
            )
           )
          )
         )
        )
       )
    "#,
    )?;
    let mut vm = vm.register_module_from_bytes("extern", wasm_bytes)?;

    // get func type of `fib`
    let func_ty = vm.func_ty(Some("extern"), "fib")?;

    // get the argument types
    assert_eq!(func_ty.args_len(), 1);
    let args = func_ty.args().expect("No argument types.");
    assert_eq!(args, [ValType::I32]);

    // get the return types
    assert_eq!(func_ty.returns_len(), 1);
    let returns = func_ty.returns().expect("No return types.");
    assert_eq!(returns, [ValType::I32]);

    // run `fib` function in the named module instance
    let returns = vm.run_func(Some("extern"), "fib", params!(10))?;
    assert_eq!(returns.len(), 1);
    assert_eq!(returns[0].to_i32(), 89);

    Ok(())
}

Implementations

Creates a new Vm to be associated with the given configuration.

Arguments
Error

If fail to create, then an error is returned.

Registers a WASM module into the vm from a wasm file, and instantiates it.

Arguments
  • mod_name - The name for the WASM module to be registered.

  • file - The target WASM file.

Error

If fail to register the target WASM, then an error is returned.

Registers a WASM module from then given in-memory wasm bytes into the Vm, and instantiates it.

Arguments
  • mod_name - The name of the WASM module to be registered.

  • bytes - The in-memory wasm bytes.

Error

If fail to register the WASM module, then an error is returned.

Registers and instantiates a WasmEdge import object into this vm.

Arguments
Error

If fail to register the given import object, then an error is returned.

Registers and instantiates a WasmEdge compiled module into this vm as a named or active module instance.

Arguments
  • mod_name - The exported name of the registered module. If None, then the module is registered as an active module instance.

  • module - The validated module to be registered.

Error

If fail to register the given module, then an error is returned.

Resets the Vm.

Runs an exported WASM function registered in a named or active module.

Arguments
  • mod_name - The name of the module instance, which holds the target exported function. If None, then the active module is used.

  • func_name - The name of the exported WASM function to run.

  • params - The parameter values passed to the exported WASM function.

Error

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

Returns the type of a WASM function.

Arguments
  • mod_name - The name of the module instance, which holds the target function. if None, then the active module is used.

  • func_name - The name of the target function.

Error

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

Invokes an exported function from the given wasm file.

Arguments
  • file - The WASM file.

  • func_name - The name of the target exported function to run.

  • args - The arguments passed to the target exported function.

Error

If fail to run, then an error is returned.

Invokes an exported function from the given in-memory wasm bytes.

Arguments
  • bytes - The in-memory wasm bytes.

  • func_name - The name of the target exported function to run.

  • args - The arguments passed to the target exported function.

Error

If fail to run, then an error is returned.

Invokes an exported function from the given compiled module.

Arguments
  • module - A compiled module.

  • func_name - The name of the target exported function to run.

  • args - The arguments passed to the target exported function.

Error

If fail to run, then an error is returned.

Returns the count of the named module instances in this store.

Error

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

Returns the names of all registered named module instances.

Error

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

Returns the named module instance with the given name.

Argument
Error

If fail to get the named module instance, then an error is returned.

Returns the active module instance.

Error

If fail to get the active module instance, then an error is returned.

Returns the internal statistics instance from the Vm.

Returns the Wasi module instance.

Notice that this function is only available when a config with the enabled wasi option is used in the creation of this Vm.

Error

If fail to get the Wasi module instance, then an error is returned.

Returns the mutable WasmEdgeProcess module instance.

Notice that this function is only available when a config with the enabled wasmedge_process option is used in the creation of this Vm.

Error

If fail to get the WasmEdgeProcess module instance, then an error is returned.

Checks if the vm contains a named module instance.

Argument
  • mod_name - The registered module’s name to check.

Trait Implementations

Formats the value using the given formatter. Read more

Runs a host function instance and returns the results. Read more

Runs a host function instance by calling its reference and returns the results. 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.