[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Regression in Integer
From: |
Eric Blake |
Subject: |
Regression in Integer |
Date: |
Wed, 25 Jul 2001 18:21:50 +0100 |
I just noticed that your changes to Integer cause an infinite loop in
Classpath (but not in gcj). You changed Integer.toString(int) to call
String.valueOf(int), since gcj implements String.valueOf(int) natively. But
when String.valueOf(int) is not native, it calls Integer.toString(i, 10),
which in turn calls Integer.toString(int) for the radix 10, causing a 3-way
infinite recursion.
Here is another case where your suggested
gnu.classpath.Configuration.FAST_NATIVE_CALLS would be useful, to break the
loop. This patch requires that gcj change to implement String.valueOf0(int)
instead of valueOf, so that configuration of Classpath can still work
without requiring two versions of String.java. It seems to me that most VMs
do not care if a native method is unimplemented if it is never called, so
this approach probably makes implementing String.valueOf0(int) optional in
other VMs.
--
Eric Blake, Elixent, Castlemead, Lwr Castle St., Bristol BS1 3AG, UK
address@hidden tel:+44(0)117 917 5611
Index: java/lang/Integer.java
===================================================================
RCS file: /cvs/classpath/java/lang/Integer.java,v
retrieving revision 1.18
diff -u -r1.18 Integer.java
--- java/lang/Integer.java 2001/07/23 20:01:44 1.18
+++ java/lang/Integer.java 2001/07/25 17:10:19
@@ -27,6 +27,8 @@
package java.lang;
+import gnu.classpath.Configuration;
+
/**
* Instances of class <code>Integer</code> represent primitive
* <code>int</code> values.
@@ -245,7 +247,10 @@
// This is tricky: in libgcj, String.valueOf(int) is a fast native
// implementation. In Classpath it just calls back to
// Integer.toString(int,int).
- return String.valueOf (i);
+ if (Configuration.FAST_NATIVE_CALLS)
+ return String.valueOf(i);
+ else
+ return toString(i, 10);
}
/**
@@ -267,10 +272,19 @@
*/
public static String toString(int num, int radix)
{
- // Use optimized method for the typical case.
- if (radix == 10 ||
- radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
- return toString(num);
+ // gcj has a fast native String.valueOf(int), but Classpath's version
+ // just calls toString(int, int), so avoid infinite recursion
+ if (Configuration.FAST_NATIVE_CALLS)
+ {
+ if (radix == 10 ||
+ radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
+ return String.valueOf(num);
+ }
+ else
+ {
+ if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
+ radix = 10;
+ }
// For negative numbers, print out the absolute value w/ a leading '-'.
// Use an array large enough for a binary number.Index:
java/lang/String.java
Index: java/lang/String.java
===================================================================
RCS file: /cvs/classpath/java/lang/String.java,v
retrieving revision 1.32
diff -u -r1.32 String.java
--- java/lang/String.java 2001/07/23 20:01:44 1.32
+++ java/lang/String.java 2001/07/25 17:11:27
@@ -32,6 +32,7 @@
import java.util.Locale;
import gnu.java.io.EncodingManager;
import java.io.*;
+import gnu.classpath.Configuration;
/**
* Strings represent an immutable set of characters.
@@ -1077,9 +1078,17 @@
* @return Integer.toString(i)
*/
public static String valueOf(int i) {
- // See Integer to understand why we call the two-arg variant.
- return Integer.toString(i, 10);
+ if (Configuration.FAST_NATIVE_CALLS)
+ return valueOf0(i);
+ else
+ // See Integer to understand why we call the two-arg variant.
+ return Integer.toString(i, 10);
}
+ /**
+ * This will not be called if Configuration.FAST_NATIVE_CALLS
+ * is false, so it might not even need implementing.
+ */
+ private static native String valueOf0(int i);
/**
* Returns a String representing a long.
- Regression in Integer,
Eric Blake <=