Sharing classes from Rust with JavaScript
wasm-bindgen
enables sharing classes from JavaScript with Rust and vice versa using simple annotations. It handles all the boilerplate stuff, such as translating a value from JavaScript to WebAssembly or WebAssembly to JavaScript, complex memory manipulations, and error-prone pointer arithmetic. Thus, wasm-bindgen
makes everything easier.
Let's see how easy it is to share classes between JavaScript and WebAssembly (from Rust):
- Create a new project:
$ cargo new --lib class_world Created library `class_world` package
- Define the
wasm-bindgen
dependency for the project. Open thecargo.toml
file and add the following content:[package] name = "class_world" version = "0.1.0" authors = ["Sendil Kumar"] edition = "2018" [lib] crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2.68"
- Open the
src/lib.rs
file and replace the content with the following:use wasm_bindgen...