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

Nashorn script engine context behaves differently to JDK7

XMLWordPrintable

      In FXML, we used to create a new bindings for a scripts in tag attributes, as we needed a new "event" variable. This is the code that does this:

      Bindings engineBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
      Bindings localBindings = scriptEngine.createBindings();
      localBindings.put(EVENT_KEY, event);
      scriptEngine.setBindings(localBindings, ScriptContext.ENGINE_SCOPE);

      // Execute the script
      try {
      scriptEngine.eval(script);
      } catch (ScriptException exception){
           throw new RuntimeException(exception);
      }

      However, this does no longer works. If there's a function already defined in the engine, it won't be found in Nashorn.
      Not sure why it worked in JDK7 actually, as localBindings appear empty both in JDK7 and JDK8 when printed out.
      The difference is that JDK7 engineBindings contain also the function name, but JDK8 does not! So even if I try to copy the full context using "localBindings.putAll(engineBindings)", this does not work, as engineBindings does NOT appear to contain the function. But when I execute this with the original engineBingins, the function is found, even though it's not in the map.

      For a full use-case, see https://javafx-jira.kenai.com/browse/RT-30986

        1. Test.java
          1.0 kB
        2. Test.java
          1.0 kB

            sundar Sundararajan Athijegannathan
            msladecek Martin Sládeček
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: