WebAssembly from the Ground Up with Patrick Dubroy and Mariano Guerra
By Chrome for Developers
Key Concepts
- WebAssembly (Wasm): A binary instruction format for a stack-based virtual machine, designed as a portable compilation target for high-level languages like C, C++, and Rust, enabling deployment on the web for client and server applications.
- Compiler: A program that translates code written in one programming language (the source language) into another programming language (the target language).
- WebAssembly Text Format (WAT): A human-readable representation of WebAssembly modules, used for debugging and prototyping.
- WebAssembly Binary Format (Wasm): The compact, efficient binary representation of WebAssembly modules, designed for machine consumption.
- M (OM): A JavaScript library for building parsers, interpreters, and compilers, particularly useful for prototyping new programming languages.
- Wafer: A simple programming language created for the book "Web Assembly from the Ground Up," designed to map closely to WebAssembly.
- Dragon Book (Compilers: Principles, Techniques, and Tools): A classic textbook on compiler design, often criticized for being dense and outdated.
- Recursive Descent Parser: A parsing method that uses a set of recursive procedures to parse an input stream.
- Concrete Syntax Tree (CST): A tree representation of the syntactic structure of source code, reflecting the grammar of the programming language.
- WebAssembly GC (Garbage Collection): A proposed feature for WebAssembly that would add automatic memory management capabilities.
- Wasmtime: A runtime for WebAssembly.
- LLVM (Low Level Virtual Machine): A collection of modular and reusable compiler and toolchain technologies.
Introduction to the Book: "Web Assembly from the Ground Up"
Patrick and Mariano, authors of "Web Assembly from the Ground Up," discuss their motivation and process for creating an online book that teaches WebAssembly by building a simple compiler in JavaScript. The book, developed over 2.5 years, aims to demystify WebAssembly by allowing readers to learn by doing, rather than just learning how to compile to WebAssembly from other languages.
Motivation and Background for Writing the Book
- Learning by Teaching: Both authors share a philosophy of learning complex topics by teaching them. Mariano has a long history of writing tutorials and short books, while Patrick has used talks as a motivation to learn.
- Interest in Compilers and WebAssembly: Mariano has a background in compilers and has been interested in WebAssembly since its inception in 2015. He saw an opportunity to apply compiler-building principles to WebAssembly.
- Collaborative Effort: The idea for the book originated from a conversation between Mariano and Patrick in Munich. They decided to join forces, acknowledging that their initial timeline estimates were significantly underestimated.
- Hobby Project: The book was developed as a hobby project, allowing them the flexibility to dedicate time to it.
- Making Compilers Accessible: A key motivation was to demonstrate that building compilers doesn't have to be "dark magic" and can be approached with simpler, understandable steps.
The Book's Approach and Target Audience
- Hands-on Learning: The book takes a practical, hands-on approach, guiding readers to generate WebAssembly binary format manually and then build a library to simplify this process.
- Contrast with Traditional Tutorials: Unlike most WebAssembly tutorials that focus on compiling existing code (e.g., C/C++) to WebAssembly using toolchains like Emscripten, this book teaches how to write WebAssembly itself.
- Demystifying WebAssembly: The authors observed that WebAssembly was often presented as a complex "blob of bits" or solely a target for toolchains, leading to the perception that it's too difficult to write or generate directly.
- Focus on Core Concepts: By writing WebAssembly by hand, readers can understand its core concepts, instruction set, and formal specification more deeply. The book aims to break down the perceived complexity by presenting WebAssembly's relatively small instruction set and clear specification.
- Benefits of Understanding WebAssembly Internals: Even for those who will use WebAssembly as a target, understanding its inner workings, capabilities, limitations, and trade-offs enhances their overall software engineering skills.
Replacing the Dragon Book with WebAssembly
The authors suggest that WebAssembly is a suitable modern target for teaching compiler engineering, potentially replacing or supplementing the classic "Dragon Book."
- Dragon Book Criticisms: The Dragon Book is perceived as intimidating ("scary dragon") and somewhat outdated, with many learners finding it difficult.
- WebAssembly as a Target: WebAssembly is presented as a simpler, formally specified, and well-documented target. Its spec is readable and concise.
- Cross-Platform Benefits: Targeting WebAssembly provides automatic cross-platform compatibility (x86, ARM, etc.) and access to modern tooling.
- LLVM vs. WebAssembly: While LLVM is another viable target, it is significantly larger and more complex to learn than WebAssembly.
- Simplicity and Directness: The authors advocate for targeting simpler, direct constructs when teaching, as excessive abstraction layers can confuse beginners. The book's primary dependencies are JavaScript.
The Role of M and the Wafer Language
The book utilizes the M library and introduces a custom language called Wafer to facilitate learning.
- M Library:
- Origin: M (originally OM) was developed at Y Combinator Research (formerly HARK) by Alex Warf and others, including Patrick.
- Purpose: Designed to simplify the creation of programming languages, parsers, interpreters, and compilers in JavaScript.
- Bootstrapping Languages: M allows developers to quickly define a grammar and generate a parser, significantly reducing the time needed to prototype a new language compared to manual parser implementation.
- Focus on Backend: By using M for parsing, the book can focus on the more interesting "backend" aspects of compilation, such as code generation for WebAssembly.
- Wafer Language:
- Design: Wafer was created specifically for the book as a simple language that maps directly to WebAssembly.
- Learning Progression: The book uses Wafer to demonstrate concepts incrementally. Readers start with a minimal compiler and gradually add features like arithmetic operations.
- Short Feedback Loops: This approach provides short, understandable feedback loops, allowing learners to see the results of their efforts at each step, unlike traditional compiler courses where the final output might be out of reach.
Compiling Grammars to WebAssembly with M
Patrick discusses a project to compile M grammars directly to WebAssembly parsers.
- Meta-Project: This project involves using WebAssembly to create parsers for M itself.
- Cross-Language Accessibility: The goal is to make M accessible from languages other than JavaScript (e.g., Go, Rust) by compiling M grammars into WebAssembly modules that act as parsers.
- Leveraging Book's Library: This project utilizes the simple assembler library developed within the book, demonstrating its practical application and verifying its completeness.
Omission of the WebAssembly Text Format (WAT)
The authors intentionally excluded the WebAssembly Text Format from the book.
- Reasoning: They argue that once the execution model and binary format are understood, the text format is not strictly necessary as it's just a different representation of the same concepts.
- Focus on Binary: The primary goal of a compiler is to emit the binary format, which is more efficient for machines.
- Practical Use of WAT: Despite its omission from the book, the authors acknowledge the utility of WAT for debugging, prototyping, and human readability. They use it extensively themselves for these purposes.
- Alternative Resources: For those interested in WAT, the book recommends Dominic Elm's "Learn Web Assembly" course and the official WebAssembly specification.
- WAT's Syntactic Sugar: WAT offers syntactic sugar, including list-style and stack-style representations, and optional sections that can be generated by the runtime.
Future Plans and Potential "Part Two"
The authors express enthusiasm for expanding on the book's content.
- Wasm 2.0 Update: A likely immediate step is updating the book to reflect the Wasm 2.0 specification.
- WebAssembly GC: The addition of WebAssembly Garbage Collection is a significant feature that could warrant a separate book or extensive supplement, requiring updates to the Wafer language.
- Modern Pascal to WebAssembly: A dream project is a second book that creates a "modern Pascal" compiling to WebAssembly, incorporating WebAssembly GC, structured types, and arrays. This is inspired by the simplicity and comprehensiveness of the Pascal family of languages.
- Interpreter/Virtual Machine: They are currently working on a series of blog posts exploring WebAssembly execution semantics by building an interpreter or virtual machine, which could potentially become chapters for a future edition.
- Validation and Type Systems: Exploring WebAssembly's validation phase as a simple type checker is another area of interest.
Surprises and Challenges in Writing the Book
- Interpreting Feedback: A significant surprise was the difficulty in interpreting and acting upon reader feedback. While feedback was valuable, it was often conflicting or suggested solutions that didn't align with the authors' vision. They emphasize the importance of intuition and personal taste in the writing process.
- Tooling Development: The authors had to build substantial custom tooling for various aspects of the book's creation, including review management, payment processing, code generation, and validation. The book's development was approached like a software project with staging environments and deployments.
- Diverse Audience Backgrounds: They were surprised by the diverse technical backgrounds of their readers and workshop attendees, many of whom were not primarily JavaScript/TypeScript developers but had strong reasons to learn WebAssembly. This reinforced the need for a clear and accessible subset of JavaScript in the book.
"Wasm Not Wasm" - Current Streaming Habits
- Patrick: Recently watched Season 2 of "Quarterback" on Netflix, finding it interesting to see how top athletes approach their work, even though he doesn't follow NFL.
- Mariano: Follows talks from the "Better Software Conference" and has recently resumed watching Formula 1, particularly enjoying post-race analysis from experts.
Local Get, Global Set: Personal Aspirations and Community Support
- Patrick's "Global Set": Encourages more people to write technical content (blog posts, newsletters, books). He believes in the value of reading diverse perspectives and highlights authors like Max Bernstein and Torsten Ball.
- Mariano's "Global Set": Advocates for supporting and promoting individuals and projects with smaller audiences or platforms doing interesting work. He mentions the Gleam programming language, Sonic Pi, and the "Future of Coding" newsletter as examples. He suggests promoting interesting content and supporting creators economically or through their work.
Reaching the Authors
The authors can be reached via:
- Blue Sky and Mastodon: Links to their profiles and the book's accounts are available in the show notes.
- Blog: The book's blog has an RSS feed.
- Mailing List: For updates.
Conclusion
The discussion highlights the authors' passion for WebAssembly and compiler education. "Web Assembly from the Ground Up" offers a unique, hands-on approach to understanding WebAssembly by building a compiler, emphasizing accessibility and demystifying complex topics. The authors' experiences underscore the challenges and rewards of creating technical content and their commitment to fostering a community around learning and sharing knowledge.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "WebAssembly from the Ground Up with Patrick Dubroy and Mariano Guerra". What would you like to know?