Git Product home page Git Product logo

graaljs-standalone's Introduction

GraalJS Application Example

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

Requirements:

  • 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)

How to execute

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

Assigning a different port to Postgres

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

Requirements

Java 11 or above installed.

Aliases

  • rungpg - Starts a docker container running a postgres database
  • graaljs - Runs the GraalJS REPL or runs a script if passed

Running the postgres script

After sourcing the setup.sh in a terminal session, you can execute:

1. Starting the postgres database

startpg

2. Call the postgres_graal_script.js using the graaljs aliase

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
--------------------------------------------------------------

Debug with Visual Studio Code

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.

Debug with Chrome Inspector

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.

References:

graaljs-standalone's People

Contributors

abner avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.