By @abner - Ábner Oliveira
This project allows to write scripts Java using Javascript trough the GraalJS Engine in a standalone way.
The script provided as an example connects to a Postgres database and prints the rows of the sample table.
If you have GraalVM installed you can just use the
js
binary provided in the GraalVM installation. But if you want to use the very optmized Graaljs engine with a normal Java JDK, this repository just will facilitate the things for you.
Suggested Reading: https://chrisseaton.com/truffleruby/codeone18/ten-things-graal.pdf
- Any Java SDK at version 11 or superior
- Platforms supported: Linux, MacOS or Windows (with WSL or Git Bash)
- Docker (optional to run the Postgres instance)
Run the command bellow, inside this repository root path, in order to setup the jars folder with the Java dependencies needed to run javascript inside the JVM through GraalJS and also to load two aliases commands: startpg
[runs Postgres] and graaljs
[starts the javascript REPL or run a given script].
source ./setup.sh
If you want to override the port used both to run the postgres container and used to connect to the database, you can pass POSTGRES_PORT variable to the setup.sh script:
POSTGRES_PORT=5433 source ./setup.sh
Java 11 or above installed.
rungpg
- Starts a docker container running a postgres databasegraaljs
- Runs the GraalJS REPL or runs a script if passed
After sourcing the setup.sh in a terminal session, you can execute:
startpg
The graaljs is a bash alias which calls the Java Class
com.oracle.truffle.js.shell.JSLauncher
passing the psotgres_graal_script.js as the script it should execute.
graaljs postgres_graal_script.js
It Outputs:
--------------------------------------------------------------
GRAAL JS - Postgres script - PG Port: 5432
--------------------------------------------------------------
--------------------------------------------------------------
Row 1 :::: 1 | Angel
Row 2 :::: 2 | Beth
Row 3 :::: 3 | Sophie
Row 4 :::: 4 | Zeneth
--------------------------------------------------------------
If you're using the Visual Studio Code Editor, you can execute the Debug Launcher graaljs-standalone
in onder
to execute the script with the VsCode Debugger cappabilities.
In order to inspect the script execution through Chrome Inspector, just run the script using the alias graaljs_debug
:
graaljs_debug postgres_graal_script.js
Then open the url chrome://inspect
on Chrome or Chromium browser and click in inspect
under the GraalVM
label.