java.lang.Object's methods are pushed into Object.prototype. The less of them, the better. Moving them to Class
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 23 Apr 2014 17:43:36 +0200
changeset 1483cecf27be3802
parent 1435 1b30bba2b38d
child 1484 76cdd49e774b
java.lang.Object's methods are pushed into Object.prototype. The less of them, the better. Moving them to Class
rt/emul/compact/src/main/java/java/lang/System.java
rt/emul/mini/src/main/java/java/lang/Class.java
rt/emul/mini/src/main/java/java/lang/Object.java
     1.1 --- a/rt/emul/compact/src/main/java/java/lang/System.java	Fri Feb 07 10:03:32 2014 +0100
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/System.java	Wed Apr 23 17:43:36 2014 +0200
     1.3 @@ -43,7 +43,7 @@
     1.4      }
     1.5      
     1.6      public static int identityHashCode(Object obj) {
     1.7 -        return obj.defaultHashCode();
     1.8 +        return Class.defaultHashCode(obj);
     1.9      }
    1.10  
    1.11      public static String getProperty(String name) {
     2.1 --- a/rt/emul/mini/src/main/java/java/lang/Class.java	Fri Feb 07 10:03:32 2014 +0100
     2.2 +++ b/rt/emul/mini/src/main/java/java/lang/Class.java	Wed Apr 23 17:43:36 2014 +0200
     2.3 @@ -26,15 +26,16 @@
     2.4  package java.lang;
     2.5  
     2.6  import java.io.ByteArrayInputStream;
     2.7 -import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
     2.8  import java.io.InputStream;
     2.9  import java.lang.annotation.Annotation;
    2.10 +import java.lang.reflect.Array;
    2.11  import java.lang.reflect.Constructor;
    2.12  import java.lang.reflect.Field;
    2.13  import java.lang.reflect.Method;
    2.14  import java.lang.reflect.TypeVariable;
    2.15  import java.net.URL;
    2.16  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    2.17 +import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
    2.18  import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
    2.19  
    2.20  /**
    2.21 @@ -1727,4 +1728,50 @@
    2.22          "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
    2.23      )
    2.24      public native boolean desiredAssertionStatus();
    2.25 +    
    2.26 +    static void registerNatives() {
    2.27 +        boolean assertsOn = false;
    2.28 +        assert assertsOn = false;
    2.29 +        if (assertsOn) {
    2.30 +            try {
    2.31 +                Array.get(null, 0);
    2.32 +            } catch (Throwable ex) {
    2.33 +                // ignore
    2.34 +            }
    2.35 +        }
    2.36 +    }
    2.37 +
    2.38 +    @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
    2.39 +            + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
    2.40 +    )
    2.41 +    static native void registerToString();
    2.42 +    
    2.43 +    @JavaScriptBody(args = {"self"}, body
    2.44 +            = "var c = self.constructor.$class;\n"
    2.45 +            + "return c ? c : null;\n"
    2.46 +    )
    2.47 +    static native Class<?> classFor(Object self);
    2.48 +    
    2.49 +    @JavaScriptBody(args = { "self" }, body
    2.50 +            = "if (self.$hashCode) return self.$hashCode;\n"
    2.51 +            + "var h = self.computeHashCode__I ? self.computeHashCode__I() : Math.random() * Math.pow(2, 31);\n"
    2.52 +            + "return self.$hashCode = h & h;"
    2.53 +    )
    2.54 +    static native int defaultHashCode(Object self);
    2.55 +
    2.56 +    @JavaScriptBody(args = "self", body
    2.57 +            = "\nif (!self.$instOf_java_lang_Cloneable) {"
    2.58 +            + "\n  return null;"
    2.59 +            + "\n} else {"
    2.60 +            + "\n  var clone = self.constructor(true);"
    2.61 +            + "\n  var props = Object.getOwnPropertyNames(self);"
    2.62 +            + "\n  for (var i = 0; i < props.length; i++) {"
    2.63 +            + "\n    var p = props[i];"
    2.64 +            + "\n    clone[p] = self[p];"
    2.65 +            + "\n  };"
    2.66 +            + "\n  return clone;"
    2.67 +            + "\n}"
    2.68 +    )
    2.69 +    static native Object clone(Object self) throws CloneNotSupportedException;
    2.70 +    
    2.71  }
     3.1 --- a/rt/emul/mini/src/main/java/java/lang/Object.java	Fri Feb 07 10:03:32 2014 +0100
     3.2 +++ b/rt/emul/mini/src/main/java/java/lang/Object.java	Wed Apr 23 17:43:36 2014 +0200
     3.3 @@ -25,7 +25,6 @@
     3.4  
     3.5  package java.lang;
     3.6  
     3.7 -import java.lang.reflect.Array;
     3.8  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     3.9  import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
    3.10  
    3.11 @@ -40,23 +39,9 @@
    3.12   */
    3.13  @JavaScriptPrototype(container = "Object.prototype", prototype = "new Object")
    3.14  public class Object {
    3.15 -
    3.16 -    private static void registerNatives() {
    3.17 -        boolean assertsOn = false;
    3.18 -        assert assertsOn = false;
    3.19 -        if (assertsOn) try {
    3.20 -            Array.get(null, 0);
    3.21 -        } catch (Throwable ex) {
    3.22 -            // ignore
    3.23 -        }
    3.24 -    }
    3.25 -    @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);" +
    3.26 -        "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
    3.27 -    )
    3.28 -    private static native void registerToString();
    3.29      static {
    3.30 -        registerNatives();
    3.31 -        registerToString();
    3.32 +        Class.registerNatives();
    3.33 +        Class.registerToString();
    3.34      }
    3.35  
    3.36      /**
    3.37 @@ -80,16 +65,10 @@
    3.38       *         <cite>The Java&trade; Language Specification</cite>.
    3.39       */
    3.40      public final Class<?> getClass() {
    3.41 -        Class<?> c = getClassImpl();
    3.42 +        Class<?> c = Class.classFor(this);
    3.43          return c == null ? Object.class : c;
    3.44      }
    3.45  
    3.46 -    @JavaScriptBody(args={}, body=
    3.47 -          "var c = this.constructor.$class;\n"
    3.48 -        + "return c ? c : null;\n"
    3.49 -    )
    3.50 -    private final native Class<?> getClassImpl();
    3.51 -    
    3.52      /**
    3.53       * Returns a hash code value for the object. This method is
    3.54       * supported for the benefit of hash tables such as those provided by
    3.55 @@ -126,18 +105,9 @@
    3.56       * @see     java.lang.System#identityHashCode
    3.57       */
    3.58      public int hashCode() {
    3.59 -        return defaultHashCode();
    3.60 +        return Class.defaultHashCode(this);
    3.61      }
    3.62 -    @JavaScriptBody(args = {}, body = 
    3.63 -        "if (this.$hashCode) return this.$hashCode;\n"
    3.64 -        + "var h = this.computeHashCode__I();\n"
    3.65 -        + "return this.$hashCode = h & h;"
    3.66 -    )
    3.67 -    final native int defaultHashCode();
    3.68  
    3.69 -    @JavaScriptBody(args = {}, body = "return Math.random() * Math.pow(2, 32);")
    3.70 -    native int computeHashCode();
    3.71 -    
    3.72      /**
    3.73       * Indicates whether some other object is "equal to" this one.
    3.74       * <p>
    3.75 @@ -249,28 +219,13 @@
    3.76       * @see java.lang.Cloneable
    3.77       */
    3.78      protected Object clone() throws CloneNotSupportedException {
    3.79 -        Object ret = clone(this);
    3.80 +        Object ret = Class.clone(this);
    3.81          if (ret == null) {
    3.82              throw new CloneNotSupportedException(getClass().getName());
    3.83          }
    3.84          return ret;
    3.85      }
    3.86  
    3.87 -    @JavaScriptBody(args = "self", body = 
    3.88 -          "\nif (!self.$instOf_java_lang_Cloneable) {"
    3.89 -        + "\n  return null;"
    3.90 -        + "\n} else {"
    3.91 -        + "\n  var clone = self.constructor(true);"
    3.92 -        + "\n  var props = Object.getOwnPropertyNames(self);"
    3.93 -        + "\n  for (var i = 0; i < props.length; i++) {"
    3.94 -        + "\n    var p = props[i];"
    3.95 -        + "\n    clone[p] = self[p];"
    3.96 -        + "\n  };"
    3.97 -        + "\n  return clone;"
    3.98 -        + "\n}"
    3.99 -    )
   3.100 -    private static native Object clone(Object self) throws CloneNotSupportedException;
   3.101 -
   3.102      /**
   3.103       * Returns a string representation of the object. In general, the
   3.104       * {@code toString} method returns a string that