Skip to content

Commit

Permalink
tighten up narrowing conversion checks
Browse files Browse the repository at this point in the history
  • Loading branch information
richhickey committed Jun 17, 2010
1 parent e526bb8 commit 4003a1c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/jvm/clojure/lang/Compiler.java
Expand Up @@ -769,21 +769,21 @@ else if(paramType == char.class)
}
else
{
Method m = intValueMethod;
Method m = null;
gen.checkCast(NUMBER_TYPE);
if(paramType == int.class)
m = intValueMethod;
m = Method.getMethod("int intCast(Object)");
else if(paramType == float.class)
m = floatValueMethod;
m = Method.getMethod("float floatCast(Object)");
else if(paramType == double.class)
m = doubleValueMethod;
else if(paramType == long.class)
m = longValueMethod;
else if(paramType == byte.class)
m = byteValueMethod;
else if(paramType == short.class)
m = shortValueMethod;
gen.invokeVirtual(NUMBER_TYPE, m);
m = Method.getMethod("double doubleCast(Object)");
else if(paramType == long.class)
m = Method.getMethod("long longCast(Object)");
else if(paramType == byte.class)
m = Method.getMethod("byte byteCast(Object)");
else if(paramType == short.class)
m = Method.getMethod("short shortCast(Object)");
gen.invokeStatic(RT_TYPE, m);
}
}
else
Expand Down
23 changes: 20 additions & 3 deletions src/jvm/clojure/lang/RT.java
Expand Up @@ -900,15 +900,19 @@ static public boolean booleanCast(boolean x){
}

static public byte byteCast(Object x){
long n = ((Number) x).longValue();
if(x instanceof Byte)
return ((Byte) x).byteValue();
long n = longCast(x);
if(n < Byte.MIN_VALUE || n > Byte.MAX_VALUE)
throw new IllegalArgumentException("Value out of range for byte: " + x);

return (byte) n;
}

static public short shortCast(Object x){
long n = ((Number) x).longValue();
if(x instanceof Short)
return ((Short) x).shortValue();
long n = longCast(x);
if(n < Short.MIN_VALUE || n > Short.MAX_VALUE)
throw new IllegalArgumentException("Value out of range for short: " + x);

Expand All @@ -919,7 +923,10 @@ static public int intCast(Object x){
if(x instanceof Integer)
return ((Integer)x).intValue();
if(x instanceof Number)
return intCast(((Number) x).longValue());
{
long n = longCast(x);
return intCast(n);
}
return ((Character) x).charValue();
}

Expand Down Expand Up @@ -958,6 +965,16 @@ static public int intCast(double x){
}

static public long longCast(Object x){
if(x instanceof Integer || x instanceof Long)
return ((Number) x).longValue();
else if (x instanceof BigInteger)
{
BigInteger bi = (BigInteger) x;
if(bi.bitLength() < 64)
return bi.longValue();
else
throw new IllegalArgumentException("Value out of range for long: " + x);
}
return ((Number) x).longValue();
}

Expand Down

0 comments on commit 4003a1c

Please sign in to comment.