Building a Java Virtual Machine Part 2: A method to the madness

Ben Zotto
7 min readJan 7, 2021

Part 2 of a series discussing the joys and pitfalls (mostly pitfalls) of hacking together a minimal JVM in Javascript. The live code base — a very rough work in progress! — is on GitHub. Subscribe for further updates! ;-)

I finished last article by walking through the simple Java class file for Hello World. This time I’m going to talk more about Javascript, and what structures are needed for using that class data.

I called my project Kopiluwak. All self-respecting Java-adjacent projects use stupid coffee puns for their names, and this is no exception. I figured a (s)crappy effort deserved a (s)crappy name. Here’s the reference, feel free to be grossed out on your own time!

The Asian Palm Civet (Jordy Meow/CC BY 3.0)

So here’s a little data structure for holding the loaded raw class data. (The KL is for kopi luwak):

function KLRawClassData(...) {
this.name = ...; // string
this.superclassName = ...; // string
this.accessFlags = ...; // bit flags
this.constantPool = ...; // 1-indexed array of tagged constants
this.interfaces = ...; // array of string names
this.fields = ...; // array of "field infos"
this.methods = ...; // array of "method infos"
this.attributes = ...; // array of "attribute infos"
}

--

--