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>
impl<'inst, T: ?Sized + SyncInst> Vm<'inst, T>
pub fn new(store: Store<'inst, T>) -> Self
Sourcepub fn register_module(
&mut self,
mod_name: Option<&str>,
module: Module,
) -> WasmEdgeResult<&mut Self>
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. IfNone
, 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.
Sourcepub fn run_func(
&mut self,
mod_name: Option<&str>,
func_name: impl AsRef<str>,
args: impl IntoIterator<Item = WasmValue>,
) -> WasmEdgeResult<Vec<WasmValue>>
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. IfNone
, 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.
Sourcepub 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>>
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. IfNone
, 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.
Sourcepub fn store(&self) -> &Store<'inst, T>
pub fn store(&self) -> &Store<'inst, T>
Returns a reference to the internal store from this vm.
Sourcepub fn store_mut(&mut self) -> &mut Store<'inst, T>
pub fn store_mut(&mut self) -> &mut Store<'inst, T>
Returns a mutable reference to the internal store from this vm.
Sourcepub fn active_module(&self) -> Option<&Instance>
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.
Sourcepub fn active_module_mut(&mut self) -> Option<&mut Instance>
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.
Sourcepub fn contains_module(&self, mod_name: impl AsRef<str>) -> bool
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.
Sourcepub fn named_instance_count(&self) -> usize
pub fn named_instance_count(&self) -> usize
Returns the count of the named module instances this vm holds.
Sourcepub fn instance_names(&self) -> Vec<String>
pub fn instance_names(&self) -> Vec<String>
Returns the names of all named module instances this vm holds.