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?