A Quick Start to We­bAssem­bly

Wikipedia de­fines We­bAssem­bly as a por­ta­ble stack ma­chine de­signed to be faster to parse than JavaScript, as well as faster to ex­e­cute. In this ar­ti­cle, the au­thor ex­plores We­bAssem­bly, cov­er­ing its in­stal­la­tion and its re­la­tion­ship with Rust.

OpenSource For You - - Fossbytes -

Web ap­pli­ca­tions these days are amaz­ing. The first time I be­came aware of the po­ten­tial power of Web ap­pli­ca­tions was when I first en­coun­tered Gmail. Al­though Ajax calls had been in use, this was the first ap­pli­ca­tion that I knew of that had used them very ef­fec­tively.

Still, more com­plex in­ter­ac­tions and us­ing lo­cal re­sources needed a plugin. Flash player is both the most used plugin as well as the best ex­am­ple of the prob­lems with plug­ins. Se­cu­rity is­sues with Flash never seem to end.

Google tried to over­come some of the is­sues with the NPAPI plug­ins with the in­tro­duc­tion of NaCl, the na­tive clients. The NaCl clients run in a sand­box, min­imis­ing se­cu­rity risks. Google in­tro­duced PNaCl, or Por­ta­ble NaCl, which is an ar­chi­tec­ture-in­de­pen­dent ver­sion of NaCl.

Mozilla did not fol­low Google’s lead with a na­tive client, but in­stead de­cided to take a dif­fer­ent ap­proach, drop­ping NPAPI from cur­rent ver­sions of Fire­fox.

The so­lu­tion pro­posed by Mozilla was asm.js,a sub­set of the JavaScript lan­guage, which could run an ahead-of-its-time com­pil­ing and op­ti­mis­ing engine. A re­lated con­cept was that you could pro­gram in C/

C++ and com­pile the code to asm.js us­ing a tool like Em­scripten. The ad­van­tage is that any ap­pli­ca­tion writ­ten for asm.js would run in any browser sup­port­ing JavaScript. How­ever, it would run sig­nif­i­cantly faster if the browser used op­ti­mi­sa­tion for asm.js.

The next step has been the in­tro­duc­tion of a byte-code stan­dard for Web browsers, called We­bAssem­bly. The ini­tial im­ple­men­ta­tion tar­gets the asm.js fea­ture set and is be­ing de­vel­oped by all ma­jor browsers, in­clud­ing Mozilla, Google, Mi­crosoft and Ap­ple.

As in the case of asm.js, you may write the ap­pli­ca­tion in C/C++ and use a com­piler like Em­scripten to cre­ate a We­bAssem­bly mod­ule.

In­stal­la­tion

The de­vel­op­ment tools for We­bAssem­bly and Em­scripten are not yet avail­able in the of­fi­cial repos­i­to­ries. You can fol­low the in­struc­tions from http://we­bassem­bly.org/ get­ting-started/de­vel­op­ers-guide/ for the in­stal­la­tion.

For Linux, you need to build Emp­scripten from the source. It takes a sub­stan­tial amount of time for down­load­ing and build­ing it, though.

You can test your in­stal­la­tion by try­ing a sim­ple C pro­gram, hello.c, as fol­lows:

#in­clude <stdio.h> int main(int argc, char **argv){

printf(“hello, world\n”); }

Com­pile the pro­gram as fol­lows to get a JavaScript out­put:

$ emcc hello.c -o hello.js Now, test it and get the ex­pected re­sult.

$ node hello.js hello, world

You can check the size of the hello.js file and it will be about 320K!

Now, com­pile the pro­gram as fol­lows to get the We­bAssem­bly re­sult:

$ 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 no­tice that it cre­ates an HTML file, a js file and a wasm file. The over­all size is smaller. You need the HTML file, as the js file will not ex­e­cute with the node

com­mand. For test­ing, run the fol­low­ing code: $ em­run –no_browser –port 8080 .

Web server root di­rec­tory: <your cur­rent di­rec­tory> Now lis­ten­ing at http://lo­cal­host:8080/

Open the browser http://lo­cal­host:8080/hello.html and you should see ‘hello world’ printed.

We­bAssem­bly and Rust

Rust is a pro­gram­ming lan­guage spon­sored by Mozilla Re­search. It’s used for cre­at­ing highly con­cur­rent and safe sys­tems. The syn­tax is sim­i­lar to that of C/C++.

Since Fire­fox uses Rust, it seemed nat­u­ral that it should be pos­si­ble to pro­gram in Rust and com­pile to We­bAssem­bly. You may fol­low the steps given at https://goo.gl/LPIL8B to in­stall and test com­pil­ing Rust code to We­bAssem­bly.

Rust is avail­able in many repos­i­to­ries; how­ever, you will need to use the rustup in­staller from https://www. rustup.rs/ to in­stall the com­piler in your lo­cal en­vi­ron­ment and then add the mod­ules needed for We­bAssem­bly as fol­lows:

$ curl https://sh.rustup.rs -sSf | sh

$ source ~/.cargo/env

$ rustup tar­get add as­mjs-un­known-em­scripten $ rustup tar­get add wasm32-un­known-em­scripten

You may now write your first Rust pro­gram, hello.rs, as fol­lows:

fn main(){ println!(“Hello, Em­scripten!”); }

Com­pile and run the pro­gram and ver­ify that you get the ex­pected out­put:

$ rustc hello.rs $ hello

Hello, Em­scripten!

Now, you may com­pile to JavaScript and test it as fol­lows:

$ rustc --tar­get=as­mjs-un­known-em­scripten hello.rs $ node hello.js Hello, Em­scripten! You can cre­ate the wasm tar­get and an HTML front:

$ rustc --tar­get=wasm32-un­known-em­scripten hello.rs -o hello.html

You can test it as with the C ex­am­ple, as fol­lows:

$ em­run –no_browser –port 8080 .

Web server root di­rec­tory: <your cur­rent di­rec­tory> Now lis­ten­ing at http://lo­cal­host:8080/

Why bother?

The im­por­tance of these projects can­not be un­der­es­ti­mated. JavaScript has be­come very pop­u­lar and, with Node.js, on the server side as well.

There is still a need to be able to write se­cure and re­li­able Web ap­pli­ca­tions even though the growth of mo­bile apps has been ex­plo­sive.

It would ap­pear that mo­bile de­vices and ‘apps’ are tak­ing over; how­ever, there are very few in­stances in which the util­ity of an app is jus­ti­fied. In most cases, there is no rea­son that the same re­sult can­not be achieved us­ing the browser. For ex­am­ple, I do not find a need for the Facebook app. Brows­ing m.facebook.com is a per­fectly fine ex­pe­ri­ence.

When an e-com­merce site of­fers me a bet­ter price if I use its app on a mo­bile de­vice, it makes me very sus­pi­cious. The sus­pi­cions seem all too of­ten to be jus­ti­fied by the per­mis­sions sought by many of the apps at the time of in­stal­la­tion. Since it is hard to know which app publisher to trust, I pre­fer find­ing pri­vacy-friendly apps, e.g., https://goo.gl/aUmns3.

Cod­ing com­plex ap­pli­ca­tions in JavaScript is hard. Fire­foxOS may not have suc­ceeded, but given the sup­port by all the ma­jor browser de­vel­op­ers, the fu­ture of We­bAssem­bly should be bright. You can be sure that tools like Em­scripten will emerge for even more lan­guages, and you can ex­pect apps to lose their im­por­tance in favour of the far safer and more trust­wor­thy We­bAssem­bly code.

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.