Calling the JavaScript API via WebAssembly
JavaScript provides a rich API to work with objects, arrays, maps, sets, and so on. If we want to use or define them in Rust, then we need to provide the necessary bindings. Handcrafting those bindings will be a huge process. But what if we have bindings to those APIs? That is a common API for both Node.js and a browser environment that will create a platform where we can write the code completely in Rust and use wasm_bindgen
to create necessary code.
The rustwasm team's answer to that is the js-sys crate.
They provide bindings to JavaScript's standard built-in objects, including their methods and properties.
In this example, let's see how to call a JavaScript API via WebAssembly:
- Create a default project with the
cargo new
command...