Java 8 Nashorn数组下标越界异常

4

有什么办法可以阻止Nashorn一直抛出ArrayIndexOutOfBoundsException错误吗?

这段看似微不足道而且正确无误的代码片段却会导致Nashorn抛出异常:

文件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)

你能否发一下 java -version 的输出结果?我注意到 Nashorn 在 JDK 1.8 的各个小版本中有所变化,所以这可能会产生影响。 - David P. Caldwell
@DavidP.Caldwell 是的,我忘了,已经完成了(将其附加到问题中)。 - KajMagnus
嗨@Jarrod Robertson,实际上这不是http://stackoverflow.com/questions/32568261/how-to-avoid-java-lang-arrayindexoutofboundsexception的重复。这个问题实际上是关于Nashorn始终抛出ArrayIndexOutOfBoundsException甚至对于正确的代码-原因是Nashorn中的一个错误。 - KajMagnus
1个回答

1

显然这是Java 8中的一个bug。我在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

(编辑:在8u40中还存在另一个ArrayIndexOutOfBounds错误,请在此处阅读更多信息:http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-March/004268.html "无法解析JSON:{ 0: null, 100: null }"。该问题已在8u60中得到修复。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接