WebAssembly, bytecode for browser
This joint project between browser vendors uses asm.js as a starting point for an IR of the Web.
The goal was to maintain compatibility between Asm.js and wasm for a few years. Not only for the support in browsers, but also because qu'Asm.js is also in development, and new features which will be include into it will be indispensable also to wasm. Eg threads with shared memory, support for multi-core processors.
The wasm code currently was that of Asm.js but experiments have shown with the new code that parsing time (not the speed of execution) can be multiplied by 20. The size of the binary code generated is three times more compact than in Asm.js. The code is binary but a more readable text view is possible.
According to Brendan Eich, wasm is in fact a "compressed AST encoding" and not a true bytecode. AST for abstract syntax tree.
Some other advantage of wasm:
- It will have a garbage collector and DOM access.
- Two notations of the code are possible: binary for executing or text for reading. Like an assembly language - but it is not one.
- A wasm LLVM backend is proposed initially. We can generate wasm code with an option from C/C++. It will then be available for other languages.
And a disadvantage:
Why a new intermediate language?
Why not just use LLVM, or the Java bytecode, or .NET?
The primary objective of wasm is to be able to run source code written in C and C ++ in web applications. An immense amount of functions has been written in these languages, being able to reuse them with comparable execution time is really appreciable. 3D games are mostly written in C++: they become portable with wasm.
Wasm wants to be to the applications in the browser what Vulkan is to OpenGL and DirectX: a universal and portable intermediate code with a speed of execution close to native.
The LLVM bitcode was not retained because it is not portable. It contains metadata and is designed to produce executable binaries, not to work directly on all systems.
Moreover Google has already tried to use LLVM in the browser with NaCL and this has not been adopted by other publishers.
However, we use Emscriptem to compile the intermediate language of LLVM in wasm, so we keep the benefit of its tool chain.
The .NET runtime has an intermediate code that can run with a virtual machine or be compiled. But it is not suitable for C and C++ languages. Microsoft uses C++/CLI which has difference with native C++.
The Java virtual machine is not an option either because, if many languages have been ported on this platform, this is not the case for C and C ++. Without even counting Oracle's propensity to sue for a reason as futile as the use of its API. The entire Web could be faced with the army of surly lawyers of the firm, which would be disastrous. WebAssembly will allow you to develop in peace.
Quotes from Brendan Eich about WebAssembly
The continued evolution of ASM.js is wasm.
There are lots of languages you might compile to wasm.
Assuming stasis on the web — it’s not a good assumption, I think that was the mistake that happened long ago with projects like Portable Native Client and Dart, too
How to create and use WebAssembly code
A tutorial is provided on the site: Developer's guide to WebAssembly.
Tools to generate wasm code and run it are available on GitHub.
- binaryen. Script to run wasm code on the command line.
- asm2wasm. Compile asm.js to WebAssembly.
- wasm2asm. Compile WebAssembly to asm.js.
- s2wasm.Compile the code specially produced by LLVM in WebAssembly. LLVM has its own format for this.
- Emscriptem. Compiles C and C ++ to asm.js and with BINARYEN option compiles then asm.js to wasm.
- Ilwasm. Converts a subset of the intermediate .NET code in wasm.
- WebAssembly Community Group. At the W3C, the group dedicated to the new language is open to new participants. Currently they are the team of V8 and NaCl of Google, the Emscriptem team from Mozilla, Microsoft and Apple's languages and runtim team.
- From Asm.js to WebAssembly. Presentation by Brendan Eich.
- Why we need WebAssembly. Interview of BE.
- FAQ of wasm. On GitHub. See especially "Why not use LLVM bitcode".
- WebAssembly design. On GitHub, all details about wasm.
- Future Features. What the language will become.