JDK-8069258 : ScriptEngine provides an unusable class when passing JavaScript Objects
  • Type: Bug
  • Component: core-libs
  • Sub-Component: javax.script
  • Affected Version: 8u25
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux_ubuntu
  • CPU: x86
  • Submitted: 2015-01-14
  • Updated: 2015-03-25
  • Resolved: 2015-03-25
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Client VM (build 25.25-b02, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Ubunbtu 14.04

A DESCRIPTION OF THE PROBLEM :
If a Java method has arguments of type Object, nashorn javaScript calling said java method will try to pass objects as an unusable type.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Arg is (java.util.Map | jdk.nashorn.api.ScriptObjectMirror)
ACTUAL -
Arg is jdk.nashorn.internal.scripts.JO4@<hash_code>

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public static class Helper {
     public void doObj(Object arg) {
         System.out.println("Arg is " + arg);
     }
}

public static void main(String args[]) throws Exception {
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

      engine.put("helper", new Helper());
      engine.eval("var x = {foo:\"bar\"};\n" +
                  "helper.doObj(x);\n");
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Avoiding parameter types of 'Object' in Java when I know JavaScript will pass js-Objects to Java method.


Comments
Per submitter's response this is confirmed fixed in 8u40.
25-03-2015

Received confirmation from the submitter: ****************************************************************************** On 3/22/2015 5:28 AM, ........... wrote: > Hello ......., > This is resolved with 8u40. > Thank you, > .......... ********************************************************************************
25-03-2015

Reopen after the customer confirms the issue with 8u40 with a fresh test case.
28-01-2015

Sent an email to the submitter to confirm the resolution with JDK 8u40 ea b19.
27-01-2015

As part of JDK-8050977, script objects are wrapped as ScriptObjectMirror when script object is passed. ScriptObjectMirror is documented here: http://download.java.net/jdk9/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/ScriptObjectMirror.html This change (JDK-8050977) has been backported to jdk8u-dev. As for toString printing "[object Object]", that says nothing about "usability" of script object from java side.
19-01-2015

If you run this modified source against Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b19), you'll see that arg is of type jdk.nashorn.api.scripting.ScriptObjectMirror import javax.script.*; public class Main { public static class Helper { public void doObj(Object arg) { System.out.println("Arg is " + arg.getClass()); } } public static void main(String args[]) throws Exception { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.put("helper", new Helper()); engine.eval("var x = {foo:\"bar\"};\n" + "helper.doObj(x);\n"); } }
19-01-2015

Closing this issue as "incomplete". If "known java type" is expected for script object arguments, that has been addressed as part of another bug fix and backported to jdk8u-dev.
19-01-2015

Could reproduce this with JDK 8u25 and 8u40. With JDK 9ea, Arg is [object Object].
19-01-2015