Programming Webassembly with Rust Unified Development for Web, Mobile, and Embedded Applications

Programming Webassembly with Rust Unified Development for Web, Mobile, and Embedded Applications

Prepared exclusively for luming Under Construction: The book you’re reading is still under development. As part of our Beta book program, we’re releasing this copy well before a normal book would be released. That way you’re able to get this content a couple of months before it’s available in finished form, and we’ll get feedback to make the book even better. The idea is that everyone wins! Be warned: The book has not had a full technical edit, so it will contain errors. ßIt has not been copyedited, so it will be full of typos, spelling mistakes, and the occasional creative piece of grammar. And there’s been no effort spent doing layout, so you’ll find bad page breaks, over-long code lines, incorrect hyphen- ation, and all the other ugly things that you wouldn’t expect to see in a finished book. It also doesn't have an index. We can’t be held liable if you use this book to try to create a spiffy application and you somehow end up with a strangely shaped farm implement instead. Despite all this, we think you’ll enjoy it! Download Updates: Throughout this process you’ll be able to get updated ebooks from your account at pragprog.com/my_account. When the book is com- plete, you’ll get the final version (and subsequent updates) from the same ad- dress. Send us your feedback: In the meantime, we’d appreciate you sending us your feedback on this book at pragprog.com/titles/khrust/errata, or by using the links at the bottom of each page. Thank you for being part of the Pragmatic community! Andy Prepared exclusively for luming Programming WebAssembly with Rust Unified Development for Web, Mobile, and Embedded Applications Kevin Hoffman The Pragmatic Bookshelf Raleigh, North Carolina Prepared exclusively for luming Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2019 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. ISBN-13: 978-1-68050-636-5 Book version: B4.0—March 5, 2019 Prepared exclusively for luming For my grandfather—Walter K. MacAdam— inventor, tinkerer, and IEEE president. He quietly supported my exposure to computers and programming throughout my childhood, often in ways I didn’t know until after his death. I always wanted to grow up to be like him, and I only wish he could’ve seen this book. Prepared exclusively for luming Prepared exclusively for luming Contents Change History . ix Acknowledgments . xi Introduction . xiii Part I — Building a Foundation 1. WebAssembly Fundamentals . 3 Introducing WebAssembly 3 Understanding WebAssembly Architecture 8 Building a WebAssembly Application 12 Wrapping Up 16 2. Building WebAssembly Checkers . 19 Playing Checkers, the Board Game 19 Coping with Data Structure Constraints 20 Implementing Game Rules 34 Moving Players 35 Testing Wasm Checkers 38 Wrapping Up 41 Part II — Interacting with JavaScript 3. Wading into WebAssembly with Rust . 45 Introducing Rust 45 Installing Rust 47 Building Hello WebAssembly in Rust 47 Creating Rusty Checkers 50 Coding the Rusty Checkers WebAssembly Interface 60 Prepared exclusively for luming Contents • vi Playing Rusty Checkers in JavaScript 64 Wrapping Up 65 4. Integrating WebAssembly with JavaScript . 67 Creating a Better “Hello, World” 67 Building the Rogue WebAssembly Game 73 Experimenting Further 87 Wrapping Up 88 5. Advanced JavaScript Integration with Yew . 89 Getting Started with Yew 89 Building a Live Chat Application 99 Wrapping Up 113 Part III — Working with Non-Web Hosts 6. Hosting Modules Outside the Browser . 117 How to Be a Good Host 117 Interpreting WebAssembly Modules with Rust 119 Building a Console Host Checkers Player 122 Wrapping Up 131 7. Exploring the Internet of WebAssembly Things . 133 Overview of the Generic Indicator Module 134 Creating Indicator Modules 137 Building Rust Applications for ARM Devices 142 Hosting Indicator Modules on a Raspberry Pi 144 Hardware Shopping List 153 Endless Possibilities 154 Wrapping Up 156 8. Building WARoS—The WebAssembly Robot System . 157 An Homage to Crobots 158 Designing the WARoS API 159 Building the WARoS Match Engine 164 Creating WebAssembly Robots 188 Robots in the Cloud 193 Wrapping Up 194 Conclusion 194 A1. WebAssembly and Serverless . 197 Serverless 101 197 Prepared exclusively for luming Contents • vii Intersection of WebAssembly and Serverless 198 WebAssembly in the Cloud 198 Serverless WebAssembly in the Wild 199 Integration with OpenFaaS 201 A2. Securing WebAssembly Modules . 203 General Security Concerns 203 Browser-Based Attack Vectors 203 Signing and Encrypting WebAssembly Modules 204 Index . 209 Prepared exclusively for luming Change History The book you’re reading is in beta. This means that we update it frequently. Here is the list of the major changes that have been made at each beta release of the book, with the most recent change first. B4.0: March 5, 2019 • Production is complete. Now it’s on to layout and the printer. B3.0: January 16, 2019 • Added appendices. • Corrected minor errata. • Content-complete and heading to production. B2.0: December 12, 2018 • Added Chapter 7. • Corrected minor errata. B1.0: November 28, 2018 • Initial beta release. Prepared exclusively for luming report erratum • discuss Acknowledgments This book would not have been possible without the infinite patience and support of my wife, who has far more faith in me than I do. I would also like to thank all of the technical reviewers for keeping me honest and accurate: Vijay Raghavan Aravamudhan, Jacob Chae, Nick Fitzgerald, Peter Perlepes, Jason Pike, Sean Boyle, Martjin Reuvers, Balaji Sivaraman, and Stephen Wolff. Prepared exclusively for luming report erratum • discuss Introduction I’m old enough to have lived through quite a few seismic changes in the way developers build software and the kinds of products we can build. I was just starting my career when DPMI gave us native access to 32-bit integers, allowed unfettered access to a heap greater than 640k, and enabled the creation of ground-breaking games like DOOM. I remember the potential behind Java’s promise of write once, run anywhere. I was there when small, local communi- ties built around dial-up bulletin board systems (BBSes) faded as the world became a single, digital community riding the wave of the Internet’s surge toward ubiquity. I experienced the shift in solution design from client/server to fat server to fat client and back again, today landing on cloud native applications, microservices, and independent functions where everything including our infrastructure is a service. I remember the web’s growth from a billion archipelagos of text (often blinking!) and Under Construction signs where the coolest places were the ones with the most intricate full-page background images, to the vast, sprawling engine of commerce, communication, lifestyle, and social connection that it is today. The web has gone from a place where only an elite few dabbled in that strange new world to a place where millions of people spend their days coding some of the most powerful and complex applications of the modern era. I firmly believe that we stand on the precipice of another seismic change— WebAssembly. This new technology holds within it the potential to radically change how developers build applications for the web. Moreover, as you’ll see throughout this book, WebAssembly is more than just a new pebble thrown into the ocean of web technologies. It’s a tsunami that can change not only how consumers interact with and how developers build applications but also fundamentally alter the kinds of applications we can create. It may even transform our core definition of the word application. Prepared exclusively for luming report erratum • discuss Introduction • xiv Today’s Web Technology Today’s web is a veritable playground for developers. You have the luxury of easy access to broadband speeds (with exceptions). Browsers are faster and more powerful than they’ve ever been, and the workstations people use to run those browsers have oodles of RAM, storage, and multiple cores—even the mobile devices. Today’s JavaScript is nothing like the primordial 1995 JavaScript that drew so much ire from the developer community. It dominates the web development landscape so much that its own ubiquity has become something of a joke or a meme. Competition between browser vendors (they didn’t call it the browser wars for nothing) has spurred years of refining the way their products execute JavaScript, which will be a key discussion point when you get into the details of browser-hosted WebAssembly. Modern browsers have virtual machines responsible for JavaScript execution. Internally they optimize and produce a form of bytecode from processed JavaScript. This, coupled with more memory and processing power, means that JavaScript is actually fast.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    228 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us