Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript on Scala -Nashorn-

JavaScript on Scala -Nashorn-

Introduction of Nashorn.
JavaScript runtime on JVM, also on Scala.

petitviolet

May 16, 2016
Tweet

More Decks by petitviolet

Other Decks in Technology

Transcript

  1. JavaScript on Scala
    AdTech ✕ Scala MeetUp
    Fringe81 Co., Ltd.
    @petitviolet

    View full-size slide

  2. Nashorn 㱺
    +7.

    View full-size slide

  3. What is Nashorn?
    • JavaScript runtime on JVM
    㱺 JavaScript on Scala!!!
    • Project Nashorn
    • http://openjdk.java.net/projects/nashorn/

    View full-size slide

  4. Example
    https://goo.gl/1KrXoo
    object NashornExample extends App {

    val ENGINE_NAME = "nashorn"

    val engine = new ScriptEngineManager()

    .getEngineByName(ENGINE_NAME)

    .asInstanceOf[ScriptEngine with Invocable]


    val fName = "func"

    val f = s"function $fName(a, b) { return a + b; };"

    engine.asInstanceOf[Compilable].compile(f).eval()


    val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] }

    val result = engine.invokeFunction(fName, argments:_*)

    println(s"result => $result")

    }

    View full-size slide

  5. object NashornExample extends App {

    val ENGINE_NAME = "nashorn"

    val engine = new ScriptEngineManager()

    .getEngineByName(ENGINE_NAME)

    .asInstanceOf[ScriptEngine with Invocable]


    val fName = "func"

    val f = s"function $fName(a, b) { return a + b; };"

    engine.asInstanceOf[Compilable].compile(f).eval()


    val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] }

    val result = engine.invokeFunction(fName, argments:_*)

    println(s"result => $result")

    }
    Preparation
    Example
    https://goo.gl/1KrXoo

    View full-size slide

  6. object NashornExample extends App {

    val ENGINE_NAME = "nashorn"

    val engine = new ScriptEngineManager()

    .getEngineByName(ENGINE_NAME)

    .asInstanceOf[ScriptEngine with Invocable]


    val fName = "func"

    val f = s"function $fName(a, b) { return a + b; };"

    engine.asInstanceOf[Compilable].compile(f).eval()


    val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] }

    val result = engine.invokeFunction(fName, argments:_*)

    println(s"result => $result")

    }
    Compilation
    Example
    https://goo.gl/1KrXoo

    View full-size slide

  7. object NashornExample extends App {

    val ENGINE_NAME = "nashorn"

    val engine = new ScriptEngineManager()

    .getEngineByName(ENGINE_NAME)

    .asInstanceOf[ScriptEngine with Invocable]


    val fName = "func"

    val f = s"function $fName(a, b) { return a + b; };"

    engine.asInstanceOf[Compilable].compile(f).eval()


    val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] }

    val result = engine.invokeFunction(fName, argments:_*)

    println(s"result => $result")

    }
    Execution
    Example
    https://goo.gl/1KrXoo

    View full-size slide

  8. Procedure
    • Preparation
    • create ScriptEngine
    • Compilation
    • compile String as a JavaScript function
    • Execution
    • feed AnyRef arguments to ScriptEngine

    View full-size slide

  9. ↑↑↑ʘ(^o^)ʗ↑↑↑
    • Store JavaScript functions in DB as Strings
    • Use stored functions as Scala function!
    • Enable replacing algorithms dynamically
    • e.g. Scoring, Sorting, Selection, …

    View full-size slide

  10. ↓↓↓ʗ(^o^)ʘ↓↓↓
    • Static typing(Scala) <=> Dynamic typing(JS)
    • lose type information
    • Unexpected input causes unexpected output
    • e.g. null, NaN, Infinity, ReferenceError, …
    • Cannot avoid such results with compilation
    • Runtime!!!

    View full-size slide

  11. Lose type information?
    • No
    • Can use Java types in JavaScript
    • Java.Type(“”)
    • https://docs.oracle.com/javase/jp/8/docs/technotes/guides/scripting/nashorn/api.html
    • Enable casting a result of execution
    • asInstanceOf[T]
    • https://gist.github.com/petitviolet/4c446066da25c150a0eb50b39b4522d3

    View full-size slide

  12. Happy Hacking!
    We’re hiring!

    View full-size slide