JDK-8072853 : SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-02-10
  • Updated: 2015-09-29
  • Resolved: 2015-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u60Fixed 9 b66Fixed
Related Reports
Relates :  
Description
The spec 
http://docs.oracle.com/javase/8/docs/api/javax/script/ScriptContext.html 
asserts plenty of IAE/NPE exception throwing for getter/setter methods, however this is not completely fulfilled by the instance of SimpleScriptContext used by NashornScriptEngine

The problem shows up with GLOBAL_SCOPE. 

Please try the sample which represents the JCK conformance test which operates with all the available script engine on an abstract level - without any assumption that the Nashorn engine is present or not on the certified system. 

The following code throws no exceptions with JDK9b48: 

import javax.script.*; 

public class Test { 

    public static void main(String[] args) { 

        for (ScriptEngineFactory f : new ScriptEngineManager().getEngineFactories()) { 

            ScriptEngine e = f.getScriptEngine(); 
            ScriptContext c = e.getContext(); 
            System.err.println("context: " + c); 
            System.err.println("engine: " + e); 

            c.getAttribute("", ScriptContext.GLOBAL_SCOPE); 
            c.getAttribute(null, ScriptContext.GLOBAL_SCOPE); 
            c.removeAttribute(null, ScriptContext.GLOBAL_SCOPE); 
            c.removeAttribute("", ScriptContext.GLOBAL_SCOPE); 
            c.setAttribute("", "value", ScriptContext.GLOBAL_SCOPE); 
            c.setAttribute(null, "value", ScriptContext.GLOBAL_SCOPE); 
        } 
    } 
} 

The output would be like 

context: javax.script.SimpleScriptContext@19afc07 
engine: jdk.nashorn.api.scripting.NashornScriptEngine@2db087 


The problem (no exceptions thrown) is perfectly reproducible with just SimpleScriptContext instance: 

        new SimpleScriptContext().getAttribute("", ScriptContext.GLOBAL_SCOPE); 
        new SimpleScriptContext().getAttribute(null, ScriptContext.GLOBAL_SCOPE); 
        new SimpleScriptContext().removeAttribute(null, ScriptContext.GLOBAL_SCOPE); 
        new SimpleScriptContext().removeAttribute("", ScriptContext.GLOBAL_SCOPE); 
        new SimpleScriptContext().setAttribute("", "value", ScriptContext.GLOBAL_SCOPE); 
        new SimpleScriptContext().setAttribute(null, "value", ScriptContext.GLOBAL_SCOPE); 


The list of failing JCK9 tests: 

api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_IAE_emptyName] 
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_NPE] 
api/javax_script/ScriptContext/index.html#Exceptions[removeAttribute_IAE_emptyName] 
api/javax_script/ScriptContext/index.html#Exceptions[removeAttribute_NPE] 
api/javax_script/ScriptContext/index.html#Exceptions[setAttribute_IAE_emptyName] 
api/javax_script/ScriptContext/index.html#Exceptions[setAttribute_NPE] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[getAttribute_IAE_emptyName] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[getAttribute_NPE] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[removeAttribute_IAE_emptyName] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[removeAttribute_NPE] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[setAttribute_IAE_emptyName] 
api/javax_script/SimpleScriptContext/index.html#Exceptions[setAttribute_NPE]