WebAssembly Will Change the Way You Code

WebAssembly Will Change the Way You Code

by   Sandro Pasquali  |  November 19, 2018  |  0

WebAssembly (WASM) is a new type of code that runs in browsers. Previously, developers of browser-based web applications had only one choice of programming language and/or environment: JavaScript. This was good and much gold did the people earn; nevertheless, some weren’t happy that they had only one choice. WASM is here to change that. WASM, according to Mozilla, “is not primarily intended to be written by hand, rather it is designed to be an effective compilation target for low-level source languages like C, C++, Rust, etc.” This is the key point. You can now write “web scripts” using the language of your choice. That’s huge.

Is WASM trying to kill JavaScript? No, says the inventor of JavaScript, Brendan Eich. The goal of WASM is to enable a more linguistically-diverse development ecosystem delivering extremely fast (native or near-native speed) code through efficient compilation and parallelization (the ability for web software to take advantage of multiple processors). More huge.

This last point is maybe the most important. InRhythm engineer Will Bratches pointed out in a recent lightning talk that increases in speed are no longer achieved at the chip level—Moore’s Law is no more. What we do now is spread work across cores, leveraging parallelization and managing concurrency. Client-side JavaScript is not designed for that world. New languages like Go and Rust are. WASM unifies the two. 

Here comes the “hello world!” moment. Head over to WebAssembly Explorer—it’s sort of a CodePen for WASM. On the left panel, paste this code:

int plusOne(int num) {
return num + 1;

Next, click “compile”. You’ll see something like this:

WASM WebAssembly Screenshot

Yes, that’s assembly code on the right (I know!). A more human, readable version is in the center column, the WAT view, which you can read more about here.  Just scan through it—you’ll see our function name plusOne represented as $_27plusOnei. You’ll also see that we’re establishing 32-bit integer parameters, setting their scope to “local” and so forth.  You can, for example, see how the assembly code  (add)s one to the (ex)tended (a)ccumulator register for 32-bit numbers:

add eax, 1

…is represented in the WAT:

(i32.const 1)

You can now hit the “download” button, which will deliver a .wasm file.  That’s really it—you have the WASM binary to use. There’s a tiny bit of work you have to do to compile the WASM so it works in your browser. The easiest thing to do is clone this simple example and load the index.html in your browser. Check the console. Now read through the code.

There’s a lot more to this subject, so start exploring. You’ll be creating first-person shooters in your browser in no time!

Sign up for InRhythm updates: IR events, tech news, and more!

Leave a Reply

check out our new interactive scrum framework