Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8023631

engine.js init script should be loaded into every global instance created by engines

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 8
    • 8
    • core-libs
    • None
    • b106
    • generic
    • generic

    Description

      Nashorn script engine initializes "engine.js" script into the default global instance created/initialized at the engine constructor. But other globals created by ScriptEngine.createBindings() or created to associate with non-nashorn ENGINE_SCOPE Bindings object (such as SimpleBindings) are not initialized properly by loading engine.js definitions into them.

      With JDK- 8023560 ( Arbitrary javax.script.Bindings objects as ENGINE_SCOPE objects are not handled as expected ), arbitrary Bindings objects are associated with nashorn Global instances. But even before that change, ScriptEngine.createBindings also created new Nashorn global instances. So, not initializing engine.js in non-default global instances is a pre-existing bug. But this bug surfaced when testing jdk javax.script tests after JDK-8023560 changes.

      The following simplified test demonstrates the problem:

      public class Main {
         public static void main(String[] args) throws Exception {
             ScriptEngineManager m = new ScriptEngineManager();
             ScriptEngine e = m.getEngineByName("nashorn");
             Bindings globalScope = e.getContext().getBindings(ScriptContext.GLOBAL_SCOPE);
             globalScope.put("x", 33);

             Bindings engineScope = (Bindings) e.eval("this");
             System.out.println(engineScope.get("__noSuchProperty__"));
             System.out.println(engineScope.get("print"));

             Bindings b = e.createBindings();
             System.out.println(b.get("__noSuchProperty__"));
             System.out.println(b.get("print"));

             ScriptContext ctx = new SimpleScriptContext();
             ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
             ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);

             e.eval("print(x)");
             e.eval("print(x)", ctx);
         }
      }

      Output is as follows:

      function (name) {
              'use strict';
              return engine.__noSuchProperty__(this, context, name);
          }
      function print() {
          var writer = context.getWriter();
          if (! (writer instanceof java.io.PrintWriter)) {
              writer = new java.io.PrintWriter(writer);
          }
          
          var buf = new java.lang.StringBuilder();
          for (var i = 0; i < arguments.length; i++) {
              if (i != 0) {
                  buf.append(' ');
              }
              buf.append(String(arguments[i]));
          }
          writer.println(buf.toString());
      }
      null
      function print() { [native code] }
      33
      Exception in thread "main" javax.script.ScriptException: ReferenceError: "x" is not defined in <eval> at line number 1
      at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:484)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:468)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:450)
      at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:165)
      at Main.main(Main.java:23)
      Caused by: <eval>:1 ReferenceError: "x" is not defined
      at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:66)
      at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:328)
      at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:300)
      at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:287)
      at jdk.nashorn.internal.runtime.ScriptObject.noSuchProperty(ScriptObject.java:2034)
      at jdk.nashorn.internal.runtime.ScriptObject.findGetMethod(ScriptObject.java:1737)
      at jdk.nashorn.internal.runtime.ScriptObject.lookup(ScriptObject.java:1651)
      at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:75)
      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:138)
      at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
      at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
      at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:518)
      at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:204)
      at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:364)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:466)
      ... 3 more

      Note that __noSuchProperty__ and print definitions are not available with the global created by ScriptEngine.createBindings method - whereas these engine.js definitions are in default global instance.

      Attachments

        Activity

          People

            sundar Sundararajan Athijegannathan
            sundar Sundararajan Athijegannathan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: