Hi,

You can only invoke a global function or a method on a specific object - for the former you'd use Invocable.invokeFunction and for the later Invocable.invokeMethod.

invokeMethod requires specific script object, name of the method and the arguments. invokeFunction requires the global function name and the arguments.

"x.a.b.c" is not a function - it evaluates to a function when executed as a script. So, in your case, you could do something like:

  inv.invokeMethod(engine.eval("x.a.b"), "c", 2, 3);

Please note that engine.eval("x.a.b") evaluates as the script object on which the desired method in defined. "c" is the method name.

Hope this helps,
-Sundar

On Wednesday 04 September 2013 04:50 AM, Viktor Gamov wrote:
Hello nashorn team,

I have a question regarding how to invoke function that deeply nested inside 
object.
Here is example:

@Test
public void useCase_7() throws ScriptException, LoadError, 
NoSuchMethodException {
     engine.eval("x = {a: {b: {c: function(a, b) { return a + b } } } }");
     Invocable inv = (Invocable) engine;
     Assert.assertEquals("function reference chain", 
inv.invokeFunction("x.a.b.c", 2, 3), 5.0);
}

And I'm getting java.lang.NoSuchMethodException: No such function x.a.b.c

Invoke method doesn't seem to work as well.

@Test
public void useCase_8() throws ScriptException, LoadError, 
NoSuchMethodException {
     engine.eval("x = {a: {b: {c: function(a, b) { return a + b } } } }");
     Invocable inv = (Invocable) engine;
     Object x = engine.get("x");
     Assert.assertEquals("function reference chain", inv.invokeMethod(x, 
"a.b.c", 2, 3), 5.0);
}

What is correct way to do that?
Please, advice
Thanks          

--
With Best Regards,
Vik Gamov

Reply via email to