Empowering JavaScript apps

with WebAssembly

Mariot Tsitoara @ Manchester Web Meetup | October | 2019

Hi! I'm Mariot

Mozilla TechSpeaker.
Python and JS Developer
Author of Beginning Git and GitHub (Apress, 2020).
Savior of Hyrule

My Communities

Mariot Tsitoara
Civilization VI

Civilization VI

Football Manager

Football Manager

Let's start!

WebAssembly is

JS and WASM
  • Binary format
  • A compilation target for other languages to compile to
  • A powerup for JS applications
  • Interface from/to JS

WebAssembly is not

JS and WASM
  • A programming language
  • A replacement to JavaScript

Why do we need this?

Java Applets

Mathematical application

NASA World Wind

Why did Java Applets disappear?

  • Need to install Java on your computer.
  • Zero integration with the web.
  • Lost strength due to Flash, Silverlight, HTML5
  • Problems with mobile devices
  • Lots of Security issues
Logo de Java Applets

....and thus, Java applets began to disappear...

....until JavaScript

We love JS

But it has its problems...

Performance

How browsers run JS code

JS execution

Source: Lin Clark © 2017.

Let's see an
execution example...

An addition in JS


	function sum(a, b) {
		return a + b;
	}
						

Pretty simple, right?
It "should be a single" CPU instruction

How ECMA-262
defines an addition

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating MultiplicativeExpression.
  4. Let rval be GetValue(rref).
  5. Let lprim be ToPrimitive(lval).
  6. Let rprim be ToPrimitive(rval).
  7. If Type(lprim) is String or Type(rprim) is String, then
    • Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
    • Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim).

Source: ECMAScript® Language Specification. (Sec. 11.6.1)

Why?

You have to do some sacrifices

Programmers

Understand Programming languages

Dispositivos

Understand binary or low level code.

How to solve this?

WebAssembly

JS and WASM

How browsers run WASM

WASM execution

Source: Lin Clark © 2017.

Binary format

WASM binary explorer

Check it with WASM Code Explorer

WebAssembly Text Format

(module
	(type $type0 (func (param i32)))
	(type $type1 (func))
	(import "sys" "print" (func $import0 (param i32)))
	(memory (;0;) 200 200)
	(export "memory" (memory 0))
	(export "main" (func $func1))
	(func $func1
	i32.const 0
	call $import0
	)
	(data (i32.const 0)
	"Hello, world\00"
	)
)

Hello World in WAT

You might think it's just trendy....

No!
it has great support!

web browsers

How it's created?

Compiler target

Each supported language's compiler will generate a .wasm file from the compiler.

Rust

rustc --target=wasm32-unknown-uknown project.rs -o project.html

GO

GOOS=js GOARCH=wasm go build -o main.wasm

Emscripten

It's a toolchain to take high languages code to WASM

C/C++LLVMEmscripten + BinaryenWASM

Execute

emcc project.c -s WASM=1 -o project.html

What do I get with this?

Compilation result

You'll get at least these 3 files:

  1. The .wasm file.
  2. JS code to import WASM module.
  3. HTML page to execute the WASM module.

Code interaction

JS and C interaction

Comunicate code between JS and WASM

Calls C functions from JS

C

#include <math.h>
extern "C" {
	int int_sqrt(int x) {
		return sqrt(x);
	}
}

JavaScript

int_sqrt = Module.cwrap('int_sqrt', 'number', ['number'])
int_sqrt(12)
int_sqrt(28)

Call JS functions from C

With emscripten_run_script()

emscripten_run_script(
	"alert('I love alerts')"
);

With EM_ASM()

#include <emscripten.h>
int main() {
	EM_ASM(
	alert('DO NOT USE ALERTS!');
	throw 'execution ready';
	);
	return 0;
}}

What can I do with WASM?

WASM video editor

WASM Video Editor

Applies video effects on realtime

Aprende C (Learn C)

Learn C with WASM

A website to learn C;
code is executed at browser level

AutoCAD on the web

AutoCAD web

Source: Twitter.

WordPress Gutenberg Parser

WordPress Gutenberg

WordPress' new editor uses a parser written in Rust.

PIB: PHP In the Browser

Zend interpreter migrated to WASM

PIB: PHP in the browser

More info at: https://github.com/oraoto/pib

We can go
full-stack with PHP!

Bye Bye JS! (Just kidding)

Future WASM features

  • Multi-threading and Atomics
  • Exception handling
  • Garbage collection
  • Global mutable variables
  • SIMD.
  • JS BigInt ←→ WebAssembly i64.

More at: https://webassembly.org/docs/future-features/

Don't wait!

Test it now with WebAssembly Studio!

mariot.tsitoara@pm.me
mariot.rahona.mg