Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8170715

Nashorn passing a function through bindings

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_102-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_102-ea-b04)
      Java HotSpot(TM) 64-Bit Server VM (build 25.102-b04, mixed mode)
      ---and---
      openjdk version "9-internal"
      OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src)
      OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux <name> 4.4.0-51-generic #72-Ubuntu SMP Thu Nov 24 18:29:54 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When calling a java function within JS it is not possible to work in different scopes (global/engine).


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      As example: https://gist.githubusercontent.com/MisterErwin/afb02ed9ab7f3e3ce39af3e82f10c3f0/raw/82bc1db3d06272a47ff463fcf3cbade55cdb2866/Foo.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Calling a function with and without a JS "Proxy-Function" should work
      ACTUAL -
      The java.lang.ClassCastException as seen in the error field

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      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)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      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));

      engine.invokeFunction("withoutProxy"); //Works
      engine.invokeFunction("withProxy"); //ClassCastException

      (https://gist.githubusercontent.com/MisterErwin/afb02ed9ab7f3e3ce39af3e82f10c3f0/raw/82bc1db3d06272a47ff463fcf3cbade55cdb2866/Foo.java)
      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              psonal Pallavi Sonal (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: