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

1 回答 1

1

显然这是 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

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

于 2014-10-10T06:38:02.120 回答