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

Reviving functions in script engine

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Withdrawn
    • Icon: P3 P3
    • None
    • 6u7
    • core-libs

      E:\alcatel\borsotti\jtest>java -version
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

      Does this problem occur on J2SE 1.4.x or 5.0.x ? Yes / No (pick one)
      Not known

      Operating System Configuration Information (be specific):
      Windows Vista
      SP1


      Hardware Configuration Information (be specific):
      AMD Athlon X2-64 2Gb RAM


      Bug Description:
      The Rhino scripting engine embedded in Java 6 recreates the "print" and "println" functions each time eva() is called.

      Compile the following program:

      import java.util.*;
      import java.io.*;
      import javax.script.*;

      public class ScriptPrint {
          public static void main(String[] args) throws Exception {

              ScriptEngineManager factory = new ScriptEngineManager();
              ScriptEngine engine = factory.getEngineByName("JavaScript");
              ScriptContext cxt = engine.getContext();
              Bindings bin = cxt.getBindings(ScriptContext.ENGINE_SCOPE);

              System.out.println("scope----first----");
              for (Map.Entry<String,Object> e : bin.entrySet()){
                  System.out.printf("ent: %s %s%n",e,e.getValue().getClass());
              }


              engine.eval("");
              System.out.println("scope----second----");
              for (Map.Entry<String,Object> e : bin.entrySet()){
                  System.out.printf("ent: %s %s%n",e,e.getValue().getClass());

              }

              engine.eval("");
              System.out.println("scope----third----");
              for (Map.Entry<String,Object> e : bin.entrySet()){
                  System.out.printf("ent: %s %s%n",e,e.getValue().getClass());

              }
          }
      }

      Then run it:

      E:\alcatel\borsotti\jtest>java ScriptPrint
      scope----first----
      scope----second----
      ent: println=sun.org.mozilla.javascript.internal.InterpretedFunction@133f1d7 class sun.org.mozilla.javascript.internal.I

      nterpretedFunction
      ent: context=javax.script.SimpleScriptContext@14a9972 class javax.script.SimpleScriptContext
      ent: print=sun.org.mozilla.javascript.internal.InterpretedFunction@a01335 class sun.org.mozilla.javascript.internal.Inte

      rpretedFunction
      scope----third----
      ent: println=sun.org.mozilla.javascript.internal.InterpretedFunction@14d3343 class sun.org.mozilla.javascript.internal.I
      nterpretedFunction
      ent: context=javax.script.SimpleScriptContext@14a9972 class javax.script.SimpleScriptContext

      ent: print=sun.org.mozilla.javascript.internal.InterpretedFunction@1608e05 class sun.org.mozilla.javascript.internal.Int
      erpretedFunction

      Note that the execution of an eval(""), which should produce nothing, makes the

      engine re-create the "print" and "println" functions.
      It does this even when the calling program has deleted them (either with
      bin.remove("print") or with engine.eval("delete print")).

      This means that a script cannot redefine these functions.

      Remedy: refrain from creating these functions in the engine, or make it
      create them only once.

            sundar Sundararajan Athijegannathan
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: