GraalVM demos: Polyglot JavaScript, Java, R application

This page describes a simple example of a polyglot application you can run with GraalVM.

Prerequisites

Preparation

Download or clone the repository and navigate into the polyglot-javascript-java-r directory:

  1. git clone https://github.com/graalvm/graalvm-demos
  2. cd graalvm-demos/polyglot-javascript-java-r

Build the benchmark. You can manually execute npm install, but there’s also a build.sh script included for your convenience.

Execute:

  1. ./build.sh

Now you are all set to run the polyglot JavaScript, Java, R application.

Export the GraalVM home directory as the $GRAALVM_HOME and add $GRAALVM_HOME/binto the path, using a command-line shell for Linux:

  1. export GRAALVM_HOME=/home/${current_user}/path/to/graalvm

and for macOS:

  1. export GRAALVM_HOME=/Users/${current_user}/path/to/graalvm/Contents/Home

Note that your paths are likely to be different depending on the download location.

Note that this application contains R code, so you need to install the Rlanguage component to add R support to GraalVM, run the GraalVM Updater commandto do it:

  1. gu install R

For more information on using GraalVM Updater please refer to its documentation.

Running the application

To run the application, you need to execute the server.js file.You can run it with the following command (or run the run.sh script):

  1. $GRAALVM_HOME/bin/node --polyglot --jvm server.js

If you would like to run the benchmark on a different instance of Node,you can run it with whatever node you have. However, presumably, the polyglotcapability will not be supported.

Open localhost:3000 and enjoy the output of the polyglot app.Play with the source code and restart the application to see what elseyou can do with the mix of JavaScript, Java, and R.

Debugging polyglot applications

GraalVM also supports debugging of polyglot applications and provides a built-inimplementation of the Chrome DevTools Protocol. Add the —inspect parameter to the command line, open the URL the applicationprints at the startup in the Chrome browser and start debugging, set breakpoints,evaluate expressions of this app in JavaScript and R code alike.

A note about the application

This sample application for brevity contains large-ish snippets of codeinside the strings. This is not the best approach for structuring polyglot apps,but it is the easiest to show in a single file.