JDK-8071594 : NashornScriptEngine returns javax.script.ScriptContext instance which doesn't completely conform to the spec regarding exceptions throwing
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Not an Issue
  • Submitted: 2015-01-26
  • Updated: 2015-02-10
  • Resolved: 2015-02-10
Related Reports
Relates :  
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 state/instance of SimpleScriptContext used by NashornScriptEngine; the following code will produce no exceptions:


        new ScriptEngineManager()
            .getEngineFactories()
            .stream()
            .map(ScriptEngineFactory::getScriptEngine)
            .map(engine -> {
                System.err.println("engine: " + engine);
                return engine.getContext();
            })
            .forEach(c -> {
                System.err.println("context: " + c);
                c.getAttribute("");
                c.getAttribute(null);
                c.getAttribute("", ScriptContext.ENGINE_SCOPE);
                c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
                c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
                c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
                c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
                c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
                c.getAttributesScope("");
                c.getAttributesScope(null);
            });

However if SimpleScriptContext instance is created manually the expected exceptions are thrown - any of the following lines WILL cause the expected NPE or IAE:

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

---

The following JCK9 tests will fail due to the problem with Nashorn:

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[getAttribute_noScope_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[getAttribute_noScope_NPE]
api/javax_script/ScriptContext/index.html#Exceptions[getAttributesScope_noScope_IAE_emptyName]
api/javax_script/ScriptContext/index.html#Exceptions[getAttributesScope_noScope_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]

Please note these tests are not targeted or specific for Nashorn - they check any implementation found on a particular JavaSE platform and they fail for Oracle JDK.




Comments
Separate bug filed for the second part described (JDK-8072853)
10-02-2015

Can not reproduce with the latest jdk8u-dev/nashorn and jdk9-dev/nashorn builds.
09-02-2015

I tried the following standalone program on jdk8u-dev/nashorn and jdk9-dev/nashorn latest builds. I got the exceptions as expected: import javax.script.*; public class Main { public static void main(String[] args) throws Exception { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine e = m.getEngineByName("nashorn"); ScriptContext c = e.getContext(); try { c.getAttribute(""); } catch (Exception e1) { System.out.println(e1); } try { c.getAttribute(null); } catch (Exception e2) { System.out.println(e2); } try { c.getAttribute("", ScriptContext.ENGINE_SCOPE); } catch (Exception e3) { System.out.println(e3); } try { c.getAttribute(null, ScriptContext.ENGINE_SCOPE); } catch (Exception e4) { System.out.println(e4); } try { c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); } catch (Exception e5) { System.out.println(e5); } try { c.removeAttribute("", ScriptContext.ENGINE_SCOPE); } catch (Exception e6) { System.out.println(e6); } try { c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); } catch (Exception e7) { System.out.println(e7); } try { c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); } catch (Exception e8) { System.out.println(e8); } try { c.getAttributesScope(""); } catch (Exception e9) { System.out.println(e9); } try { c.getAttributesScope(null); } catch (Exception e10) { System.out.println(e10); } } } Please check with the latest nashorn.jar built using jdk8u-dev/nashorn and jdk9/nashorn repos with changeset: 1200:09bd5b8abcba tag: tip user: sundar date: Fri Feb 06 19:28:26 2015 +0530 summary: 8071989: NashornScriptEngine returns javax.script.ScriptContext instance with insonsistent get/remove methods behavior for undefined attributes changeset: 1182:dbfbf5423642 tag: tip user: sundar date: Fri Feb 06 19:28:26 2015 +0530 summary: 8071989: NashornScriptEngine returns javax.script.ScriptContext instance with insonsistent get/remove methods behavior for undefined attributes respectively and re-open this issue (if needed).
09-02-2015