Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorBrian Sletten

WebAssembly: The Definitive Guide provides a thorough and accessible introduction to one of the most transformative technologies hitting our industry. What started as a way to use languages other than just JavaScript in the browser has evolved into a comprehensive path toward portability, performance, increased security and greater code reuse across an impressive collection of deployment targets. The goals may sound familiar, but in practice, we're finally getting our safe, fast, portable, and secure software development environment offering the potential for reuse. This practical book introduces the elements of this technology incrementally while building to several concrete, code-driven examples of practical but cutting edge WebAssembly uses.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# WebAssembly: The Definitive Guide — Reading Guide ## 【One-Line Pitch】 A practical, incremental introduction to WebAssembly that takes you from the first lines of WAT (WebAssembly Text format) through compiling C/C++ to running real applications in the browser — ideal for developers who want portable, fast, and secure code without abandoning their favorite languages. ## 【Book Arc】 - **Opening (~0%–10%)**: Establishes why WebAssembly matters — the problems with JavaScript-only development, dependency bloat, and the historical attempts at portable code. Sets the stage for WebAssembly as a solution that is "safe, fast, and portable" without being an assault on JavaScript. - **Early (~10%–23%)**: Introduces the fundamentals — the stack-based execution model, WAT syntax, and how to compile and inspect modules using tools like `wat2wasm` and `wasm-objdump`. Covers module instantiation patterns, including the shift from `instantiate` to the more efficient `instantiateStreaming`. - **Early-to-Middle (~23%–39%)**: Dives into memory management — how `ArrayBuffer` and `TypedArray` views work, reading strings from shared memory, and the mechanics of passing data between JavaScript and WebAssembly. Transitions into C compilation with `clang` and the realities of manual memory handling. - **Middle (~39%–48%)**: Introduces Emscripten as the practical toolchain for porting C/C++ to the web. Covers the "proper Hello World," build systems like Makefiles, and the operational challenges of adapting native code (main loops, I/O) to the browser environment. - **Late (~48%–end)**: Explores real-world use cases — including a bitmap image processing example and a discussion of libsodium (cryptography) as motivation for mixing C/C++ with the browser. Emphasizes the broader ecosystem and the potential for code reuse across deployment targets. ## 【Key Takeaways】 - **WebAssembly is about options, not replacing JavaScript** (Early): It lets you solve problems in the language of your choice without caring what language other components are written in — increasing code reuse and business value longevity. - **The stack machine model is the core mental model** (Early): Understanding how instructions push/pop values on a stack (e.g., `i32.sub`) is essential for reading WAT and debugging modules — even simple arithmetic becomes a sequence of explicit operations. - **Load-time performance matters as much as runtime performance** (Early): Using `WebAssembly.instantiateStreaming` instead of fetching then compiling in two steps reduces latency by compiling while bytes are still arriving over the network. - **Memory is shared, not copied** (Early): `ArrayBuffer` holds raw bytes; `TypedArray` views (like `Uint32Array` vs `Uint8Array`) interpret those bytes differently. Understanding this distinction is critical for passing strings and structured data across the JS/Wasm boundary. - **Manual C integration is possible but painful** (Middle): Using `clang` directly requires you to manage headers, libraries, and pointer emulation yourself — a viable learning exercise but not sustainable for real projects. - **Emscripten is the practical path for C/C++** (Middle): Built on LLVM, it handles the hard parts — memory allocation, module linking, and adapting native main loops to the browser's single-threaded environment — making "Hello World" actually simple. - **Real-world libraries are the payoff** (Late): Projects like libsodium (cryptography) and bitmap image processing demonstrate that WebAssembly can bring serious native code to the web with minimal friction, opening doors for performance-critical and security-sensitive applications. ## 【Reading Tips】 - **Skim the historical context** (Opening): The early chapters on the web's evolution and prior portability attempts are interesting but not essential — jump ahead if you're eager to write code. - **Deep-read the stack machine and memory chapters** (Early): These are the conceptual foundation. Work through the examples with `wat2wasm` and `wasm-objdump` yourself — the debugging workflow is a skill you'll reuse. - **Don't get bogged down in assembly listings** (Early): The `clang -S` output in Chapter 5 is illustrative but not something you need to memorize. Focus on the *why* (platform-specific code) rather than the *how* (each instruction). - **Treat Emscripten as the main event** (Middle): Once you hit the Emscripten chapters, slow down. The Makefile and build-system details are practical — copy them, modify them, and get a real C++ project running in your browser. - **Use the companion repository**: The book references `https://github.com/bsletten/wasm_tdg` for all code examples — clone it early and run examples as you read rather than typing everything by hand. ## 【Coverage Limits】 This guide covers the book's progression from fundamentals through practical C/C++ integration, but the excerpts do not cover later chapters on advanced topics like WASI (WebAssembly System Interface), threading, or deployment beyond the browser — those would require reading the full text. ##
Page 8
l chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a questi...
View in text
Excerpt 2
a mathematical sequence of events in a platform-neutral way. When the code is ultimately converted to native instructions in a runtime host, the values will ...
View in text
Excerpt 3
tartproc ## %bb.0: pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset %rbp, -16 movq %rsp, %rbp .cfi_def_cfa_register %rbp movl %edi, -4(%rbp) movl ...
View in text
Excerpt 4
requires an example image.bmp file in the current directory. I just found one online and dropped it in place with that name. After running the command, you w...
View in text
Excerpt 5
cessing GPUs from JavaScript, but there is via WebGL, which is supported in nearly every browser. By pretending to do 3D graphics, the Figure 9-4. Relative p...
View in text
Excerpt 6
onments are not always here to meet the expectations of the code we are running. We can still be in control. what was passed into the program. File descripto...
View in text
Excerpt 7
for you to learn about them as they add support over time. If the whole process seems a little hodgepodge, well, that is a fair criticism, which is why there...
View in text
Excerpt 8
limited to native applications, not running in the browser. This is for reasons that I hope are pretty clear by now. The native libraries that provide the co...
View in text
Tags
AI categories
Web TechnologyProgramming LanguageBackend
ISBN: 1492089834
Publish Year: 2021
Language: English
Pages: 437
File Format: PDF
File Size: 8.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…