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 of posts, I will be exploring what GraalVM can bring to JVM ecosystem. This is the fifth post in the series which explores passing complex objects from Scala to JavaScript. You can read all the posts in the series here

Passing Values From Scala To JavaScript

In last post, we saw how to consume JavaScript object inside the Scala program. In this post, we will be discussing about how to pass Scala objects to JavaScript.

Enable Allow Access on Context

By default, the guest language, JavaScript, doesn’t have access to any objects from host language Scala. This is done for security purposes. But we can override this by enabling access on the context level.

The below code shows how to do that

val context = Context.newBuilder().allowAllAccess(true).build()

Here we are using builder pattern of context to pass extra parameter. Using allowAllAccess method on context, we allow access to host environment.

Create a Scala Object

The below code creates a person object in Scala

case class Person(name:String, age:Int)

val person = Person("John",20)

Make Scala Object Available to JavaScript

Not every object created in Scala is available to JavaScript by default. It needs to be explicitly made available.

context.getBindings("js").putMember("person",person)

In above code, using putMember method, we make person object available for JavaScript.

Using Scala Object from JavaScript

The below code show accessing the person object in JavaScript code.

val result = context.eval("js","person.name() == 'John' && person.age() == 20").asBoolean()

As you can see from the code, we can access the person object as a normal JavaScript object.

Code

You can access complete code on github.

Conclusion

Polyglot nature of GraalVM makes it very attractive to mix and match different languages on same VM. Ability to pass values between different languages makes it seamless to do different computation in different languages.