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

JSON.stringify returns "undefined" for JS objects defined in another thread

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 7u72, 8u40
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.8.0_40-ea"
      Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.40-b21, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      x64 Windows 8 [Version 6.2.9200]


      A DESCRIPTION OF THE PROBLEM :
      When a JavaScript object is created in a script engine instance and passed through Java/Scala code to another script engine instance in another thread, JSON.stringify returns "undefined" for that object. The object itself works ok, all its contents is available to JS code but stringifying it fails.

      REGRESSION. Last worked in version 8u25

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a JS object like var obj = {test: 45} in JS.
      Pass it to another JS engine running in another thread via Java code.
      Execute JSON.stringify(obj)
      ---------- BEGIN SOURCE ----------
      import javax.script.*;

      public class Main {
          static String script = "function run() {\n" +
                  " var obj = {id: \"testing\"};\n" +
                  " Object.freeze(obj);\n" +
                  " main.testRun(obj);\n" +
                  " main.testRun2(obj);\n" +
                  "}\n" +
                  "\n" +
                  "function print(obj) {\n" +
                  " return obj.id + \" -- \" + JSON.stringify(obj);\n" +
                  "}\n";
          ScriptEngineManager mgr = new ScriptEngineManager();
          ScriptEngine nashorn = mgr.getEngineByName("nashorn");

          public static void main(String [] args) throws Exception {
              System.out.println("Java version " +
      System.getProperty("java.version"));
              Main main = new Main();
              main.run();
          }

          void run() throws Exception {
              Bindings bindings = nashorn.getBindings(ScriptContext.ENGINE_SCOPE);
              bindings.put("main", this);
              nashorn.eval(script);
              ((Invocable)nashorn).invokeFunction("run");
          }

          public void testRun(Object obj) throws Exception {
              System.out.println("Object: " + obj);
              // use same engine instance
              String s = (String) ((Invocable)nashorn).invokeFunction("print",
      obj);
              System.out.println("Result: " + s);
          }

          public void testRun2(Object obj) throws Exception {
              System.out.println("Object: " + obj);
              // another engine instance
              ScriptEngine nashorn2 = mgr.getEngineByName("nashorn");
              nashorn2.eval(script);
              String s = (String) ((Invocable)nashorn2).invokeFunction("print",
      obj);
              System.out.println("Result: " + s);
          }
      }
      ---------END SOURCE----------


      REPRODUCIBILITY :
      This bug can be reproduced always.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: