Book Image

Learn WebAssembly

By : Mike Rourke
Book Image

Learn WebAssembly

By: Mike Rourke

Overview of this book

WebAssembly is a brand-new technology that represents a paradigm shift in web development. This book teaches programmers to leverage this technology to write high-performance applications that run in the browser. This book introduces you to powerful WebAssembly concepts to help you write lean and powerful web applications with native performance. You start with the evolution of web programming, the state of things today, and what can be done with the advent and release of WebAssembly. We take a look at the journey from JavaScript to asm.js to WebAssembly. We then move on to analyze the anatomy of a WebAssembly module and the relationship between binary and text formats, along with the corresponding JavaScript API. Further on, you'll implement all the techniques you've learned to build a high-performance application using C and WebAssembly, and then port an existing game written in C++ to WebAssembly using Emscripten. By the end of this book, you will be well-equipped to create high-performance applications and games for the web using WebAssembly.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Dedication
PacktPub.com
Contributors
Preface
Index

Fetching and instantiating a Wasm file


Now that we have a Wasm file, we'll need some JavaScript code to compile and execute it. There's a few steps we'll have to follow to ensure the code can be successfully utilized in the browser. In this section, we will write some common JavaScript loading code that we can reuse for other examples, create an HTML file that demonstrates the use of the Wasm module, and test the results in the browser.

Common JavaScript loading code

We will fetch and instantiate a .wasm file in several of the examples, so it makes sense to move the JavaScript loading code to a common file. The actual fetch and instantiation code is only a few lines, but having to repeatedly redefine the importObj object that Emscripten expects is a waste of time. We'll make this code available in a commonly accessible file to expedite the code-writing process. Create a new folder named /common in the /book-examples folder and add a file named load-wasm.js with the following contents:

/**
...