0

When calling a java function within JS it is not possible to work in different scopes (global/engine).

engine.eval("function proxy(a){return Java.type('Foo').bar(a);}",engine.getBindings(ScriptContext.GLOBAL_SCOPE));
engine.eval("function withoutProxy(){return Java.type('Foo').bar(m);}",engine.getBindings(ScriptContext.ENGINE_SCOPE));
engine.eval("function withProxy(){return proxy(m);}", engine.getBindings(ScriptContext.ENGINE_SCOPE));

(Where m is a JS function declared in the engine scope)

Invoking withoutProxy works fine. But withProxy throws

java.lang.ClassCastException: Cannot cast jdk.nashorn.api.scripting.ScriptObjectMirror to java.util.function.Function
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$Recompilation$7$17A$\^eval\_.proxy(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:639)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.ScriptObjectMirror.call(ScriptObjectMirror.java:117)
at jdk.nashorn.internal.scripts.Script$Recompilation$6$20$\^eval\_.withProxy(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:383)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)

(the stacktrace is with using 1.8.0_102-ea. Same exception with jdk1.8.0_05 and jdk1.8.0_73.)

So: Did I do something wrong, is it intended or a bug?

(Gist of my test class)

4
  • I don't have any error with JDK 1.8.0_51. This could have been a fixed bug?
    – Tunaki
    Commented Jun 3, 2016 at 10:30
  • In the oracle/java bugtracker it says that something similar (just changing bindings) was fixed post 8u40 (so 8u60 and above). But afaik 1.8.0_102-ea is never than 1.8.0_51. Commented Jun 3, 2016 at 11:08
  • I have also tested it on a different machine. (Ubuntu with java 1.8.0_91). Same error. (To be surer that it is now caused my some sort of misconfiguration) Commented Jun 3, 2016 at 12:12
  • This is new issue related to working with different scope, We at Oracle evaluating this issue and get back to you.
    – Fairoz
    Commented Aug 3, 2016 at 10:03

1 Answer 1

0

This issue is resolved now with http://bugs.java.com/view_bug.do?bug_id=8162839 in JDK 9-ea b 147 which can be downloaded from https://jdk9.java.net/download/

1
  • The most important part of the answer is that the issue is resolved (in ea builds). So I just went ahead and accepted it. Commented Dec 13, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.