Stream: wasmtime

Topic: wasmtime-go: host state on store context?


view this post on Zulip Alex Vidal (Nov 04 2025 at 19:57):

In Rust, a Store<T> has a data method that returns T. When you create a store you can pass in arbitrary data, and then get a pointer to it later. This makes it suitable for storing store-specific state.

Is there something similar in wasmtime-go? Based on the package docs, it doesn't appear to be available. To get something similar in Go one option is to recreate the linker for each store so you can link host functions as methods on a "state" struct, but it'd be nice to reuse the linker and then access the state in host calls via, eg, caller.Data().

Am I missing something?

view this post on Zulip Alex Crichton (Nov 04 2025 at 20:04):

Currently this isn't supported yeah. The C API provides a void* pointer storage in a wasmtime_store_t (which is what the Go Store uses) which would probably be suitable for mapping an interface{} into similar to how linker host functions work though

view this post on Zulip Alex Vidal (Nov 04 2025 at 20:08):

Was just looking at the store.go implementation and noticed the gStoreMap and the slab and so on and it seemed reasonable to attach something like extra interface{} to the storeData type and make it available via Storelike.Data() which the caller can cast to their own type.

Set it via NewStoreData(engine *Engine, data any) *Store maybe?

I know little about wasmtime internals and even less about cgo, but it seems like there's room to do this.

view this post on Zulip Alex Crichton (Nov 04 2025 at 20:15):

oh true! It's been awhile so I forgot about this...

view this post on Zulip Alex Crichton (Nov 04 2025 at 20:15):

but yeah adding a field to storeData seems reasonable!

view this post on Zulip Alex Vidal (Nov 04 2025 at 20:15):

I'll give it a shot myself

view this post on Zulip Alex Vidal (Nov 04 2025 at 20:16):

Been awhile since I've worked on my wasmtime go project but had some recent interest to give it another shot, so now I'm looking at general refactorings

view this post on Zulip Alex Vidal (Nov 04 2025 at 20:17):

Currently I have a huge struct with a bunch of methods attached that I relink and have to remember to reset per instantiation, but if I can separate the struct from the functions it should be a bit cleaner..

view this post on Zulip Alex Crichton (Nov 04 2025 at 20:22):

That'd be much appreciated, thanks!

view this post on Zulip Alex Vidal (Nov 04 2025 at 21:18):

Ok. Got a working version. Forgot I need to fork to send a pull request. Incoming....

view this post on Zulip Alex Vidal (Nov 04 2025 at 21:20):

https://github.com/bytecodealliance/wasmtime-go/pull/260

This patch introduces Storelike.Data and NewStoreWithData which can be used to attach arbitrary data to a *Store which is also accessible from *Caller. Using this, embedders can use a shared linker...

view this post on Zulip Alex Vidal (Nov 04 2025 at 22:31):

And merged, amazing.

Thanks for the quick review @Alex Crichton !

view this post on Zulip Alex Vidal (Nov 04 2025 at 22:32):

I guess last step is release v38.0.1?

view this post on Zulip Alex Crichton (Nov 04 2025 at 22:32):

Mind filing an issue for that? That'll remind me to get to it tomorrow

view this post on Zulip Alex Crichton (Nov 04 2025 at 22:32):

(winding down for the day on my end)

view this post on Zulip Alex Vidal (Nov 04 2025 at 22:32):

Yup, of course. No rush.


Last updated: Dec 06 2025 at 06:05 UTC