pub struct RuntimeInstance<'b, T: Config = ()> {
pub store: Store<'b, T>,
}Fields§
§store: Store<'b, T>Implementations§
Source§impl<'b, T: Config> RuntimeInstance<'b, T>
impl<'b, T: Config> RuntimeInstance<'b, T>
pub fn new(user_data: T) -> Self
pub fn new_with_default_module( user_data: T, validation_info: &ValidationInfo<'b>, ) -> Result<(Self, ModuleAddr), RuntimeError>
pub fn new_named( user_data: T, module_name: &str, validation_info: &ValidationInfo<'b>, ) -> Result<(Self, ModuleAddr), RuntimeError>
pub fn add_module( &mut self, module_name: &str, validation_info: &ValidationInfo<'b>, ) -> Result<ModuleAddr, RuntimeError>
pub fn get_function_by_name( &self, module_name: &str, function_name: &str, ) -> Result<FunctionRef, RuntimeError>
pub fn get_function_by_index( &self, module_addr: ModuleAddr, function_idx: usize, ) -> Result<FunctionRef, RuntimeError>
Sourcepub fn invoke_typed<Params: InteropValueList, Returns: InteropValueList>(
&mut self,
function_ref: &FunctionRef,
params: Params,
) -> Result<Returns, RuntimeError>
pub fn invoke_typed<Params: InteropValueList, Returns: InteropValueList>( &mut self, function_ref: &FunctionRef, params: Params, ) -> Result<Returns, RuntimeError>
Invokes a function with the given parameters of type Param, and return types of type Returns.
Sourcepub fn invoke(
&mut self,
function_ref: &FunctionRef,
params: Vec<Value>,
) -> Result<Vec<Value>, RuntimeError>
pub fn invoke( &mut self, function_ref: &FunctionRef, params: Vec<Value>, ) -> Result<Vec<Value>, RuntimeError>
Invokes a function with the given parameters. The return types depend on the function signature.
Sourcepub fn create_resumable(
&self,
function_ref: &FunctionRef,
params: Vec<Value>,
fuel: u32,
) -> Result<ResumableRef, RuntimeError>
pub fn create_resumable( &self, function_ref: &FunctionRef, params: Vec<Value>, fuel: u32, ) -> Result<ResumableRef, RuntimeError>
Creates a new resumable, which when resumed for the first time invokes the function function_ref is associated
to, with the arguments params. The newly created resumable initially stores fuel units of fuel. Returns a
[ResumableRef] associated to the newly created resumable on success.
Sourcepub fn resume(
&mut self,
resumable_ref: ResumableRef,
) -> Result<RunState, RuntimeError>
pub fn resume( &mut self, resumable_ref: ResumableRef, ) -> Result<RunState, RuntimeError>
resumes the resumable associated to resumable_ref. Returns a [RunState] associated to this resumable if the
resumable ran out of fuel or completely executed.
Sourcepub fn access_fuel_mut<R>(
&mut self,
resumable_ref: &mut ResumableRef,
f: impl FnOnce(&mut Option<u32>) -> R,
) -> Result<R, RuntimeError>
pub fn access_fuel_mut<R>( &mut self, resumable_ref: &mut ResumableRef, f: impl FnOnce(&mut Option<u32>) -> R, ) -> Result<R, RuntimeError>
calls its argument f with a mutable reference of the fuel of the respective ResumableRef.
Fuel is stored as an Option<u32>, where None means that fuel is disabled and Some(x) means that x units of fuel is left.
A ubiquitious use of this method would be using f to read or mutate the current fuel amount of the respective ResumableRef.
§Example
use wasm::{resumable::RunState, validate, RuntimeInstance};
let wasm = [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02,
0x01, 0x00, 0x07, 0x09, 0x01, 0x05, 0x6c, 0x6f,
0x6f, 0x70, 0x73, 0x00, 0x00, 0x0a, 0x09, 0x01,
0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b ];
// a simple module with a single function looping forever
let (mut instance, _module) = RuntimeInstance::new_named((), "module", &validate(&wasm).unwrap()).unwrap();
let func_ref = instance.get_function_by_name("module", "loops").unwrap();
let mut resumable_ref = instance.create_resumable(&func_ref, vec![], 0).unwrap();
instance.access_fuel_mut(&mut resumable_ref, |x| { assert_eq!(*x, Some(0)); *x = None; }).unwrap();Sourcepub fn add_host_function_typed<Params: InteropValueList, Returns: InteropValueList>(
&mut self,
module_name: &str,
name: &str,
host_func: fn(&mut T, Vec<Value>) -> Result<Vec<Value>, HaltExecutionError>,
) -> Result<FunctionRef, RuntimeError>
pub fn add_host_function_typed<Params: InteropValueList, Returns: InteropValueList>( &mut self, module_name: &str, name: &str, host_func: fn(&mut T, Vec<Value>) -> Result<Vec<Value>, HaltExecutionError>, ) -> Result<FunctionRef, RuntimeError>
Adds a host function under module namespace module_name with name name.
roughly similar to func_alloc in https://webassembly.github.io/spec/core/appendix/embedding.html#functions
except the host function is made visible to other modules through these names.
pub fn add_host_function( &mut self, module_name: &str, name: &str, host_func_ty: FuncType, host_func: fn(&mut T, Vec<Value>) -> Result<Vec<Value>, HaltExecutionError>, ) -> Result<FunctionRef, RuntimeError>
pub fn user_data(&self) -> &T
pub fn user_data_mut(&mut self) -> &mut T
Sourcepub fn global_type(&self, global_addr: GlobalAddr) -> GlobalType
pub fn global_type(&self, global_addr: GlobalAddr) -> GlobalType
Returns the global type of some global instance by its addr.
Sourcepub fn global_read(&self, global_addr: GlobalAddr) -> Value
pub fn global_read(&self, global_addr: GlobalAddr) -> Value
Returns the current value of some global instance by its addr.
Sourcepub fn global_write(
&mut self,
global_addr: GlobalAddr,
val: Value,
) -> Result<(), RuntimeError>
pub fn global_write( &mut self, global_addr: GlobalAddr, val: Value, ) -> Result<(), RuntimeError>
Sets a new value of some global instance by its addr.
§Errors
Sourcepub fn lookup_export(
&self,
module_name: Cow<'static, str>,
name: Cow<'static, str>,
) -> Result<ExternVal, RuntimeError>
pub fn lookup_export( &self, module_name: Cow<'static, str>, name: Cow<'static, str>, ) -> Result<ExternVal, RuntimeError>
Look-up an export by its name and the name of its exporting module.