Browse files

Remove the three generic type conversion methods from RubyObject/IRub…

…yObject to TypeConverter utility class. They were never overridden, and so there's no need for them to live in the IRubyObject/RubyObject hierarchy.

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@5047 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent 1057389 commit 4d75cc1470f5843b4b66f89f50ed81b88b27b431 @headius headius committed Nov 26, 2007
View
3 src/org/jruby/RubyEnumerable.java
@@ -40,6 +40,7 @@
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.util.TypeConverter;
/**
* The implementation of Ruby's Enumerable module.
@@ -471,7 +472,7 @@ public static IRubyObject zip(IRubyObject self, final IRubyObject[] args, final
final ThreadContext context = runtime.getCurrentContext();
for (int i = 0; i < args.length; i++) {
- args[i] = args[i].convertToType(runtime.getArray(), MethodIndex.TO_A, "to_a");
+ args[i] = TypeConverter.convertToType(args[i], runtime.getArray(), MethodIndex.TO_A, "to_a");
}
final int aLen = args.length + 1;
View
2 src/org/jruby/RubyEnumerator.java
@@ -113,7 +113,7 @@ public IRubyObject initialize(IRubyObject[] args) {
*/
@JRubyMethod(name = "each", frame = true)
public IRubyObject each(Block block) {
- return object.callMethod(getRuntime().getCurrentContext(), method.asSymbol(), methodArgs, block);
+ return object.callMethod(getRuntime().getCurrentContext(), method.asInternedString(), methodArgs, block);
}
@JRubyMethod(name = "enum_with_index")
View
3 src/org/jruby/RubyFile.java
@@ -62,6 +62,7 @@
import org.jruby.util.IOModes;
import org.jruby.util.JRubyFile;
import org.jruby.util.IOHandler.InvalidValueException;
+import org.jruby.util.TypeConverter;
/**
* Ruby File class equivalent in java.
@@ -364,7 +365,7 @@ public IRubyObject initialize(IRubyObject[] args, Block block) {
throw getRuntime().newArgumentError(0, 1);
}
else if (args.length < 3) {
- IRubyObject fd = args[0].convertToTypeWithCheck(getRuntime().getFixnum(), MethodIndex.TO_INT, "to_int");
+ IRubyObject fd = TypeConverter.convertToTypeWithCheck(args[0], getRuntime().getFixnum(), MethodIndex.TO_INT, "to_int");
if (!fd.isNil()) {
args[0] = fd;
return super.initialize(args, block);
View
6 src/org/jruby/RubyFixnum.java
@@ -200,7 +200,7 @@ public RubyString to_s(IRubyObject[] args) {
public IRubyObject id2name() {
RubySymbol symbol = RubySymbol.getSymbolLong(getRuntime(), value);
- if (symbol != null) return getRuntime().newString(symbol.asSymbol());
+ if (symbol != null) return getRuntime().newString(symbol.asInternedString());
return getRuntime().getNil();
}
@@ -683,7 +683,7 @@ public IRubyObject freeze() {
}
// Piece of mri rb_to_id
- public String asSymbol() {
+ public String asInternedString() {
getRuntime().getWarnings().warn("do not use Fixnums as Symbols");
// FIXME: I think this chunk is equivalent to MRI id2name (and not our public method
@@ -694,7 +694,7 @@ public String asSymbol() {
throw getRuntime().newArgumentError("" + value + " is not a symbol");
}
- return symbol.asSymbol();
+ return symbol.asInternedString();
}
public static RubyFixnum unmarshalFrom(UnmarshalStream input) throws java.io.IOException {
View
19 src/org/jruby/RubyKernel.java
@@ -72,6 +72,7 @@
import org.jruby.util.IdUtil;
import org.jruby.util.ShellLauncher;
import org.jruby.util.Sprintf;
+import org.jruby.util.TypeConverter;
/**
* Note: For CVS history, see KernelModule.java.
@@ -106,7 +107,7 @@ public static IRubyObject at_exit(IRubyObject recv, Block block) {
@JRubyMethod(name = "autoload?", required = 1, module = true, visibility = Visibility.PRIVATE)
public static IRubyObject autoload_p(final IRubyObject recv, IRubyObject symbol) {
RubyModule module = recv instanceof RubyModule ? (RubyModule) recv : recv.getRuntime().getObject();
- String name = module.getName() + "::" + symbol.asSymbol();
+ String name = module.getName() + "::" + symbol.asInternedString();
IAutoloadMethod autoloadMethod = recv.getRuntime().getLoadService().autoloadFor(name);
if (autoloadMethod == null) return recv.getRuntime().getNil();
@@ -118,7 +119,7 @@ public static IRubyObject autoload_p(final IRubyObject recv, IRubyObject symbol)
public static IRubyObject autoload(final IRubyObject recv, IRubyObject symbol, final IRubyObject file) {
Ruby runtime = recv.getRuntime();
final LoadService loadService = runtime.getLoadService();
- final String baseName = symbol.asSymbol(); // interned, OK for "fast" methods
+ final String baseName = symbol.asInternedString(); // interned, OK for "fast" methods
final RubyModule module = recv instanceof RubyModule ? (RubyModule) recv : runtime.getObject();
String nm = module.getName() + "::" + baseName;
@@ -153,7 +154,7 @@ public static IRubyObject method_missing(IRubyObject recv, IRubyObject[] args, B
if (args.length == 0 || !(args[0] instanceof RubySymbol)) throw runtime.newArgumentError("no id given");
- String name = args[0].asSymbol();
+ String name = args[0].asInternedString();
ThreadContext context = runtime.getCurrentContext();
Visibility lastVis = context.getLastVisibility();
CallType lastCallType = context.getLastCallType();
@@ -302,7 +303,7 @@ public static IRubyObject new_float(IRubyObject recv, IRubyObject object) {
}else if(object.isNil()){
throw recv.getRuntime().newTypeError("can't convert nil into Float");
} else {
- RubyFloat rFloat = (RubyFloat)object.convertToType(recv.getRuntime().getFloat(), MethodIndex.TO_F, "to_f");
+ RubyFloat rFloat = (RubyFloat)TypeConverter.convertToType(object, recv.getRuntime().getFloat(), MethodIndex.TO_F, "to_f");
if (Double.isNaN(rFloat.getDoubleValue())) throw recv.getRuntime().newArgumentError("invalid value for Float()");
return rFloat;
}
@@ -321,14 +322,14 @@ public static IRubyObject new_integer(IRubyObject recv, IRubyObject object) {
return RubyNumeric.str2inum(recv.getRuntime(),(RubyString)object,0,true);
}
- IRubyObject tmp = object.convertToType(recv.getRuntime().getInteger(), MethodIndex.TO_INT, "to_int", false);
+ IRubyObject tmp = TypeConverter.convertToType(object, recv.getRuntime().getInteger(), MethodIndex.TO_INT, "to_int", false);
if (tmp.isNil()) return object.convertToInteger(MethodIndex.TO_I, "to_i");
return tmp;
}
@JRubyMethod(name = "String", required = 1, module = true, visibility = Visibility.PRIVATE)
public static IRubyObject new_string(IRubyObject recv, IRubyObject object) {
- return object.convertToType(recv.getRuntime().getString(), MethodIndex.TO_S, "to_s");
+ return TypeConverter.convertToType(object, recv.getRuntime().getString(), MethodIndex.TO_S, "to_s");
}
@JRubyMethod(name = "p", rest = true, module = true, visibility = Visibility.PRIVATE)
@@ -763,7 +764,7 @@ public static IRubyObject caller(IRubyObject recv, IRubyObject[] args, Block blo
@JRubyMethod(name = "catch", required = 1, frame = true, module = true, visibility = Visibility.PRIVATE)
public static IRubyObject rbCatch(IRubyObject recv, IRubyObject tag, Block block) {
ThreadContext context = recv.getRuntime().getCurrentContext();
- CatchTarget target = new CatchTarget(tag.asSymbol());
+ CatchTarget target = new CatchTarget(tag.asInternedString());
try {
context.pushCatch(target);
return block.yield(context, tag);
@@ -786,7 +787,7 @@ public static IRubyObject rbCatch(IRubyObject recv, IRubyObject tag, Block block
public static IRubyObject rbThrow(IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = recv.getRuntime();
- String tag = args[0].asSymbol();
+ String tag = args[0].asInternedString();
ThreadContext context = runtime.getCurrentContext();
CatchTarget[] catches = context.getActiveCatches();
@@ -851,7 +852,7 @@ public static IRubyObject trace_var(IRubyObject recv, IRubyObject[] args, Block
proc = RubyProc.newProc(recv.getRuntime(), block, Block.Type.PROC);
}
if (args.length == 2) {
- proc = (RubyProc)args[1].convertToType(recv.getRuntime().getProc(), 0, "to_proc", true);
+ proc = (RubyProc)TypeConverter.convertToType(args[1], recv.getRuntime().getProc(), 0, "to_proc", true);
}
recv.getRuntime().getGlobalVariables().setTraceVar(var, proc);
View
52 src/org/jruby/RubyModule.java
@@ -985,7 +985,7 @@ public void setMethodVisibility(IRubyObject[] methods, Visibility visibility) {
}
for (int i = 0; i < methods.length; i++) {
- exportMethod(methods[i].asSymbol(), visibility);
+ exportMethod(methods[i].asInternedString(), visibility);
}
}
@@ -1052,7 +1052,7 @@ public IRubyObject define_method(IRubyObject[] args, Block block) {
}
IRubyObject body;
- String name = args[0].asSymbol();
+ String name = args[0].asInternedString();
DynamicMethod newMethod = null;
ThreadContext tc = getRuntime().getCurrentContext();
Visibility visibility = tc.getCurrentVisibility();
@@ -1385,7 +1385,7 @@ public IRubyObject attr(IRubyObject[] args) {
Arity.checkArgumentCount(getRuntime(), args, 1, 2);
boolean writeable = args.length > 1 ? args[1].isTrue() : false;
- addAccessor(args[0].asSymbol(), true, writeable);
+ addAccessor(args[0].asInternedString(), true, writeable);
return getRuntime().getNil();
}
@@ -1396,7 +1396,7 @@ public IRubyObject attr(IRubyObject[] args) {
@JRubyMethod(name = "attr_reader", required = 1, rest = true, visibility = Visibility.PRIVATE)
public IRubyObject attr_reader(IRubyObject[] args) {
for (int i = 0; i < args.length; i++) {
- addAccessor(args[i].asSymbol(), true, false);
+ addAccessor(args[i].asInternedString(), true, false);
}
return getRuntime().getNil();
@@ -1408,7 +1408,7 @@ public IRubyObject attr_reader(IRubyObject[] args) {
@JRubyMethod(name = "attr_writer", required = 1, rest = true, visibility = Visibility.PRIVATE)
public IRubyObject attr_writer(IRubyObject[] args) {
for (int i = 0; i < args.length; i++) {
- addAccessor(args[i].asSymbol(), false, true);
+ addAccessor(args[i].asInternedString(), false, true);
}
return getRuntime().getNil();
@@ -1420,7 +1420,7 @@ public IRubyObject attr_writer(IRubyObject[] args) {
@JRubyMethod(name = "attr_accessor", required = 1, rest = true, visibility = Visibility.PRIVATE)
public IRubyObject attr_accessor(IRubyObject[] args) {
for (int i = 0; i < args.length; i++) {
- addAccessor(args[i].asSymbol(), true, true);
+ addAccessor(args[i].asInternedString(), true, true);
}
return getRuntime().getNil();
@@ -1479,7 +1479,7 @@ public RubyArray public_instance_methods(IRubyObject[] args) {
@JRubyMethod(name = "instance_method", required = 1)
public IRubyObject instance_method(IRubyObject symbol) {
- return newMethod(null, symbol.asSymbol(), false);
+ return newMethod(null, symbol.asInternedString(), false);
}
/** rb_class_protected_instance_methods
@@ -1607,7 +1607,7 @@ public RubyModule module_function(IRubyObject[] args) {
setMethodVisibility(args, Visibility.PRIVATE);
for (int i = 0; i < args.length; i++) {
- String name = args[i].asSymbol();
+ String name = args[i].asInternedString();
DynamicMethod method = searchMethod(name);
assert !method.isUndefined() : "undefined method '" + name + "'";
getSingletonClass().addMethod(name, new WrapperMethod(getSingletonClass(), method, Visibility.PUBLIC));
@@ -1634,26 +1634,26 @@ public IRubyObject method_undefined(IRubyObject nothing) {
@JRubyMethod(name = "method_defined?", required = 1)
public RubyBoolean method_defined_p(IRubyObject symbol) {
- return isMethodBound(symbol.asSymbol(), true) ? getRuntime().getTrue() : getRuntime().getFalse();
+ return isMethodBound(symbol.asInternedString(), true) ? getRuntime().getTrue() : getRuntime().getFalse();
}
@JRubyMethod(name = "public_method_defined?", required = 1)
public IRubyObject public_method_defined(IRubyObject symbol) {
- DynamicMethod method = searchMethod(symbol.asSymbol());
+ DynamicMethod method = searchMethod(symbol.asInternedString());
return getRuntime().newBoolean(!method.isUndefined() && method.getVisibility() == Visibility.PUBLIC);
}
@JRubyMethod(name = "protected_method_defined?", required = 1)
public IRubyObject protected_method_defined(IRubyObject symbol) {
- DynamicMethod method = searchMethod(symbol.asSymbol());
+ DynamicMethod method = searchMethod(symbol.asInternedString());
return getRuntime().newBoolean(!method.isUndefined() && method.getVisibility() == Visibility.PROTECTED);
}
@JRubyMethod(name = "private_method_defined?", required = 1)
public IRubyObject private_method_defined(IRubyObject symbol) {
- DynamicMethod method = searchMethod(symbol.asSymbol());
+ DynamicMethod method = searchMethod(symbol.asInternedString());
return getRuntime().newBoolean(!method.isUndefined() && method.getVisibility() == Visibility.PRIVATE);
}
@@ -1672,15 +1672,15 @@ public RubyModule private_class_method(IRubyObject[] args) {
@JRubyMethod(name = "alias_method", required = 2, visibility = Visibility.PRIVATE)
public RubyModule alias_method(IRubyObject newId, IRubyObject oldId) {
- defineAlias(newId.asSymbol(), oldId.asSymbol());
+ defineAlias(newId.asInternedString(), oldId.asInternedString());
callMethod(getRuntime().getCurrentContext(), "method_added", newId);
return this;
}
@JRubyMethod(name = "undef_method", required = 1, rest = true, visibility = Visibility.PRIVATE)
public RubyModule undef_method(IRubyObject[] args) {
for (int i=0; i<args.length; i++) {
- undef(args[i].asSymbol());
+ undef(args[i].asInternedString());
}
return this;
}
@@ -1693,7 +1693,7 @@ public IRubyObject module_eval(IRubyObject[] args, Block block) {
@JRubyMethod(name = "remove_method", required = 1, rest = true, visibility = Visibility.PRIVATE)
public RubyModule remove_method(IRubyObject[] args) {
for(int i=0;i<args.length;i++) {
- removeMethod(args[i].asSymbol());
+ removeMethod(args[i].asInternedString());
}
return this;
}
@@ -1786,7 +1786,7 @@ private void doIncludeModule(RubyModule includedModule) {
@JRubyMethod(name = "class_variable_defined?", required = 1)
public IRubyObject class_variable_defined_p(IRubyObject var) {
- String internedName = validateClassVariable(var.asSymbol());
+ String internedName = validateClassVariable(var.asInternedString());
RubyModule module = this;
do {
if (module.fastHasClassVariable(internedName)) {
@@ -1802,23 +1802,23 @@ public IRubyObject class_variable_defined_p(IRubyObject var) {
*/
@JRubyMethod(name = "class_variable_get", required = 1, visibility = Visibility.PRIVATE)
public IRubyObject class_variable_get(IRubyObject var) {
- return fastGetClassVar(validateClassVariable(var.asSymbol()));
+ return fastGetClassVar(validateClassVariable(var.asInternedString()));
}
/** rb_mod_cvar_set
*
*/
@JRubyMethod(name = "class_variable_set", required = 2, visibility = Visibility.PRIVATE)
public IRubyObject class_variable_set(IRubyObject var, IRubyObject value) {
- return fastSetClassVar(validateClassVariable(var.asSymbol()), value);
+ return fastSetClassVar(validateClassVariable(var.asInternedString()), value);
}
/** rb_mod_remove_cvar
*
*/
@JRubyMethod(name = "remove_class_variable", required = 1, visibility = Visibility.PRIVATE)
public IRubyObject remove_class_variable(IRubyObject name) {
- String internedName = validateClassVariable(name.asSymbol());
+ String internedName = validateClassVariable(name.asInternedString());
IRubyObject value;
if ((value = deleteClassVariable(internedName)) != null) {
@@ -1866,28 +1866,28 @@ public RubyArray class_variables() {
@JRubyMethod(name = "const_defined?", required = 1)
public RubyBoolean const_defined_p(IRubyObject symbol) {
// Note: includes part of fix for JRUBY-1339
- return getRuntime().newBoolean(fastIsConstantDefined(validateConstant(symbol.asSymbol())));
+ return getRuntime().newBoolean(fastIsConstantDefined(validateConstant(symbol.asInternedString())));
}
/** rb_mod_const_get
*
*/
@JRubyMethod(name = "const_get", required = 1)
public IRubyObject const_get(IRubyObject symbol) {
- return fastGetConstant(validateConstant(symbol.asSymbol()));
+ return fastGetConstant(validateConstant(symbol.asInternedString()));
}
/** rb_mod_const_set
*
*/
@JRubyMethod(name = "const_set", required = 2)
public IRubyObject const_set(IRubyObject symbol, IRubyObject value) {
- return fastSetConstant(validateConstant(symbol.asSymbol()), value);
+ return fastSetConstant(validateConstant(symbol.asInternedString()), value);
}
@JRubyMethod(name = "remove_const", required = 1, visibility = Visibility.PRIVATE)
public IRubyObject remove_const(IRubyObject name) {
- String id = validateConstant(name.asSymbol());
+ String id = validateConstant(name.asInternedString());
IRubyObject value;
if ((value = deleteConstant(id)) != null) {
if (value != getRuntime().getUndef()) {
@@ -1925,10 +1925,10 @@ private boolean hasConstantInHierarchy(final String name) {
public IRubyObject const_missing(IRubyObject name, Block block) {
/* Uninitialized constant */
if (this != getRuntime().getObject()) {
- throw getRuntime().newNameError("uninitialized constant " + getName() + "::" + name.asSymbol(), "" + getName() + "::" + name.asSymbol());
+ throw getRuntime().newNameError("uninitialized constant " + getName() + "::" + name.asInternedString(), "" + getName() + "::" + name.asInternedString());
}
- throw getRuntime().newNameError("uninitialized constant " + name.asSymbol(), name.asSymbol());
+ throw getRuntime().newNameError("uninitialized constant " + name.asInternedString(), name.asInternedString());
}
/** rb_mod_constants
@@ -2068,7 +2068,7 @@ public boolean fastIsClassVarDefined(String internedName) {
* FIXME: any good reason to have two identical methods? (same as remove_class_variable)
*/
public IRubyObject removeCvar(IRubyObject name) { // Wrong Parameter ?
- String internedName = validateClassVariable(name.asSymbol());
+ String internedName = validateClassVariable(name.asInternedString());
IRubyObject value;
if ((value = deleteClassVariable(internedName)) != null) {
View
83 src/org/jruby/RubyObject.java
@@ -69,6 +69,7 @@
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.marshal.CoreObjectType;
+import org.jruby.util.TypeConverter;
/**
*
@@ -478,86 +479,40 @@ public void callInit(IRubyObject[] args, Block block) {
/** rb_to_id
*
*/
- public String asSymbol() {
+ public String asInternedString() {
IRubyObject asString = checkStringType();
- if(!asString.isNil()) return ((RubyString)asString).asSymbol();
+ if(!asString.isNil()) return ((RubyString)asString).asInternedString();
throw getRuntime().newTypeError(inspect().toString() + " is not a symbol");
}
public RubyArray convertToArray() {
- return (RubyArray) convertToType(getRuntime().getArray(), MethodIndex.TO_ARY, "to_ary");
+ return (RubyArray) TypeConverter.convertToType(this, getRuntime().getArray(), MethodIndex.TO_ARY, "to_ary");
}
public RubyHash convertToHash() {
- return (RubyHash)convertToType(getRuntime().getHash(), MethodIndex.TO_HASH, "to_hash");
+ return (RubyHash)TypeConverter.convertToType(this, getRuntime().getHash(), MethodIndex.TO_HASH, "to_hash");
}
public RubyFloat convertToFloat() {
- return (RubyFloat) convertToType(getRuntime().getFloat(), MethodIndex.TO_F, "to_f");
+ return (RubyFloat) TypeConverter.convertToType(this, getRuntime().getFloat(), MethodIndex.TO_F, "to_f");
}
public RubyInteger convertToInteger() {
return convertToInteger(MethodIndex.TO_INT, "to_int");
}
public RubyInteger convertToInteger(int convertMethodIndex, String convertMethod) {
- IRubyObject val = convertToType(getRuntime().getInteger(), convertMethodIndex, convertMethod, true);
+ IRubyObject val = TypeConverter.convertToType(this, getRuntime().getInteger(), convertMethodIndex, convertMethod, true);
if (!(val instanceof RubyInteger)) throw getRuntime().newTypeError(getMetaClass().getName() + "#" + convertMethod + " should return Integer");
return (RubyInteger)val;
}
public RubyString convertToString() {
- return (RubyString) convertToType(getRuntime().getString(), MethodIndex.TO_STR, "to_str");
- }
-
- /** convert_type
- *
- */
- public final IRubyObject convertToType(RubyClass target, int convertMethodIndex, String convertMethod, boolean raise) {
- if (!respondsTo(convertMethod)) {
- if (raise) {
- String type;
- if (isNil()) {
- type = "nil";
- } else if (this instanceof RubyBoolean) {
- type = isTrue() ? "true" : "false";
- } else {
- type = target.getName();
- }
- throw getRuntime().newTypeError("can't convert " + getMetaClass().getName() + " into " + type);
- } else {
- return getRuntime().getNil();
- }
- }
- return callMethod(getRuntime().getCurrentContext(), convertMethodIndex, convertMethod);
+ return (RubyString) TypeConverter.convertToType(this, getRuntime().getString(), MethodIndex.TO_STR, "to_str");
}
public final IRubyObject convertToType(RubyClass target, int convertMethodIndex) {
- return convertToType(target, convertMethodIndex, (String)MethodIndex.NAMES.get(convertMethodIndex));
- }
-
- /** rb_convert_type
- *
- */
- public final IRubyObject convertToType(RubyClass target, int convertMethodIndex, String convertMethod) {
- if (target.isInstance(this)) return this;
- IRubyObject val = convertToType(target, convertMethodIndex, convertMethod, true);
- if (!target.isInstance(val)) throw getRuntime().newTypeError(getMetaClass() + "#" + convertMethod + " should return " + target.getName());
- return val;
- }
-
- /*
- * @see org.jruby.runtime.builtin.IRubyObject#convertToTypeWithCheck(java.lang.String, java.lang.String)
- */
- /** rb_check_convert_type
- *
- */
- public final IRubyObject convertToTypeWithCheck(RubyClass target, int convertMethodIndex, String convertMethod) {
- if (target.isInstance(this)) return this;
- IRubyObject val = convertToType(target, convertMethodIndex, convertMethod, false);
- if (val.isNil()) return val;
- if (!target.isInstance(val)) throw getRuntime().newTypeError(getMetaClass() + "#" + convertMethod + " should return " + target.getName());
- return val;
+ return TypeConverter.convertToType(this, target, convertMethodIndex, (String)MethodIndex.NAMES.get(convertMethodIndex));
}
/** rb_obj_as_string
@@ -574,7 +529,7 @@ public RubyString asString() {
*
*/
public IRubyObject checkStringType() {
- IRubyObject str = convertToTypeWithCheck(getRuntime().getString(), MethodIndex.TO_STR, "to_str");
+ IRubyObject str = TypeConverter.convertToTypeWithCheck(this, getRuntime().getString(), MethodIndex.TO_STR, "to_str");
if(!str.isNil() && !(str instanceof RubyString)) {
str = getRuntime().newString("");
}
@@ -585,7 +540,7 @@ public IRubyObject checkStringType() {
*
*/
public IRubyObject checkArrayType() {
- return convertToTypeWithCheck(getRuntime().getArray(), MethodIndex.TO_ARY, "to_ary");
+ return TypeConverter.convertToTypeWithCheck(this, getRuntime().getArray(), MethodIndex.TO_ARY, "to_ary");
}
/** specific_eval
@@ -758,7 +713,7 @@ public IRubyObject initialize_copy(IRubyObject original) {
public RubyBoolean respond_to_p(IRubyObject[] args) {
Arity.checkArgumentCount(getRuntime(), args, 1, 2);
- String name = args[0].asSymbol();
+ String name = args[0].asInternedString();
boolean includePrivate = args.length > 1 ? args[1].isTrue() : false;
return getRuntime().newBoolean(getMetaClass().isMethodBound(name, !includePrivate));
@@ -1086,7 +1041,7 @@ public RubyArray singleton_methods(IRubyObject[] args) {
@JRubyMethod(name = "method", required = 1)
public IRubyObject method(IRubyObject symbol) {
- return getMetaClass().newMethod(this, symbol.asSymbol(), true);
+ return getMetaClass().newMethod(this, symbol.asInternedString(), true);
}
public IRubyObject anyToString() {
@@ -1183,7 +1138,7 @@ public IRubyObject send(IRubyObject[] args, Block block) {
if (args.length < 1) {
throw getRuntime().newArgumentError("no method name given");
}
- String name = args[0].asSymbol();
+ String name = args[0].asInternedString();
IRubyObject[] newArgs = new IRubyObject[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
@@ -1256,7 +1211,7 @@ public void removeFinalizers() {
@JRubyMethod(name = "instance_variable_defined?", required = 1)
public IRubyObject instance_variable_defined_p(IRubyObject name) {
- if (variableTableFastContains(validateInstanceVariable(name.asSymbol()))) {
+ if (variableTableFastContains(validateInstanceVariable(name.asInternedString()))) {
return getRuntime().getTrue();
}
return getRuntime().getFalse();
@@ -1265,7 +1220,7 @@ public IRubyObject instance_variable_defined_p(IRubyObject name) {
@JRubyMethod(name = "instance_variable_get", required = 1)
public IRubyObject instance_variable_get(IRubyObject name) {
IRubyObject value;
- if ((value = variableTableFastFetch(validateInstanceVariable(name.asSymbol()))) != null) {
+ if ((value = variableTableFastFetch(validateInstanceVariable(name.asInternedString()))) != null) {
return value;
}
return getRuntime().getNil();
@@ -1274,17 +1229,17 @@ public IRubyObject instance_variable_get(IRubyObject name) {
@JRubyMethod(name = "instance_variable_set", required = 2)
public IRubyObject instance_variable_set(IRubyObject name, IRubyObject value) {
ensureInstanceVariablesSettable();
- return variableTableFastStore(validateInstanceVariable(name.asSymbol()), value);
+ return variableTableFastStore(validateInstanceVariable(name.asInternedString()), value);
}
@JRubyMethod(name = "remove_instance_variable", required = 1, frame = true)
public IRubyObject remove_instance_variable(IRubyObject name, Block block) {
ensureInstanceVariablesSettable();
IRubyObject value;
- if ((value = variableTableRemove(validateInstanceVariable(name.asSymbol()))) != null) {
+ if ((value = variableTableRemove(validateInstanceVariable(name.asInternedString()))) != null) {
return value;
}
- throw getRuntime().newNameError("instance variable " + name.asSymbol() + " not defined", name.asSymbol());
+ throw getRuntime().newNameError("instance variable " + name.asInternedString() + " not defined", name.asInternedString());
}
@JRubyMethod(name = "instance_variables")
View
3 src/org/jruby/RubyObjectSpace.java
@@ -40,6 +40,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.util.TypeConverter;
public class RubyObjectSpace {
@@ -64,7 +65,7 @@ public static IRubyObject define_finalizer(IRubyObject recv, IRubyObject[] args,
if(args[1] instanceof RubyProc) {
proc = (RubyProc)args[1];
} else {
- proc = (RubyProc)args[1].convertToType(runtime.getProc(), 0, "to_proc", true);
+ proc = (RubyProc)TypeConverter.convertToType(args[1], runtime.getProc(), 0, "to_proc", true);
}
} else {
proc = runtime.newProc(Block.Type.PROC, block);
View
5 src/org/jruby/RubyRegexp.java
@@ -58,6 +58,7 @@
import org.jruby.regexp.PatternSyntaxException;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Visibility;
+import org.jruby.util.TypeConverter;
/**
*
@@ -778,7 +779,7 @@ public static IRubyObject union(IRubyObject recv, IRubyObject[] args) {
}
if (args.length == 1) {
- IRubyObject arg = args[0].convertToTypeWithCheck(runtime.getRegexp(), 0, "to_regexp");
+ IRubyObject arg = TypeConverter.convertToTypeWithCheck(args[0], runtime.getRegexp(), 0, "to_regexp");
if (!arg.isNil()) {
return arg;
}
@@ -790,7 +791,7 @@ public static IRubyObject union(IRubyObject recv, IRubyObject[] args) {
if (i > 0) {
buffer.append("|");
}
- IRubyObject arg = args[i].convertToTypeWithCheck(runtime.getRegexp(), 0, "to_regexp");
+ IRubyObject arg = TypeConverter.convertToTypeWithCheck(args[i], runtime.getRegexp(), 0, "to_regexp");
if (arg.isNil()) {
arg = quote(recv, args[i].convertToString());
}
View
2 src/org/jruby/RubyString.java
@@ -436,7 +436,7 @@ public int op_cmp(RubyString other) {
/** rb_to_id
*
*/
- public String asSymbol() {
+ public String asInternedString() {
// TODO: callers that don't need interned string should be modified
// to call toString, there are just a handful of places this happens.
View
24 src/org/jruby/RubyStruct.java
@@ -154,7 +154,7 @@ private IRubyObject setByName(String name, IRubyObject value) {
modify();
for (int i = 0,k=member.getLength(); i < k; i++) {
- if (member.eltInternal(i).asSymbol().equals(name)) {
+ if (member.eltInternal(i).asInternedString().equals(name)) {
return values[i] = value;
}
}
@@ -168,7 +168,7 @@ private IRubyObject getByName(String name) {
assert !member.isNil() : "uninitialized struct";
for (int i = 0,k=member.getLength(); i < k; i++) {
- if (member.eltInternal(i).asSymbol().equals(name)) {
+ if (member.eltInternal(i).asInternedString().equals(name)) {
return values[i];
}
}
@@ -200,7 +200,7 @@ public static RubyClass newInstance(IRubyObject recv, IRubyObject[] args, Block
RubyArray member = runtime.newArray();
for (int i = (name == null && !nilName) ? 0 : 1; i < args.length; i++) {
- member.append(runtime.fastNewSymbol(args[i].asSymbol()));
+ member.append(runtime.fastNewSymbol(args[i].asInternedString()));
}
RubyClass newStruct;
@@ -236,7 +236,7 @@ public static RubyClass newInstance(IRubyObject recv, IRubyObject[] args, Block
// define access methods.
for (int i = (name == null && !nilName) ? 0 : 1; i < args.length; i++) {
- String memberName = args[i].asSymbol();
+ String memberName = args[i].asInternedString();
newStruct.defineMethod(memberName, callbackFactory.getMethod("get"));
newStruct.defineMethod(memberName + "=", callbackFactory.getMethod("set", RubyKernel.IRUBY_OBJECT));
}
@@ -294,7 +294,7 @@ public static RubyArray members(IRubyObject recv, Block block) {
RubyArray result = recv.getRuntime().newArray(member.getLength());
for (int i = 0,k=member.getLength(); i < k; i++) {
- result.append(recv.getRuntime().newString(member.eltInternal(i).asSymbol()));
+ result.append(recv.getRuntime().newString(member.eltInternal(i).asInternedString()));
}
return result;
@@ -330,7 +330,7 @@ public IRubyObject set(IRubyObject value, Block block) {
modify();
for (int i = 0,k=member.getLength(); i < k; i++) {
- if (member.eltInternal(i).asSymbol().equals(name)) {
+ if (member.eltInternal(i).asInternedString().equals(name)) {
return values[i] = value;
}
}
@@ -350,7 +350,7 @@ public IRubyObject get(Block block) {
assert !member.isNil() : "uninitialized struct";
for (int i = 0,k=member.getLength(); i < k; i++) {
- if (member.eltInternal(i).asSymbol().equals(name)) {
+ if (member.eltInternal(i).asInternedString().equals(name)) {
return values[i];
}
}
@@ -411,7 +411,7 @@ public IRubyObject inspect() {
sb.append(", ");
}
- sb.append(member.eltInternal(i).asSymbol()).append("=");
+ sb.append(member.eltInternal(i).asInternedString()).append("=");
sb.append(values[i].callMethod(getRuntime().getCurrentContext(), "inspect"));
}
@@ -452,7 +452,7 @@ public IRubyObject each_pair(Block block) {
public IRubyObject aref(IRubyObject key) {
if (key instanceof RubyString || key instanceof RubySymbol) {
- return getByName(key.asSymbol());
+ return getByName(key.asInternedString());
}
int idx = RubyNumeric.fix2int(key);
@@ -470,7 +470,7 @@ public IRubyObject aref(IRubyObject key) {
public IRubyObject aset(IRubyObject key, IRubyObject value) {
if (key instanceof RubyString || key instanceof RubySymbol) {
- return setByName(key.asSymbol(), value);
+ return setByName(key.asInternedString(), value);
}
int idx = RubyNumeric.fix2int(key);
@@ -536,9 +536,9 @@ public static RubyStruct unmarshalFrom(UnmarshalStream input) throws java.io.IOE
Ruby runtime = input.getRuntime();
RubySymbol className = (RubySymbol) input.unmarshalObject();
- RubyClass rbClass = pathToClass(runtime, className.asSymbol());
+ RubyClass rbClass = pathToClass(runtime, className.asInternedString());
if (rbClass == null) {
- throw runtime.newNameError("uninitialized constant " + className, className.asSymbol());
+ throw runtime.newNameError("uninitialized constant " + className, className.asInternedString());
}
RubyArray mem = members(rbClass, Block.NULL_BLOCK);
View
2 src/org/jruby/RubySymbol.java
@@ -95,7 +95,7 @@ public int getNativeTypeIndex() {
*
* @return a String representation of the symbol
*/
- public String asSymbol() {
+ public String asInternedString() {
return symbol;
}
View
2 src/org/jruby/RubyThread.java
@@ -297,7 +297,7 @@ private IRubyObject getSymbolKey(IRubyObject originalKey) {
if (originalKey instanceof RubySymbol) {
return originalKey;
} else if (originalKey instanceof RubyString) {
- return getRuntime().fastNewSymbol(originalKey.asSymbol());
+ return getRuntime().fastNewSymbol(originalKey.asInternedString());
} else if (originalKey instanceof RubyFixnum) {
getRuntime().getWarnings().warn("Do not use Fixnums as Symbols");
throw getRuntime().newArgumentError(originalKey + " is not a symbol");
View
10 src/org/jruby/RubyUndef.java
@@ -23,7 +23,7 @@ public RubyString asString() {
return null;
}
- public String asSymbol() {
+ public String asInternedString() {
return null;
}
@@ -123,14 +123,6 @@ public RubyString convertToString() {
return null;
}
- public IRubyObject convertToType(RubyClass targetType, int convertMethodIndex,
- String convertMethod, boolean raiseOnError) {
- return null;
- }
- public IRubyObject convertToType(RubyClass targetType, int convertMethodIndex, String convertMethod) {
- return null;
- }
-
public IRubyObject convertToTypeWithCheck(RubyClass targetType, int convertMethodIndex,
String convertMethod) {
return null;
View
3 src/org/jruby/ast/util/ArgsUtil.java
@@ -35,6 +35,7 @@
import org.jruby.RubyArray;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.MethodIndex;
+import org.jruby.util.TypeConverter;
/**
*
@@ -72,7 +73,7 @@ public static RubyArray convertToRubyArray(Ruby runtime, IRubyObject value, bool
return RubyArray.newArrayNoCopyLight(runtime, new IRubyObject[] {value});
}
- IRubyObject newValue = value.convertToType(runtime.getArray(), MethodIndex.TO_ARY, "to_ary", false);
+ IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), MethodIndex.TO_ARY, "to_ary", false);
if (newValue.isNil()) {
return RubyArray.newArrayNoCopyLight(runtime, new IRubyObject[] {value});
View
9 src/org/jruby/evaluator/ASTInterpreter.java
@@ -161,6 +161,7 @@
import org.jruby.regexp.PatternSyntaxException;
import org.jruby.runtime.Binding;
import org.jruby.runtime.InterpretedBlock;
+import org.jruby.util.TypeConverter;
public class ASTInterpreter {
public static IRubyObject eval(Ruby runtime, ThreadContext context, Node node, IRubyObject self, Block block) {
@@ -560,7 +561,7 @@ public static IRubyObject aryToAry(Ruby runtime, IRubyObject value) {
if (value instanceof RubyArray) return value;
if (value.respondsTo("to_ary")) {
- return value.convertToType(runtime.getArray(), MethodIndex.TO_A, "to_ary", false);
+ return TypeConverter.convertToType(value, runtime.getArray(), MethodIndex.TO_A, "to_ary", false);
}
return runtime.newArray(value);
@@ -1777,11 +1778,11 @@ private static IRubyObject whileNode(Ruby runtime, ThreadContext context, Node n
IRubyObject reason = jumpError.reason();
// admittedly inefficient
- if (reason.asSymbol().equals("break")) {
+ if (reason.asInternedString().equals("break")) {
return jumpError.exit_value();
- } else if (reason.asSymbol().equals("next")) {
+ } else if (reason.asInternedString().equals("next")) {
break loop;
- } else if (reason.asSymbol().equals("redo")) {
+ } else if (reason.asInternedString().equals("redo")) {
continue;
}
}
View
8 src/org/jruby/javasupport/Java.java
@@ -130,7 +130,7 @@ public RubyClass defineClassUnder(final RubyModule pkg, final String name, final
final Ruby runtime = pkg.getRuntime();
return (RubyClass)get_proxy_class(
runtime.getJavaSupport().getJavaUtilitiesModule(),
- JavaClass.forName(runtime, packageName.asSymbol() + name));
+ JavaClass.forName(runtime, packageName.asInternedString() + name));
}
public RubyModule defineModuleUnder(final RubyModule pkg, final String name) {
@@ -141,7 +141,7 @@ public RubyModule defineModuleUnder(final RubyModule pkg, final String name) {
final Ruby runtime = pkg.getRuntime();
return (RubyModule)get_interface_module(
runtime.getJavaSupport().getJavaUtilitiesModule(),
- JavaClass.forName(runtime, packageName.asSymbol() + name));
+ JavaClass.forName(runtime, packageName.asInternedString() + name));
}
};
@@ -420,7 +420,7 @@ private static RubyModule createPackageModule(final RubyModule parent, final Str
private static final Pattern CAMEL_CASE_PACKAGE_SPLITTER = Pattern.compile("([a-z][0-9]*)([A-Z])");
public static IRubyObject get_package_module(IRubyObject recv, IRubyObject symObject) {
- String sym = symObject.asSymbol();
+ String sym = symObject.asInternedString();
RubyModule javaModule = recv.getRuntime().getJavaSupport().getJavaModule();
IRubyObject value;
if ((value = javaModule.fastGetConstantAt(sym)) != null) {
@@ -438,7 +438,7 @@ public static IRubyObject get_package_module(IRubyObject recv, IRubyObject symOb
public static IRubyObject get_package_module_dot_format(IRubyObject recv, IRubyObject dottedName) {
Ruby runtime = recv.getRuntime();
- RubyModule module = getJavaPackageModule(runtime, dottedName.asSymbol());
+ RubyModule module = getJavaPackageModule(runtime, dottedName.asInternedString());
return module == null ? runtime.getNil() : module;
}
View
14 src/org/jruby/javasupport/JavaClass.java
@@ -1018,12 +1018,12 @@ public static synchronized JavaClass forName(Ruby runtime, String className) {
}
public static JavaClass for_name(IRubyObject recv, IRubyObject name) {
- return forName(recv.getRuntime(), name.asSymbol());
+ return forName(recv.getRuntime(), name.asInternedString());
}
private static final Callback __jsend_method = new Callback() {
public IRubyObject execute(IRubyObject self, IRubyObject[] args, Block block) {
- String name = args[0].asSymbol();
+ String name = args[0].asInternedString();
DynamicMethod method = self.getMetaClass().searchMethod(name);
int v = method.getArity().getValue();
@@ -1149,13 +1149,13 @@ public RubyArray declared_class_methods() {
}
public JavaMethod java_method(IRubyObject[] args) throws ClassNotFoundException {
- String methodName = args[0].asSymbol();
+ String methodName = args[0].asInternedString();
Class[] argumentTypes = buildArgumentTypes(args);
return JavaMethod.create(getRuntime(), javaClass(), methodName, argumentTypes);
}
public JavaMethod declared_method(IRubyObject[] args) throws ClassNotFoundException {
- String methodName = args[0].asSymbol();
+ String methodName = args[0].asInternedString();
Class[] argumentTypes = buildArgumentTypes(args);
return JavaMethod.createDeclared(getRuntime(), javaClass(), methodName, argumentTypes);
}
@@ -1245,7 +1245,7 @@ public JavaConstructor declared_constructor(IRubyObject[] args) {
private Class[] buildClassArgs(IRubyObject[] args) {
Class[] parameterTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
- String name = args[i].asSymbol();
+ String name = args[i].asInternedString();
parameterTypes[i] = getRuntime().getJavaSupport().loadJavaClass(name);
}
return parameterTypes;
@@ -1301,7 +1301,7 @@ private RubyArray buildFieldResults(Field[] fields) {
}
public JavaField field(IRubyObject name) {
- String stringName = name.asSymbol();
+ String stringName = name.asInternedString();
try {
Field field = javaClass().getField(stringName);
return new JavaField(getRuntime(),field);
@@ -1311,7 +1311,7 @@ public JavaField field(IRubyObject name) {
}
public JavaField declared_field(IRubyObject name) {
- String stringName = name.asSymbol();
+ String stringName = name.asInternedString();
try {
Field field = javaClass().getDeclaredField(stringName);
return new JavaField(getRuntime(),field);
View
2 src/org/jruby/javasupport/proxy/JavaProxyClass.java
@@ -637,7 +637,7 @@ public static RubyObject get_with_class(IRubyObject recv, RubyClass clazz) {
"invalid method name defined for proxy (or ancestor): " +
ancestor + ": " + methodName);
}
- names.add(methodName.asSymbol());
+ names.add(methodName.asInternedString());
}
}
} else if (!EXCLUDE_MODULES.contains(ancestor.getName())) {
View
5 src/org/jruby/javasupport/util/RuntimeHelpers.java
@@ -48,6 +48,7 @@
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
+import org.jruby.util.TypeConverter;
/**
* Helper methods which are called by the compiler. Note: These will show no consumers, but
@@ -504,7 +505,7 @@ public static String getLocalJumpTypeOrRethrow(RaiseException re) {
IRubyObject reason = jumpError.reason();
- return reason.asSymbol();
+ return reason.asInternedString();
}
throw re;
@@ -539,7 +540,7 @@ public static Block getBlockFromBlockPassBody(IRubyObject proc, Block currentBlo
// If not already a proc then we should try and make it one.
if (!(proc instanceof RubyProc)) {
- proc = proc.convertToType(runtime.getProc(), 0, "to_proc", false);
+ proc = TypeConverter.convertToType(proc, runtime.getProc(), 0, "to_proc", false);
if (!(proc instanceof RubyProc)) {
throw runtime.newTypeError("wrong argument type "
View
32 src/org/jruby/runtime/builtin/IRubyObject.java
@@ -42,7 +42,6 @@
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyInteger;
-import org.jruby.RubyModule;
import org.jruby.RubyProc;
import org.jruby.RubyString;
import org.jruby.runtime.Block;
@@ -157,7 +156,7 @@
*
* @return String the symbol name
*/
- String asSymbol();
+ String asInternedString();
/** rb_obj_as_string
* @return
@@ -196,35 +195,6 @@
RubyString convertToString();
/**
- * Converts this object to type 'targetType' using 'convertMethod' method (MRI: convert_type).
- *
- * @param targetType is the type we are trying to convert to
- * @param convertMethod is the method to be called to try and convert to targeType
- * @param raiseOnError will throw an Error if conversion does not work
- * @return the converted value
- */
- IRubyObject convertToType(RubyClass targetType, int convertMethodIndex, String convertMethod, boolean raiseOnError);
-
- /**
- * Converts this object to type 'targetType' using 'convertMethod' method and raises TypeError exception on failure (MRI: rb_convert_type).
- *
- * @param targetType is the type we are trying to convert to
- * @param convertMethod is the method to be called to try and convert to targeType
- * @return the converted value
- */
- IRubyObject convertToType(RubyClass targetType, int convertMethodIndex, String convertMethod);
-
- /**
- * Higher level conversion utility similar to convertToType but it can throw an
- * additional TypeError during conversion (MRI: rb_check_convert_type).
- *
- * @param targetType is the type we are trying to convert to
- * @param convertMethod is the method to be called to try and convert to targeType
- * @return the converted value
- */
- IRubyObject convertToTypeWithCheck(RubyClass targetType, int convertMethodIndex, String convertMethod);
-
- /**
*
* @return
*/
View
14 src/org/jruby/runtime/marshal/UnmarshalStream.java
@@ -156,10 +156,10 @@ private IRubyObject unmarshalObjectDirectly(int type) throws IOException {
RubySymbol moduleName = (RubySymbol) unmarshalObject();
RubyModule tp = null;
try {
- tp = runtime.getClassFromPath(moduleName.asSymbol());
+ tp = runtime.getClassFromPath(moduleName.asInternedString());
} catch (RaiseException e) {
if (runtime.fastGetModule("NameError").isInstance(e.getException())) {
- throw runtime.newArgumentError("undefined class/module " + moduleName.asSymbol());
+ throw runtime.newArgumentError("undefined class/module " + moduleName.asInternedString());
}
throw e;
}
@@ -269,7 +269,7 @@ private IRubyObject defaultObjectUnmarshal() throws IOException {
type = getClassFromPath(runtime, className.toString());
} catch (RaiseException e) {
if (runtime.fastGetModule("NameError").isInstance(e.getException())) {
- throw runtime.newArgumentError("undefined class/module " + className.asSymbol());
+ throw runtime.newArgumentError("undefined class/module " + className.asInternedString());
}
throw e;
@@ -288,7 +288,7 @@ public void defaultVariablesUnmarshal(IRubyObject object) throws IOException {
List<Variable<IRubyObject>> attrs = new ArrayList<Variable<IRubyObject>>(count);
for (int i = count; --i >= 0; ) {
- String name = unmarshalObject().asSymbol();
+ String name = unmarshalObject().asInternedString();
IRubyObject value = unmarshalObject();
attrs.add(new VariableEntry<IRubyObject>(name, value));
}
@@ -300,7 +300,7 @@ public void defaultVariablesUnmarshal(IRubyObject object) throws IOException {
private IRubyObject uclassUnmarshall() throws IOException {
RubySymbol className = (RubySymbol)unmarshalObject();
- RubyClass type = (RubyClass)runtime.getClassFromPath(className.asSymbol());
+ RubyClass type = (RubyClass)runtime.getClassFromPath(className.asInternedString());
// All "C" marshalled objects descend from core classes, which are all RubyObject
RubyObject result = (RubyObject)unmarshalObject();
@@ -311,7 +311,7 @@ private IRubyObject uclassUnmarshall() throws IOException {
}
private IRubyObject userUnmarshal() throws IOException {
- String className = unmarshalObject().asSymbol();
+ String className = unmarshalObject().asInternedString();
ByteList marshaled = unmarshalString();
RubyModule classInstance = findClass(className);
if (!classInstance.respondsTo("_load")) {
@@ -324,7 +324,7 @@ private IRubyObject userUnmarshal() throws IOException {
}
private IRubyObject userNewUnmarshal() throws IOException {
- String className = unmarshalObject().asSymbol();
+ String className = unmarshalObject().asInternedString();
IRubyObject marshaled = unmarshalObject();
RubyClass classInstance = findClass(className);
IRubyObject result = classInstance.allocate();
View
4 src/org/jruby/util/Sprintf.java
@@ -184,7 +184,7 @@ final int intValue(IRubyObject obj) {
}
// basically just forcing a TypeError here to match MRI
- obj = obj.convertToType(obj.getRuntime().getFixnum(), MethodIndex.TO_INT, "to_int", true);
+ obj = TypeConverter.convertToType(obj, obj.getRuntime().getFixnum(), MethodIndex.TO_INT, "to_int", true);
return (int)((RubyFixnum)obj).getLongValue();
}
@@ -579,7 +579,7 @@ private static CharSequence rubySprintf(CharSequence charFormat, Args args) {
arg = RubyNumeric.str2inum(arg.getRuntime(),(RubyString)arg,0,true);
break;
default:
- arg = arg.convertToType(arg.getRuntime().getInteger(), MethodIndex.TO_I, "to_i", true);
+ arg = TypeConverter.convertToType(arg, arg.getRuntime().getInteger(), MethodIndex.TO_I, "to_i", true);
break;
}
type = arg.getMetaClass().index;
View
90 src/org/jruby/util/TypeConverter.java
@@ -0,0 +1,90 @@
+/*
+ ***** BEGIN LICENSE BLOCK *****
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Common Public
+ * License Version 1.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the CPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the CPL, the GPL or the LGPL.
+ ***** END LICENSE BLOCK *****/
+package org.jruby.util;
+
+import org.jruby.RubyBoolean;
+import org.jruby.RubyClass;
+import org.jruby.runtime.builtin.IRubyObject;
+
+public class TypeConverter {
+ /**
+ * Converts this object to type 'targetType' using 'convertMethod' method (MRI: convert_type).
+ *
+ * @param targetType is the type we are trying to convert to
+ * @param convertMethod is the method to be called to try and convert to targeType
+ * @param raiseOnError will throw an Error if conversion does not work
+ * @return the converted value
+ */
+ public static final IRubyObject convertToType(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod, boolean raise) {
+ if (!obj.respondsTo(convertMethod)) {
+ if (raise) {
+ String type;
+ if (obj.isNil()) {
+ type = "nil";
+ } else if (obj instanceof RubyBoolean) {
+ type = obj.isTrue() ? "true" : "false";
+ } else {
+ type = target.getName();
+ }
+ throw obj.getRuntime().newTypeError("can't convert " + obj.getMetaClass().getName() + " into " + type);
+ } else {
+ return obj.getRuntime().getNil();
+ }
+ }
+ return obj.callMethod(obj.getRuntime().getCurrentContext(), convertMethodIndex, convertMethod);
+ }
+
+ /**
+ * Converts this object to type 'targetType' using 'convertMethod' method and raises TypeError exception on failure (MRI: rb_convert_type).
+ *
+ * @param targetType is the type we are trying to convert to
+ * @param convertMethod is the method to be called to try and convert to targeType
+ * @return the converted value
+ */
+ public static final IRubyObject convertToType(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod) {
+ if (target.isInstance(obj)) return obj;
+ IRubyObject val = convertToType(obj, target, convertMethodIndex, convertMethod, true);
+ if (!target.isInstance(val)) throw obj.getRuntime().newTypeError(obj.getMetaClass() + "#" + convertMethod + " should return " + target.getName());
+ return val;
+ }
+
+ /**
+ * Higher level conversion utility similar to convertToType but it can throw an
+ * additional TypeError during conversion (MRI: rb_check_convert_type).
+ *
+ * @param targetType is the type we are trying to convert to
+ * @param convertMethod is the method to be called to try and convert to targeType
+ * @return the converted value
+ */
+ public static final IRubyObject convertToTypeWithCheck(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod) {
+ if (target.isInstance(obj)) return obj;
+ IRubyObject val = TypeConverter.convertToType(obj, target, convertMethodIndex, convertMethod, false);
+ if (val.isNil()) return val;
+ if (!target.isInstance(val)) throw obj.getRuntime().newTypeError(obj.getMetaClass() + "#" + convertMethod + " should return " + target.getName());
+ return val;
+ }
+}

0 comments on commit 4d75cc1

Please sign in to comment.