Search code examples
javanashorn

Java Nashorn inconsistent binding behavior. Is this a bug?


Nashorn doesn't handle bindings in a consistent way. I suspect it's a bug but I guess it might be some optimization side effect.

The inconsistent behaviour can be reproduced with a simple test case:

package jsbugtest;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class JsBugTest {

  public static Object resolve(ScriptEngine engine, String script) {
    Object r = null;
    try {
      r = engine.eval(script);
    } catch (Exception ex) {
      System.out.println("exception: " + ex.getMessage());
      r = null;
    }
    return r;
  }

  public static void runTest()
  {
     ScriptEngineManager mgr = new ScriptEngineManager(null);
     ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
     String script = "DataA + 'foo';";    
     Bindings binds = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);    
     Object ret;

     for (int i = 0; i < 12; i++) {
       binds.remove("DataA");
       ret = resolve(jsEngine, script);
       if (ret != null) {
         System.out.println("Iteration " + i + ": Returned value should be null but is: \"" + ret + "\"");
       }

       binds.put("DataA", "foo");
       ret = resolve(jsEngine, script);
       if (ret == null) {
         System.out.println("Iteration " + i + " failed");
       }
     }    
  }

  public static void main(String[] args) {
    JsBugTest.runTest();
  }  
}

When running this program it produces the following output:

exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
exception: ReferenceError: "DataA" is not defined in <eval> at line number 1
Iteration 8: Returned value should be null but is: "undefinedfoo"
Iteration 9: Returned value should be null but is: "undefinedfoo"
Iteration 10: Returned value should be null but is: "undefinedfoo"
Iteration 11: Returned value should be null but is: "undefinedfoo"

So the first 7 iterations works as expected but then suddenly something happens that makes scriptengine evaluate DataA to undefined instead of throwing an exception.

Is this a bug?


Solution

  • Yep, it's a bug. I filed https://bugs.openjdk.java.net/browse/JDK-8136544 to keep track of it.