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

Objects and Functions returned by loadWithNewGlobal behave different

XMLWordPrintable

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


      ADDITIONAL OS VERSION INFORMATION :
      Linux 4.3.0-1-amd64 #1 SMP Debian 4.3.3-2 (2015-12-17) x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      returned Objects cannot be used in Object.defineProperties
      (e.g. run with jjs)
      -----
       var x = loadWithNewGlobal({script:'({get:function(){return 1;}})',name:'x'});
       var o = {};
       Object.defineProperty(o,'x',x);
      -----
      bug1.js:3 TypeError: [object Object] is not an Object

      returned Functions cannot be used in Object.defineProperties
      -----
       var x = loadWithNewGlobal({script:'(function(){return 1;})',name:'x'});
       var o = {};
       Object.defineProperty(o,'x',{get:x});
      -----
      bug2.js:3 TypeError: function(){return 1;} is not a function

      returned Function cannot be directly used as SAM
      -----
       var x = loadWithNewGlobal({script:'(function(){return 1;})',name:'x'});
       javax.swing.SwingUtilities.invokeLater(x);
      -----
      Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.api.scripting.ScriptObjectMirror to java.lang.Runnable


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      *save one of the examples above as textfile
      *run jjs <file>


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No error
      ACTUAL -
      TypeError: [object Object] is not an Object


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      class Bug{
        public static void main(String[] ar) throws Exception{
          new javax.script.ScriptEngineManager().getEngineByName("nashorn").eval(
            "var x = loadWithNewGlobal({script:'(function(){print(\"X\");})',name:'x'});\n"+
            "javax.swing.SwingUtilities.invokeLater(x);");
        }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      *No workaround found for Object.defineProperties
      *for SAM: use Constructor, or wrapper function

            sundar Sundararajan Athijegannathan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: