際際滷

際際滷Share a Scribd company logo
WebAssembly
Jay Phelps | @_jayphelps
The
Revolutionhas begun
Jay Phelps | @_jayphelps
WebAssembly will change the way
we think of "web apps"
Senior Software Engineer |
@_jayphelps
Jay Phelps
Jay Phelps | @_jayphelps
So...what is WebAssembly? aka wasm
Jay Phelps | @_jayphelps
Ef鍖cient, low-level bytecode for the Web
Jay Phelps | @_jayphelps
Ef鍖cient, low-level bytecode for the Web
Jay Phelps | @_jayphelps
Fast to load and execute
Jay Phelps | @_jayphelps
Ef鍖cient, low-level bytecode for the Web
Jay Phelps | @_jayphelps
0x6a01101010
Jay Phelps | @_jayphelps
Intended as a compilation target
int	factorial(int	n)	{	
		if	(n	==	0)	{	
				return	1;	
		}	else	{	
				return	n	*	factorial(n	-	1);	
		}	
}
int	factorial(int	n)	{	
		if	(n	==	0)	{	
				return	1;	
		}	else	{	
				return	n	*	factorial(n	-	1);	
		}	
}
00	61	73	6D	01	00	00	00	01	
86	80	80	80	00	01	60	01	7F	
01	7F	03	82	80	80	80	00	01	
00	06	81	80	80	80	00	00	0A	
9D	80	80	80	00	01	97	80	80	
80	00	00	20	00	41	00	46	04	
40	41	01	0F	0B	20	00	41	01	
6B	10	00	20	00	6C	0B
Jay Phelps | @_jayphelps
Safe and portable
just like JavaScript is
Jay Phelps | @_jayphelps
Is it going to kill JavaScript?
Jay Phelps | @_jayphelps
Nope!says browser vendors
*
Jay Phelps | @_jayphelps
*well...maybe...some day...a long time from now
(my own opinion)
Jay Phelps | @_jayphelps
Will we compile JavaScript to WebAssembly?
Jay Phelps | @_jayphelps
JavaScript is an extremely dynamic language
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelps
Compiling JavaScript to WebAssembly would run slower
What about a something JavaScript-like?
Jay Phelps | @_jayphelps
AssemblyScript, TurboScript, ThinScript, etc
Jay Phelps | @_jayphelps
class	Coordinates	{	
		x:	i64;	
		y:	i64;	
		z:	i64;	
		constructor(x:	i64,	y:	i64,	z:	i64)	{	
				this.x	=	x;	
				this.y	=	y;	
				this.z	=	z;	
		}	
}	
export	function	example()	{	
		let	position	=	new	Coordinates(10,	20,	30);	
		//	later	
		delete	position;	
}
Jay Phelps | @_jayphelps
WebAssembly is still missing things
for broad adoption
Jay Phelps | @_jayphelps
v1 MVP is best suited for languages like
C/C++ and Rust
Jay Phelps | @_jayphelps
But other languages soon!
Things like Java, OCaml, and even brand new ones
Jay Phelps | @_jayphelps
type	schoolPerson	=	Teacher	|	Director	|	Student(string);	
let	greeting	=	(stranger)	=>	
		switch	stranger	{	
				|	Teacher	=>	"Hey	professor!"	
				|	Director	=>	"Hello	director."	
				|	Student("Richard")	=>	"Still	here	Ricky?"	
				|	Student(anyOtherName)	=>	"Hey,	"	++	anyOtherName	++	"."	
		};
Jay Phelps | @_jayphelps
When should I target WebAssembly right now?
Jay Phelps | @_jayphelps
Heavily CPU-bound number computations
Jay Phelps | @_jayphelps
Games
Physics Simulation
Encryption
Compression
Video Decoding
Audio Mixing
Language Detection
Jay Phelps | @_jayphelps
Games
Physics Simulation
Encryption
Compression
Video Decoding
Audio Mixing
Language Detection
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelpshttps://goo.gl/yvV92X
Demo
https://goo.gl/YWMpdp
Demo
asm-dom
https://goo.gl/XWBeme
Jay Phelps | @_jayphelps
Other use cases just around the corner
Jay Phelps | @_jayphelps
You'll likely consume compiled
WebAssembly libraries even sooner
Jay Phelps | @_jayphelps
What was that binary stuff?
int	factorial(int	n)	{	
		if	(n	==	0)	{	
				return	1;	
		}	else	{	
				return	n	*	factorial(n	-	1);	
		}	
}
00	61	73	6D	01	00	00	00	01	
86	80	80	80	00	01	60	01	7F	
01	7F	03	82	80	80	80	00	01	
00	06	81	80	80	80	00	00	0A	
9D	80	80	80	00	01	97	80	80	
80	00	00	20	00	41	00	46	04	
40	41	01	0F	0B	20	00	41	01	
6B	10	00	20	00	6C	0B
00	61	73	6D	01	00	00	00	01	
86	80	80	80	00	01	60	01	7F	
01	7F	03	82	80	80	80	00	01	
00	06	81	80	80	80	00	00	0A	
9D	80	80	80	00	01	97	80	80	
80	00	00	20	00	41	00	46	04	
40	41	01	0F	0B	20	00	41	01	
6B	10	00	20	00	6C	0B
00	61	73	6D	01	00	00	00	01	
86	80	80	80	00	01	60	01	7F	
01	7F	03	82	80	80	80	00	01	
00	06	81	80	80	80	00	00	0A	
9D	80	80	80	00	01	97	80	80	
80	00	00	20	00	41	00	46	04	
40	41	01	0F	0B	20	00	41	01	
6B	10	00	20	00	6C	0B
00	61	73	6D	01	00	00	00	01	
86	80	80	80	00	01	60	01	7F	
01	7F	03	82	80	80	80	00	01	
00	06	81	80	80	80	00	00	0A	
9D	80	80	80	00	01	97	80	80	
80	00	00	20	00	41	00	46	04	
40	41	01	0F	0B	20	00	41	01	
6B	10	00	20	00	6C	0B
03	82	80	80	80	0
	81	80	80	80	00	0
	80	80	00	01	97	8
	00	20	00	41	00	4
03	82	80	80	80	0
	81	80	80	80	00	0
	80	80	00	01	97	8
	00	20	00	41	00	4
Jay Phelps | @_jayphelps
Binary can be a little intimidating
Jay Phelps | @_jayphelps
Protip: don't worry about it
(unless of course, you want to)
Jay Phelps | @_jayphelps
Textual representation to the rescue!
Jay Phelps | @_jayphelps
(func	$factorial	(param	$n	i32)	(result	i32)	
		get_local	$n	
		i32.const	0	
		i32.eq	
		if	$if0	
		i32.const	1	
		return	
		end	$if0	
		get_local	$n	
		i32.const	1	
		i32.sub	
		call	$factorial	
		get_local	$n	
		i32.mul	
)
Jay Phelps | @_jayphelps
(func	$factorial	(param	$n	i32)	(result	i32)	
		get_local	$n	
		i32.const	0	
		i32.eq	
		if	$if0	
		i32.const	1	
		return	
		end	$if0	
		get_local	$n	
		i32.const	1	
		i32.sub	
		call	$factorial	
		get_local	$n	
		i32.mul	
)
Jay Phelps | @_jayphelps
WebAssembly is a stack machine language
Jay Phelps | @_jayphelps
stack machine: instructions on a stack
1	+	2
i32.add 0x6a
opcode
01101010
mnemonic
stack
i32.const	1	
i32.const	2	
i32.add
i32.const	1	
i32.const	2	
i32.add
i32.const	1	
stack
1
i32.const	1	
i32.const	2	
i32.add
i32.const	2	
stack
2
1
i32.const	1	
i32.const	2	
i32.addi32.add
stack
1
2
i32.const	1	
i32.const	2	
i32.add
stack
3
i32.add
Jay Phelps | @_jayphelps
call	$log	
i32.const	1	
i32.const	2	
i32.add
Jay Phelps | @_jayphelps
Compilers usually apply optimizations
real-world output is often less understandable to humans
i32.const	1	
i32.const	2	
i32.add	
call	$log	
Jay Phelps | @_jayphelps
i32.const	3	
call	$log	
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelps
Most tooling supports an Abstract Syntax Tree (AST)
still compiled and evaluated as a stack machine
i32.const	1	
i32.const	2	
i32.add	
call	$log	
Jay Phelps | @_jayphelps
(call	$log	
		(i32.add	
				(i32.const	1)	
				(i32.const	2)	
		)	
)
Jay Phelps | @_jayphelps
(call	$log	
		(i32.add	
				(i32.const	1)	
				(i32.const	2)	
		)	
)
Jay Phelps | @_jayphelps
s-expressions
Yep, looks like Lisp
Jay Phelps | @_jayphelps
Source map support is coming
Jay Phelps | @_jayphelps
What about memory on the heap?
Jay Phelps | @_jayphelps
A linear memory is a contiguous, byte-
addressable range of memory
Jay Phelps | @_jayphelps
Accessed with instructions like
i32.load and i32.store
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
1 byte
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
w a s m
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
w a s m
Jay Phelps | @_jayphelps
0 1 2 3 4 5 6 7 8 9 10
119 97 115 109
Jay Phelps | @_jayphelps
How do I get started?
https://mbebenita.github.io/WasmExplorer/
Jay Phelps | @_jayphelps
https://github.com/WebAssembly/wabt
Jay Phelps | @_jayphelps
https://github.com/WebAssembly/binaryen
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelps
$	emcc	main.c	-s	WASM=1	-o	app.html
Jay Phelps | @_jayphelps
webassembly.org
Jay Phelps | @_jayphelps
Webpack is adding support (!!!)
They received a $125,000 grant from MOSS
Jay Phelps | @_jayphelps
Imagine a cpp-loader / rust-loader
Jay Phelps | @_jayphelps
What's missing?
Jay Phelps | @_jayphelps
Direct access to Web APIs
You have call into JavaScript, right now
Jay Phelps | @_jayphelps
Garbage collection
also necessary for better interop with JavaScript and WebIDL
Jay Phelps | @_jayphelps
Multi-threading
Jay Phelps | @_jayphelps
Browser support?
The WebAssembly Revolution Has Begun
Jay Phelps | @_jayphelps
The revolution is just beginning
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelps
Ef鍖cient, low-level bytecode for the Web
Jay Phelps | @_jayphelps
Ef鍖cient, low-level bytecode for the Web
Jay Phelps | @_jayphelps
Jay Phelps | @_jayphelps
Questions?
@_jayphelps

More Related Content

The WebAssembly Revolution Has Begun