Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
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

Description

@tkkcc

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.

Activity

  1. arendjr commented on Aug 9, 2023

    @arendjr
    Contributor

    Thanks for the heads-up!

    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.

    Could you explain what you would like to achieve?

  2. tkkcc commented on Aug 9, 2023

    @tkkcc
    Author

    this import function can change value in GLOBAL_STATE. GLOBAL_STATE is init in the runtime code, so i think it is a host value.

    async fn import_increment_global_state() {
    // Possible "race condition", but we don't mind here
    // let val = GLOBAL_STATE.get();
    // GLOBAL_STATE.set(val + 1);
    let mut 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.

    https://github.com/khvzak/mlua/blob/c0c6a33f94127188a528801656effa89387880eb/examples/guided_tour.rs#L195C1-L215C6

        {
            let mut 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);
        }
  3. arendjr commented on Aug 9, 2023

    @arendjr
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions