Even more Rockstar: using WebAssembly to run Rust code in browser

24 Sep 2018

Esolangs  Javascript  Rust  Web 

Originally posted at https://tech.labs.oliverwyman.com/blog/2018/09/24/rockstar-using-webassembly/

Two months ago I blogged about how to be a Rockstar developer, and demonstrated it with a Rockstar interpreter called Maiden written in Rust. Now, normally Rust is considered a systems programming language (it even says so on the Rust homepage), and the command-line nature of Maiden aligned well with that. Except that Rust is slowly becoming more than that. Between “are we web yet?” and more recently WebAssembly support, Rust can be used for an ever-increasingly wider selection of projects.

TL;DR: there’s a browser version of Maiden now.

It’s worth looking at a brief history of things, and why WebAssembly is what it is now first. So back in 2008, Javascript engines had been in browsers for some time, but they had only more recently been needing to run larger programs, and so there was a flurry of interest from the various browser manufacturers. First there was by SquirrelFish for WebKit, and then later that year Chrome had its first release with a high-performance javascript engine being very explicitly one of it’s core goals (primarily to support other Google javascript-heavy tools like Gmail and Google Maps). Catching up with those improvements, saw 2009 and Mozilla coming out with the TraceMonkey improvements for Firefox

A flurry of other performance improvements continued to come in, but the next really big step came with asm.js in 2013. Asm.js is intended as a subset of Javascript (and so still works with existing engines), but eliminating hard-to-optimise parts of the spec (e.g. most of the OO stuff), which meant app writers could target that and know their code would be more likely to go faster. At about the same time, Emscripten turned up which is a tool for compiling C/C++ to Javascript via LLVM. This led to such fun as Quake, BibTex, and the entire WebKit Javascript engine being available in your browser.

This wasn’t quite fast enough though, and now the new kid on the block is WebAssembly. WebAssembly is a binary format for a stack-based virtual machine, with an explicit design goal of being faster than Javascript, and they’ve succeeded. Alon Zakai has a good article on why this is so, and there’s every indication that it’ll continue to get made faster.

Now we can get back to how Rust fits into all of this. Having a backend that compiles to this (using LLVM, and one version that uses Emscripten) only gets you so far if you want devs to actually use it. You’ll need to make it easy to use with their existing tools (both Rust and Javascript) as well. At the moment, that’s cargo web for build integration, stdweb for interoperability aids, and then yew which gets you a React-style MVC framework, that I’ve been using to make a browser version of Maiden. On the left side is your code (starting with a modulus example courtesy of my colleague Theo), and the right side is the AST and output of your code. It’s all so fast, that I was able to make it just live compile as you type!

Previously: Not all watchers are created equal (or how to make yak shaving useful) Next: Clincher: checking your signed git commits