Struct soroban_wasmi::Instance
source · pub struct Instance(/* private fields */);
Expand description
An instantiated WebAssembly Module
.
This type represents an instantiation of a Module
.
It primarily allows to access its exports
to call functions, get or set globals, read or write memory, etc.
When interacting with any Wasm code you will want to create an
Instance
in order to execute anything.
Instances are owned by a Store
.
Create new instances using Linker::instantiate
.
Implementations§
source§impl Instance
impl Instance
sourcepub fn new(
store: impl AsContextMut,
module: &Module,
imports: &[Extern],
) -> Result<Instance, Error>
pub fn new( store: impl AsContextMut, module: &Module, imports: &[Extern], ) -> Result<Instance, Error>
Creates a new Instance
from the pre-compiled Module
and the list of imports
.
Uses the official [Wasm instantiation prodecure] in order to resolve and type-check
the provided imports
and match them with the required imports of the Module
.
§Note
- This function intentionally is rather low-level for
Instance
creation. Please use theLinker
type for a more high-level API for Wasm module instantiation with name-based resolution. - Wasm module instantiation implies running the Wasm
start
function which is not to be confused with WASI’s_start
function.
§Usage
The imports
are intended to correspond 1:1 with the required imports as returned by Module::imports
.
For each import type returned by Module::imports
, create an Extern
which corresponds to that type.
Collect the Extern
values created this way into a list and pass them to this function.
§Errors
- If the number of provided imports does not match the number of imports required by the
Module
. - If the type of any provided
Extern
does not match the corresponding requiredExternType
. - If the
start
function, that is run at the end of the Wasm module instantiation, traps. - If Wasm module or instance related resource limits are exceeded.
§Panics
If any Extern
does not originate from the provided store
.
sourcepub fn get_typed_func<Params, Results>(
&self,
store: impl AsContext,
name: &str,
) -> Result<TypedFunc<Params, Results>, Error>where
Params: WasmParams,
Results: WasmResults,
pub fn get_typed_func<Params, Results>(
&self,
store: impl AsContext,
name: &str,
) -> Result<TypedFunc<Params, Results>, Error>where
Params: WasmParams,
Results: WasmResults,
Looks up an exported Func
value by name
.
Returns None
if there was no export named name
,
or if there was but it wasn’t a function.
§Errors
- If there is no export named
name
. - If there is no exported function named
name
. - If
Params
orResults
do not match the exported function type.
§Panics
If store
does not own this Instance
.
sourcepub fn exports<'ctx, T: 'ctx>(
&self,
store: impl Into<StoreContext<'ctx, T>>,
) -> ExportsIter<'ctx> ⓘ
pub fn exports<'ctx, T: 'ctx>( &self, store: impl Into<StoreContext<'ctx, T>>, ) -> ExportsIter<'ctx> ⓘ
Trait Implementations§
impl Copy for Instance
impl Eq for Instance
impl StructuralPartialEq for Instance
Auto Trait Implementations§
impl Freeze for Instance
impl RefUnwindSafe for Instance
impl Send for Instance
impl Sync for Instance
impl Unpin for Instance
impl UnwindSafe for Instance
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.