Integrate with GraalVM via JavaScript

Related to Blazing fast servers with Purescript and ES4X!, I looked a bit into ways to integrate with GraalVM. What I decided to do first was to try and use the JavaScript that the PureScript compiler produces.

Tried 2 ways:

1. Using a JavaScript context

I tried this with the Main that gets generated from spago init. See https://github.com/rintcius/hello-graal-purs/blob/main/src/main/scala/example/Hello.scala for the code. This worked fine, but of course it’s a minimal project.

Also tried with a bigger project that depends on Node modules. This doesn’t work out of the box for the Node modules. The mechanism is to replace Node deps with non-Node deps, e.g. path with path-browserify and supply these replacements via option js.commonjs-core-modules-replacements to the Graal Context.
Keep replacing until you either run out of modules to replace or patience replacing them. I ran out of the latter, so haven’t seen this working yet.

2. Using Node.js embedding

This is basically a node drop-in that GraalVM provides. I tried this with a couple of projects and they all worked.

E.g. here’s how to start a hyper example server:


❯ git clone git@github.com:purescript-hyper/hyper.git
❯ cd hyper

# build and let spago create a sample `.spago/run.js` that we can use for Graal
❯ spago run -p examples/HelloHyper.purs -m Examples.HelloHyper
Listening on http://0.0.0.0:3000
^C

❯ export GRAAL_VM_BIN=<PATH CONTAINING GRAAL'S NODE>
❯ $GRAAL_VM_BIN/node --jvm .spago/run.js
Listening on http://0.0.0.0:3000
^C

Note: although all seems to be working transparently (which I think is quite impressive), it also seems to be a lot slower than directly using Node.

I’m also interested in integrating PureScript directly, but let’s see if I have enough spare time for that. Anyway, via JavaScript seemed like a nice way to start :slight_smile:

5 Likes