The Art of WebAssembly
Build Secure, Portable, High-Performance Applications
THE ART OF WEBASSEMBLY. Copyright 2021 by Rick Battagline.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-7185-0144-7 (print)
ISBN-13: 978-1-7185-0145-4 (ebook)
Publisher: William Pollock
Executive Editor: Barbara Yien
Production Editor: Katrina Taylor
Developmental Editor: Liz Chadwick
Cover Design: Josh Ellingson
Interior Design: Octopod Studios
Technical Reviewer: Conrad Watt
Copyeditor: Anne Marie Walker
Compositor: Maureen Forys, Happenstance Type-O-Rama
Proofreader: James Fraleigh
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900;
www.nostarch.com
Library of Congress Control Number: 2021930212
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
In loving memory of my grandmother, Sue Battagline. I miss you dearly.
About the Author
Rick Battagline is a game developer and author of Hands-On Game Development with WebAssembly (Packt Publishing, 2019). He has been working with browser-based technologies since 1994, and in 2006 founded BattleLine Games LLC., an independent game studio focused on web games. His game Epoch Star was nominated for an award at the Slamdance Guerilla Games Competition. Since then, he has written hundreds of games developed in various web technologies including WebAssembly, HTML5, WebGL, JavaScript, TypeScript, ActionScript, and PHP.
About the Tech Reviewer
Conrad Watt is a Research Fellow at Peterhouse, University of Cambridge. Prior to his PhD in computer science at the University of Cambridge, he completed an undergraduate degree at Imperial College London. His research is primarily focused on the formal semantics and security characteristics of WebAssembly. He developed and maintains WasmCert-Isabelle, the first mechanization, in Isabelle/HOL, of the WebAssembly languages semantics.
Conrad is an active participant in the WebAssembly Community Group and continues to contribute to the specification of new language features, with a particular focus on threads and concurrency. His research into the relaxed memory characteristics of JavaScript and WebAssembly is a key component of WebAssemblys threads specification. Outside of his professional life, his two greatest interests would ordinarily be choral singing and foreign travel, both of which are inadvisable at the time of this writing.
Foreword
With every other language compiling to JavaScript nowadays, WebAssembly is the next step in an evolution to break free from these pre-existing boundaries. WebAssembly enables everyones favorite language to run on the web performantly, while bringing with it the potential to redefine how you can bundle up reusable software components that run not only on the web but on any platform, from blockchain to edge computing to IoT.
While the technology is still young and requires some time to mature, its sheer potential has inspired many people from all backgrounds to explore where we can take this journey. One example is AssemblyScript, a project Rick and I are working on, that looks at WebAssembly less from a systems language perspective, and more exploring the potential of fusing the best of JavaScript and the web platform with WebAssembly. AssemblyScript makes it possible to compile a variant of JavaScript that resembles TypeScript to WebAssembly, yielding super small and efficient modules, while enabling everyone with a JavaScript background to take advantage of what WebAssembly has to offer today.
There are many exciting facets to WebAssembly and its feature set, especially to those who like to explore and help shape the future of the technology. The Art of WebAssembly lays out a solid foundation to dive deeper into what may well become the future of computing, and in particular on the web.
Daniel Wirtz Creator of AssemblyScript
Acknowledgments
Thank you, Liz Chadwick. She spent a tremendous amount of time working on the first several drafts of this book. Her tireless efforts transformed my vague ideas and stream-of-consciousness writing into a coherent, well-polished draft. If you enjoy reading this book, you can attribute that to Lizs effort throughout its development.
I want to thank Conrad Watts (Invited Expert in the W3C WebAssembly Working Group), who generously provided the books technical review. His brilliance humbles me. I cannot overstate his technical expertise in this field. Any technical errors in this book were almost certainly introduced after his thorough and deeply technical review.
Thanks to Katrina Taylor and Anne Marie Walker (copyeditor). I truly appreciate the work you did to make the book ready for printing. Thank you to my friends Vineet Kapur, Steve Tack, and Terri Cohen, who took the time to read my first draft of the book and give me feedback. All of you helped me make this a better book.
Finally, thanks to Bill Pollock. Your input at critical moments helped me move forward and complete this book.
Introduction
Welcome to The Art of WebAssembly. This book teaches you how to read, write, and understand WebAssembly at the virtual machine level. It will help you learn how WebAssembly interacts with JavaScript, the web browser, and the embedding environment. By the end, youll comprehend what WebAssembly is, its ideal use cases, and how to write WebAssembly that performs at near-native speeds.
Who Should Read This Book
This book is for web developers interested in understanding when and why to use WebAssembly. If you truly want to grasp WebAssembly, you need to learn it in detail. Several books have been written on various WebAssembly toolchains. This book is not specific to writing C/C++ or Rust, or any other language for WebAssembly; instead, it explores WebAssemblys mechanisms and capabilities.
This book is for users who want to understand what WebAssembly is, what it can do, and how to use it best. WebAssembly can perform better and create smaller downloads and memory footprints than JavaScript. But developing high-performing WebAssembly applications requires more than simply writing an app in a language like C++/Rust or AssemblyScript and compiling it in WebAssembly. To build an application that executes two or three times as fast as its JavaScript equivalent, youll need to know how WebAssembly works at a deeper level.
Next page