Struct RuntimeInstance

Source
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>

Source

pub fn new(user_data: T) -> Self

Source

pub fn new_with_default_module( user_data: T, validation_info: &ValidationInfo<'b>, ) -> Result<(Self, ModuleAddr), RuntimeError>

Source

pub fn new_named( user_data: T, module_name: &str, validation_info: &ValidationInfo<'b>, ) -> Result<(Self, ModuleAddr), RuntimeError>

Source

pub fn add_module( &mut self, module_name: &str, validation_info: &ValidationInfo<'b>, ) -> Result<ModuleAddr, RuntimeError>

Source

pub fn get_function_by_name( &self, module_name: &str, function_name: &str, ) -> Result<FunctionRef, RuntimeError>

Source

pub fn get_function_by_index( &self, module_addr: ModuleAddr, function_idx: usize, ) -> Result<FunctionRef, RuntimeError>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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();
Source

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.

Source

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>

Source

pub fn user_data(&self) -> &T

Source

pub fn user_data_mut(&mut self) -> &mut T

Source

pub fn global_type(&self, global_addr: GlobalAddr) -> GlobalType

Returns the global type of some global instance by its addr.

Source

pub fn global_read(&self, global_addr: GlobalAddr) -> Value

Returns the current value of some global instance by its addr.

Source

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
Source

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.

Trait Implementations§

Source§

impl<T: Config + Default> Default for RuntimeInstance<'_, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'b, T> Freeze for RuntimeInstance<'b, T>
where T: Freeze,

§

impl<'b, T = ()> !RefUnwindSafe for RuntimeInstance<'b, T>

§

impl<'b, T> Send for RuntimeInstance<'b, T>
where T: Send,

§

impl<'b, T = ()> !Sync for RuntimeInstance<'b, T>

§

impl<'b, T> Unpin for RuntimeInstance<'b, T>
where T: Unpin,

§

impl<'b, T = ()> !UnwindSafe for RuntimeInstance<'b, 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>,

Source§

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>,

Source§

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.