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 procedure 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
Instancecreation. Please use theLinkertype for a more high-level API for Wasm module instantiation with name-based resolution. - Wasm module instantiation implies running the Wasm
startfunction which is not to be confused with WASI’s_startfunction.
§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
Externdoes not match the corresponding requiredExternType. - If the
startfunction, 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
ParamsorResultsdo not match the exported function type.
§Panics
If store does not own this Instance.