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?
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
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.
oh true! It's been awhile so I forgot about this...
but yeah adding a field to storeData seems reasonable!
I'll give it a shot myself
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
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..
That'd be much appreciated, thanks!
Ok. Got a working version. Forgot I need to fork to send a pull request. Incoming....
https://github.com/bytecodealliance/wasmtime-go/pull/260
And merged, amazing.
Thanks for the quick review @Alex Crichton !
I guess last step is release v38.0.1?
Mind filing an issue for that? That'll remind me to get to it tomorrow
(winding down for the day on my end)
Yup, of course. No rush.
Last updated: Dec 06 2025 at 06:05 UTC