OpenSource For You

A Quick Start to WebAssembl­y

Wikipedia defines WebAssembl­y 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 WebAssembl­y, covering its installati­on and its relationsh­ip with Rust.

-

Web applicatio­ns these days are amazing. The first time I became aware of the potential power of Web applicatio­ns was when I first encountere­d Gmail. Although Ajax calls had been in use, this was the first applicatio­n that I knew of that had used them very effectivel­y.

Still, more complex interactio­ns 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 introducti­on of NaCl, the native clients. The NaCl clients run in a sandbox, minimising security risks. Google introduced PNaCl, or Portable NaCl, which is an architectu­re-independen­t 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 applicatio­n written for asm.js would run in any browser supporting JavaScript. However, it would run significan­tly faster if the browser used optimisati­on for asm.js.

The next step has been the introducti­on of a byte-code standard for Web browsers, called WebAssembl­y. The initial implementa­tion 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 applicatio­n in C/C++ and use a compiler like Emscripten to create a WebAssembl­y module.

Installati­on

The developmen­t tools for WebAssembl­y and Emscripten are not yet available in the official repositori­es. You can follow the instructio­ns from http://webassembl­y.org/ getting-started/developers-guide/ for the installati­on.

For Linux, you need to build Empscripte­n from the source. It takes a substantia­l amount of time for downloadin­g and building it, though.

You can test your installati­on 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 WebAssembl­y 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.

WebAssembl­y and Rust

Rust is a programmin­g 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 WebAssembl­y. You may follow the steps given at https://goo.gl/LPIL8B to install and test compiling Rust code to WebAssembl­y.

Rust is available in many repositori­es; however, you will need to use the rustup installer from https://www. rustup.rs/ to install the compiler in your local environmen­t and then add the modules needed for WebAssembl­y 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 underestim­ated. 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 applicatio­ns 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 permission­s sought by many of the apps at the time of installati­on. 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 applicatio­ns in JavaScript is hard. FirefoxOS may not have succeeded, but given the support by all the major browser developers, the future of WebAssembl­y 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 trustworth­y WebAssembl­y code.

 ??  ??

Newspapers in English

Newspapers from India