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);
}
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:
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.
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.
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.
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.