JDK-8055687 : Wrong "this" passed to JSObject.eval call
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2014-08-20
  • Updated: 2015-01-21
  • Resolved: 2014-08-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b29Fixed
Description
Test:

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

public class Main {
  public static void main(String[] args) throws Exception {
     ScriptEngineManager m = new ScriptEngineManager();
     ScriptEngine e = m.getEngineByName("nashorn");
     JSObject jsobj = (JSObject) e.eval("({ foo: 23})");
     jsobj.eval("print(Object.keys(this))");
     jsobj.eval("print(this.foo)");
  }
}

Output expected:
foo
23

Output seen

undefined

Clearly, "global" is passed as "this" to the script evaluated by JSObject.eval.