Skip to content

Commit

Permalink
Removes most of the remaining JSNI in JRE
Browse files Browse the repository at this point in the history
Change-Id: I03cafe1d203f6a330c1ba769390155e19055801e
Review-Link: https://gwt-review.googlesource.com/#/c/18642/
  • Loading branch information
gkdn committed Jun 7, 2017
1 parent 9acbf24 commit 9ee3048
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 47 deletions.
2 changes: 2 additions & 0 deletions dev/core/test/com/google/gwt/dev/CompilerTest.java
Expand Up @@ -2747,7 +2747,9 @@ private Set<String> getStaleTypeNames(MinimalRebuildCache relinkMinimalRebuildCa
"java.lang.Double", "java.lang.Double",
"java.lang.HasCharSequenceTypeMarker", "java.lang.HasCharSequenceTypeMarker",
"java.lang.HasComparableTypeMarker", "java.lang.HasComparableTypeMarker",
"java.lang.Integer$NativeNumber",
"java.lang.Number", "java.lang.Number",
"java.lang.Number$JavaLangNumber",
"java.lang.String", "java.lang.String",
"java.lang.String$NativeFunction", "java.lang.String$NativeFunction",
"java.lang.String$NativeString", "java.lang.String$NativeString",
Expand Down
8 changes: 2 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/Boolean.java
Expand Up @@ -114,17 +114,13 @@ public String toString() {


// CHECKSTYLE_OFF: Utility Methods for unboxed Boolean. // CHECKSTYLE_OFF: Utility Methods for unboxed Boolean.
protected static Boolean $create(boolean x) { protected static Boolean $create(boolean x) {
return createNative(x); return JsUtils.uncheckedCast(x);
} }


protected static Boolean $create(String x) { protected static Boolean $create(String x) {
return createNative(Boolean.parseBoolean(x)); return JsUtils.uncheckedCast(Boolean.parseBoolean(x));
} }


private static native Boolean createNative(boolean x) /*-{
return x;
}-*/;

@JsMethod @JsMethod
protected static boolean $isInstance(Object instance) { protected static boolean $isInstance(Object instance) {
return "boolean".equals(JsUtils.typeOf(instance)); return "boolean".equals(JsUtils.typeOf(instance));
Expand Down
8 changes: 2 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/Double.java
Expand Up @@ -379,17 +379,13 @@ public String toString() {


// CHECKSTYLE_OFF: Utility Methods for unboxed Double. // CHECKSTYLE_OFF: Utility Methods for unboxed Double.
protected static Double $create(double x) { protected static Double $create(double x) {
return createNative(x); return JsUtils.uncheckedCast(x);
} }


protected static Double $create(String s) { protected static Double $create(String s) {
return createNative(Double.parseDouble(s)); return JsUtils.uncheckedCast(Double.parseDouble(s));
} }


private static native Double createNative(double x) /*-{
return x;
}-*/;

@JsMethod @JsMethod
protected static boolean $isInstance(Object instance) { protected static boolean $isInstance(Object instance) {
return "number".equals(JsUtils.typeOf(instance)); return "number".equals(JsUtils.typeOf(instance));
Expand Down
36 changes: 24 additions & 12 deletions user/super/com/google/gwt/emul/java/lang/Integer.java
Expand Up @@ -15,6 +15,9 @@
*/ */
package java.lang; package java.lang;


import javaemul.internal.JsUtils;
import jsinterop.annotations.JsType;

/** /**
* Wraps a primitive <code>int</code> as an object. * Wraps a primitive <code>int</code> as an object.
*/ */
Expand Down Expand Up @@ -206,17 +209,25 @@ public static int sum(int a, int b) {
} }


public static String toBinaryString(int value) { public static String toBinaryString(int value) {
return toUnsignedRadixString(value, 2); return toUnsignedString(value, 2);
} }


public static String toHexString(int value) { public static String toHexString(int value) {
return toUnsignedRadixString(value, 16); return toUnsignedString(value, 16);
} }


public static String toOctalString(int value) { public static String toOctalString(int value) {
return toUnsignedRadixString(value, 8); return toUnsignedString(value, 8);
}

private static String toUnsignedString(int value, int radix) {
return toRadixString(toUnsigned(value), radix);
} }


private static native int toUnsigned(int value) /*-{
return (value >>> 0);
}-*/;

public static String toString(int value) { public static String toString(int value) {
return String.valueOf(value); return String.valueOf(value);
} }
Expand All @@ -228,6 +239,16 @@ public static String toString(int value, int radix) {
return toRadixString(value, radix); 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);
}

public static Integer valueOf(int i) { public static Integer valueOf(int i) {
if (i > -129 && i < 128) { if (i > -129 && i < 128) {
int rebase = i + 128; int rebase = i + 128;
Expand All @@ -249,15 +270,6 @@ public static Integer valueOf(String s, int radix)
return Integer.valueOf(Integer.parseInt(s, radix)); return Integer.valueOf(Integer.parseInt(s, radix));
} }


private static native String toRadixString(int value, int radix) /*-{
return value.toString(radix);
}-*/;

private static native String toUnsignedRadixString(int value, int radix) /*-{
// ">>> 0" converts the value to unsigned number.
return (value >>> 0).toString(radix);
}-*/;

private final transient int value; private final transient int value;


public Integer(int value) { public Integer(int value) {
Expand Down
12 changes: 6 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/Number.java
Expand Up @@ -19,7 +19,9 @@


import javaemul.internal.JsUtils; import javaemul.internal.JsUtils;
import javaemul.internal.NativeRegExp; import javaemul.internal.NativeRegExp;

import jsinterop.annotations.JsMethod; import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;


/** /**
* Abstract base class for numeric wrapper classes. * Abstract base class for numeric wrapper classes.
Expand Down Expand Up @@ -127,16 +129,14 @@ static class __ParseLong {
} }
} }


@JsType(isNative = true, name = "Number$impl", namespace = "java.lang")
private static class JavaLangNumber { }

@JsMethod @JsMethod
private static boolean $isInstance(Object instance) { private static boolean $isInstance(Object instance) {
return "number".equals(JsUtils.typeOf(instance)) || instanceOfJavaLangNumber(instance); return "number".equals(JsUtils.typeOf(instance)) || instance instanceof JavaLangNumber;
} }


private static native boolean instanceOfJavaLangNumber(Object instance) /*-{
// Note: The instanceof Number here refers to java.lang.Number in j2cl.
return instance instanceof Number;
}-*/;

/** /**
* @skip * @skip
* *
Expand Down
15 changes: 11 additions & 4 deletions user/super/com/google/gwt/emul/java/math/BigDecimal.java
Expand Up @@ -41,6 +41,8 @@
import javaemul.internal.JsUtils; import javaemul.internal.JsUtils;
import javaemul.internal.NativeRegExp; import javaemul.internal.NativeRegExp;


import jsinterop.annotations.JsType;

/** /**
* This class represents immutable arbitrary precision decimal numbers. Each * This class represents immutable arbitrary precision decimal numbers. Each
* {@code BigDecimal} instance is represented with a unscaled arbitrary * {@code BigDecimal} instance is represented with a unscaled arbitrary
Expand Down Expand Up @@ -491,13 +493,18 @@ private static int toIntScale(double doubleScale) {
* Convert a double to a string with {@code digits} precision. The resulting * Convert a double to a string with {@code digits} precision. The resulting
* string may still be in exponential notation. * string may still be in exponential notation.
* *
* @param d double value
* @param digits number of digits of precision to include * @param digits number of digits of precision to include
* @return non-localized string representation of {@code d} * @return non-localized string representation of {@code d}
*/ */
private static native String toPrecision(double d, int digits) /*-{ private static String toPrecision(double value, int digits) {
return d.toPrecision(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) { private static BigDecimal valueOf(double smallValue, double scale) {
return new BigDecimal(smallValue, scale); return new BigDecimal(smallValue, scale);
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/ */
package javaemul.internal; package javaemul.internal;


import javaemul.internal.annotations.DoNotAutobox;
import javaemul.internal.annotations.UncheckedCast; import javaemul.internal.annotations.UncheckedCast;
import jsinterop.annotations.JsMethod; import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsProperty; import jsinterop.annotations.JsProperty;
Expand Down Expand Up @@ -56,7 +57,7 @@ public static native boolean unsafeCastToBoolean(Object bool) /*-{
}-*/; }-*/;


@UncheckedCast @UncheckedCast
public static native <T> T uncheckedCast(Object o) /*-{ public static native <T> T uncheckedCast(@DoNotAutobox Object o) /*-{
return o; return o;
}-*/; }-*/;


Expand Down
26 changes: 14 additions & 12 deletions user/test/com/google/gwt/emultest/java/lang/IntegerTest.java
Expand Up @@ -292,18 +292,20 @@ public void testToString() {
assertEquals("-2147483648", Integer.toString(Integer.MIN_VALUE)); assertEquals("-2147483648", Integer.toString(Integer.MIN_VALUE));
assertEquals("0", Integer.toString(0)); assertEquals("0", Integer.toString(0));


assertEquals("17777777777", Integer.toString(2147483647, 8)); assertEquals("7fffffff", Integer.toString(Integer.MAX_VALUE, 16));
assertEquals("7fffffff", Integer.toString(2147483647, 16)); assertEquals("2147483647", Integer.toString(Integer.MAX_VALUE, 10));
assertEquals("1111111111111111111111111111111", Integer.toString(2147483647, 2)); assertEquals("17777777777", Integer.toString(Integer.MAX_VALUE, 8));
assertEquals("2147483647", Integer.toString(2147483647, 10)); assertEquals("1111111111111111111111111111111", Integer.toString(Integer.MAX_VALUE, 2));
assertEquals("-17777777777", Integer.toString(-2147483647, 8));
assertEquals("-7fffffff", Integer.toString(-2147483647, 16)); assertEquals("-7fffffff", Integer.toString(-Integer.MAX_VALUE, 16));
assertEquals("-1111111111111111111111111111111", Integer.toString(-2147483647, 2)); assertEquals("-2147483647", Integer.toString(-Integer.MAX_VALUE, 10));
assertEquals("-2147483647", Integer.toString(-2147483647, 10)); assertEquals("-17777777777", Integer.toString(-Integer.MAX_VALUE, 8));
assertEquals("-20000000000", Integer.toString(-2147483648, 8)); assertEquals("-1111111111111111111111111111111", Integer.toString(-Integer.MAX_VALUE, 2));
assertEquals("-80000000", Integer.toString(-2147483648, 16));
assertEquals("-10000000000000000000000000000000", Integer.toString(-2147483648, 2)); assertEquals("-80000000", Integer.toString(Integer.MIN_VALUE, 16));
assertEquals("-2147483648", Integer.toString(-2147483648, 10)); assertEquals("-2147483648", Integer.toString(Integer.MIN_VALUE, 10));
assertEquals("-20000000000", Integer.toString(Integer.MIN_VALUE, 8));
assertEquals("-10000000000000000000000000000000", Integer.toString(Integer.MIN_VALUE, 2));
} }


public void testValueOf() { public void testValueOf() {
Expand Down

0 comments on commit 9ee3048

Please sign in to comment.