Skip to content
Browse files

Use more strict double and float creation in 1.9

  • Loading branch information...
1 parent c40656a commit 877199caaf79a8d861669a60a1e62f0244685863 @enebo enebo committed Sep 12, 2011
Showing with 59 additions and 6 deletions.
  1. +49 −6 src/org/jruby/util/Pack.java
  2. +10 −0 src/org/jruby/util/TypeConverter.java
View
55 src/org/jruby/util/Pack.java
@@ -121,7 +121,11 @@ private static float obj2flt(Ruby runtime, IRubyObject o) {
}
}
- return (float)RubyKernel.new_float(o, o).getDoubleValue();
+ return (float) RubyKernel.new_float(o, o).getDoubleValue();
+ }
+
+ private static float obj2flt19(Ruby runtime, IRubyObject o) {
+ return (float) TypeConverter.toFloat(runtime, o).getDoubleValue();
}
private static double obj2dbl(Ruby runtime, IRubyObject o) {
@@ -137,6 +141,10 @@ private static double obj2dbl(Ruby runtime, IRubyObject o) {
return RubyKernel.new_float(o, o).getDoubleValue();
}
+
+ private static double obj2dbl19(Ruby runtime, IRubyObject o) {
+ return TypeConverter.toFloat(runtime, o).getDoubleValue();
+ }
static {
hex_table = ByteList.plain("0123456789ABCDEF");
@@ -159,9 +167,15 @@ private static double obj2dbl(Ruby runtime, IRubyObject o) {
public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
return RubyFloat.newFloat(runtime, decodeFloatLittleEndian(enc));
}
+
public void encode(Ruby runtime, IRubyObject o, ByteList result){
encodeFloatLittleEndian(result, obj2flt(runtime, o));
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result){
+ encodeFloatLittleEndian(result, obj2flt19(runtime, o));
+ }
};
// single precision, big-endian
converters['g'] = new Converter(4) {
@@ -171,23 +185,36 @@ public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
public void encode(Ruby runtime, IRubyObject o, ByteList result){
encodeFloatBigEndian(result, obj2flt(runtime, o));
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result){
+ encodeFloatBigEndian(result, obj2flt19(runtime, o));
+ }
};
// single precision, native
Converter tmp = new Converter(4) {
public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
- if (Platform.BYTE_ORDER == Platform.BIG_ENDIAN) {
- return RubyFloat.newFloat(runtime, decodeFloatBigEndian(enc));
- } else {
- return RubyFloat.newFloat(runtime, decodeFloatLittleEndian(enc));
- }
+ return RubyFloat.newFloat(runtime,
+ Platform.BYTE_ORDER == Platform.BIG_ENDIAN ?
+ decodeFloatBigEndian(enc) : decodeFloatLittleEndian(enc));
}
+
public void encode(Ruby runtime, IRubyObject o, ByteList result){
if (Platform.BYTE_ORDER == Platform.BIG_ENDIAN) {
encodeFloatBigEndian(result, obj2flt(runtime, o));
} else {
encodeFloatLittleEndian(result, obj2flt(runtime, o));
}
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result) {
+ if (Platform.BYTE_ORDER == Platform.BIG_ENDIAN) {
+ encodeFloatBigEndian(result, obj2flt19(runtime, o));
+ } else {
+ encodeFloatLittleEndian(result, obj2flt19(runtime, o));
+ }
+ }
};
converters['F'] = tmp; // single precision, native
converters['f'] = tmp; // single precision, native
@@ -197,9 +224,15 @@ public void encode(Ruby runtime, IRubyObject o, ByteList result){
public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
return RubyFloat.newFloat(runtime, decodeDoubleLittleEndian(enc));
}
+
public void encode(Ruby runtime, IRubyObject o, ByteList result){
encodeDoubleLittleEndian(result, obj2dbl(runtime, o));
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result){
+ encodeDoubleLittleEndian(result, obj2dbl19(runtime, o));
+ }
};
// double precision, big-endian
converters['G'] = new Converter(8) {
@@ -209,6 +242,11 @@ public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
public void encode(Ruby runtime, IRubyObject o, ByteList result){
encodeDoubleBigEndian(result, obj2dbl(runtime, o));
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result){
+ encodeDoubleBigEndian(result, obj2dbl19(runtime, o));
+ }
};
// double precision, native
tmp = new Converter(8) {
@@ -226,6 +264,11 @@ public void encode(Ruby runtime, IRubyObject o, ByteList result){
encodeDoubleLittleEndian(result, obj2dbl(runtime, o));
}
}
+
+ @Override
+ public void encode19(Ruby runtime, IRubyObject o, ByteList result){
+ encodeDoubleLittleEndian(result, obj2dbl19(runtime, o));
+ }
};
converters['D'] = tmp; // double precision, native
converters['d'] = tmp; // double precision, native
View
10 src/org/jruby/util/TypeConverter.java
@@ -34,6 +34,7 @@
import org.jruby.RubyInteger;
import org.jruby.RubyNumeric;
import org.jruby.exceptions.RaiseException;
+import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class TypeConverter {
@@ -136,6 +137,15 @@ public static final IRubyObject convertToType19(IRubyObject obj, RubyClass targe
return val;
}
+ // MRI: rb_to_float 1.9
+ public static RubyNumeric toFloat(Ruby runtime, IRubyObject obj) {
+ RubyClass floatClass = runtime.getFloat();
+
+ if (floatClass.isInstance(obj)) return (RubyNumeric) obj;
+ if (!runtime.getNumeric().isInstance(obj)) throw runtime.newTypeError(obj, "Float");
+
+ return (RubyNumeric) convertToType19(obj, floatClass, "to_f", true);
+ }
/**
* Checks that this object is of type DATA and then returns it, otherwise raises failure (MRI: Check_Type(obj, T_DATA))
*

0 comments on commit 877199c

Please sign in to comment.
Something went wrong with that request. Please try again.