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

By: on September 24, 2018

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!


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>