title: Why does Java 8's Nashorn engine in strict mode throw a java.lang.ClassCastException when calling apply() and passing the arguments object directly?
body: <p>When I call eval (in strict mode) on a nashorn engine with the following script I get an exception:</p>
  
  <pre><code>var yfunc = function () {
    (null).apply(null, arguments);
  };
  yfunc();
  </code></pre>
  
  <p>I've truncated my personal situation heavily. The "(null)" on line 2 can be replaced with anything between parenthesis or a local variable, either way just something that shouldn't throw a compile error, and it will yield the same result. </p>
  
  <p>The issue seems to be explicitly that "arguments" is passed directly as the second argument of calling a method called "apply". Any of the following changes will undo the thrown exception:</p>
  
  <ol>
  <li>Putting "arguments" in a variable first (but simply wrapping it in parenthesis doesn't work!)</li>
  <li>Calling something other than apply</li>
  <li>Passing "arguments" in a different argument slot when calling apply</li>
  <li>Calling print() (with or without passing any arguments) as a preceding line of code inside yfunc() (weird huh?)</li>
  <li>Defining more than 0 parameters for yfunc()</li>
  <li>Binding yfunc first and then calling the bound method</li>
  <li>Calling yfunc via Function.apply (not so much with Function.call!)</li>
  </ol>
  
  <p>The Exception thrown is this:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to jdk.nashorn.internal.runtime.ScriptFunction
  at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
  at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
  at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(&lt;eval&gt;:4)
  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.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
  at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
  at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
  at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
  at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
  </code></pre>
  
  <p>When I call this method with an owner, the exception thrown changes. Example code: </p>
  
  <pre><code>var yfunc = {
      method: function () {
            (null).apply(null, arguments);
      }
  };
  var x = yfunc.method();
  </code></pre>
  
  <p>Then the thrown exception looks like this:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.runtime.ScriptFunction
  at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
  at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
  at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(&lt;eval&gt;:5)
  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.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
  at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
  at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
  at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
  at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
  </code></pre>
  
  <p>I've reproduced the issue so far on specifically these environments:</p>
  
  <ul>
  <li>windows 7 64bit -> jdk 1.8.0_60 64bit</li>
  <li>windows 8 64bit -> jdk 1.8.0_131 64bit</li>
  </ul>
  
  <p>I can't seem to find anything on the internet about similar issues. Do I need to report this to Oracle/OpenJDK?</p>
  
  <p><strong>Minor update</strong></p>
  
  <p>Added items 6 and 7 to list of "following changes will undo the thrown exception".</p>
  
  <p><strong>Final update</strong></p>
  
  <p>Bug filed: <a href="http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8184720" rel="nofollow noreferrer">JDK-8184720</a></p>