Why is Java 8 Nashorn engine in strict mode throwing a java.lang.ClassCastException when calling apply () and passing the arguments object directly?

When I call eval (in strict mode) on the nashorn engine with the following script, I get an exception:

var yfunc = function () {
  (null).apply(null, arguments);
};
yfunc();

      

I cut my personal situation a lot. The "(Null)" on line 2 can be replaced with anything between a parenthesis or a local variable, either way, it's just something that shouldn't throw a compilation error, and it will give the same result.

Obviously, the problem is that "arguments" are passed directly as the second argument to the call to the "apply" method. Any of the following changes will override the exception:

  • Putting "arguments" in a variable first (but simply wrapping it in parentheses doesn't work!)
  • Calling something other than an app
  • Passing "arguments" in another argument slot when called is applied
  • Calling print () (with or without passing any arguments) as the previous line of code inside yfunc () (weird huh?)
  • Defining more than 0 parameters for yfunc ()
  • Linking yfunc first and then calling the associated method
  • Calling yfunc via Function.apply (not much with Function.call!)

Thrown exception:

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(<eval>: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)

      

When I call this method with owner, the exception will be changed. Sample code:

var yfunc = {
    method: function () {
          (null).apply(null, arguments);
    }
};
var x = yfunc.method();

      

Then the thrown exception looks like this:

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(<eval>: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)

      

I have reproduced this issue so far specifically in these environments:

  • windows 7 64bit → jdk 1.8.0_60 64bit
  • windows 8 64bit → jdk 1.8.0_131 64bit

I cannot find anything on the internet for similar problems. Do I need to report this to Oracle / OpenJDK?

Minor update

Added items 6 and 7 to the "next changes will override the exception" list.

Final update

Error: JDK-8184720

+3


source to share


1 answer


Yes, it seems to be a mistake. Please include a bug.



+2


source







All Articles