wasmtime

Struct Instance

Source
pub struct Instance(/* private fields */);
Available on crate feature runtime only.
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. When interacting with any wasm code you’ll want to make an Instance to call any code or execute anything.

Instances are owned by a Store which is passed in at creation time. It’s recommended to create instances with Linker::instantiate or similar Linker methods, but a more low-level constructor is also available as Instance::new.

Implementations§

Source§

impl Instance

Source

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

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 specified in the module) 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 performs 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 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. For an easier time passing imports by doing name-based resolution it’s recommended to instead use the Linker type.

§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>(). For more about error handling see the Trap documentation.

§Panics

This function will panic if called with a store associated with a asynchronous config. This function will also panic if any Extern supplied is not owned by store.

Source

pub async fn new_async<T>( store: impl AsContextMut<Data = T>, module: &Module, imports: &[Extern], ) -> Result<Instance>
where T: Send,

Available on crate feature async only.

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.

This function will also panic, like Instance::new, if any Extern specified does not belong to store.

Source

pub fn module<'a, T: 'a>( &self, store: impl Into<StoreContext<'a, T>>, ) -> &'a Module

Get this instance’s module.

Source

pub fn exports<'a, T: 'a>( &'a self, store: impl Into<StoreContextMut<'a, T>>, ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a

Returns the list of exported items from this Instance.

§Panics

Panics if store does not own this instance.

Source

pub fn get_export(&self, store: impl AsContextMut, 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.

§Panics

Panics if store does not own this instance.

§Why does get_export take a mutable context?

This method requires a mutable context because an instance’s exports are lazily populated, and we cache them as they are accessed. This makes instantiating a module faster, but also means this method requires a mutable context.

Source

pub fn get_module_export( &self, store: impl AsContextMut, export: &ModuleExport, ) -> Option<Extern>

Looks up an exported Extern value by a ModuleExport value.

This is similar to Instance::get_export but uses a ModuleExport value to avoid string lookups where possible. ModuleExports can be obtained by calling Module::get_export_index on the Module that this instance was instantiated with.

This method will search the module for an export with a matching entity index and return the value, if found.

Returns None if there was no export with a matching entity index.

§Panics

Panics if store does not own this instance.

Source

pub fn get_func(&self, store: impl AsContextMut, 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.

§Panics

Panics if store does not own this instance.

Source

pub fn get_typed_func<Params, Results>( &self, store: impl AsContextMut, name: &str, ) -> Result<TypedFunc<Params, Results>>
where Params: WasmParams, Results: WasmResults,

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

§Panics

Panics if store does not own this instance.

Source

pub fn get_table(&self, store: impl AsContextMut, 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.

§Panics

Panics if store does not own this instance.

Source

pub fn get_memory(&self, store: impl AsContextMut, name: &str) -> Option<Memory>

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.

§Panics

Panics if store does not own this instance.

Source

pub fn get_shared_memory( &self, store: impl AsContextMut, name: &str, ) -> Option<SharedMemory>

Looks up an exported SharedMemory value by name.

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

§Panics

Panics if store does not own this instance.

Source

pub fn get_global(&self, store: impl AsContextMut, name: &str) -> Option<Global>

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.

§Panics

Panics if store does not own this instance.

Trait Implementations§

Source§

impl Clone for Instance

Source§

fn clone(&self) -> Instance

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Instance

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Instance

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.