WebAssembly in Action
With examples using C++ and Emscripten
Gerard Gallant
- 448 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
WebAssembly in Action
With examples using C++ and Emscripten
Gerard Gallant
About This Book
Summary WebAssembly in Action introduces the WebAssembly stack and walks you through the process of writing and running browser-based applications. Expert developer Gerard Gallant gives you a firm foundation of the structure of a module, HTML basics, JavaScript Promises, and the WebAssembly JavaScript API. About the technologyWrite high-performance browser-based applications without relying only on JavaScript! By compiling to the WebAssembly binary format, your C, C++, or Rust code runs at near-native speed in the browser. WebAssembly delivers greater speed, opportunities to reuse existing code, and access to newer and faster libraries. Plus, you can easily interact with JavaScript when you need to. About the book WebAssembly in Action teaches you how to write and run high-performance browser-based applications using C++ and other languages supported by WebAssembly. In it, you'll learn to create native WebAssembly modules, interact with JavaScript components, and maximize performance with web workers and pthreads. And you'll love how the clearly organized sections make it a breeze to find the important details about every function, feature, and technique. What's inside Dynamic linking of multiple modules at runtime
Communicating between modules and JavaScript
Debugging with WebAssembly Text Format
Threading with web workers and pthreadsAbout the readerWritten for developers with a basic understanding of C/C++, JavaScript, and HTML. About the author Gerard Gallant is a Microsoft Certified Professional and a Senior Software Developer at Dovico Software. He blogs regularly on Blogger.com and DZone.com.
Frequently asked questions
Information
Part 1. First steps
Chapter 1. Meet WebAssembly
- What WebAssembly is
- The problems that WebAssembly solves
- How WebAssembly works
- What makes WebAssembly secure
- The languages you can use to create a WebAssembly module
1.1. What is WebAssembly?
1.1.1. Asm.js, the forerunner to WebAssembly
- You donât write asm.js directly. Instead, you write your logic using C or C++ and convert it into JavaScript. Converting code from one language to another is known as transpiling.
- Faster code execution for high computations. When a browserâs JavaScript engine sees a special string called the asm pragma statement ("use asm";), it acts as a flag, telling the browser that it can use the low-level system operations rather than the more expensive JavaScript operations.
- Faster code execution from the very first call. Type-hints are included to tell JavaScript what type of data a variable will hold. For example, a | 0 would be used to hint that the variable a will hold a 32-bit integer value. This works because a bitwise OR operation of zero doesnât change the original value, so there are no side effects to doing this. These type-hints serve as a promise to the JavaScript engine indicating that, if the code declares a variable as an integer, it will never change to a string, for example. Consequently, the JavaScript engine doesnât have to monitor the code to find out what the types are. It can simply compile the code as itâs declared.
function AsmModule() { "use asm"; 1 return { add: function(a, b) { a = a | 0; 2 b = b | 0; return (a + b) | 0; 3 } } }
- 1 Flag telling JavaScript that the code that follows is asm.js
- 2 Type-hint indicating that the parameter is a 32-bit integer
- 3 Type-hint indicating that the return value is a 32-bit integer
- All the type-hints can make the files really large.
- The asm.js file is a JavaScript file, so it still has to be read in and parsed by the JavaScript engine. This becomes an issue on devices like phones because all that processing slows load time and uses battery power.
- To add additional features, browser makers would have to modify the JavaScript language itself, which isnât desirable.
- JavaScript is a programming language and wasnât intended to be a compiler target.
1.1.2. From asm.js to MVP
- WebAssembly is a low-level assembly-like language that can run at near-native speeds in all modern desktop browsers as well as many mobile browsers.
- WebAssembly files are designed to be compact and, as a result, can be transmitted and downloaded fast. The files are also designed in such a way that they can be parsed and initialized quickly.
- WebAssembly is designed as a compile target so that code written in languages such as C++, Rust, and others can now run on the web.