Skip to content

Shared types in back-and-forth communication between the host and the protocol #37

@kajacx

Description

@kajacx

Backstory
I want to use WAI to create a plugin system, where I define the host protocol, implemented by the host, and the plugin protocol, implemented by the plugin. The host will call functions implemented by the plugin, and the plugin can call functions implemented by the host.

I have a working example in this repo, both with wasmer runtime, and wasmtime runtime.

The problem

When I want to use a custom type, like vector3f in this example, I have to define it in both the host protocol, and in the plugin protocol.

Host protocol:

move-y: func(vec: vector3f) -> vector3f

record vector3f {
  x: float32,
  y: float32,
  z: float32,
}

Plugin protocol:

move-vec: func(vec: vector3f) -> vector3f

record vector3f {
  x: float32,
  y: float32,
  z: float32,
}

This works, but if I want to call the moveY host function from the moveVec plugin function with the same Vector3f type, I quickly run into a problem: it's not the same Vector3f type!

Current solution / workaround

Currently I am solving this by manually converting the host Vector3f type to the plugin one and back, like so:

    fn move_vec(vec: sample_protocol_plugin::Vector3f) -> sample_protocol_plugin::Vector3f {
        let mut as_host = sample_protocol_host::Vector3f {
            x: vec.x,
            y: vec.y,
            z: vec.z,
        };

        as_host.x += 1.0;

        as_host = move_y(as_host);

        as_host.z += 1.0;

        sample_protocol_plugin::Vector3f {
            x: as_host.x,
            y: as_host.y,
            z: as_host.z,
        }
    }

Needless to say, this is not ideal. Even if the implementation of the conversion could be generated by a macro, I would still need to call ::from() and .into() and worry about which type is which.

Solutions?

This would probably need a separate types.wai file with just the types definitions, then a macro to just import the types from this file, and then finally a way for the import! and export! macros to use the type definitions generated by the previous type macro, instead of generating their own.

Not sure if WAI bindgen supports this, but it would be cool if it did. Any other suggestions on how to (more) easily exchange the same type between host and plugin back and forth are welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions