Jason Williams

Building a JS Interpreter in Rust Part 1

So I’ve decided to have a go at building a JS interpreter in Rust. I’ve wanted to do this for a while for a couple of reasons:

  • Learn Rust
  • Learn more about how JS implementations work
  • Learn more about the JS specification
  • There isn’t a fully fledged compiler in Rust yet for JS
  • It’s fun!

The interpreter itself is called Boa and you can join me on every step of the way on this too. First make sure you have Rust installed, check out the repository and build.

It works on very basic javascript, for instance you can create variables, objects, arrays and print their properties, concatenate strings etc. Go take a look and have a play with it.

Below is a working example.


Is Rust the best language for this?

Rust itself is a systems language and certainly at the right level to building an interpreter or compiler. Its next to C/C++ in terms of performance whilst also offering safety, concurrency, a great standard library and modules to help build any program. There will be plenty of scope for parallelism in future and Rust should make this quite safe without me shooting myself in the foot.

Rust has also proved its weight as a language with the performance improvements it’s given to firefox with Project Quantum.

How it works

It starts with a lexer, the job of a lexer is to convert a stream of characters from the source code into tokens. The lexer does this by scanning through characters and analysing when a token starts and finishes, once it has a token it adds it to an array (or vector in Rusts case), this is then sent to the parser.

The parser does a similar job but instead deals with a stream of tokens, it uses these to create expressions. So a couple of tokens representing a function call would generate a CallExpr. Expressions are then evaluated into an Abstract Syntax Tree, this is a high-level expression which represents the whole programme. We then create an executor, which then works it way through the tree and prints out the final value.


I will attempt to go through this post by post, including what i’ve done so far and the pain points i’ve come across, you don’t need to understand Rust to follow these posts.

Hopefully future posts will cover:

  • Building a Rust Project
  • Lexing/Parsing
  • Garbage Collection
  • Implementing Objects and prototypes
  • Performance
  • JITing (@_mttyng has been working on a JIT for this)
  • Edge Cases

These posts have since moved to Boa's own blog.