vendredi 29 mai 2015

Passing undefined to Nashorn Javascript in Scala/Java

I need to evaluate this function in Javascript from Scala/Java

function hello(a, b) {
    return a+b;
}

I did this basic code:

val factory = new ScriptEngineManager(null)
val engine = factory.getEngineByName("JavaScript")

val body =
  """
    |function hello(a, b) {
    |    return a+b;
    |}
  """.stripMargin
engine match {
  case engine: Invocable =>
    engine.eval(body)
    println(engine.invokeFunction("hello", null, 1: java.lang.Double))
}

For the parameter a I'm passing a null and I get a 1.0 as a result. If I hack my javascript (I DON'T WONT TO DO THIS) and I make it:

function hello(a, b) {
    if (a === null) {
        a = undefined;
    }
    return a+b;
}

I get the expected NaN.

The correct solution would be passing an undefined to the invokeFunction: How do I do this?

Aucun commentaire:

Enregistrer un commentaire