pub struct Instance { /* private fields */ }
Expand description
An instantiated WebAssembly module.
This type represents the instantiation of a Module
. Once instantiated
you can access the exports
which are of type
Extern
and provide the ability to call functions, set globals, read
memory, etc. This is where all the fun stuff happens!
An Instance
is created from two inputs, a Module
and a list of
imports, provided as a list of Extern
values. The Module
is the wasm
code that was compiled and we’re instantiating, and the Extern
imports
are how we’re satisfying the imports of the module provided. On successful
instantiation an Instance
will automatically invoke the wasm start
function.
When interacting with any wasm code you’ll want to make an Instance
to
call any code or execute anything!
Implementations
sourceimpl Instance
impl Instance
sourcepub fn new(
store: &Store,
module: &Module,
imports: &[Extern]
) -> Result<Instance, Error>
pub fn new(
store: &Store,
module: &Module,
imports: &[Extern]
) -> Result<Instance, Error>
Creates a new Instance
from the previously compiled Module
and
list of imports
specified.
This method instantiates the module
provided with the imports
,
following the procedure in the core specification to
instantiate. Instantiation can fail for a number of reasons (many
specified below), but if successful the start
function will be
automatically run (if provided) and then the Instance
will be
returned.
Per the WebAssembly spec, instantiation includes running the module’s
start function, if it has one (not to be confused with the _start
function, which is not run).
Note that this is a low-level function that just performance an
instantiation. See the Linker
struct for an API which provides a
convenient way to link imports and provides automatic Command and Reactor
behavior.
Providing Imports
The imports
array here is a bit tricky. The entries in the list of
imports
are intended to correspond 1:1 with the list of imports
returned by Module::imports
. Before calling Instance::new
you’ll
want to inspect the return value of Module::imports
and, for each
import type, create an Extern
which corresponds to that type.
These Extern
values are all then collected into a list and passed to
this function.
Note that this function is intentionally relatively low level. It is the intention that we’ll soon provide a higher level API which will be much more ergonomic for instantiating modules. If you need the full power of customization of imports, though, this is the method for you!
Errors
This function can fail for a number of reasons, including, but not limited to:
- The number of
imports
provided doesn’t match the number of imports returned by themodule
’sModule::imports
method. - The type of any
Extern
doesn’t match the correspondingExternType
entry that it maps to. - The
start
function in the instance, if present, traps. - Module/instance resource limits are exceeded.
When instantiation fails it’s recommended to inspect the return value to
see why it failed, or bubble it upwards. If you’d like to specifically
check for trap errors, you can use error.downcast::<Trap>()
.
sourcepub fn ty(&self) -> InstanceType
pub fn ty(&self) -> InstanceType
Returns the type signature of this instance.
sourcepub fn exports<'instance>(
&'instance self
) -> impl ExactSizeIterator<Item = Export<'instance>> + 'instance
pub fn exports<'instance>(
&'instance self
) -> impl ExactSizeIterator<Item = Export<'instance>> + 'instance
Returns the list of exported items from this Instance
.
sourcepub fn get_export(&self, name: &str) -> Option<Extern>
pub fn get_export(&self, name: &str) -> Option<Extern>
Looks up an exported Extern
value by name.
This method will search the module for an export named name
and return
the value, if found.
Returns None
if there was no export named name
.
sourcepub fn get_func(&self, name: &str) -> Option<Func>
pub fn get_func(&self, name: &str) -> Option<Func>
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.
sourcepub fn get_table(&self, name: &str) -> Option<Table>
pub fn get_table(&self, name: &str) -> Option<Table>
Looks up an exported Table
value by name.
Returns None
if there was no export named name
, or if there was but
it wasn’t a table.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Instance
impl !Send for Instance
impl !Sync for Instance
impl Unpin for Instance
impl !UnwindSafe for Instance
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Pointable for T
impl<T> Pointable for T
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more