Skip to content

Commit

Permalink
Mild TypeConverter refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Sep 13, 2011
1 parent 10a41f9 commit eadf3a0
Showing 1 changed file with 22 additions and 44 deletions.
66 changes: 22 additions & 44 deletions src/org/jruby/util/TypeConverter.java
Expand Up @@ -49,9 +49,7 @@ public class TypeConverter {
*/
@Deprecated
public static final IRubyObject convertToType(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod, boolean raise) {
if (!obj.respondsTo(convertMethod)) {
return handleUncoercibleObject(raise, obj, target);
}
if (!obj.respondsTo(convertMethod)) return handleUncoercibleObject(raise, obj, target);

return obj.callMethod(obj.getRuntime().getCurrentContext(), convertMethod);
}
Expand All @@ -67,9 +65,7 @@ public static final IRubyObject convertToType(IRubyObject obj, RubyClass target,
* @return the converted value
*/
public static final IRubyObject convertToType(IRubyObject obj, RubyClass target, String convertMethod, boolean raise) {
if (!obj.respondsTo(convertMethod)) {
return handleUncoercibleObject(raise, obj, target);
}
if (!obj.respondsTo(convertMethod)) return handleUncoercibleObject(raise, obj, target);

return obj.callMethod(obj.getRuntime().getCurrentContext(), convertMethod);
}
Expand All @@ -86,10 +82,8 @@ public static final IRubyObject convertToType(IRubyObject obj, RubyClass target,
*/
public static final IRubyObject convertToType19(IRubyObject obj, RubyClass target, String convertMethod, boolean raise) {
IRubyObject r = obj.checkCallMethod(obj.getRuntime().getCurrentContext(), convertMethod);
if (r == null) {
return handleUncoercibleObject(raise, obj, target);
}
return r;

return r == null ? handleUncoercibleObject(raise, obj, target) : r;
}

/**
Expand Down Expand Up @@ -149,18 +143,16 @@ public static final IRubyObject convertToType19(IRubyObject obj, RubyClass targe
* @return the converted value
*/
public static final IRubyObject checkData(IRubyObject obj) {
if(obj instanceof org.jruby.runtime.marshal.DataType) {
return obj;
}
String type;
if (obj.isNil()) {
type = "nil";
} else if (obj instanceof RubyBoolean) {
type = obj.isTrue() ? "true" : "false";
} else {
type = obj.getMetaClass().getRealClass().getName();
}
throw obj.getRuntime().newTypeError("wrong argument type " + type + " (expected Data)");
if(obj instanceof org.jruby.runtime.marshal.DataType) return obj;

throw obj.getRuntime().newTypeError("wrong argument type " + typeAsString(obj) + " (expected Data)");
}

private static String typeAsString(IRubyObject obj) {
if (obj.isNil()) return "nil";
if (obj instanceof RubyBoolean) return obj.isTrue() ? "true" : "false";

return obj.getMetaClass().getRealClass().getName();
}

/**
Expand Down Expand Up @@ -237,21 +229,17 @@ public static final IRubyObject convertToTypeOrRaise(IRubyObject obj, RubyClass

// rb_check_to_integer
public static IRubyObject checkIntegerType(Ruby runtime, IRubyObject obj, String method) {
if (obj instanceof RubyFixnum) {
return obj;
}
if (obj instanceof RubyFixnum) return obj;

IRubyObject conv = TypeConverter.convertToType(obj, runtime.getInteger(), method, false);
return conv instanceof RubyInteger ? conv : runtime.getNil();
}

// 1.9 rb_check_to_float
public static IRubyObject checkFloatType(Ruby runtime, IRubyObject obj) {
if (obj instanceof RubyFloat) {
return obj;
}
if (!(obj instanceof RubyNumeric)) {
return runtime.getNil();
}
if (obj instanceof RubyFloat) return obj;
if (!(obj instanceof RubyNumeric)) return runtime.getNil();

return TypeConverter.convertToTypeWithCheck(obj, runtime.getFloat(), "to_f");
}

Expand All @@ -261,18 +249,8 @@ public static IRubyObject checkHashType(Ruby runtime, IRubyObject obj) {
}

public static IRubyObject handleUncoercibleObject(boolean raise, IRubyObject obj, RubyClass target) throws RaiseException {
if (raise) {
String type;
if (obj.isNil()) {
type = "nil";
} else if (obj instanceof RubyBoolean) {
type = obj.isTrue() ? "true" : "false";
} else {
type = obj.getMetaClass().getRealClass().getName();
}
throw obj.getRuntime().newTypeError("can't convert " + type + " into " + target);
} else {
return obj.getRuntime().getNil();
}
if (raise) throw obj.getRuntime().newTypeError("can't convert " + typeAsString(obj) + " into " + target);

return obj.getRuntime().getNil();
}
}

0 comments on commit eadf3a0

Please sign in to comment.