You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
modify unserializable struct in plugin runtime without global state #196
examples/example-rust-wasmer2-runtime/src/spec/mod.rs gives a way for plugin to modify a host struct using global state. can this be done without global state?
BTW, the example-rust-runtime/spec/mod.rs in readme does't exist.
What do you mean by modifying a host struct exactly? The global state it is modifying in that example lives within the plugin, so wouldn't affect the host at all.
// Possible "race condition", but we don't mind here
// let val = GLOBAL_STATE.get();
// GLOBAL_STATE.set(val + 1);
letmut lock = GLOBAL_STATE.lock().unwrap();
let value = *lock + 1;
*lock = value;
}
the GLOBAL_STATE can also hold an unserializable struct, like a egui context. thus i can let plugin control ui via such import function.
but i want to avoid GLOBAL_STATE style.
here is an example code from mlua. the rust_val is a host value, it can be modified in plugin(lua side). it can be a complex unserializable struct and don't need to be global.
{letmut rust_val = 0;
lua.scope(|scope| {// We create a 'sketchy' Lua callback that holds a mutable reference to the variable// `rust_val`. Outside of a `Lua::scope` call, this would not be allowed// because it could be unsafe.
lua.globals().set("sketchy",
scope.create_function_mut(|_,()| {
rust_val = 42;Ok(())})?,)?;
lua.load("sketchy()").exec()})?;assert_eq!(rust_val,42);}
Oh, you're right, that's my bad. Yes, that one is indeed kept in host memory. Frankly, it's not something we support very well, since the use cases we focus on are more situations where the plugin does things for the host without assuming persistence. Still, I think it's a useful request to consider when we eventually migrate to Wasmer 4.
examples/example-rust-wasmer2-runtime/src/spec/mod.rsgives a way for plugin to modify a host struct using global state. can this be done without global state?BTW, the
example-rust-runtime/spec/mod.rsin readme does't exist.