R I C K B A T T A G L I N E T H E A R T O F W E B A S S E M B L Y B U I L D S E C U R E , P O R T A B L E , H I G H - P E R F O R M A N C E A P P L I C A T I O N S
(This page has no text content)
(This page has no text content)
San Francisco T H E A R T O F W E B A S S E M B LY B U I L D S E C U R E , P O R T A B L E , H I G H - P E R F O R M A N C E A P P L I C A T I O N S
[S] 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; info@nostarch.com 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.
(This page has no text content)
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 tech- nologies 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 charac- teristics of WebAssembly. He developed and maintains WasmCert-Isabelle, the first mechanization, in Isabelle/HOL, of the WebAssembly language’s 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 WebAssembly’s threads specification. Outside of his profes- sional life, his two greatest interests would ordinarily be choral singing and foreign travel, both of which are inadvisable at the time of this writing.
(This page has no text content)
B R I E F C O N T E N T S Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1: An Introduction to WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: WebAssembly Text Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 3: Functions and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 4: Low-Level Bit Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter 5: Strings in WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter 6: Linear Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Chapter 7: Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Chapter 8: Working with the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Chapter 9: Optimizing Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Chapter 10: Debugging WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Chapter 11: AssemblyScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
(This page has no text content)
C O N T E N T S I N D E T A I L FOREWORD xv ACKNOWLEDGMENTS xvii INTRODUCTION xix Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Why Users Are Interested in WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx Why the World Needs WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi 1 AN INTRODUCTION TO WEBASSEMBLY 1 What Is WebAssembly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Reasons to Use WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Better Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Integrating Legacy Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Portability and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 JavaScript Skeptics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 WebAssembly’s Relationship with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Why Learn WAT? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 WAT Coding Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The Embedding Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 WASI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Node .js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Our First Node .js WebAssembly App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Calling the WebAssembly Module from Node .js . . . . . . . . . . . . . . . . . . . . . . 15 The .then Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 The Time Is Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 WEBASSEMBLY TEXT BASICS 19 Writing the Simplest Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Hello World in WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Creating Our WAT Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Creating the JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 WAT Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Global Variables and Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Unpacking S-Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Indexed Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Converting Between Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 if/else Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
xii Contents in Detail Loops and Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The block Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The loop Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Using block and loop Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Branching with br_table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3 FUNCTIONS AND TABLES 45 When to Call Functions from WAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Writing an is_prime Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Passing Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Creating Internal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Adding the is_prime Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Declaring an Imported Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 JavaScript Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Passing Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Objects in WAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Performance Implications of External Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Function Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Creating a Function Table in WAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4 LOW-LEVEL BIT MANIPULATION 69 Binary, Decimal, and Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Integers and Floating-Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 High- and Low-Order Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Bit Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Shifting and Rotating Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Masking Bits with AND and OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 XOR Bit Flip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Big-Endian vs . Little-Endian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5 STRINGS IN WEBASSEMBLY 87 ASCII and Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Strings in Linear Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Passing the String Length to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Null-Terminated Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Length-Prefixed Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Copying Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Creating Number Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Setting a Hexadecimal String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Setting a Binary String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Contents in Detail xiii 6 LINEAR MEMORY 115 Linear Memory in WebAssembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 JavaScript Memory Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Creating the WebAssembly Memory Object . . . . . . . . . . . . . . . . . . . . . . . . 121 Logging to the Console with Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Creating the JavaScript in store_data .js . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Base Address, Stride, and Offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Loading Data Structures from JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Displaying the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Collision Detection Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 7 WEB APPLICATIONS 139 The DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Setting Up a Simple Node Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Our First WebAssembly Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Defining the HTML Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 The JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 The HTML Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Our Completed Web App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Hex and Binary Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 The HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 The WAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Compile and Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8 WORKING WITH THE CANVAS 157 Rendering to the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Defining the Canvas in HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Defining JavaScript Constants in HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Creating Random Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Bitmap Image Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 The requestAnimationFrame Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 The WAT Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Imported Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Clearing the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Absolute Value Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Setting a Pixel Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Drawing the Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Setting and Getting Object Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 The $main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Compiling and Running the App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
xiv Contents in Detail 9 OPTIMIZING PERFORMANCE 185 Using a Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Chrome Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Firefox Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 wasm-opt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Installing Binaryen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Running wasm-opt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Looking at Optimized WAT Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Strategies for Improving Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Inlining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Multiply and Divide vs . Shift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 DCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Comparing the Collision Detection App with JavaScript . . . . . . . . . . . . . . . . . . . . . . 205 Hand Optimizing WAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Logging Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 More Sophisticated Testing with benchmark .js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Comparing WebAssembly and JavaScript with - -print-bytecode . . . . . . . . . . . . . . . . . 219 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 10 DEBUGGING WEBASSEMBLY 223 Debugging from the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Logging Messages to the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Using Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Stack Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 The Firefox Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 The Chrome Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 11 ASSEMBLYSCRIPT 247 AssemblyScript CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Hello World AssemblyScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 JavaScript for Our Hello World App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Hello World with the AssemblyScript Loader . . . . . . . . . . . . . . . . . . . . . . . . 253 AssemblyScript String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Object Oriented Programming in AssemblyScript . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Using Private Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 JavaScript Embedding Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 AssemblyScript Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Extending Classes in AssemblyScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Performance of Loader vs . Direct WebAssembly Calls . . . . . . . . . . . . . . . . . 264 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 FINAL THOUGHTS 269 INDEX 271
F O R E W O R D With every other language compiling to JavaScript nowadays, WebAssembly is the next step in an evolution to break free from these pre-existing bound- aries. WebAssembly enables everyone’s 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, espe- cially to those who like to explore and help shape the future of the technol- ogy. 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
(This page has no text content)
A C K N O W L E D G M E N T S Thank you, Liz Chadwick. She spent a tremendous amount of time work- ing 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 Liz’s effort throughout its development. I want to thank Conrad Watts (Invited Expert in the W3C WebAssembly Working Group), who generously provided the book’s 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.
(This page has no text content)
Comments 0
Loading comments...
Reply to Comment
Edit Comment