summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2010-06-23 20:00:10 -0400
committerRich Hickey <richhickey@gmail.com>2010-06-23 20:00:10 -0400
commit845c63e9317826a5564ef766550562b3fbe68181 (patch)
tree751fd8b3d33c985ac9bb0e41196faa85d71c0c8d
parentc8ce4638779e91ba0792549e3dbded2393662a00 (diff)
box longs-and-smaller as longs
-rw-r--r--src/clj/clojure/core.clj18
-rw-r--r--src/clj/clojure/main.clj2
-rw-r--r--src/clj/clojure/pprint/cl_format.clj4
-rw-r--r--src/clj/clojure/pprint/column_writer.clj2
-rw-r--r--src/clj/clojure/pprint/pretty_writer.clj2
-rw-r--r--src/jvm/clojure/lang/Compiler.java5
-rw-r--r--src/jvm/clojure/lang/Numbers.java101
-rw-r--r--src/jvm/clojure/lang/Reflector.java10
-rw-r--r--test/clojure/test_clojure/java_interop.clj28
-rw-r--r--test/clojure/test_clojure/protocols.clj2
-rw-r--r--test/clojure/test_clojure/reader.clj18
-rw-r--r--test/clojure/test_clojure/sequences.clj2
-rw-r--r--test/clojure/test_clojure/test.clj2
13 files changed, 96 insertions, 100 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index e63a10d8..b35722b6 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -2953,43 +2953,37 @@
(defn long
"Coerce to long"
- {:tag Long
- :inline (fn [x] `(. clojure.lang.RT (longCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (longCast ~x)))
:added "1.0"}
[^Number x] (clojure.lang.RT/longCast x))
(defn float
"Coerce to float"
- {:tag Float
- :inline (fn [x] `(. clojure.lang.RT (floatCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (floatCast ~x)))
:added "1.0"}
[^Number x] (clojure.lang.RT/floatCast x))
(defn double
"Coerce to double"
- {:tag Double
- :inline (fn [x] `(. clojure.lang.RT (doubleCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (doubleCast ~x)))
:added "1.0"}
[^Number x] (clojure.lang.RT/doubleCast x))
(defn short
"Coerce to short"
- {:tag Short
- :inline (fn [x] `(. clojure.lang.RT (shortCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (shortCast ~x)))
:added "1.0"}
[^Number x] (clojure.lang.RT/shortCast x))
(defn byte
"Coerce to byte"
- {:tag Byte
- :inline (fn [x] `(. clojure.lang.RT (byteCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (byteCast ~x)))
:added "1.0"}
[^Number x] (clojure.lang.RT/byteCast x))
(defn char
"Coerce to char"
- {:tag Character
- :inline (fn [x] `(. clojure.lang.RT (charCast ~x)))
+ {:inline (fn [x] `(. clojure.lang.RT (charCast ~x)))
:added "1.1"}
[x] (. clojure.lang.RT (charCast x)))
diff --git a/src/clj/clojure/main.clj b/src/clj/clojure/main.clj
index 31cdd957..61fcae75 100644
--- a/src/clj/clojure/main.clj
+++ b/src/clj/clojure/main.clj
@@ -72,7 +72,7 @@
(= c (int \newline)) :line-start
(= c -1) :stream-end
(= c (int \;)) (do (.readLine s) :line-start)
- (or (Character/isWhitespace c) (= c (int \,))) (recur (.read s))
+ (or (Character/isWhitespace (char c)) (= c (int \,))) (recur (.read s))
:else (do (.unread s c) :body))))
(defn repl-read
diff --git a/src/clj/clojure/pprint/cl_format.clj b/src/clj/clojure/pprint/cl_format.clj
index b5addeb8..22bb11b2 100644
--- a/src/clj/clojure/pprint/cl_format.clj
+++ b/src/clj/clojure/pprint/cl_format.clj
@@ -1095,9 +1095,9 @@ Note this should only be used for the last one in the sequence"
Integer
(let [c (char x)]
- (let [mod-c (if @last-was-whitespace? (Character/toUpperCase ^Character (char x)) c)]
+ (let [mod-c (if @last-was-whitespace? (Character/toUpperCase (char x)) c)]
(.write writer (int mod-c))
- (dosync (ref-set last-was-whitespace? (Character/isWhitespace ^Character (char x))))))))))))
+ (dosync (ref-set last-was-whitespace? (Character/isWhitespace (char x))))))))))))
(defn- init-cap-writer
"Returns a proxy that wraps writer, capitalizing the first word"
diff --git a/src/clj/clojure/pprint/column_writer.clj b/src/clj/clojure/pprint/column_writer.clj
index ae996e22..f02ac0dc 100644
--- a/src/clj/clojure/pprint/column_writer.clj
+++ b/src/clj/clojure/pprint/column_writer.clj
@@ -76,4 +76,6 @@
(.write ^Writer (get-field this :base) s))
Integer
+ (c-write-char this x)
+ Long
(c-write-char this x))))))))
diff --git a/src/clj/clojure/pprint/pretty_writer.clj b/src/clj/clojure/pprint/pretty_writer.clj
index f3fd4dbf..438a9cb5 100644
--- a/src/clj/clojure/pprint/pretty_writer.clj
+++ b/src/clj/clojure/pprint/pretty_writer.clj
@@ -399,6 +399,8 @@
(add-to-buffer this (make-buffer-blob s white-space oldpos newpos))))))
Integer
+ (p-write-char this x)
+ Long
(p-write-char this x))))
(flush []
diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java
index cccd253e..1c6bfdc1 100644
--- a/src/jvm/clojure/lang/Compiler.java
+++ b/src/jvm/clojure/lang/Compiler.java
@@ -727,7 +727,10 @@ static public abstract class HostExpr implements Expr, MaybePrimitiveExpr{
else
{
if(returnType == int.class)
- gen.invokeStatic(INTEGER_TYPE, intValueOfMethod);
+ {
+ gen.visitInsn(I2L);
+ gen.invokeStatic(NUMBERS_TYPE, Method.getMethod("Number num(long)"));
+ }
else if(returnType == float.class)
{
gen.visitInsn(F2D);
diff --git a/src/jvm/clojure/lang/Numbers.java b/src/jvm/clojure/lang/Numbers.java
index f20530a0..42dc9611 100644
--- a/src/jvm/clojure/lang/Numbers.java
+++ b/src/jvm/clojure/lang/Numbers.java
@@ -319,24 +319,21 @@ static public Number rationalize(Number x){
return x;
}
-static Number box(int val){
- return Integer.valueOf(val);
-}
-
-static Number box(long val){
- if(val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE)
- return Integer.valueOf((int) val);
- else
- return Long.valueOf(val);
-}
-
-static Double box(double val){
- return Double.valueOf(val);
-}
+//static Number box(int val){
+// return Integer.valueOf(val);
+//}
-static Double box(float val){
- return Double.valueOf((double) val);
-}
+//static Number box(long val){
+// return Long.valueOf(val);
+//}
+//
+//static Double box(double val){
+// return Double.valueOf(val);
+//}
+//
+//static Double box(float val){
+// return Double.valueOf((double) val);
+//}
static public Number reduceBigInteger(BigInteger val){
int bitLength = val.bitLength();
@@ -344,7 +341,7 @@ static public Number reduceBigInteger(BigInteger val){
// return val.intValue();
// else
if(bitLength < 64)
- return box(val.longValue());
+ return num(val.longValue());
else
return val;
}
@@ -476,7 +473,7 @@ final static class LongOps implements Ops{
}
final public Number add(Number x, Number y){
- return box(Numbers.add(x.longValue(),y.longValue()));
+ return num(Numbers.add(x.longValue(),y.longValue()));
}
final public Number addP(Number x, Number y){
@@ -484,11 +481,11 @@ final static class LongOps implements Ops{
long ret = lx + ly;
if ((ret ^ lx) < 0 && (ret ^ ly) < 0)
return BIGINTEGER_OPS.add(x, y);
- return box(ret);
+ return num(ret);
}
final public Number multiply(Number x, Number y){
- return box(Numbers.multiply(x.longValue(), y.longValue()));
+ return num(Numbers.multiply(x.longValue(), y.longValue()));
}
final public Number multiplyP(Number x, Number y){
@@ -496,7 +493,7 @@ final static class LongOps implements Ops{
long ret = lx * ly;
if (ly != 0 && ret/ly != lx)
return BIGINTEGER_OPS.multiply(x, y);
- return box(ret);
+ return num(ret);
}
static long gcd(long u, long v){
while(v != 0)
@@ -513,12 +510,12 @@ final static class LongOps implements Ops{
long val = y.longValue();
long gcd = gcd(n, val);
if(gcd == 0)
- return Integer.valueOf(0);
+ return num(0);
n = n / gcd;
long d = val / gcd;
if(d == 1)
- return box(n);
+ return num(n);
if(d < 0)
{
n = -n;
@@ -528,11 +525,11 @@ final static class LongOps implements Ops{
}
public Number quotient(Number x, Number y){
- return box(x.longValue() / y.longValue());
+ return num(x.longValue() / y.longValue());
}
public Number remainder(Number x, Number y){
- return box(x.longValue() % y.longValue());
+ return num(x.longValue() % y.longValue());
}
public boolean equiv(Number x, Number y){
@@ -546,36 +543,36 @@ final static class LongOps implements Ops{
//public Number subtract(Number x, Number y);
final public Number negate(Number x){
long val = x.longValue();
- return box(Numbers.minus(val));
+ return num(Numbers.minus(val));
}
final public Number negateP(Number x){
long val = x.longValue();
if(val > Long.MIN_VALUE)
- return box(-val);
+ return num(-val);
return BigInteger.valueOf(val).negate();
}
public Number inc(Number x){
long val = x.longValue();
- return box(Numbers.inc(val));
+ return num(Numbers.inc(val));
}
public Number incP(Number x){
long val = x.longValue();
if(val < Long.MAX_VALUE)
- return box(val + 1);
+ return num(val + 1);
return BIGINTEGER_OPS.inc(x);
}
public Number dec(Number x){
long val = x.longValue();
- return box(Numbers.dec(val));
+ return num(Numbers.dec(val));
}
public Number decP(Number x){
long val = x.longValue();
if(val > Long.MIN_VALUE)
- return box(val - 1);
+ return num(val - 1);
return BIGINTEGER_OPS.dec(x);
}
}
@@ -986,42 +983,42 @@ final static class LongBitOps implements BitOps{
}
public Number not(Number x){
- return box(~x.longValue());
+ return num(~x.longValue());
}
public Number and(Number x, Number y){
- return box(x.longValue() & y.longValue());
+ return num(x.longValue() & y.longValue());
}
public Number or(Number x, Number y){
- return box(x.longValue() | y.longValue());
+ return num(x.longValue() | y.longValue());
}
public Number xor(Number x, Number y){
- return box(x.longValue() ^ y.longValue());
+ return num(x.longValue() ^ y.longValue());
}
public Number andNot(Number x, Number y){
- return box(x.longValue() & ~y.longValue());
+ return num(x.longValue() & ~y.longValue());
}
public Number clearBit(Number x, int n){
if(n < 63)
- return (box(x.longValue() & ~(1L << n)));
+ return (num(x.longValue() & ~(1L << n)));
else
return toBigInteger(x).clearBit(n);
}
public Number setBit(Number x, int n){
if(n < 63)
- return box(x.longValue() | (1L << n));
+ return num(x.longValue() | (1L << n));
else
return toBigInteger(x).setBit(n);
}
public Number flipBit(Number x, int n){
if(n < 63)
- return box(x.longValue() ^ (1L << n));
+ return num(x.longValue() ^ (1L << n));
else
return toBigInteger(x).flipBit(n);
}
@@ -1036,13 +1033,13 @@ final static class LongBitOps implements BitOps{
public Number shiftLeft(Number x, int n){
if(n < 0)
return shiftRight(x, -n);
- return box(Numbers.shiftLeft(x.longValue(), n));
+ return num(Numbers.shiftLeft(x.longValue(), n));
}
public Number shiftRight(Number x, int n){
if(n < 0)
return shiftLeft(x, -n);
- return box(x.longValue() >> n);
+ return num(x.longValue() >> n);
}
}
@@ -1577,9 +1574,9 @@ static int throwIntOverflow(){
throw new ArithmeticException("integer overflow");
}
-static public Number num(int x){
- return Integer.valueOf(x);
-}
+//static public Number num(int x){
+// return Integer.valueOf(x);
+//}
static public int unchecked_int_add(int x, int y){
return x + y;
@@ -1716,7 +1713,7 @@ static public int unchecked_int_remainder(int x, int y){
//}
static public Number num(long x){
- return box(x);
+ return Long.valueOf(x);
}
static public long unchecked_long_add(long x, long y){
@@ -1754,7 +1751,7 @@ static public Number addP(long x, long y){
long ret = x + y;
if ((ret ^ x) < 0 && (ret ^ y) < 0)
return addP((Number)x,(Number)y);
- return box(ret);
+ return num(ret);
}
static public long minus(long x, long y){
@@ -1768,7 +1765,7 @@ static public Number minusP(long x, long y){
long ret = x - y;
if (((ret ^ x) < 0 && (ret ^ ~y) < 0))
return minusP((Number)x,(Number)y);
- return box(ret);
+ return num(ret);
}
static public long minus(long x){
@@ -1780,7 +1777,7 @@ static public long minus(long x){
static public Number minusP(long x){
if(x == Long.MIN_VALUE)
return BigInteger.valueOf(x).negate();
- return box(-x);
+ return num(-x);
}
static public long inc(long x){
@@ -1792,7 +1789,7 @@ static public long inc(long x){
static public Number incP(long x){
if(x == Long.MAX_VALUE)
return BIGINTEGER_OPS.inc(x);
- return box(x + 1);
+ return num(x + 1);
}
static public long dec(long x){
@@ -1804,7 +1801,7 @@ static public long dec(long x){
static public Number decP(long x){
if(x == Long.MIN_VALUE)
return BIGINTEGER_OPS.dec(x);
- return box(x - 1);
+ return num(x - 1);
}
@@ -1819,7 +1816,7 @@ static public Number multiplyP(long x, long y){
long ret = x * y;
if (y != 0 && ret/y != x)
return multiplyP((Number)x,(Number)y);
- return box(ret);
+ return num(ret);
}
static public long unchecked_long_divide(long x, long y){
diff --git a/src/jvm/clojure/lang/Reflector.java b/src/jvm/clojure/lang/Reflector.java
index d5e7fa7f..312a0a14 100644
--- a/src/jvm/clojure/lang/Reflector.java
+++ b/src/jvm/clojure/lang/Reflector.java
@@ -406,7 +406,8 @@ static public boolean paramArgTypeMatch(Class paramType, Class argType){
return true;
if(paramType == int.class)
return argType == Integer.class
- || argType == long.class;// || argType == FixNum.class;
+ || argType == long.class
+ || argType == Long.class;// || argType == FixNum.class;
else if(paramType == float.class)
return argType == Float.class
|| argType == double.class;
@@ -450,12 +451,9 @@ public static Object prepRet(Object x){
// return ((Boolean) x).booleanValue() ? RT.T : null;
if(x instanceof Boolean)
return ((Boolean) x)?Boolean.TRUE:Boolean.FALSE;
- else if(x instanceof Long)
+ else if(x instanceof Integer)
{
- long val = ((Long)x).longValue();
- if(val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE)
- return Integer.valueOf((int) val);
- return x;
+ return ((Integer)x).longValue();
}
else if(x instanceof Float)
return Double.valueOf(((Float) x).doubleValue());
diff --git a/test/clojure/test_clojure/java_interop.clj b/test/clojure/test_clojure/java_interop.clj
index 93d35935..77ed538b 100644
--- a/test/clojure/test_clojure/java_interop.clj
+++ b/test/clojure/test_clojure/java_interop.clj
@@ -76,21 +76,21 @@
(deftest test-instance?
; evaluation
(are [x y] (= x y)
- (instance? java.lang.Integer (+ 1 2)) true
- (instance? java.lang.Long (+ 1 2)) false )
+ (instance? java.lang.Integer (+ 1 2)) false
+ (instance? java.lang.Long (+ 1 2)) true )
; different types
(are [type literal] (instance? literal type)
- 1 java.lang.Integer
+ 1 java.lang.Long
1.0 java.lang.Double
1M java.math.BigDecimal
\a java.lang.Character
"a" java.lang.String )
- ; it is an int, nothing else
+ ; it is a Long, nothing else
(are [x y] (= (instance? x 42) y)
- java.lang.Integer true
- java.lang.Long false
+ java.lang.Integer false
+ java.lang.Long true
java.lang.Character false
java.lang.String false ))
@@ -199,14 +199,14 @@
(are [x] (= (alength (make-array Integer x)) x)
0 1 5 )
- (let [a (make-array Integer 5)]
+ (let [a (make-array Long 5)]
(aset a 3 42)
(are [x y] (= x y)
(aget a 3) 42
- (class (aget a 3)) Integer ))
+ (class (aget a 3)) Long ))
; multi-dimensional
- (let [a (make-array Integer 3 2 4)]
+ (let [a (make-array Long 3 2 4)]
(aset a 0 1 2 987)
(are [x y] (= x y)
(alength a) 3
@@ -214,7 +214,7 @@
(alength (first (first a))) 4
(aget a 0 1 2) 987
- (class (aget a 0 1 2)) Integer )))
+ (class (aget a 0 1 2)) Long )))
(deftest test-to-array
@@ -264,8 +264,8 @@
(class (first a)) (class (first v)) ))
; given type
- (let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])]
- (are [x] (= x Integer)
+ #_(let [a (into-array Integer/TYPE [(byte 2) (short 3) (int 4)])]
+ (are [x] (= x Long)
(class (aget a 0))
(class (aget a 1))
(class (aget a 2)) ))
@@ -273,8 +273,8 @@
; different kinds of collections
(are [x] (and (= (alength (into-array x)) (count x))
(= (vec (into-array x)) (vec x))
- (= (alength (into-array Integer/TYPE x)) (count x))
- (= (vec (into-array Integer/TYPE x)) (vec x)))
+ (= (alength (into-array Long/TYPE x)) (count x))
+ (= (vec (into-array Long/TYPE x)) (vec x)))
()
'(1 2)
[]
diff --git a/test/clojure/test_clojure/protocols.clj b/test/clojure/test_clojure/protocols.clj
index 57062aac..b2f03dd7 100644
--- a/test/clojure/test_clojure/protocols.clj
+++ b/test/clojure/test_clojure/protocols.clj
@@ -55,7 +55,7 @@
(testing "protocol fns throw IllegalArgumentException if no impl matches"
(is (thrown-with-msg?
IllegalArgumentException
- #"No implementation of method: :foo of protocol: #'clojure.test-clojure.protocols.examples/ExampleProtocol found for class: java.lang.Integer"
+ #"No implementation of method: :foo of protocol: #'clojure.test-clojure.protocols.examples/ExampleProtocol found for class: java.lang.Long"
(foo 10))))
(testing "protocols generate a corresponding interface using _ instead of - for method names"
(is (= ["bar" "baz" "baz" "foo" "with_quux"] (method-names clojure.test_clojure.protocols.examples.ExampleProtocol))))
diff --git a/test/clojure/test_clojure/reader.clj b/test/clojure/test_clojure/reader.clj
index d11eb311..e3bee190 100644
--- a/test/clojure/test_clojure/reader.clj
+++ b/test/clojure/test_clojure/reader.clj
@@ -53,14 +53,14 @@
(deftest Numbers
; Read Integer
- (is (instance? Integer 2147483647))
- (is (instance? Integer +1))
- (is (instance? Integer 1))
- (is (instance? Integer +0))
- (is (instance? Integer 0))
- (is (instance? Integer -0))
- (is (instance? Integer -1))
- (is (instance? Integer -2147483648))
+ (is (instance? Long 2147483647))
+ (is (instance? Long +1))
+ (is (instance? Long 1))
+ (is (instance? Long +0))
+ (is (instance? Long 0))
+ (is (instance? Long -0))
+ (is (instance? Long -1))
+ (is (instance? Long -2147483648))
; Read Long
(is (instance? Long 2147483648))
@@ -77,7 +77,7 @@
(recur (inc i) (conj l i))
l))]
(is (= [4 3 2 1 0] sequence))
- (is (every? #(instance? Integer %)
+ (is (every? #(instance? Long %)
sequence))))
; Read BigInteger
diff --git a/test/clojure/test_clojure/sequences.clj b/test/clojure/test_clojure/sequences.clj
index 4da82e98..e69fdbbd 100644
--- a/test/clojure/test_clojure/sequences.clj
+++ b/test/clojure/test_clojure/sequences.clj
@@ -27,7 +27,7 @@
avec (into [] arange)
alist (into () arange)
obj-array (into-array arange)
- int-array (into-array Integer/TYPE arange)
+ int-array (into-array Integer/TYPE (map #(Integer. (int %)) arange))
long-array (into-array Long/TYPE arange)
float-array (into-array Float/TYPE arange)
char-array (into-array Character/TYPE (map char arange))
diff --git a/test/clojure/test_clojure/test.clj b/test/clojure/test_clojure/test.clj
index 002af625..8a7a1e1c 100644
--- a/test/clojure/test_clojure/test.clj
+++ b/test/clojure/test_clojure/test.clj
@@ -36,7 +36,7 @@
(is (= 3 (+ 2 2)) "Should fail"))
(deftest can-test-instance
- (is (instance? Integer (+ 2 2)) "Should pass")
+ (is (instance? Long (+ 2 2)) "Should pass")
(is (instance? Float (+ 1 1)) "Should fail"))
(deftest can-test-thrown