Wasmer: Possibility of a Wasmer Bindgen-Type Library

Created on 12 Jul 2019  ยท  2Comments  ยท  Source: wasmerio/wasmer

Summary

I have been investigating Wasmer as a way to support scripting for games as discussed in amethyst/amethyst#1729 and I wanted to get the Wasmer team's thoughts on creating a library like wasm-bindgen except for creating Rust<->Rust bindings using WASM.

For example, in your host Rust application you could write code like this:

// Import structs that are used in both the game and in the Rust host.
use custom_data::MyCustomStruct;

#[wasmer_bindgen]
extern "C" {
    // Function defined inside of the WASM module
    fn my_module_function(string_param: &str);
}

/// Function exposed to the WASM module
#[wasmer_bindgen]
pub fn my_game_function(string_param: &str, struct_param: MyCustomStruct) {
    // Do something inside of the game
    // ...    

    // I can call functions inside of the WASM module
    my_module_function("Hello module");
}

When writing your WASM module in Rust you would essentially do the opposite:

// Import structs that are used in both the game and in the Rust host.
use custom_data::MyCustomStruct;

#[wasmer_bindgen]
extern "C" {
    // Function defined inside of the game
    fn my_game_function(string_param: &str, struct_param: MyCustomStruct);
}

/// Function exposed to the game
#[wasmer_bindgen]
pub fn my_module_function(string_param: &str) {
    let data = MyCustomStruct {
        game: "data",
        number: 1,
    };

    // I can call functions inside of the game
    my_game_function("string", data);
}

Additional Details

This would let you intuitively extend Rust applications with Rust WASM modules, but you could probably do something similar with the other languages that Wasmer supports like C, Python, etc. I'm not sure exactly what that would look like, but it would be impressive if you could create an interface that would allow you to create bindings for any combination of supported host and target languages. For example:

  • Rust<->Rust
  • Rust<->Python
  • Python<->PHP
  • etc.

I'm not sure whether or not it would be feasible to support every combination or if that would be difficult because of the different ways that each language treats objects and types.

Personally, a Rust<->Rust bindings setup would be the most useful to me, as I could create bindings to other languages such as Python using Rust, but it would be a much nicer developer experience if bindings could be directly created for other languages. It would be amazing to be able to provide a similar experience to the wasm_bindgen crate in a more universal fashion with Wasmer.

Final Notes

There may be some fundamental flaws to this approach that I'm missing; I'm not extremely versed in language inter-op or WASM. I'm hoping to get feedback on what the feasibility of this idea might be.

โ“ question

Most helpful comment

So. That's a quite large topic. WebAssembly has a working group to work on a proposal called Web IDL Bindings, https://github.com/WebAssembly/webidl-bindings/. I think it is best described by the explainer. Things you must know: This is experimental, it aims at prototyping bindings, and the name is subject to change.

In short: We are contributing to it, we are building something, we are experimenting with such bindings. It's too early to make it public, but we have nice results so far. The best you can do is to watch this issue, I'll post news as soon as I can.

All 2 comments

So. That's a quite large topic. WebAssembly has a working group to work on a proposal called Web IDL Bindings, https://github.com/WebAssembly/webidl-bindings/. I think it is best described by the explainer. Things you must know: This is experimental, it aims at prototyping bindings, and the name is subject to change.

In short: We are contributing to it, we are building something, we are experimenting with such bindings. It's too early to make it public, but we have nice results so far. The best you can do is to watch this issue, I'll post news as soon as I can.

If you're looking to build WebAssembly modules that have capabilities that let you do things like build games, check out https://waxosuit.io - it aims to fill some of the gap for non-browser hosting left behind by tooling like wasm-bindgen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webmaster128 picture webmaster128  ยท  4Comments

ensch picture ensch  ยท  5Comments

nlewycky picture nlewycky  ยท  4Comments

fd picture fd  ยท  3Comments

vitiral picture vitiral  ยท  4Comments