japgolly / scala-graal

Make usage of Graal features easy and safe from Scala. Also features Scala-based React SSR.

GitHub

scala-graal

Build Status

libraryDependencies ++= Seq(
  "com.github.japgolly.scala-graal"  %% "core"           % "1.1.1"
  "com.github.japgolly.scala-graal" %%% "core-js"        % "1.1.1"
  "com.github.japgolly.scala-graal" %%% "ext-boopickle"  % "1.1.1"
  "com.github.japgolly.scala-graal"  %% "ext-prometheus" % "1.1.1"
)

Goals

  • Make it quick, easy and safe to interface with embedded languages from Scala
  • Hide and automate a lot of required GraalVM boilerplate
  • Support React SSR for Scala.JS applications

Demo

import japgolly.scalagraal._

// Use semantics and implicit config for JS
// (GraalVM also supports Python, R, Ruby, LLVM)
import japgolly.scalagraal.js._
import GraalJs._

// 1. Pre-compile expression functions for fast invocation.
// 2. Typeclasses translate and/or marshall data between JVM and JS.
val expr: (Int, Int) => Expr[String] =
  Expr.apply2((a, b) => s"($a + $b) * 2 + '!'").compile(_.asString)

// Use a basic synchronous JS environment
val ctx = GraalContext()

val result = ctx.eval(expr(3, 8))
assert(result == Right("22!"))

Learning

Features

  • Expressions
    • composition
    • purity
    • result parsing
    • error handling
    • null handling
    • binding typeclasses
    • binding codecs (eg binary/json/whatever)
  • Service
    • single-threaded
    • multi-threaded pool
    • synchronous
    • asynchronous
    • optional time limits
    • before/around/after hooks
    • automatic metrics
  • Warmup
    • ability to warmup VM
    • rules (eg. up to 10000 reps/thread & up to 30 sec | until completes within 20ms)
  • React SSR (Server-Side Rendering)
    • support for rendering JS components from JVM
    • conversion to constant-time (conditionally)
    • window and especially window.location management
  • Integrations
    • Prometheus - export metrics to Prometheus
    • BooPickle - marshall data back and forth using binary codecs