Skip to content

Instantly share code, notes, and snippets.

@hugoferreira
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save hugoferreira/5e14eaac93c9b37bfe68 to your computer and use it in GitHub Desktop.

Select an option

Save hugoferreira/5e14eaac93c9b37bfe68 to your computer and use it in GitHub Desktop.
Bridging Scala and Javascript in Java 8
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