pub struct Store { /* private fields */ }
Expand description
A Store
is a collection of WebAssembly instances and host-defined items.
All WebAssembly instances and items will be attached to and refer to a
Store
. For example instances, functions, globals, and tables are all
attached to a Store
. Instances are created by instantiating a
Module
within a Store
.
Store
is not thread-safe and cannot be sent to other threads. All items
which refer to a Store
additionally are not threadsafe and can only be
used on the original thread that they were created on.
A Store
is not intended to be a long-lived object in a program. No form of
GC is implemented at this time so once an instance is created within a
Store
it will not be deallocated until all references to the Store
have
gone away (this includes all references to items in the store). This makes
Store
unsuitable for creating an unbounded number of instances in it
because Store
will never release this memory. It’s instead recommended to
have a long-lived Engine
and instead create a Store
for a more scoped
portion of your application.
Stores and Clone
Using clone
on a Store
is a cheap operation. It will not create an
entirely new store, but rather just a new reference to the existing object.
In other words it’s a shallow copy, not a deep copy.
Stores and Default
You can create a store with default configuration settings using
Store::default()
. This will create a brand new Engine
with default
ocnfiguration (see Config
for more information).
Implementations
sourceimpl Store
impl Store
sourcepub fn new(engine: &Engine) -> Store
pub fn new(engine: &Engine) -> Store
Creates a new store to be associated with the given Engine
.
sourcepub fn same(a: &Store, b: &Store) -> bool
pub fn same(a: &Store, b: &Store) -> bool
Returns whether the stores a
and b
refer to the same underlying
Store
.
Because the Store
type is reference counted multiple clones may point
to the same underlying storage, and this method can be used to determine
whether two stores are indeed the same.
sourcepub fn interrupt_handle(&self) -> Result<InterruptHandle>
pub fn interrupt_handle(&self) -> Result<InterruptHandle>
Creates an InterruptHandle
which can be used to interrupt the
execution of instances within this Store
.
An InterruptHandle
handle is a mechanism of ensuring that guest code
doesn’t execute for too long. For example it’s used to prevent wasm
programs for executing infinitely in infinite loops or recursive call
chains.
The InterruptHandle
type is sendable to other threads so you can
interact with it even while the thread with this Store
is executing
wasm code.
There’s one method on an interrupt handle:
InterruptHandle::interrupt
. This method is used to generate an
interrupt and cause wasm code to exit “soon”.
When are interrupts delivered?
The term “interrupt” here refers to one of two different behaviors that are interrupted in wasm:
- The head of every loop in wasm has a check to see if it’s interrupted.
- The prologue of every function has a check to see if it’s interrupted.
This interrupt mechanism makes no attempt to signal interrupts to native code. For example if a host function is blocked, then sending an interrupt will not interrupt that operation.
Interrupts are consumed as soon as possible when wasm itself starts executing. This means that if you interrupt wasm code then it basically guarantees that the next time wasm is executing on the target thread it will return quickly (either normally if it were already in the process of returning or with a trap from the interrupt). Once an interrupt trap is generated then an interrupt is consumed, and further execution will not be interrupted (unless another interrupt is set).
When implementing interrupts you’ll want to ensure that the delivery of interrupts into wasm code is also handled in your host imports and functionality. Host functions need to either execute for bounded amounts of time or you’ll need to arrange for them to be interrupted as well.
Return Value
This function returns a Result
since interrupts are not always
enabled. Interrupts are enabled via the
Config::interruptable
method, and if
this store’s Config
hasn’t been configured to enable
interrupts then an error is returned.
Examples
// Enable interruptable code via `Config` and then create an interrupt
// handle which we'll use later to interrupt running code.
let engine = Engine::new(Config::new().interruptable(true));
let store = Store::new(&engine);
let interrupt_handle = store.interrupt_handle()?;
// Compile and instantiate a small example with an infinite loop.
let module = Module::new(&engine, r#"
(func (export "run") (loop br 0))
"#)?;
let instance = Instance::new(&store, &module, &[])?;
let run = instance
.get_func("run")
.ok_or(anyhow::format_err!("failed to find `run` function export"))?
.get0::<()>()?;
// Spin up a thread to send us an interrupt in a second
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(1));
interrupt_handle.interrupt();
});
let trap = run().unwrap_err();
assert!(trap.to_string().contains("wasm trap: interrupt"));
Trait Implementations
sourceimpl TrapInfo for Store
impl TrapInfo for Store
sourcefn is_wasm_code(&self, addr: usize) -> bool
fn is_wasm_code(&self, addr: usize) -> bool
Returns whether the given program counter lies within wasm code, indicating whether we should handle a trap or not. Read more
sourcefn custom_signal_handler(
&self,
call: &dyn Fn(&SignalHandler<'_>) -> bool
) -> bool
fn custom_signal_handler(
&self,
call: &dyn Fn(&SignalHandler<'_>) -> bool
) -> bool
Uses call
to call a custom signal handler, if one is specified. Read more
sourcefn max_wasm_stack(&self) -> usize
fn max_wasm_stack(&self) -> usize
Returns the maximum size, in bytes, the wasm native stack is allowed to grow to. Read more
Auto Trait Implementations
impl !RefUnwindSafe for Store
impl !Send for Store
impl !Sync for Store
impl Unpin for Store
impl !UnwindSafe for Store
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