WebAssembly: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
mNo edit summary
m →‎Interpeters: remove duplicate
 
(16 intermediate revisions by 4 users not shown)
Line 9:
Unlike Java and Dalvik, interpreting WebAssembly binary is much easier, has no legal concerns, and there are many already written libraries to do so in many languages (most notably [[C]] and [[Rust]]).
 
== ComparitionComparison to Other Bytecode Formats ==
 
=== Lua ===
 
[[Lua]] is a very popular scripting language. However it's compiler, interpreter and execution environment is mixed into a single library. That's good for integrating with user space applications, but makes it difficult to embed into kernels (which would only need the bytecode interpreter part). The bytecode format is not really standardized, although seems quite stable.
 
=== Python ===
Line 21:
=== Java ===
 
[[Java_For_Starters|Java]] Has a very well defined bytecode format, but unfortunately there's no simple bytecode interpreter library. Both the official JVM and it's OpenSource counterparts are huge bloated software, not really designed for embedding into kernels. Even if there were a simple interpreter library, Java is licensed by Oracle and use is subject to certain legal terms. To clearify, Android does not use the Java bytecode at all, it's interpreting Dalvik bytecode which is compiled from Java source.
 
=== WebAssembly ===
Line 35:
 
* '''WASM''': a file which contains [https://www.w3.org/TR/wasm-core-1/#binary binary WebAssembly bytecode], also the name of the reference interpreter.
* '''WAT''': WebAssembly Text source formaTformat, is a [https://www.w3.org/TR/wasm-core-1/#text plain text representation] of the bytecode (see [https://github.com/WebAssembly/binaryen Binaryen] wasm-at tool).
* '''WABT''': WebAssembly Binary Toolkit, equivalent of binutils. Includes assembler, objdump and other tools (see [https://github.com/WebAssembly/wabt WABT]).
* '''MVP''': Minimal Viable Product, which means the smallest WASM interpreter (nothing Web related required or included). Defined in [https://www.w3.org/TR/wasm-core/ WebAssembly Core Specification]
* '''WASI''': WebAssembly System Interface, this defines an ABI and a set of functions how the bytecode interacts with the OS and non-WASM libraries.
* '''EmScripten''': a WASI module, functions like memory allocation, everything that the EmScripten compiler may generate code for. Very simply speaking it's WebAssembly's current libc.
* '''Web API''': a standardized way to [https://www.w3.org/TR/wasm-web-api/ integrate WebAssembly into webpages], not our concern right now. Note that Web API is ''optional'' for WebAssembly.
 
Line 45 ⟶ 46:
WebAssembly has a very clean module linking interface, the [https://github.com/WebAssembly/WASI WASI]. Also defined for OS-independent, easy [https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/ non-Web integration]. There're official POSIX and SDL modules (more like library wrappers) for WebAssembly, but the number of unofficial, third-party libraries are growing day by day.
 
Linkage for JavaScript, C/C++, Rust, Python and many other languages are now available and well tested. For kernels, you'll be interested in the [https://github.com/WebAssembly/wasm-c-api C] or Rust linkage mostly. You can find and incomplete and growing [https://github.com/appcypher/awesome-wasm-langs list here].
 
Right now EmScripten module is used for an OS interface, but this is going to change pretty soon. Big effort is put into WASI to provide a fully featured libc interface for WASM bytecode using musl as reference. Once WASI specs got frozen, all you'll need to implement/include is a WASI interpreter.
 
== Binary Format ==
 
A WASM file starts with a four byte magic, "asm\001", where the last character denotes format version. As such, it's pretty easy to identify.
 
After the header come several sections, one of which contains the bytecode. The binutils equivalent [https://github.com/WebAssembly/wabt WABT] includes a tool called [https://webassembly.github.io/wabt/doc/wasm-objdump.1.html wasm-objdump] to dump these and disassemble bytecode. The details and encodings are specified by the WebAssembly Core Specification, section [https://www.w3.org/TR/wasm-core-1/#binary binary format].
 
== See Also ==
Line 52 ⟶ 61:
* [https://www.w3.org/TR/wasm-core-1 WebAssembly Core Specification]
* [https://github.com/WebAssembly/WASI WASI], ABI specification to run WASM outside of Web browsers
* [https://engithub.wikipedia.org/wikicom/WebAssembly/WASI/blob/main/docs/WASI-overview.md WebAssembly]WASI on WikiPediaoverview]
* [https://github.com/CommonWA/cwa-spec CommonWA] minimal specification of the standard API for non-Web usermode environments
* [https://en.wikipedia.org/wiki/WebAssembly WebAssembly] on Wikipedia
* [https://developer.mozilla.org/en-US/docs/WebAssembly WebAssembly] on Mozilla Development Network
* [https://www.rust-lang.org/what/wasm Rust and WebAssembly]
 
==== Compilers ====
* [https://emscripten.org/ EmScripten], a reference C/C++ compiler that produces WASM bytecode
* [https://surma.dev/things/c-to-webassembly/ LLVM] good description on how to use LLVM's default wasm target '''without''' the EmScripten bloat
* [https://github.com/tweag/asterius Asterius], a [[Haskell]] compiler that produces WASM bytecode
* [https://rustwasm.github.io/ wasm-pack] a Rust compiler that produces WASM bytecode
* [https://github.com/i-net-software/JWebAssembly JWebAssembly], a Java bytecode to WebAssembly bytecode converter
* [https://github.com/WebAssembly/binaryen Binaryen], low level tools for WebAssembly, provided by W3C
* [https://github.com/bytecodealliance/wasmtime/tree/master/cranelift Cranelift], a WASM to native code compiler in Rust
 
==== Interpeters ====
* [https://github.com/WebAssembly/spec/tree/master/interpreter Reference Interpreter] in '''OCaml''', provided by W3C
* [https://github.com/mohansonWebAssembly/pywasmwasmint pywasmlibwasmint] Interpreter in Python'''C++''', a library designed for embedding, provided by W3C
* [https://github.com/perlin-networkWebAssembly/lifewabt/tree/master/src/interp lifewasm-interp] Interpreter in Go'''C++''', part of WABT, more complete than libwasmint
* [https://github.com/bytecodealliance/wasmtime/ wasmtime] yet another wasm interpreter, this time in '''Rust''' with WASI support (this is the reference implementation for WASI)
* [https://github.com/paritytech/wasmi wasmi], Interpreter in Rust
* [https://github.com/kanaka/wac wac, wax, wace] Interpreter in '''C''', this provides MVP, WASI, EmScripten-compatibility; probably the easiest to embed in a kernel
* [https://github.com/soundandformwasm3/m3wasm3 m3/wasmwasm3] Interpreter in '''C''', aims for high performance and WASI compatibility
* [https://github.com/paritytech/wasmi wasmi], Interpreter in '''Rust'''
* [https://github.com/mohanson/pywasm pywasm] Interpreter in '''Python'''
* [https://github.com/perlin-network/life life] Interpreter in '''Go'''
* [https://github.com/kenny-ngo/wasmjit wasmjit] a Linux kernel module in '''C''' to run wasm bytecode
* [https://github.com/cervus-v/cervus cervus] WebAssembly subsystem for Linux in '''C'''
* [https://github.com/pablotron/pwasm PWASM] easy to integrate WASM execution library (uses DynASM JIT) written in '''C'''
 
==== Examples ====
* [https://github.com/kwast-os/kwast kwast] a kernel written in Rust which runs wasm bytecode in userspace (under heavy development as of early 2020).
* [https://github.com/ColinEberhardt/wasm-mandelbrot wasm-mandelbrot] example on how to use EmScripten, wasm-toolchain, clang + BinaryEm, etc.
* [https://github.com/mdn/webassembly-examples/ WebAssembly examples] by Mozilla
* [https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm Tutorial on Rust to WASM] by Mozilla
 
[[Category:CompilersInstruction Set Architecture]]
[[Category:Standards]]