Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ji] (auto) convert java numbers #4718

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions core/src/main/java/org/jruby/util/Pack.java
Expand Up @@ -83,23 +83,32 @@ public class Pack {
private static final Converter[] converters = new Converter[256];

private static long num2quad(IRubyObject arg) {
if (arg == arg.getRuntime().getNil()) {
return 0L;
}
else if (arg instanceof RubyBignum) {
BigInteger big = ((RubyBignum)arg).getValue();
if (arg.isNil()) return 0L;
if (arg instanceof RubyBignum) {
BigInteger big = ((RubyBignum) arg).getValue();
return big.longValue();
}
return RubyNumeric.num2long(arg);
}

private static float obj2flt(Ruby runtime, IRubyObject o) {
return (float) TypeConverter.toFloat(runtime, o).getDoubleValue();
return (float) toFloat(runtime, o).getDoubleValue();
}

private static double obj2dbl(Ruby runtime, IRubyObject o) {
return TypeConverter.toFloat(runtime, o).getDoubleValue();
}
return toFloat(runtime, o).getDoubleValue();
}

// MRI: rb_to_float 1.9
private static RubyFloat toFloat(Ruby runtime, IRubyObject obj) {
if (obj instanceof RubyNumeric) {
return ((RubyNumeric) obj).convertToFloat();
}
if (obj instanceof RubyString || obj.isNil()) {
throw runtime.newTypeError(obj, "Float");
}
return (RubyFloat) TypeConverter.convertToType(obj, runtime.getFloat(), "to_f", true);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old TypeConverter.toFloat appears to only have been used by Pack. Perhaps we can just replace it with the newer impl rather than having two around? I see in MRI it is used in pack.c, but also in random.c. rb_to_float itself is in object.c.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK thanks, will look into it ...


static {
uu_table =
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/util/TypeConverter.java
Expand Up @@ -131,12 +131,13 @@ public static IRubyObject convertToType19(ThreadContext context, IRubyObject obj
return convertToType(context, obj, target, sites);
}

// MRI: rb_to_float 1.9
// NOTE: no longer used, pack has specific conversion rules (e.g. String#to_f should not happen)
// thus has been moved and re-implemented directly in org.jruby.util.Pack (MRI: rb_to_float 1.9)
public static RubyNumeric toFloat(Ruby runtime, IRubyObject obj) {
if (obj instanceof RubyFloat) return (RubyNumeric) obj;

return (RubyNumeric) convertToType(obj, runtime.getFloat(), "to_f", true);
}

/**
* Checks that this object is of type DATA and then returns it, otherwise raises failure (MRI: Check_Type(obj, T_DATA))
*
Expand Down