Skip to content

Commit

Permalink
[Truffle] Moving Kernel#Integer to kernel.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Apr 1, 2015
1 parent 04c7e9f commit 78b3cfd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 240 deletions.
168 changes: 0 additions & 168 deletions spec/truffle/tags/core/kernel/Integer_tags.txt

This file was deleted.

1 change: 1 addition & 0 deletions spec/truffle/tags/library/thread/sizedqueue/max_tags.txt
@@ -0,0 +1 @@
fails:Thread::SizedQueue#max= raises a TypeError when given a non-numeric value
1 change: 1 addition & 0 deletions spec/truffle/tags/library/thread/sizedqueue/new_tags.txt
@@ -0,0 +1 @@
fails:Thread::SizedQueue#new raises a TypeError when the given argument is not Numeric
Expand Up @@ -1050,78 +1050,6 @@ public RubyArray instanceVariables(RubyBasicObject self) {

}

@CoreMethod(names = "Integer", isModuleFunction = true, required = 1)
public abstract static class IntegerNode extends CoreMethodNode {

@Child private DoesRespondDispatchHeadNode toIntRespondTo;
@Child private CallDispatchHeadNode toInt;
@Child private FixnumOrBignumNode fixnumOrBignum;

public IntegerNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toIntRespondTo = new DoesRespondDispatchHeadNode(context, false, false, MissingBehavior.CALL_METHOD_MISSING, null);
toInt = new CallDispatchHeadNode(context, false, false, MissingBehavior.CALL_METHOD_MISSING, null);
}

public IntegerNode(IntegerNode prev) {
super(prev);
toIntRespondTo = prev.toIntRespondTo;
toInt = prev.toInt;
fixnumOrBignum = prev.fixnumOrBignum;
}

@Specialization
public int integer(int value) {
return value;
}

@Specialization
public long integer(long value) {
return value;
}

@Specialization
public RubyBignum integer(RubyBignum value) {
return value;
}

@Specialization
public int integer(double value) {
return (int) value;
}

@Specialization
public Object integer(RubyString value) {
notDesignedForCompilation();

if (value.toString().length() == 0) {
return 0;
}

try {
return Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
if (fixnumOrBignum == null) {
CompilerDirectives.transferToInterpreter();
fixnumOrBignum = insert(new FixnumOrBignumNode(getContext(), getSourceSection()));
}

return fixnumOrBignum.fixnumOrBignum(new BigInteger(value.toString()));
}
}

@Specialization
public Object integer(VirtualFrame frame, Object value) {
if (toIntRespondTo.doesRespondTo(frame, "to_int", value)) {
return toInt.call(frame, value, "to_int", null);
} else {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().typeErrorCantConvertInto(value, getContext().getCoreLibrary().getIntegerClass(), this));
}
}

}

@CoreMethod(names = {"is_a?", "kind_of?"}, required = 1)
public abstract static class IsANode extends CoreMethodNode {

Expand Down
40 changes: 40 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/kernel.rb
Expand Up @@ -88,6 +88,46 @@ def FloatValue(obj)
end
private :FloatValue

def Integer(obj, base=nil)
if obj.kind_of? String
if obj.empty?
raise ArgumentError, "invalid value for Integer: (empty string)"
else
base ||= 0
return obj.to_inum(base, true)
end
end

if base
raise ArgumentError, "base is only valid for String values"
end

case obj
when Integer
obj
when Float
if obj.nan? or obj.infinite?
raise FloatDomainError, "unable to coerce #{obj} to Integer"
else
obj.to_int
end
when NilClass
raise TypeError, "can't convert nil into Integer"
else
# Can't use coerce_to or try_convert because I think there is an
# MRI bug here where it will return the value without checking
# the return type.
if obj.respond_to? :to_int
if val = obj.to_int
return val
end
end

Rubinius::Type.coerce_to obj, Integer, :to_i
end
end
module_function :Integer

##
# MRI uses a macro named StringValue which has essentially the same
# semantics as obj.coerce_to(String, :to_str), but rather than using that
Expand Down

0 comments on commit 78b3cfd

Please sign in to comment.