Ways to generate WebAssembly

There are several compiler toolchain projects that can help developers compile their code from any language to wasm. Compiling native code has huge implications for the web. It means that most major performance-intensive code can now be run on the web. For instance, C++ code can be compiled to wasm using the emscripten LLVM backend. The emscripten project takes in the LLVM IR generated by the C++ compiler and converts it into WebAssembly modules in wasm format. There are also projects such as AssemblyScript, which convert TypeScript code into WebAssembly using another emscripten-like tool, called binaryen. Rust also supports emitting WebAssembly code by default using LLVM's native WebAssembly backend. Compiling to wasm with Rust is very simple. First, we need to add the wasm by running the following code:

rustup target add wasm32-unknown-unknown

Once that is done, we can compile any Rust program to wasm by running the following code:

 cargo build --target=wasm32-unknown-unknown

This is the bare minimum that's needed to create a wasm file from a Rust crate, but it's a lot of hand holding from there. Fortunately, there are amazing projects being developed around the wasm and Rust ecosystem that allow for more higher-level, intuitive interaction with JavaScript and Rust, and vice versa. We'll explore one such project, called wasm-bindgen, and build a real-world web application soon.

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

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