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

JavaScript variable 'n' not stored in Binding object

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 6
    • Fix Version/s: 6
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      b53
    • CPU:
      x86
    • OS:
      linux

      Description

      J2SE Version (please include all output from java -version flag):

        java version "1.6.0-ea"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b45)
        Java HotSpot(TM) Client VM (build 1.6.0-ea-b45, mixed mode, sharing)

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

        No

      Operating System Configuration Information (be specific):

        Linux

      Bug Description:

        JavaScript variable 'n' not stored in Bindings object

        The attached program demonstrates strange behavior of the Bindings
        object. It evaluates a script that creates two JavaScript varaibles
        named n and m. After running the script, we iterate through the
        Bindings object and see that it contains an entry for m, but not for n.

        this is somehow related to the name "n". Other
       letters and words work,
        but "n" doesn't!

        Also, something in the script execution environment adds variabes
        "print" and "context" to the Bindings. I suspect that these should be
        put somewhere else (at least "print" should be) It shouldn't pollute
        our Bindings object, but should be put with other global JavaScript
        symbols.

         See the attached program for details.


      Steps to Reproduce (be specific):

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

           public class Bug {
      public static void main(String[] args)
      throws ScriptException, NoSuchMethodException
      {
      ScriptEngineManager sem = new ScriptEngineManager();
      ScriptEngine js = sem.getEngineByExtension("js");
      Bindings b = js.createBindings();

      String script = "var n = 1; var m = 1;";

      js.eval(script, b);


      // now iterate the bindings to see what variables are stored
      Set<Map.Entry<String,Object>> entryset = b.entrySet();
      for(Map.Entry<String,Object> entry : entryset) {
      String name = entry.getKey();
      Object value = entry.getValue();
      String type = value.getClass().getName();
      System.out.printf("%s: %s: %s%n", name, type, value);
      }
      }
           }
      J2SE Version (please include all output from java -version flag):

        java version "1.6.0-ea"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b45)
        Java HotSpot(TM) Client VM (build 1.6.0-ea-b45, mixed mode, sharing)

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

        No

      Operating System Configuration Information (be specific):

        Linux

      Bug Description:

        Java to Javascript Type conversion inconsistencies

        Java values stored in a javax.script.Bindings object are converted to
        JavaScript values in a different way than Java values read from the
        fields of a Java object.

        In particular, a char value stored in a Bindings object converts to a
        JavaScript string. But a char value read as the field of a Java object
        converts to a number.

        Also, Java wrapper types like Integer and Boolean do not convert to
        JavaScript types when read from the fields of a JavaObject they just
        become wrapped Java objects. BigInteger and BigDecimal convert to
        numbers when stored in a Bindings object, but just become wrapped
        JavaObject values when they are read as fields of an object.

        I suspect that there are two different sets of code doing the
        conversions. One is javax.script code that does Bindings conversions.
        And the other is LiveConnect code somewhere in the rhino sources.

        In any case, it seems like a consistent set of conversions is
        important.

        See the attached program for details.

      Steps to Reproduce (be specific):

      import javax.script.*;
      import java.math.*;


      public class JavaToJavaScript {
           public static class Data {
      public boolean t = true;
      public boolean f = false;
      public Boolean T = Boolean.TRUE;
      public Boolean F = Boolean.FALSE;

      public byte b = Byte.MAX_VALUE;
      public Byte B = Byte.MIN_VALUE;

      public short s = Short.MAX_VALUE;
      public Short S = Short.MIN_VALUE;

      public int i = Integer.MAX_VALUE;
      public Integer I = Integer.MIN_VALUE;

      public long l = Long.MAX_VALUE;
      public Long L = Long.MIN_VALUE;

      public float fl = Float.MAX_VALUE;
      public Float FL = Float.MIN_VALUE;

      public double d = Double.MAX_VALUE;
      public Double D = Double.MIN_VALUE;

      public BigInteger bi = new BigInteger("1000000");
      public BigDecimal bd = new
      BigDecimal("1.23456789012345678901234567890E1000000");

      public char c = 'a';
      public Character C = 'A';

      public String st = "hello world";
      public java.util.Date today = new java.util.Date();

      public int[] ints = { 1, 2, 3 };
      public char[] getChars() { return new char[] { 'j', 's' }; };
           }

           public static void main(String[] args) {
      ScriptEngineManager sem = new ScriptEngineManager();
      ScriptEngine js = sem.getEngineByExtension("js");

      Bindings b = new SimpleBindings();
      b.put("data", new Data());
      b.put("n", null);
      b.put("b", new Boolean(true));
      b.put("y", new Byte((byte)1));
      b.put("s", new Short((short)1));
      b.put("i", new Integer(1));
      b.put("l", new Long(Long.MAX_VALUE));
      b.put("f", new Float(1.0));
      b.put("d", new Double(1.0));
      b.put("bi", new java.math.BigInteger("100000000"));
      b.put("bd", new java.math.BigDecimal("1.23456789e1000"));
      b.put("nan", 0.0/0.0);
      b.put("c", new Character('a'));
      b.put("st", "hello world");
      b.put("today", new java.util.Date());
      b.put("js", js);

      String script1 =
      "function p(x) { print(typeof x + ': ' + x); } " +
      "p(b); p(y); p(s); p(i); p(l); p(f); p(d); p(bi); p(bd); p(nan);" +
      "p(c); p(st); p(today); p(js); p(null);";

      String script2 =
      "for(var p in data) {" +
      " var v = data[p];" +
      " var t; " +
      " if (v == null) t = 'null';" +
      " else t = typeof v;" +
      " if (t == 'function') continue; " +
      " if (t == 'object') " +
      " t = Object.prototype.toString.apply(v);" +
      " print(t + ' ' + p + ' = ' + v);"+
      "}" +
      " print(data.bd - 1);" +
      " print(data.l - 1);" +
      " print(data.ints[0]);" +
      " var chars = data.getChars(); " +
      "for(var i = 0; i < chars.length; i++) print(chars[i]);" +
      "";

      try {
      js.eval(script1, b);
      System.out.println("----------");
      js.eval(script2, b);
      }
      catch(ScriptException ex) {
      System.out.println(ex);
      }
           }
      }

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: