This post was written by Martin Entlicher and Oleg Šelajev.
GraalVM is an embeddable high-performance polyglot virtual machine that aims to run all programming languages very fast. It is a large and versatile project, and if you are interested in what else GraalVM can do for you, look at the “10 things GraalVM can do” blog post by Chris Seaton.
After downloading GraalVM, you’ll find the
js utility in the
> 1 + 2
> print("Hello, polyglot world!")
Hello, polyglot world!
So it can run node apps. If you start
node with the
—-jvm flag, your code will have access to the JDK classes out of the box.
$ node --jvm
> var BigInteger = Java.type(‘java.math.BigInteger’);
Demo polyglot application
- Clone the graalvm/graalvm-demos repository and navigate to the
npm installto install the dependencies,
- start the application:
$GRAALVM_HOME/bin/node --polyglot --jvm --inspect server.js
Debugging in Chrome
To be able to attach Chrome dev tools to GraalVM, the launcher (e.g., the node executable) needs to run with the
--inspect option, which can be followed by an optional
--inspectoption prints out a URL that is possible to paste into Chrome to attach the debugger. See Debugging Node.js with Google Chrome for instance, for the hints on how to use the Chrome client. When the debugger is attached, expand the file tree on the left side to explore the loaded scripts. You’ll see all node modules loaded under the
node_modules folder and the main script
server.js next to it. To intercept the web requests and start actual debugging, add a breakpoint to line 19, in the body of
Open http://localhost:8088/ in another browser window and write
log(x^2 + 1) to the function field. Press the “Draw Function” button and wait until the breakpoint is hit. Now you can use standard debugging actions to step through the application execution and tooltips to inspect the variable values.
Now at line 28, step into the call to
plotFunction call. This is R code that plots the function. Again, R data is available, together with the seamless call stack. All debugging features, including breakpoints, are available in all GraalVM languages.
On top of all that you can use the console to evaluate code written in the language of the current frame in focus.
Polyglot applications allow you to reuse the existing modules from any language ecosystem. Normally, it is a maintenance nightmare when you need to use different tooling to see what’s going on in the different components of the application you’re working on. With GraalVM’s language virtualization ability you can use one set of tools to debug all languages and libraries alike, without distracting yourself with the pesky details like what language a particular piece of code is written in.
Coverage Profiling in Chrome
If you want to take it to the next level, find the Coverage tab, which is located near the Console tab in the Chrome dev tools. Select it and click the record button to start capturing the coverage. Use the application a little, for example, having a function expression in the browser window, press the Draw Function button. The code that is used to fulfill the requests you make is marked in source scripts, including the node modules and other languages.
The ability to seamlessly debug through the different languages simplifies development of polyglot apps and allows for enhanced productivity by using the existing libraries from any programming languages ecosystems.
If you would like to implement your own language-agnostic tool for GraalVM, the best starting point is the getting started with instruments on GraalVM page.
To try GraalVM go to https://www.graalvm.org/. There are links to downloads and documentation there, and more examples like we’ve shown in this blog post.
Try following the instructions here and adapting them to see what else you can do. Let us know how you’re experimenting with GraalVM in your application and send us any feedback, for example to shelajev.