chifflier opened Issue #2491:
Hi,
Is there a way to store data ininstance
?
I have a situation where guest calls a host functionget_info_xxx
, and host needs to identify which instance is asking to send the correct data.
I tried looking at examples and doc and could not find the solution. Best I can think of is storing data or an identifier by capturing it in the environment ofFunc
when creating the Instance, however this is not convenient, since the data is evolving over time (and not known when creating the instance).
So, at the moment I'm considering having a global structure to store per-instance data, and give an identifier to instances (captured in callbacks). This may work, but require lots of code (global variables and unsafe access, need for a lock, etc.) that could be avoided if host could just update per-instance data before calling.Is there a better way of having (dynamic) per-instance data?
Thanks!
alexcrichton commented on Issue #2491:
Currently you cannot attach data to an
Instance
or awasm_instance_t
. Can you clarify whether you're using the C or the Rust API? The current intention is that any instance-specific data can live in theFunc
items you hook up into an instance, but we can of course always add other storage mechanisms too!
chifflier commented on Issue #2491:
Oh, forgot to mention that. I'm using the Rust API
In most cases, closures are a good way to have per-instance data. But my data will change before every call: the application uses WASM for out of browser plugins, so the application processes data, then call WASM function for each plugin. There is also contextual data, that would be too costly to copy to guest before every call, so the intent is to provide it "on-demand" when the guest requests it.
I was thinking that maybe storing data in the instance and make it available inCaller
would be a solution (wasmer has adata
member in theCtx
structure, for example), but I am not familiar with available APIs, and wanted to make sure I am not missing something obvious.I'm still experimenting: I tested a solution with a global
RwLock<HashMap<...>>
which works but adds lots of locking code everywhere. Maybe something simpler is to create anArc<Mutex<...>>
before creating the imports, and capture it in the closure inFunc
?
alexcrichton commented on Issue #2491:
Unfortunately
Caller
isn't a great solution here I think. That's sort of a hack around the lack of interface types today, but in general it's very limiting from a wasm-semantics point of view to place restrictions on who and what can call a function (e.g. that your import would only work when called from the wasm module specifically).I'm not familiar enough with your use case but I suspect a combination of
Rc
andRefCell
would be able to help too?
chifflier commented on Issue #2491:
It works! I tested two solutions, one based on
Arc<Mutex<T>>
and one onRc<RefCell<T>>
. Basically, the application main loop keeps a reference and updates data, while each closure also have a reference and can access data. The choice between the two depends on the application using threads, as usual.Thanks @alexcrichton for your help, and the explanations.
alexcrichton commented on Issue #2491:
Ok great, glad it worked out!
alexcrichton closed Issue #2491:
Hi,
Is there a way to store data ininstance
?
I have a situation where guest calls a host functionget_info_xxx
, and host needs to identify which instance is asking to send the correct data.
I tried looking at examples and doc and could not find the solution. Best I can think of is storing data or an identifier by capturing it in the environment ofFunc
when creating the Instance, however this is not convenient, since the data is evolving over time (and not known when creating the instance).
So, at the moment I'm considering having a global structure to store per-instance data, and give an identifier to instances (captured in callbacks). This may work, but require lots of code (global variables and unsafe access, need for a lock, etc.) that could be avoided if host could just update per-instance data before calling.Is there a better way of having (dynamic) per-instance data?
Thanks!
Last updated: Nov 22 2024 at 16:03 UTC