C++ Header-Only JavaScript Interpreter

This is an old project I started a couple years ago and didn’t finish fully.
It’s very easy to embed, and can run basic JS. You can visit the repl here.

JS Snippet that it can successfully run (The full file tests JSON more thoroughly and tests recursion):

// Objects and nested objects
let obj = {
	nested: {
		func: function() { console.log("Nested function called!") }
	},

	i: 32
};

// Accessing nested members
obj.nested.func();

// Arrow functions
let test = (arg => { console.log(arg) })("Arrow functions!");

// JSON
console.log(JSON.stringify(obj));

// Classes
class MyClass {
	constructor() {
		this.property = "Some value";
	}

	toString() {
		return JSON.stringify(this);
	}
}

console.log(new MyClass());

The icing on the cake for this project however, is that the APIs implemented aren’t made in C++.
The JSON parser and stringification is done completely by JS and inside the interpreter, and you can view the implementations for String, Stream, Array, Console, Primitives, and JSON.
C++ only has to interfere a little bit to enable it to be implemented through JS. (e.g Object.keys requires metadata on the object properties that only the interpreter internals can access)

Remember that it is not complete though.
Some operators might not be implemented and neither are floating point numbers. (Division will always round down)

2 Likes