Struct wasmedge_sdk::Vm

source ·
pub struct Vm<'inst, T: ?Sized + SyncInst> { /* 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.

use std::collections::HashMap;
use wasmedge_sdk::{params, Store, Module, WasmVal, wat2wasm, ValType, NeverType, Vm, vm::SyncInst};

// create a Vm context
let mut vm =
    Vm::new(Store::new(None, HashMap::<String, &mut dyn SyncInst>::new()).unwrap());
// register a wasm module from the given in-memory wasm bytes
// load wasm module
let result = wat2wasm(
    br#"(module
    (export "fib" (func $fib))
    (func $fib (param $n i32) (result i32)
     (if
      (i32.lt_s
       (local.get $n)
       (i32.const 2)
      )
      (then
        (return i32.const 1)
      )
     )
     (return
      (i32.add
       (call $fib
        (i32.sub
         (local.get $n)
         (i32.const 2)
        )
       )
       (call $fib
        (i32.sub
         (local.get $n)
         (i32.const 1)
        )
       )
      )
     )
    )
   )
"#,
);
assert!(result.is_ok());
let wasm_bytes = result.unwrap();
// run `fib` function from the wasm bytes
let fib_module = Module::from_bytes(None, wasm_bytes).unwrap();
vm.register_module(None, fib_module).unwrap();
let result = vm.run_func(None, "fib", params!(10i32));
assert!(result.is_ok());
let returns = result.unwrap();
assert_eq!(returns.len(), 1);
assert_eq!(returns[0].to_i32(), 89);

Implementations§

source§

impl<'inst, T: ?Sized + SyncInst> Vm<'inst, T>

source

pub fn new(store: Store<'inst, T>) -> Self

source

pub fn register_module( &mut self, mod_name: Option<&str>, module: Module ) -> WasmEdgeResult<&mut Self>

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.

source

pub fn run_func( &mut self, mod_name: Option<&str>, func_name: impl AsRef<str>, args: impl IntoIterator<Item = WasmValue> ) -> WasmEdgeResult<Vec<WasmValue>>

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.

source

pub fn run_func_with_timeout( &mut self, mod_name: Option<&str>, func_name: impl AsRef<str>, args: impl IntoIterator<Item = WasmValue>, timeout: Duration ) -> WasmEdgeResult<Vec<WasmValue>>

Runs an exported wasm function in a (named or active) module instance with a timeout setting

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

  • timeout - The maximum execution time of the function to be run.

§Error

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

source

pub fn store(&self) -> &Store<'inst, T>

Returns a reference to the internal store from this vm.

source

pub fn store_mut(&mut self) -> &mut Store<'inst, T>

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

source

pub fn active_module(&self) -> Option<&Instance>

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.

source

pub fn active_module_mut(&mut self) -> Option<&mut Instance>

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.

source

pub fn contains_module(&self, mod_name: impl AsRef<str>) -> bool

Checks if the vm contains a named module instance.

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

pub fn named_instance_count(&self) -> usize

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

source

pub fn instance_names(&self) -> Vec<String>

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

Trait Implementations§

source§

impl<'inst, T: Debug + ?Sized + SyncInst> Debug for Vm<'inst, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'inst, T> Freeze for Vm<'inst, T>
where T: ?Sized,

§

impl<'inst, T> RefUnwindSafe for Vm<'inst, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'inst, T> Send for Vm<'inst, T>
where T: Send + ?Sized,

§

impl<'inst, T> Sync for Vm<'inst, T>
where T: Sync + ?Sized,

§

impl<'inst, T> Unpin for Vm<'inst, T>
where T: ?Sized,

§

impl<'inst, T> !UnwindSafe for Vm<'inst, T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.