JDK-8130751 : Nashorn recursive invocation of eval clobbers bindings
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u45
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: os_x
  • CPU: x86
  • Submitted: 2015-07-01
  • Updated: 2015-07-08
  • Resolved: 2015-07-02
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
OS X 10.10

A DESCRIPTION OF THE PROBLEM :
Test program showing buggy behavior attached below.  Basically it looks like a recursive call to eval changes the original bindings part-way through the execution of the enclosing script.



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package edu;

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

public class Test {

   static ScriptEngine engine;
   
   public static void main (String[] args) {
      NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
      engine = factory.getScriptEngine(new String[]{"--global-per-engine"});
      Bindings bindings = new SimpleBindings();
      bindings.put("hello", "world");
      try {
         engine.eval("print(hello)", bindings); // prints: world
         //  Following prints:
         //    nested
         //    javax.script.ScriptException: ReferenceError: "hello" is not defined in <eval> at line number 1
         engine.eval("Packages.edu.Test.nested(); print(hello);", bindings);
      } catch (ScriptException e) { System.out.println(e); }
      System.out.println(bindings.get("hello")); // prints: world
   }
   
   public static void nested () {
      try { engine.eval("print('nested')"); } catch (ScriptException e) { System.out.println(e); }
   }
}
---------- END SOURCE ----------

SUPPORT :
YES