This is a review of the book WebAssembly in Action, an hands-on guide to bring your C++ code to the browser (or Node.js) using WebAssembly.

What is WebAssembly

This is a book that contains information about both the theory and the practice of using WebAssembly to bring C++ code on the browser. If you do not know what WebAssembly is, this is an explanation:

WebAssembly, or wasm, is a low-level bytecode format for in-browser client-side scripting. If you are writing a compiler for a programming language one option is to target a platform, like the JVM or .NET, and compile your language to the target bytecode. WebAssembly occupies the same role, so when you compiling to WebAssembly you are making your software available for all platforms where it is supported, in other words all browsers.

In practical terms, WebAssembly is implemented by browsers’ developers on the back of the existing JavaScript engines. Essentially, it is designed to substitute JavaScript as the destination of compilers and transpilers on the web.

If you need a more in-depth introduction of WebAssembly you can read our article about it: Understand WebAssembly: Why it Will Change the Web.

Still, this does not explain why you should care about WebAssembly. In dramatic terms: the monopoly of JavaScript in our lives in the browser is over. In less dramatic terms: you can now program in other languages and use them in the browser. This is good for productivity and amazing for re-use and maintenance. You can now bring your existing codebase in the browser. You can have the core functionality of your software in one library an re-use everywhere, from mobile to the browser. Of course it will not be trivial to bring a 20+ years old codebase in the browser, but now at least it is feasible.

Is This Book for You?

The book recommends having a basic understanding of C++ and JavaScript. Although, to be fair, it also explains basic elements (like what is the std::vector class) that should be common knowledge even among novice users of C++. One thing to notice is that the book goes really in-depth into explaining the inner workings of WebAssembly. This is true to the point that it dedicates a chapter to build a WebAssembly module by writing WebAssembly text format by hand. So, it could also be useful if you need in-depth technical information about WebAssembly and plan to use another language, other than C++.

Part of the example program written in WebAssembly Text Format

How the Book is Organized

The book is divided in four parts: two are dedicated to explaining the WebAssembly format and how to use it, the others two are dedicated to advanced topics.

The four parts are:

  • First steps. The first one is dedicated to explaining what is WebAssembly and a detailed description of the format of a module. The last chapter of this part shows how to create your first WebAssembly module (i.e., a program in a sense) with all the basic information to use Emscripten (i.e., the tool that compile C++ in WebAssembly)
  • Working with modules. This part explains how to adapt an existing C++ codebase to use it with WebAssembly. In addition to that shows the different methods that you can use to interact between the JavaScript and C++ parts.
  • Advanced Topics. It is a collection of different advanced topics: combining dynamically multiple WebAssembly modules, using web workers and threads and using WebAssembly in Node.js
  • Debugging and Testing. The last part also contains a diverse collection of topics: an example program used to describe the WebAssembly Text Format, how to debug WebAssembly code and how to organized testing (with JavaScript) both in the browser and on Node.js

There are also a few appendices going into even more detail about Emscripten (i.e., the tool used to transpile C++ into WebAssembly), the WebAssembly Text Format and interoperability between JavaScript and C++.

Should You Buy This Book?

In terms of classification this is a peculiar book: it is a guide, but also a reference book with chapters detailing every single possible combination of elements. It is about WebAssembly, but also the specific C/C++ WebAssembly tooling (i.e., Emscripten). It leads you to very advanced topics, like debugging code by looking at the WebAssembly text format, but it also assumes you know nothing about how to test software.

This is what I mean when I say that is also a reference book:

  • it shows the code of one example and then it shows you how to use it if you include standard C/C++ functions in the WebAssembly module (e.g., malloc) or how to use it if you replace it with your own version of these basic functions
  • It shows you how to use it if you use function pointers to communicate between C++ and JavaScript code, or how to do it if you prefer to just call directly the functions.
  • It shows how to call functions in the WebAssembly module using the WebAssembly helper functions or directly from JavaScript (but then if you have to manage the memory allocation yourself).

It is very thorough, and I honestly cannot think of a combination or option that was not explored.

The author notes that taking advantage of WebAssembly helper functions and glue code is recommended in production. However, he is also very careful to hold your hand in case you need or want to use WebAssembly directly: from loading a module to passing memory to it. This second action is more complicated than you might think: for security reasons WebAssembly memory is separated from standard JavaScript memory, so you have to copy data to a memory array reserved to this scope. This book shows you to allocate and free the memory in there. This may seem trivial to C/C++ expert developers, but it is a bit daunting if you have always used programming languages with managed memory.

Despite the fact that the book tells you that you have to know C++ and JavaScript, it is very forgiving and basically assumes you know very little. For instance, it guides you even on the basics of doing unit testing on both the browser and the server (on Node.js), which should be common knowledge. The part that I liked the most however, is literally how the book is written: there are code, diagrams and detailed comments for everything.

For example, this is what you can see throughout the book. A diagram explaining the organization of the current example:

A diagram that shows the organization of a WebAssembly project in the browser

Every piece of code that was written or edited for the current project:

Code changed in a file for a specific project

And a detailed explanation of most complex changes:

Code for memory allocation in a project

The positive side is that the book is really clear. I cannot think of a passage that was hard to follow, or a piece of code which I did not understand. At the end of each chapter there is also a summary of what was learned. On the other hand, if you like quick books which only show you code, you will find that the book is longer than you will like. Basically, if you complain about repetition and filler content, this is not the book for you. However, I think that this approach works best for the topic at hand: WebAssembly is a technology that bridges very different worlds. When have you ever seen JavaScript and C++ together, before this?

Summary

This is a great book if you are looking for a clear guide on how to use WebAssembly and how to bring C++ code into the browser. Every aspect is considered, from the things to pay attention in writing the original C++ code, to testing the results in JavaScript. Everything is very well explained, and it is easy to follow what is going on. This book has both theory and code. It even has exercises and an appendix with solution to each exercise. This is how university books should be written.

It is also a good book for people looking for a detailed explanation on the WebAssembly format because they want to build their own WebAssembly tooling. Yes, you could find the same information online, but you will take hours to find it and here it is explained better. For example, the description of how to interpret control flow statements bytecode in a stack-based machine it is fundamental to make WebAssembly tools. And if you have never worked on compilers, you might not even be aware that you need to know that.

The only people that would not like this book are the ones looking for a quick read: what are to some detailed explanations and a careful examination of each option might seem to them just a lot of filler content.

Disclaimer: the publisher gave us a copy of the book for the review and we do have an affiliate agreement, but our review is honest. To avoid a conflict of interest we simply do not publish reviews of books we do not like.

The Best Programming Languages

Get the guide to the best programming languages to use in each situation delivered to your email and read it when you want on the device you want

Powered by ConvertKit