Java 8 Nashorn ArrayIndexOutOfBoundsException

问题描述:

如果有什么,我能做些什么来阻止Nashorn始终抛出ArrayIndexOutOfBoundsException?Java 8 Nashorn ArrayIndexOutOfBoundsException

这个看似微不足道的,正确的(有没有错误本文中)的代码片段导致犀牛的例外:

文件test.js:

var test; 
(function (test) { 
    (function (renderer) { 
     var posts = { 
      0: null, 
      1: null, 
      2: null, 
      3: null // comment out this line --> no exception thrown 
     }; 
    })(test.renderer = {}); // comment out 'test.renderer = ' --> no exception thrown 
})(test = {}); 

然后运行该脚本,像这样:
/usr/lib/jvm/java-8-oracle/bin/jjs test.js

结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 
     at jdk.nashorn.internal.runtime.AccessorProperty.initGetterSetter(AccessorProperty.java:271) 
     at jdk.nashorn.internal.runtime.AccessorProperty.<init>(AccessorProperty.java:250) 
     at jdk.nashorn.internal.runtime.SetMethodCreator.createNewFieldSetter(SetMethodCreator.java:166) 
     at jdk.nashorn.internal.runtime.SetMethodCreator.createNewPropertySetter(SetMethodCreator.java:156) 
     at jdk.nashorn.internal.runtime.SetMethodCreator.createSetMethod(SetMethodCreator.java:122) 
     at jdk.nashorn.internal.runtime.SetMethodCreator.createGuardedInvocation(SetMethodCreator.java:78) 
     at jdk.nashorn.internal.runtime.ScriptObject.findSetMethod(ScriptObject.java:1911) 
     at jdk.nashorn.internal.runtime.ScriptObject.lookup(ScriptObject.java:1632) 
     at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:96) 
     at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176) 
     at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124) 
     at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144) 
     at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232) 
     at jdk.nashorn.internal.scripts.Script$test.L:2(test.js:3) 
     at jdk.nashorn.internal.scripts.Script$test.runScript(test.js:2) 
     at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535) 
     at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209) 
     at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378) 
     at jdk.nashorn.tools.Shell.apply(Shell.java:383) 
     at jdk.nashorn.tools.Shell.runScripts(Shell.java:312) 
     at jdk.nashorn.tools.Shell.run(Shell.java:168) 
     at jdk.nashorn.tools.Shell.main(Shell.java:132) 
     at jdk.nashorn.tools.Shell.main(Shell.java:111) 

当我通过脚本引擎从JVM中运行文件时,也会发生这种情况。

背景:我试图在Nashorn中运行TypeScript生成的代码。上面的代码片段是简化的TypeScript编译器输出。

操作系统:Linux Mint 17,64位。 Java版本:

$ java -version 
java version "1.8.0_20" 
Java(TM) SE Runtime Environment (build 1.8.0_20-b26) 
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode) 
+0

您可以发布java -version的输出吗?我注意到Nashorn正在改变JDK 1.8的各种小版本,所以它可能会有所作为。 – 2014-10-04 11:33:41

+0

@ DavidP.Caldwell是的,我忘了,完成了(附加到问题)。 – KajMagnus 2014-10-05 05:10:00

+0

嗨@Jarrod Robertson,实际上这不是http://*.com/questions/32568261/how-to-avoid-java-lang-arrayindexoutofboundsexception的重复。这个问题是关于Nashorn *总是抛出'ArrayIndexOutOfBoundsException',即使是*正确的*代码 - 原因是Nashorn中的错误。 – KajMagnus 2016-03-04 03:21:59

显然这是Java 8中的一个错误。我发布到Nashorn邮件列表;这里是回复:

I can reproduce the problem with 8u20, however it seems to be fixed in 
JDK 9 and JDK8u40. You can download a preview release of 8u40 here: 

https://jdk8.java.net/download.html 

电子邮件:http://mail.openjdk.java.net/pipermail/nashorn-dev/2014-October/003614.html

编辑:还有另一个的ArrayIndexOutOfBounds错误8u40,在这里阅读更多:http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-March/004268.html“无法解析JSON:{0:空, 100:null}“。该问题已在8u60中修复。)