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

engine.eval call from a java method which was called from a previous engine.eval results in wrong ScriptContext being used.

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8u60, 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b68
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        http://mail.openjdk.java.net/pipermail/nashorn-dev/2015-May/004572.html

        import javax.script.*;
        import jdk.nashorn.api.scripting.*;

        public class Main {
            ScriptEngineFactory factory = new NashornScriptEngineFactory();
            ScriptEngine engine = factory.getScriptEngine();
            Bindings globalBindings = new SimpleBindings();
            Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
            ScriptContext context = new SimpleScriptContext();

            public static void main(String[] ar) throws Exception {
                Main main = new Main();
                main.context.setBindings(main.globalBindings, ScriptContext.GLOBAL_SCOPE);
                main.context.setBindings(main.engineBindings, ScriptContext.ENGINE_SCOPE);

                main.engineBindings.put("program", main);
                main.globalBindings.put("text", "initial text");

                main.engine.eval(
                    " print(text); " +
                    " program.someMethod(); " +
                    " print(text);", main.context);

                /* Expected output:
                   initial text
                   another text
                   initial text

                   Output seen:
                   initial text
                   another text
                   another text
                */
            }

            public void someMethod() throws Exception {
                // a context with a new global bindings, same engine bindings
                ScriptContext context = new SimpleScriptContext();
                Bindings globalBindings = new SimpleBindings();
                context.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE);
                context.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);

                globalBindings.put("text", "another text");
                engine.eval("print(text)", context);
            }
        }

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  • Created:
                    Updated:
                    Resolved: