A portable machine architecture for the web.
Warning, still a big work in progress.
Define a portable, size- and load-time-efficient binary format to serve as a compilation target which can be compiled to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms, including mobile and IoT.
- Design
- Semantics
- Reference Implementation
- Binary Encoding
- ilwasm
- binaryren
- wassembler
- demo
- backend prototype
- WasmExplorer
- Javascript API
- Spec
Binary Protocol
Chrome
As of Chrome 56 the flag #enable-webassembly can be enabled to add support for
WebAssembly.
Firefox
- Parse WebAssembly textual AST
- Read binary format
- Emit binary format
- Pretty print textual AST
- Verify soundness of code
- Interpreter
- High level code generation API for retargeting
- Translation to LLVM IR backend
$ git clone https://github.com/sdiehl/wasm
$ cd wasm
$ stack install$ wasm fac.wasm$ git clone https://github.com/sdiehl/wasm
$ cd wasm
$ cabal sandbox init
$ cabal install$ stack test$ stack ghci wasmIn Chrome:
WebAssembly.compile(new Uint8Array(
`00 61 73 6d 0d 00 00 00 01 09 02 60 00 00 60 01
7f 01 7f 03 03 02 00 01 05 04 01 00 80 01 07 07
01 03 66 6f 6f 00 01 08 01 00 0a 3a 02 02 00 0b
35 01 01 7f 20 00 41 04 6c 21 01 03 40 01 01 01
0b 03 7f 41 01 0b 20 01 41 e4 00 6c 41 cd 02 20
01 1b 21 01 41 00 20 01 36 02 00 41 00 21 01 41
00 28 02 00 0f 0b 0b 0e 01 00 41 00 0b 08 00 00
00 00 2c 00 00 00`.split(/[\s\r\n]+/g).map(v => parseInt(v, 16))
)).then(mod => {
let m = new WebAssembly.Instance(mod)
console.log('foo(1) =>', m.exports.foo(1))
console.log('foo(2) =>', m.exports.foo(2))
console.log('foo(3) =>', m.exports.foo(3))
})In V8:
$ git clone [email protected]:v8/v8.git
$ make native wasm=on
$ out/native/v8 --expose-wasm
V8 version 4.9.0 (candidate)
d8> buffer = readbuffer('test.bin');
[object ArrayBufferr
d8> module = WASM.instantiateModule(buffer, {});
{memory: [object ArrayBuffer], test: function test() { [native code] }}
d8> module.test()
3Core modules
- Entry - Driver
- Syntax - Frontend AST
- Eval - Interpreter
- Parser - Parser
- Lexer - Lexer
- Pretty - Pretty Printer
- Test - Test suite
Factorial
(module
;; Recursive factorial
(func (param i64) (result i64)
(if_else (i64.eq (get_local 0) (i64.const 0))
(i64.const 1)
(i64.mul (get_local 0) (call 0 (i64.sub (get_local 0) (i64.const 1))))
)
)
)Generates:
Length: 47 (0x2f) bytes
0000: 00 61 73 6d 01 01 00 7f 02 01 09 00 00 15 00 00 .asm............
0010: 00 1a 00 04 51 20 00 00 00 00 42 00 42 01 7e 20 ....Q ....B.B.~
0020: 00 00 00 00 12 7d 20 00 00 00 00 42 01 06 00 .....} ....B...
Constants
(module
(export "test" 0)
(func (result i32)
(i32.add (i32.const 1) (i32.const 2)))
)Generates
Length: 23 (0x17) bytes
0000: 00 61 73 6d 01 01 00 7f 02 01 09 00 00 15 00 00 .asm............
0010: 00 02 00 41 00 06 00 ...A...