Wasmtime :: Store, Instance, Module, Repl

Quoting Store in wasmtime - Rust

A Store is intended to be a short-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 the Store itself is dropped. This makes Store unsuitable for creating an unbounded number of instances in it because Store will never release this memory. It’s recommended to have a Store correspond roughly to the lifetime of a “main instance” that an embedding is interested in executing.

Quoting wasmtime - Rust , the standard wasmtime setup looks something like:

let module = Module::new(&engine, wat)?;

    let mut store = Store::new(&engine, 4);

    let instance = Instance::new(&mut store, &module, &[host_hello.into()])?;
    let hello = instance.get_typed_func::<(), (), _>(&mut store, "hello")?;

    hello.call(&mut store, ())?;

So the problem here: based on the above quote, Store does not drop Instances when they are no longer in use. Therefore, if we try to build a REPL on Wasm, it is likely going to retain all old instances (code) as the REPL runs.

Is there a way to somehow avoid this? I would like to keep the "memory" associated with a Store, but drop all the Instance (module code) when they are no longer in use.

Quoting: Memory in wasmtime - Rust

A WebAssembly linear memory.

WebAssembly memories represent a contiguous array of bytes that have a size that is always a multiple of the WebAssembly page size, currently 64 kilobytes.

WebAssembly memory is used for global data (not to be confused with wasm global items), statics in C/C++/Rust, shadow stack memory, etc. Accessing wasm memory is generally quite fast.

Memories, like other wasm items, are owned by a Store .

this sounds like what I need, except the last statement of Memory is owned by Store. Since the only way to drop Instance is via dropping Store, I need some way to create a Memory that can outlive Store (and reattach to new Store).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.