Move native functionality out of Integer,Number,BigDecimal.

PiperOrigin-RevId: 360343062
Change-Id: I0bc4c1517a59769255e34f0b715cabfc4ff46be6
diff --git a/dev/core/test/com/google/gwt/dev/CompilerTest.java b/dev/core/test/com/google/gwt/dev/CompilerTest.java
index cfcae5d..308973d 100644
--- a/dev/core/test/com/google/gwt/dev/CompilerTest.java
+++ b/dev/core/test/com/google/gwt/dev/CompilerTest.java
@@ -2767,6 +2767,8 @@
         "java.lang.Throwable$HasJavaThrowable",
         "java.lang.Throwable$NativeError",
         "java.lang.Throwable$NativeTypeError",
+        "javaemul.internal.JsUtils",
+        "javaemul.internal.JsUtils$NativeNumber",
         "javaemul.internal.HashCodes",
         "javaemul.internal.NativeRegExp",
         "javaemul.internal.NativeRegExp$Match"));
diff --git a/user/super/com/google/gwt/emul/java/lang/Integer.java b/user/super/com/google/gwt/emul/java/lang/Integer.java
index 93e16dc..1e2f584 100644
--- a/user/super/com/google/gwt/emul/java/lang/Integer.java
+++ b/user/super/com/google/gwt/emul/java/lang/Integer.java
@@ -16,7 +16,6 @@
 package java.lang;
 
 import javaemul.internal.JsUtils;
-import jsinterop.annotations.JsType;
 
 /**
  * Wraps a primitive <code>int</code> as an object.
@@ -220,35 +219,16 @@
     return toUnsignedString(value, 8);
   }
 
-  private static String toUnsignedString(int value, int radix) {
-    return toRadixString(toUnsigned(value), radix);
+  static String toUnsignedString(int value, int radix) {
+    return JsUtils.uintToString(value, radix);
   }
 
-  @SuppressWarnings("unusable-by-js")
-  private static native double toUnsigned(int value) /*-{
-    // Might return a number that is larger than int32
-    return (value >>> 0);
-  }-*/;
-
   public static String toString(int value) {
     return String.valueOf(value);
   }
 
   public static String toString(int value, int radix) {
-    if (radix == 10 || radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
-      return String.valueOf(value);
-    }
-    return toRadixString(value, radix);
-  }
-
-  private static String toRadixString(double value, int radix) {
-    NativeNumber number = JsUtils.uncheckedCast(value);
-    return number.toString(radix);
-  }
-
-  @JsType(isNative = true, name = "Number", namespace = "<window>")
-  private interface NativeNumber {
-    String toString(int radix);
+    return JsUtils.intToString(value, radix);
   }
 
   public static Integer valueOf(int i) {
diff --git a/user/super/com/google/gwt/emul/java/lang/Number.java b/user/super/com/google/gwt/emul/java/lang/Number.java
index e487b9b..55ce31b 100644
--- a/user/super/com/google/gwt/emul/java/lang/Number.java
+++ b/user/super/com/google/gwt/emul/java/lang/Number.java
@@ -16,10 +16,8 @@
 package java.lang;
 
 import java.io.Serializable;
-
 import javaemul.internal.JsUtils;
 import javaemul.internal.NativeRegExp;
-
 import jsinterop.annotations.JsMethod;
 import jsinterop.annotations.JsType;
 
@@ -192,12 +190,9 @@
     if (!__isValidDouble(s)) {
       throw NumberFormatException.forInputString(s);
     }
-    return parseFloat(s);
+    return JsUtils.parseFloat(s);
   }
 
