Struct wasmedge_sdk::Vm

source ·
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, VmBuilder, WasmVal};
use wasmedge_types::{wat2wasm, ValType};

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

    // 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)?;

    // 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§

Registers a wasm module into this vm as a named or active module instance.

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

  • module - The module to be registered.

Error

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

Registers a wasm module into the vm from a wasm file.

Arguments
  • mod_name - The exported name for the registered module.

  • file - A wasm file or an AOT wasm file.

Error

If fail to register, then an error is returned.

Registers a wasm module from the given in-memory wasm bytes into this vm.

Arguments
  • mod_name - The exported name for the registered module.

  • bytes - The in-memory wasm bytes.

Error

If fail to register, then an error is returned.

Registers an import object into this vm.

Arguments
  • import - The import object to be registered.
Error

If fail to register, then an error is returned.

Auto detect all plugins in WASMEDGE_PLUGIN_PATH

Error

If fail to register plugin instance, then an error is returned.

Runs an exported wasm function in a (named or active) module instance.

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

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

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

Asynchronously runs an exported wasm function in a (named or active) module instance.

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

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

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

Runs an exported wasm function from the given wasm module.

This method is a shortcut of calling register_module and run_func in sequence.

Arguments
  • module - A wasm module.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

Runs an exported wasm function from the given wasm module.

To use this method, turn on the async feature.

Arguments
  • module - A wasm module.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

Runs an exported wasm function from the given wasm file.

Arguments
  • file - A wasm file or an AOT wasm file.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

Asynchronously runs an exported wasm function from the given wasm file.

Arguments
  • file - A wasm file or an AOT wasm file.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

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

Arguments
  • bytes - The in-memory wasm bytes.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

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

Arguments
  • bytes - The in-memory wasm bytes.

  • func_name - The exported name of the target wasm function.

  • args - The arguments to be passed to the target wasm function.

Error

If fail to run, then an error is returned.

Returns a reference to the internal statistics from this vm.

Returns a mutable reference to the internal statistics from this vm.

Returns a reference to the internal executor from this vm.

Returns a mutable reference to the internal executor from this vm.

Returns a reference to the internal store from this vm.

Returns a mutable reference to the internal store from this vm.

Returns a reference to the wasi module instance from this vm.

To retrieve the [wasi module instance], a config with the enabled wasi option should be given when create this vm.

Returns a mutable reference to the wasi module instance from this vm.

To retrieve the [wasi module instance], a config with the enabled wasi option should be given when create this vm.

Returns a reference to the named module instance with the given name from this vm.

Argument
  • name - The exported name of the target module instance.
Error

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

Returns a mutable reference to the named module instance with the given name.

Argument
  • name - The exported name of the target module instance.
Error

If fail to get the mutable reference to the target module instance, then an error is returned.

Returns a reference to the active module instance from this vm.

Error

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

Returns a mutable reference to the active module instance from this vm.

Error

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

Checks if the vm contains a named module instance.

Argument
  • mod_name - The exported name of the target module instance.

Returns the count of the named module instances this vm holds.

Returns the names of all named module instances this vm holds.

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.