A portable machine architecture for the web.
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
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 test/fac.wast
$ git clone https://github.com/sdiehl/wasm
$ cd wasm
$ cabal sandbox init
$ cabal install
$ stack test
$ stack ghci wasm
In 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()
3
Core modules
- Entry - Driver
- Syntax - Frontend AST
- Eval - Interpreter
- Parser - Parser
- Lexer - Lexer
- Pretty - Pretty Printer
- Test - Test suite
(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))))
)
)
)
(module
(export "test" 0)
(func (result i32)
(i32.add (i32.const 1) (i32.const 2))))
Will generate
0000000 0101 0100 0102 0009 1500 0000 0500 4000
0000010 0109 0209 7406 7365 0074
000001a