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

Java to JavaScript Type conversion inconsistencies



    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:


      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)


      Operating System Configuration Information (be specific):


      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

        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

      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);
      js.eval(script2, b);
      catch(ScriptException ex) {




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