Publié le dans WebAssembly

WebAssembly, Wasmtime/WASI, Rust and Friends đŸ€©

WebAssembly is the official 4th language of the Web platform. đŸ„ł

WASI: WASM outside of the browser

Java Bytecode

  • Kotlin/Native
  • TeaVM: Compiler of Java bytecode to JavaScript and WebAssembly
  • Bytecoder: A Low Level Bytecode to JavaScript and WebAssembly Transpiler
  • JWebAssembly: Java bytecode to WebAssembly compiler

Python Science + ML to the browser

  • Pyodide: Bringing the scientific Python stack to the browser

Rust

While possible, using Rust instead of JavaScript to make UI updates is probably not how to profit from the power of WASM. No tests I found so far showed a big difference in term of speed. It's even sometimes worse with WASM. Nothing can replace the DOM API so far. That's the lowest-level API to do UI updates.

extern crate wasm_bindgen; // give acces to the DOM via WASM

use wasm_bindgen::prelude::*;

// ~1KB Allocator optimized for wasm. Slower than the Rust one but a lot lighter.
// You need to download the wasm file before executing it.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
  // console.log the app panics
  #[cfg(debug_assertions)]
  console_error_panic_hook::set_once();

  // Looks like the DOM API, right?
  let window = web_sys::window().expect("no global 'window' exists");
  let document = window.document().expect("should have a document on window");
  let body = document.body().expect("document should have a body");

  // Manufacture the element we're gonna append
  let val = document.create_element("div")?;
  val.set_inner_html("Hello from Rust!");

  body.append_child(&val)?;

  // Everyting went well so far
  Ok(())
}
wasm-pack build --target web (--dev)

Examples

Rust on Windows

While on Linux and macOS, compiling a software is straightforward with the Terminal, on Windows it's another story. Microsoft came with an tricky but easy solution: Windows Subsystem Linux.

You can also use MSYS2 and ConEmu to have something similar. Both tools don't need admin rights to be installed and launched. MSYS2 is based on MinGW and Pacman (from Arch Linux).

Install Rust from https://rustup.rs/_