A Quick Start to WebAssembly
Wikipedia defines WebAssembly as a portable stack machine designed to be faster to parse than JavaScript, as well as faster to execute. In this article, the author explores WebAssembly, covering its installation and its relationship with Rust.
Web applications these days are amazing. The first time I became aware of the potential power of Web applications was when I first encountered Gmail. Although Ajax calls had been in use, this was the first application that I knew of that had used them very effectively.
Still, more complex interactions and using local resources needed a plugin. Flash player is both the most used plugin as well as the best example of the problems with plugins. Security issues with Flash never seem to end.
Google tried to overcome some of the issues with the NPAPI plugins with the introduction of NaCl, the native clients. The NaCl clients run in a sandbox, minimising security risks. Google introduced PNaCl, or Portable NaCl, which is an architecture-independent version of NaCl.
Mozilla did not follow Google’s lead with a native client, but instead decided to take a different approach, dropping NPAPI from current versions of Firefox.
The solution proposed by Mozilla was asm.js,a subset of the JavaScript language, which could run an ahead-of-its-time compiling and optimising engine. A related concept was that you could program in C/
C++ and compile the code to asm.js using a tool like Emscripten. The advantage is that any application written for asm.js would run in any browser supporting JavaScript. However, it would run significantly faster if the browser used optimisation for asm.js.
The next step has been the introduction of a byte-code standard for Web browsers, called WebAssembly. The initial implementation targets the asm.js feature set and is being developed by all major browsers, including Mozilla, Google, Microsoft and Apple.
As in the case of asm.js, you may write the application in C/C++ and use a compiler like Emscripten to create a WebAssembly module.
Installation
The development tools for WebAssembly and Emscripten are not yet available in the official repositories. You can follow the instructions from http://webassembly.org/ getting-started/developers-guide/ for the installation.
For Linux, you need to build Empscripten from the source. It takes a substantial amount of time for downloading and building it, though.
You can test your installation by trying a simple C program, hello.c, as follows:
#include <stdio.h> int main(int argc, char **argv){
printf(“hello, world\n”); }
Compile the program as follows to get a JavaScript output:
$ emcc hello.c -o hello.js Now, test it and get the expected result.
$ node hello.js hello, world
You can check the size of the hello.js file and it will be about 320K!
Now, compile the program as follows to get the WebAssembly result:
$ emcc hello.c -s WASM=1 -o hello.html
$ ls -lh
-rw-rw-r-- 1 anil anil 85 Aug 24 21:49 hello.c -rw-rw-r-- 1 anil anil 101K Aug 24 21:50 hello.html -rw-rw-r-- 1 anil anil 100K Aug 24 21:50 hello.js -rw-rw-r-- 1 anil anil 45K Aug 24 21:50 hello.wasm
You will notice that it creates an HTML file, a js file and a wasm file. The overall size is smaller. You need the HTML file, as the js file will not execute with the node
command. For testing, run the following code: $ emrun –no_browser –port 8080 .
Web server root directory: <your current directory> Now listening at http://localhost:8080/
Open the browser http://localhost:8080/hello.html and you should see ‘hello world’ printed.
WebAssembly and Rust
Rust is a programming language sponsored by Mozilla Research. It’s used for creating highly concurrent and safe systems. The syntax is similar to that of C/C++.
Since Firefox uses Rust, it seemed natural that it should be possible to program in Rust and compile to WebAssembly. You may follow the steps given at https://goo.gl/LPIL8B to install and test compiling Rust code to WebAssembly.
Rust is available in many repositories; however, you will need to use the rustup installer from https://www. rustup.rs/ to install the compiler in your local environment and then add the modules needed for WebAssembly as follows:
$ curl https://sh.rustup.rs -sSf | sh
$ source ~/.cargo/env
$ rustup target add asmjs-unknown-emscripten $ rustup target add wasm32-unknown-emscripten
You may now write your first Rust program, hello.rs, as follows:
fn main(){ println!(“Hello, Emscripten!”); }
Compile and run the program and verify that you get the expected output:
$ rustc hello.rs $ hello
Hello, Emscripten!
Now, you may compile to JavaScript and test it as follows:
$ rustc --target=asmjs-unknown-emscripten hello.rs $ node hello.js Hello, Emscripten! You can create the wasm target and an HTML front:
$ rustc --target=wasm32-unknown-emscripten hello.rs -o hello.html
You can test it as with the C example, as follows:
$ emrun –no_browser –port 8080 .
Web server root directory: <your current directory> Now listening at http://localhost:8080/
Why bother?
The importance of these projects cannot be underestimated. JavaScript has become very popular and, with Node.js, on the server side as well.
There is still a need to be able to write secure and reliable Web applications even though the growth of mobile apps has been explosive.
It would appear that mobile devices and ‘apps’ are taking over; however, there are very few instances in which the utility of an app is justified. In most cases, there is no reason that the same result cannot be achieved using the browser. For example, I do not find a need for the Facebook app. Browsing m.facebook.com is a perfectly fine experience.
When an e-commerce site offers me a better price if I use its app on a mobile device, it makes me very suspicious. The suspicions seem all too often to be justified by the permissions sought by many of the apps at the time of installation. Since it is hard to know which app publisher to trust, I prefer finding privacy-friendly apps, e.g., https://goo.gl/aUmns3.
Coding complex applications in JavaScript is hard. FirefoxOS may not have succeeded, but given the support by all the major browser developers, the future of WebAssembly should be bright. You can be sure that tools like Emscripten will emerge for even more languages, and you can expect apps to lose their importance in favour of the far safer and more trustworthy WebAssembly code.