Now Reading
I’ve written a JVM in Rust · Andrea Bergia’s Web site

I’ve written a JVM in Rust · Andrea Bergia’s Web site

2023-07-21 03:48:48

These days I’ve been spending fairly a little bit of time studying Rust, and as any sane individual would do, after writing a couple of 100 strains packages I’ve determined to tackle one thing slightly bit extra formidable: I’ve written a Java Digital Machine in Rust. ???? With plenty of originality, I’ve referred to as it rjvm. The code is out there on GitHub.

I need to stress that it is a toy JVM, constructed for studying functions and never a severe implementation. Specifically, it doesn’t assist:

  • generics
  • threads
  • reflection
  • annotations
  • I/O
  • simply in time compiler
  • string interning

Nonetheless, there are fairly a couple of non-trivial issues carried out:

  • management circulation statements (if, for, ...)
  • primitive and object creations
  • digital and static technique invocation
  • exceptions
  • rubbish assortment
  • class decision from a jar file

For instance, the next is a part of the check suite:

class StackTracePrinting {
    public static void predominant(String[] args) {
        Throwable ex = new Exception();
        StackTraceElement[] stackTrace = ex.getStackTrace();
        for (StackTraceElement ingredient : stackTrace) {
            tempPrint(
                    ingredient.getClassName() + "::" + ingredient.getMethodName() + " - " +
                            ingredient.getFileName() + ":" + ingredient.getLineNumber());
        }
    }

    // We use this rather than System.out.println as a result of we do not have actual I/O
    non-public static native void tempPrint(String worth);
}

It makes use of the true rt.jar containing the lessons from the OpenJDK 7 – thus, within the instance above, the java.lang.StackTraceElement class comes from an actual JDK!

I’m very proud of what I’ve realized, about Rust and about find out how to implement a digital machine. Specifically, I’m tremendous comfortable about having carried out an actual, working, rubbish collector. It’s fairly mediocre, nevertheless it’s mine and I adore it. ???? On condition that I’ve achieved what I got down to do initially, I’ve determined to cease the undertaking right here. I do know there are bugs, however I don’t plan to repair them.

On this publish, I will provide you with an summary of how my JVM works. In additional articles, I’ll go into extra element about a number of the points mentioned right here.

Code group