I need to evaluate this function in Javascript from Scala / Java
function hello(a, b) { return a+b; }
I made 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 parameter a, I pass null and I get 1.0 as a result. If I crack my javascript (I SHOULD NOT DO IT) and I will do this:
function hello(a, b) { if (a === null) { a = undefined; } return a+b; }
I get the expected NaN.
The correct solution would be to pass undefined to invokeFunction: How do I do this?
source share