Chapter 4
Integrating WebAssembly with JavaScript

So far, everything you’ve done in this book has been isolated almost entirely within the realm of WebAssembly. You’ve written a checkers engine in raw wast, then you wrote an upgraded one in Rust and compiled it to the wasm32-unknown-unknown target. Even though both of these projects had places where JavaScript could attach its tentacles, you can’t really refer to either of those samples as tightly integrated.

In this chapter, you’ll take a look at the Rust WebAssembly ecosystem, including the tooling and libraries available to help bridge the gap between WebAssembly and JavaScript. You’ll start off by creating a new “Hello, World” template that illustrates a new way of communicating between JavaScript and Rust.

Next, you’ll explore tools like wasm-bindgen that allow Rust to see JavaScript classes (and JavaScript to use Rust structures), expose and invoke callbacks in either language, send strings as function parameters, and return complex values, all while maintaining Rust’s strict sharing rules.

By the end of the chapter, you’ll not only know the mechanics of how to interoperate with JavaScript, but you’ll have seen patterns and examples of when and where you should divide your logic up between JavaScript and Rust WebAssembly modules by building an interactive, browser-based game.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.81.223