GraalVM is a new open source project by Oracle which is trying to make Java VM an universal VM to run all the major languages. Before GraalVM, there were already few languages like Scala, Closure which targeted JVM as their runtime. This has been hugely successful for those languages. GraalVM takes this idea further and makes it easy to target JVM so that many more languages can coexist on JVM.
GraalVM is around from 2014 as a research project. It’s been used in production by Twitter from 2017. But for general public, it became production ready in latter half of 2019.
In this series posts, I will be exploring what GraalVM can bring to JVM ecosystem. This is the second post in the series which starts exploring polyglot aspect of graalvm. You can read all the posts in the series here.
One of the main advantages of GraalVM is the ability to mix and match multiple languages in same VM. From last post, we have seen that all languages running on the Graal go through same compiler. This makes using multiple languages in same VM much smoother.
To run, graalvm and truffle, we need to add below dependencies in our build.sbt. We need to run it on JDK 8.
Here we are adding graal and truffle dependencies.
Once all the dependencies are done, we are ready to write our first polyglot example. As programming tradition, we will be starting with hello world.
In just two lines, we wrote a JS application within Java!!. Let’s see it’s parts
For any language, we need to create a context. This context allows us to configure all the needed properties of that language. Here we are creating a simple context.
Eval function on context takes a language source code and evaluates it. It’s as simple as that.
Now we have written our first polyglot program on GraalVM.
You can access complete code on github.