Practical WebAssembly

Book description

Understand the basic building blocks of WebAssembly and learn, install, and use various tools from the Rust and WebAssembly ecosystem

Key Features

  • Understand the Rust programming language and WebAssembly concepts for web development
  • Build web, mobile, and embedded apps using WebAssembly
  • Enhance the scalability and resilience of your web apps

Book Description

Rust is an open source language tuned toward safety, concurrency, and performance. WebAssembly brings all the capabilities of the native world into the JavaScript world. Together, Rust and WebAssembly provide a way to create robust and performant web applications. They help make your web applications blazingly fast and have small binaries. Developers working with JavaScript will be able to put their knowledge to work with this practical guide to developing faster and maintainable code.

Complete with step-by-step explanations of essential concepts, examples, and self-assessment questions, you'll begin by exploring WebAssembly, using the various tools provided by the ecosystem, and understanding how to use WebAssembly and JavaScript together to build a high-performing application. You'll then learn binary code to work with a variety of tools that help you to convert native code into WebAssembly. The book will introduce you to the world of Rust and the ecosystem that makes it easy to build/ship WebAssembly-based applications.

By the end of this WebAssembly Rust book, you'll be able to create and ship your own WebAssembly applications using Rust and JavaScript, understand how to debug, and use the right tools to optimize and deliver high-performing applications.

What you will learn

  • Explore WebAssembly and the different tools available in the WebAssembly ecosystem
  • Understand the raw WebAssembly binary and the WebAssembly text format
  • Use the Web and JavaScript API with wasm-bindgen
  • Optimize Rust and WebAssembly for high performance
  • Run and debug WebAssembly and Rust code
  • Explore various tools available in the RustWASM ecosystem

Who this book is for

This book is for JavaScript developers who want to deliver better performance and ship type-safe code. Rust developers or backend engineers looking to build full-stack applications without worrying too much about JavaScript programming will also find the book useful.

Table of contents

  1. Practical WebAssembly
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
  6. Section 1: Introduction to WebAssembly
  7. Chapter 1: Understanding LLVM
    1. Technical requirements
    2. Understanding compilers
      1. Compiled languages
      2. Compiler efficiency
    3. Exploring LLVM
    4. LLVM in action
    5. Summary
  8. Chapter 2: Understanding Emscripten
    1. Technical requirements
    2. Installing Emscripten using emsdk
    3. Generating asm.js using Emscripten
    4. Running Hello World with Emscripten in Node.js
    5. Running Hello World with Emscripten in the browser
    6. Exploring other options in emsdk
      1. Listing the tools and SDK
      2. Managing the tools and SDK
    7. Understanding various levels of optimizations
      1. Optimizations
      2. Closure Compiler
    8. Summary
  9. Chapter 3: Exploring WebAssembly Modules
    1. Technical requirements
    2. Understanding how WebAssembly works
      1. Understanding JavaScript execution inside the JavaScript engine
      2. Understanding WebAssembly execution inside the JavaScript engine
    3. Exploring the WebAssembly text format
      1. Building a function in WebAssembly text format
    4. Summary
  10. Section 2: WebAssembly Tools
  11. Chapter 4: Understanding WebAssembly Binary Toolkit
    1. Technical requirements
    2. Getting started with WABT
      1. Installing WABT
    3. Converting WAST into WASM
    4. Converting WASM into WAST
      1. -f or --fold-exprs
    5. Converting WASM into C
      1. simple.h
      2. simple.c
    6. Converting WAST into JSON
    7. Understanding a few other tools provided by WABT
      1. wasm-objdump
      2. wasm-strip
      3. wasm-validate
      4. wasm-interp
    8. Summary
  12. Chapter 5: Understanding Sections in WebAssembly Modules
    1. Technical requirements
    2. Exports and imports
      1. Exports
      2. Imports
    3. Globals
    4. Start
    5. Memory
    6. Summary
  13. Chapter 6: Installing and Using Binaryen
    1. Technical requirements
    2. Installing and using Binaryen
      1. Linux/macOS
      2. Windows
    3. wasm-as
    4. wasm-dis
    5. wasm-opt
    6. wasm2js
    7. Summary
  14. Section 3: Rust and WebAssembly
  15. Chapter 7: Integrating Rust with WebAssembly
    1. Technical requirements
    2. Installing Rust
    3. Converting Rust into WebAssembly via rustc
    4. Converting Rust into WebAssembly via Cargo
    5. Installing wasm-bindgen
    6. Converting Rust into WebAssembly via wasm-bindgen
    7. Summary
  16. Chapter 8: Bundling WebAssembly Using wasm-pack
    1. Technical requirements
    2. Bundling WebAssembly modules with webpack
    3. Bundling WebAssembly modules with Parcel
    4. Introducing wasm-pack
      1. Why do you need wasm-pack?
      2. How to use wasm-pack
    5. Packing and publishing using wasm-pack
    6. Summary
  17. Chapter 9: Crossing the Boundary between Rust and WebAssembly
    1. Technical requirements
    2. Sharing classes from Rust with JavaScript
    3. Sharing classes from JavaScript with Rust
    4. Calling the JavaScript API via WebAssembly
    5. Calling closures via WebAssembly
    6. Importing the JavaScript function into Rust
    7. Calling a web API via WebAssembly
    8. Summary
  18. Chapter 10: Optimizing Rust and WebAssembly
    1. Technical requirements
    2. Minimizing the WebAssembly modules
    3. Analyzing the memory model in the WebAssembly module
      1. Sharing memory between JavaScript and WebAssembly using Rust
      2. Creating a memory object in JavaScript to use in the Rust application
    4. Analyzing the WebAssembly module with Twiggy
      1. top
      2. monos
      3. garbage
    5. Summary
    6. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Practical WebAssembly
  • Author(s): Sendil Kumar Nellaiyapen
  • Release date: May 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781838828004