-  @JsMethod(namespace = "<window>")
-  private static native double parseFloat(String str);
-
   /**
    * @skip
    *
diff --git a/user/super/com/google/gwt/emul/java/math/BigDecimal.java b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
index 52e44f1..0802895 100644
--- a/user/super/com/google/gwt/emul/java/math/BigDecimal.java
+++ b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
@@ -37,12 +37,9 @@
 import static javaemul.internal.InternalPreconditions.checkNotNull;
 
 import java.io.Serializable;
-
 import javaemul.internal.JsUtils;
 import javaemul.internal.NativeRegExp;
 
-import jsinterop.annotations.JsType;
-
 /**
  * This class represents immutable arbitrary precision decimal numbers. Each
  * {@code BigDecimal} instance is represented with a unscaled arbitrary
@@ -496,16 +493,6 @@
    * @param digits number of digits of precision to include
    * @return non-localized string representation of {@code d}
    */
-  private static String toPrecision(double value, int digits) {
-    NativeNumber number = JsUtils.uncheckedCast(value);
-    return number.toPrecision(digits);
-  }
-
-  @JsType(isNative = true, name = "Number", namespace = "<window>")
-  private interface NativeNumber {
-    String toPrecision(int digits);
-  }
-
   private static BigDecimal valueOf(double smallValue, double scale) {
     return new BigDecimal(smallValue, scale);
   }
@@ -720,7 +707,7 @@
       // math.03=Infinity or NaN
       throw new NumberFormatException("Infinite or NaN"); //$NON-NLS-1$
     }
-    initFrom(toPrecision(val, 20));
+    initFrom(JsUtils.toPrecision(val, 20));
   }
 
   /**
@@ -736,11 +723,7 @@
    *           represented within the given precision without rounding.
    */
   public BigDecimal(double val, MathContext mc) {
-    if (Double.isInfinite(val) || Double.isNaN(val)) {
-      // math.03=Infinity or NaN
-      throw new NumberFormatException("Infinite or NaN"); //$NON-NLS-1$
-    }
-    initFrom(toPrecision(val, 20));
+    this(val);
     inplaceRound(mc);
   }
 
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java b/user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java
index efd46f1..c8bf1e8 100644
--- a/user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java
+++ b/user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java
@@ -18,12 +18,11 @@
 import javaemul.internal.annotations.DoNotAutobox;
 import javaemul.internal.annotations.UncheckedCast;
 import jsinterop.annotations.JsMethod;
+import jsinterop.annotations.JsType;
 
-/**
- * Provides an interface for simple JavaScript idioms that can not be expressed in Java.
- */
+/** Provides an interface for simple JavaScript idioms that can not be expressed in Java. */
 @SuppressWarnings("unusable-by-js")
-public class JsUtils {
+public final class JsUtils {
 
   @JsMethod(namespace = "<window>", name = "Date.now")
   public static native double getTime();
@@ -31,9 +30,42 @@
   @JsMethod(namespace = "<window>")
   public static native int parseInt(String s, int radix);
 
+  @JsMethod(namespace = "<window>")
+  public static native double parseFloat(String str);
+
   @JsMethod(namespace = "<window>", name = "typeof")
   public static native String typeOf(Object obj);
 
+  public static String toPrecision(double value, int precision) {
+    NativeNumber number = JsUtils.uncheckedCast(value);
+    return number.toPrecision(precision);
+  }
+
+  public static String intToString(int value, int radix) {
+    return numberToString(value, radix);
+  }
+
+  public static String uintToString(int value, int radix) {
+    return numberToString(toDoubleFromUnsignedInt(value), radix);
+  }
+
+  @JsMethod
+  public static native int toDoubleFromUnsignedInt(int value) /*-{
+    return value >>> 0;
+  }-*/;
+
+  private static String numberToString(double value, int radix) {
+    NativeNumber number = JsUtils.uncheckedCast(value);
+    return number.toString(radix);
+  }
+
+  @JsType(isNative = true, name = "Number", namespace = "<window>")
+  private interface NativeNumber {
+    String toString(int radix);
+
+    String toPrecision(int precision);
+  }
+
   public static native boolean isUndefined(Object value) /*-{
     return value === undefined;
   }-*/;