-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get instance data in host defined functions with component model #6848
Comments
You mention that a single linker is used for a store, but that's not quite accurate because you can use any number of linkers to instantiate within a So, if possible, the recommendation is to store per-instance information into a |
Sorry for not stating the question clearly. I'm using the rust API. Otherwise
I'm a little bit confused here because I thought there can be multiple instances inside a single store. My setup is as follows: fn init() {
engine = Engine::new(config)?;
store = Store::new(...);
linker = Linker::new(&engine);
MyWasmWorld::add_to_linker(&mut linker, ...)?;
...
} Then, when a plugin (guest program) is loaded, I load the wasm file and instantiate the component fn load_plugin_from_path(file_path: &str) {
Component::from_file(engine, file_path)?;
let (_, instance) = MyWasmWorld::instantiate(&mut store, &component, &linker)?;
...
} My understanding is that Or should I use multiple stores for different plugins? But even then, I still need to get the |
Oh, I think I get what you are suggesting, I can create one linker each time I instantiate a plugin. Let me try if that works. |
Ah yes it's possible to put lots of instances in the same store. The downside of that approach is that you can't incrementally delete instances within the store, it's an all-or-nothing operation (e.g. you can't unload just one plugin, you'd have to unload all of them). If possible I'd recommend having a Storing state per-linker would still work, however, if that works for you! |
@alexcrichton, not sure if this is the best place to ask the question, but is there a clean way to call guest functions from without a host function? (I'm not currently using the component model, if that makes a difference). I have a bunch of host functions that allocate their results. To do so, they rely on an It would be nice if there was a way for Or maybe I just want to expose Should I be thinking about allocation differently? Is there another way to do this? Please advise if there's a better place to post too. |
I'll follow up on #9525 as this probably isn't the best place to continue this discussion (but no worries!) |
Feature
Provide instance (
Instance
struct) or instance data to host defined functions in component model.Benefit
In #2491, it is suggested to bind the instance data to the closure passed to
Func::wrap
to store per-instance data. However, this will not work for instances in component model because to bind a function (let host export a function to be called by guests), one need to configureLinkerInstance
(e.g., useLinkerInstance::func_wrap
or similar methods). Yet, there is only a single linker set up for instantiating all the instances in a store, so there is no way to configure per-instance data.Providing host function instance and instance data will let host function able to get data tied to the instance that calls the function, or let host perform a nested call to a guest function, etc.
Implementation
I think a possible implementation is to let the function passed to
LinkerInstance::func_wrap
to be able to takeCaller
instead ofStoreContextMut
. The non-component model counterpartFunc::wrap
takesAsContextMut
to achieve this, and I'm not sure whyfunc_wrap
takesStoreContextMut
instead. Then, we can expose the instance stored in theCaller
struct. Of course, this all rely on the assumption thatCaller
can be retrieve in calls to component host.The text was updated successfully, but these errors were encountered: