Last active
August 29, 2015 14:07
-
-
Save hugoferreira/5e14eaac93c9b37bfe68 to your computer and use it in GitHub Desktop.
Bridging Scala and Javascript in Java 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.script.{Compilable, Invocable, ScriptEngineManager} | |
| import scala.beans.BeanProperty | |
| case class Pessoa(@BeanProperty var name: String, | |
| @BeanProperty var age: Double) | |
| trait CenasExtractor { | |
| def processRow(row: String, age: Double): Pessoa | |
| } | |
| object Main extends App { | |
| val manager = new ScriptEngineManager(null) | |
| val engine = manager.getEngineByName("nashorn").asInstanceOf[Compilable with Invocable] | |
| val statm = | |
| """ | |
| | var PessoaClass = Java.type("Pessoa"); | |
| | | |
| | function processRow(name, age) { | |
| | var aPessoa = new PessoaClass(name, age); | |
| | aPessoa.name = "MUAhAH!" | |
| | return aPessoa; | |
| | } | |
| """.stripMargin | |
| engine.compile(statm).eval() | |
| val f = engine.getInterface(classOf[CenasExtractor]) | |
| val r = f.processRow("Luis", 8).name | |
| println(r) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment