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

ScriptEngine.eval() returns internal classes instead of script result

XMLWordPrintable

    • 7
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0_05"
      Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
      Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Please move to category 'scripting:api' !

      Could not find a way to post this bug to this category.

      A DESCRIPTION OF THE PROBLEM :
      According to API Specification, ScriptEngine.eval() should return 'The value returned from the execution of the script. '.

      This works for simple scripts like 'a+b;'.

      As soon as a function call is involved, the returned value is an instance of 'sun.org.mozilla.javascript.internal.InterpretedFunction'.

      Script Example:

      add(a,b);
      function add(a,b)
       {return a+b };

      This behavior differs from Java 6, where the correct script result was returned.

      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code below.

      ACTUAL -
      sun.org.mozilla.javascript.internal.InterpretedFunction@e76438

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.script.Bindings;
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;

      public class EvalBug {

      public static void main(String[] args) throws Exception {
      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName("JavaScript");
      Bindings bindings = engine.createBindings();
      bindings.put("a", 10);
      bindings.put("b", 20);
      String script = "add(a,b);\nfunction add(a,b) {return a+b };";
      Object result = engine.eval(script, bindings);

      System.out.println(result);
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      import javax.script.Bindings;
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;

      public class EvalBug {

      public static void main(String[] args) throws Exception {
      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName("JavaScript");
      Bindings bindings = engine.createBindings();
      bindings.put("a", 10);
      bindings.put("b", 20);
      String script = "x=add(a,b);\nfunction add(a,b) {return a+b };";
      engine.eval(script, bindings);
      System.out.println(bindings.get("x"));
      }
      }

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: