Struct wasmtime::Instance[][src]

pub struct Instance { /* fields omitted */ }
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

impl Instance[src]

pub fn new(
    store: &Store,
    module: &Module,
    imports: &[Extern]
) -> Result<Instance, Error>
[src]

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 the module’s Module::imports method.
  • The type of any Extern doesn’t match the corresponding ExternType 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>().

Panics

This function will panic if called with a store associated with a asynchronous config.

pub async fn new_async(
    store: &Store,
    module: &Module,
    imports: &[Extern]
) -> Result<Instance, Error>
[src]

Same as Instance::new, except for usage in [asynchronous stores].

For more details about this function see the documentation on Instance::new. The only difference between these two methods is that this one will asynchronously invoke the wasm start function in case it calls any imported function which is an asynchronous host function (e.g. created with Func::new_async.

Panics

This function will panic if called with a store associated with a synchronous config. This is only compatible with stores associated with an asynchronous config.

pub fn ty(&self) -> InstanceType[src]

Returns the type signature of this instance.

pub fn store(&self) -> &Store[src]

Returns the associated Store that this Instance is compiled into.

This is the Store that generally serves as a sort of global cache for various instance-related things.

pub fn exports<'instance>(
    &'instance self
) -> impl ExactSizeIterator<Item = Export<'instance>> + 'instance
[src]

Returns the list of exported items from this Instance.

pub fn get_export(&self, name: &str) -> Option<Extern>[src]

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.

pub fn get_func(&self, name: &str) -> Option<Func>[src]

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.

pub fn get_typed_func<Params, Results>(
    &self,
    name: &str
) -> Result<TypedFunc<Params, Results>> where
    Params: WasmParams,
    Results: WasmResults
[src]

Looks up an exported Func value by name and with its type.

This function is a convenience wrapper over Instance::get_func and Func::typed. For more information see the linked documentation.

Returns an error if name isn’t a function export or if the export’s type did not match Params or Results

pub fn get_table(&self, name: &str) -> Option<Table>[src]

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.

pub fn get_memory(&self, name: &str) -> Option<Memory>[src]

Looks up an exported Memory value by name.

Returns None if there was no export named name, or if there was but it wasn’t a memory.

pub fn get_global(&self, name: &str) -> Option<Global>[src]

Looks up an exported Global value by name.

Returns None if there was no export named name, or if there was but it wasn’t a global.

Trait Implementations

impl Clone for Instance[src]

fn clone(&self) -> Instance[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl From<Instance> for Extern[src]

fn from(r: Instance) -> Self[src]

Performs the conversion.

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V