JavaScript On The JVM

Deven Phillips

Senior Software Engineer

Sungard Availability Services

Why JavaScript?

  • Quick Prototyping
  • Single Language For Full Stack
  • Event Based Concurrency
  • Broad Library Support

Why JavaScript On The JVM?

  • Better Performance
  • Access To Java Libraries
  • Multi-Threading
  • Debugging And Profiling Tools

How Does This Work?

  • This has been around for a while.... Rhino
  • New hotness - Nashorn (German for Rhinocerous!)
  • InvokeDynamic
  • Class Mapping
  • Interoperability

Scripting

#!/usr/bin/env jjs -scripting
print(
"Arguments (${$ARG.length})");
for each (arg in $ARG) {
  print("- ${arg}")
} 

Embedding

package sample1;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Hello {

  public static void main(String... args) throws Throwable {
    ScriptEngineManager engineManager = 
new ScriptEngineManager();
    ScriptEngine engine = 
engineManager.getEngineByName("nashorn");
    engine.eval("function sum(a, b) { return a + b; }");
    System.out.println(engine.eval("sum(1, 2);"));
  }
}

Integration

#!/usr/bin/env jjs -scripting

print(java.lang.System.currentTimeMillis());
// Java objects can be instantiated using the new operator:
var file = new java.io.File("sample.js");
print(file.getAbsolutePath());
print(file.absolutePath); 

Debugging & Profiling

Misson Control

jconsole

VisualVM

Project Avatar

  • NodeJS Compatible API
  • Runs Many NodeJS Libraries
  • Allows For Multi-threading
  • JIT Performance Improvements

 

 

https://avatar-js.java.net/

Vert.x

  • Idiomatic JavaScript
  • Event Based
  • Simple Concurrency
  • Fast Development Cycles

 

https://vertx.io/

JavaScript On The JVM

By Deven Phillips

JavaScript On The JVM

A quick introduction to using JavaScript from inside of the Java Virtual Machine

  • 586
Loading comments...

More from Deven Phillips