Instance in wasmtime::component - Rust (original) (raw)
pub struct Instance { /* private fields */ }
Available on crate features runtime
and component-model
only.
Expand description
An instantiated component.
This type represents an instantiated Component. Instances have exports which can be accessed through functions such asInstance::get_func or Instance::get_export. Instances are owned by aStore and all methods require a handle to the store.
Component instances are created throughLinker::instantiate and its family of methods.
This type is similar to the core wasm versionwasmtime::Instance except that it represents an instantiated component instead of an instantiated module.
Looks up an exported function by name within this Instance.
The store
argument provided must be the store that this instance lives within and the name
argument is the lookup key by which to find the exported function. If the function is found then Some
is returned and otherwise None
is returned.
The name
here can be a string such as &str
or it can be aComponentExportIndex which is loaded prior from a Component.
§Panics
Panics if store
does not own this instance.
§Examples
Looking up a function which is exported from the root of a component:
use wasmtime::{Engine, Store};
use wasmtime::component::{Component, Linker};
let engine = Engine::default();
let component = Component::new(
&engine,
r#"
(component
(core module $m
(func (export "f"))
)
(core instance <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>t</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">i (instantiate </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">an</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span></span></span></span>m))
(func (export "f")
(canon lift (core func $i "f")))
)
"#,
)?;
// Look up the function by name
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
let func = instance.get_func(&mut store, "f").unwrap();
// The function can also be looked up by an index via a precomputed index.
let export = component.get_export_index(None, "f").unwrap();
let func = instance.get_func(&mut store, &export).unwrap();
Looking up a function which is exported from a nested instance:
use wasmtime::{Engine, Store};
use wasmtime::component::{Component, Linker};
let engine = Engine::default();
let component = Component::new(
&engine,
r#"
(component
(core module $m
(func (export "f"))
)
(core instance <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo stretchy="false">(</mo><mi>i</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>a</mi><mi>t</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">i (instantiate </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mopen">(</span><span class="mord mathnormal">in</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">an</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ia</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span></span></span></span>m))
(func $f
(canon lift (core func $i "f")))
(instance $i
(export "f" (func $f)))
(export "i" (instance $i))
)
"#,
)?;
// First look up the exported instance, then use that to lookup the
// exported function.
let instance_index = component.get_export_index(None, "i").unwrap();
let func_index = component.get_export_index(Some(&instance_index), "f").unwrap();
// Then use `func_index` at runtime.
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
let func = instance.get_func(&mut store, &func_index).unwrap();
// Alternatively the `instance` can be used directly in conjunction with
// the `get_export_index` method.
let instance_index = instance.get_export_index(&mut store, None, "i").unwrap();
let func_index = instance.get_export_index(&mut store, Some(&instance_index), "f").unwrap();
let func = instance.get_func(&mut store, &func_index).unwrap();
Looks up an exported Func value by name and with its type.
This function is a convenience wrapper over Instance::get_func andFunc::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.
Looks up an exported module by name within this Instance.
The store
argument provided must be the store that this instance lives within and the name
argument is the lookup key by which to find the exported module. If the module is found then Some
is returned and otherwise None
is returned.
The name
here can be a string such as &str
or it can be aComponentExportIndex which is loaded prior from a Component.
For some examples see Instance::get_func for loading values from a component.
§Panics
Panics if store
does not own this instance.
Looks up an exported resource type by name within this Instance.
The store
argument provided must be the store that this instance lives within and the name
argument is the lookup key by which to find the exported resource. If the resource is found then Some
is returned and otherwise None
is returned.
The name
here can be a string such as &str
or it can be aComponentExportIndex which is loaded prior from a Component.
For some examples see Instance::get_func for loading values from a component.
§Panics
Panics if store
does not own this instance.
Returns the InstancePre that was used to create this instance.