KajMagnus
KajMagnus

Reputation: 11666

Java 8 Nashorn ArrayIndexOutOfBoundsException

What, if anything, can I do to stop Nashorn from throwing an ArrayIndexOutOfBoundsException all the time?

This seemingly trivial and correct (there's no bug herein) code snippet causes Nashorn to the exception:

File 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 = {});

Then run the script like so:
/usr/lib/jvm/java-8-oracle/bin/jjs test.js

Result:

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)

This happens also when I run the file from within the JVM, via the script engine.

Background: I'm trying to run TypeScript generated code in Nashorn. The above code snippet is simplified TypeScript compiler output.

Operating system: Linux Mint 17, 64 bit. Java version:

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

Upvotes: 4

Views: 682

Answers (1)

KajMagnus
KajMagnus

Reputation: 11666

Apparently this was a bug in Java 8. I posted to the Nashorn mailing list; here is the reply:

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

The email: http://mail.openjdk.java.net/pipermail/nashorn-dev/2014-October/003614.html

( Edit: There's another ArrayIndexOutOfBounds error in 8u40, read more here: http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-March/004268.html "Cannot parse JSON: { 0: null, 100: null }". That problem has been fixed in 8u60. )

Upvotes: 1

Related Questions