Skip to content

Commit

Permalink
Eliminate expectation of FIXNUM or BIGNUM from class index.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 17, 2016
1 parent f723036 commit a103596
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 77 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/RubyComplex.java
Expand Up @@ -288,8 +288,7 @@ public static void setCanonicalization(boolean canonical) {
*/
private static void realCheck(ThreadContext context, IRubyObject num) {
switch (num.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
case RATIONAL:
break;
Expand Down
45 changes: 15 additions & 30 deletions core/src/main/java/org/jruby/RubyFloat.java
Expand Up @@ -270,8 +270,7 @@ public IRubyObject op_uminus() {
@JRubyMethod(name = "+", required = 1)
public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyFloat.newFloat(context.runtime, value + ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -289,8 +288,7 @@ public IRubyObject op_plus(ThreadContext context, double other) {
@JRubyMethod(name = "-", required = 1)
public IRubyObject op_minus(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyFloat.newFloat(context.runtime, value - ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -308,8 +306,7 @@ public IRubyObject op_minus(ThreadContext context, double other) {
@JRubyMethod(name = "*", required = 1)
public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyFloat.newFloat(context.runtime, value * ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -328,8 +325,7 @@ public IRubyObject op_mul(ThreadContext context, double other) {
@JRubyMethod(name = "/", required = 1)
public IRubyObject op_fdiv(ThreadContext context, IRubyObject other) { // don't override Numeric#div !
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyFloat.newFloat(context.runtime, value / ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -354,8 +350,7 @@ public IRubyObject magnitude(ThreadContext context, IRubyObject other) {
*/
public IRubyObject op_mod(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double y = ((RubyNumeric) other).getDoubleValue();
return op_mod(context, y);
Expand Down Expand Up @@ -394,8 +389,7 @@ public IRubyObject op_mod19(ThreadContext context, IRubyObject other) {
@Override
public IRubyObject divmod(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double y = ((RubyNumeric) other).getDoubleValue();
double x = value;
Expand Down Expand Up @@ -436,8 +430,7 @@ public IRubyObject divmod19(ThreadContext context, IRubyObject other) {
*/
public IRubyObject op_pow(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyFloat.newFloat(context.runtime, Math.pow(value, ((RubyNumeric) other)
.getDoubleValue()));
Expand All @@ -453,8 +446,7 @@ public IRubyObject op_pow(ThreadContext context, double other) {
@JRubyMethod(name = "**", required = 1)
public IRubyObject op_pow19(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double d_other = ((RubyNumeric) other).getDoubleValue();
if (value < 0 && (d_other != Math.round(d_other))) {
Expand All @@ -478,8 +470,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
return context.runtime.getFalse();
}
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return RubyBoolean.newBoolean(context.runtime, value == ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -505,8 +496,7 @@ public boolean fastEqual(RubyFloat other) {
@Override
public final int compareTo(IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
return Double.compare(value, ((RubyNumeric) other).getDoubleValue());
default:
Expand All @@ -522,8 +512,7 @@ public final int compareTo(IRubyObject other) {
public IRubyObject op_cmp(ThreadContext context, IRubyObject other) {
final Ruby runtime = context.runtime;
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
if (Double.isInfinite(value)) {
return value > 0.0 ? RubyFixnum.one(runtime) : RubyFixnum.minus_one(runtime);
}
Expand Down Expand Up @@ -559,8 +548,7 @@ public IRubyObject op_cmp(ThreadContext context, double other) {
@JRubyMethod(name = ">", required = 1)
public IRubyObject op_gt(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(context.runtime, !Double.isNaN(b) && value > b);
Expand All @@ -579,8 +567,7 @@ public IRubyObject op_gt(ThreadContext context, double other) {
@JRubyMethod(name = ">=", required = 1)
public IRubyObject op_ge(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(context.runtime, !Double.isNaN(b) && value >= b);
Expand All @@ -599,8 +586,7 @@ public IRubyObject op_ge(ThreadContext context, double other) {
@JRubyMethod(name = "<", required = 1)
public IRubyObject op_lt(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(context.runtime, !Double.isNaN(b) && value < b);
Expand All @@ -619,8 +605,7 @@ public IRubyObject op_lt(ThreadContext context, double other) {
@JRubyMethod(name = "<=", required = 1)
public IRubyObject op_le(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
case FLOAT:
double b = ((RubyNumeric) other).getDoubleValue();
return RubyBoolean.newBoolean(context.runtime, !Double.isNaN(b) && value <= b);
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/org/jruby/RubyProcess.java
Expand Up @@ -646,8 +646,7 @@ private static int rlimitResourceValue(Ruby runtime, IRubyObject rval) {
}
/* fall through */

case FIXNUM:
case BIGNUM:
case INTEGER:
return rval.convertToInteger().getIntValue();
}

Expand Down Expand Up @@ -688,8 +687,7 @@ private static int rlimitResourceType(Ruby runtime, IRubyObject rtype) {
}
/* fall through */

case FIXNUM:
case BIGNUM:
case INTEGER:
return rtype.convertToInteger().getIntValue();
}

Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/org/jruby/RubyTime.java
Expand Up @@ -265,8 +265,7 @@ private static IRubyObject numExact(Ruby runtime, IRubyObject v) {
boolean typeError = false;

switch (v.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
return v;

case RATIONAL:
Expand Down Expand Up @@ -298,8 +297,7 @@ private static IRubyObject numExact(Ruby runtime, IRubyObject v) {
}

switch (v.getMetaClass().getClassIndex()) {
case FIXNUM:
case BIGNUM:
case INTEGER:
return v;

case RATIONAL:
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/javasupport/JavaUtil.java
Expand Up @@ -1463,11 +1463,12 @@ public static IRubyObject primitive_to_java(IRubyObject recv, IRubyObject object
case NIL:
javaObject = null;
break;
case FIXNUM:
javaObject = Long.valueOf(((RubyFixnum) object).getLongValue());
break;
case BIGNUM:
javaObject = ((RubyBignum) object).getValue();
case INTEGER:
if (object instanceof RubyFixnum) {
javaObject = Long.valueOf(((RubyFixnum) object).getLongValue());
} else {
javaObject = ((RubyBignum) object).getValue();
}
break;
case FLOAT:
javaObject = new Double(((RubyFloat) object).getValue());
Expand Down
27 changes: 14 additions & 13 deletions core/src/main/java/org/jruby/runtime/marshal/MarshalStream.java
Expand Up @@ -240,20 +240,21 @@ private void writeObjectData(IRubyObject value) throws IOException {
case FALSE:
write('F');
return;
case FIXNUM: {
RubyFixnum fixnum = (RubyFixnum)value;

if (isMarshalFixnum(fixnum)) {
write('i');
writeInt((int) fixnum.getLongValue());
return;
case INTEGER:
if (value instanceof RubyFixnum) {
RubyFixnum fixnum = (RubyFixnum)value;

if (isMarshalFixnum(fixnum)) {
write('i');
writeInt((int) fixnum.getLongValue());
return;
}
// FIXME: inefficient; constructing a bignum just for dumping?
value = RubyBignum.newBignum(value.getRuntime(), fixnum.getLongValue());

// fall through
}
// FIXME: inefficient; constructing a bignum just for dumping?
value = RubyBignum.newBignum(value.getRuntime(), fixnum.getLongValue());

// fall through
}
case BIGNUM:

write('l');
RubyBignum.marshalTo((RubyBignum)value, this);
return;
Expand Down
19 changes: 12 additions & 7 deletions core/src/main/java/org/jruby/util/Sprintf.java
Expand Up @@ -616,19 +616,24 @@ else if ((flags & FLAG_MINUS) != 0) {
name = null;
}

if (!(arg instanceof RubyFixnum || arg instanceof RubyBignum)) {
if (arg instanceof RubyFloat) {
arg = RubyNumeric.dbl2num(arg.getRuntime(), ((RubyFloat) arg).getValue());
} else if (arg instanceof RubyString) {
arg = ((RubyString) arg).stringToInum19(0, true);
} else {
ClassIndex type = arg.getMetaClass().getClassIndex();
if (type != ClassIndex.INTEGER) {
switch(type) {
case FLOAT:
arg = RubyNumeric.dbl2num(arg.getRuntime(),((RubyFloat)arg).getValue());
break;
case STRING:
arg = ((RubyString)arg).stringToInum19(0, true);
break;
default:
if (arg.respondsTo("to_int")) {
arg = TypeConverter.convertToType(arg, arg.getRuntime().getInteger(), "to_int", true);
} else {
arg = TypeConverter.convertToType(arg, arg.getRuntime().getInteger(), "to_i", true);
}
break;
}
type = arg.getMetaClass().getClassIndex();
}
byte[] bytes = null;
int first = 0;
Expand Down Expand Up @@ -666,7 +671,7 @@ else if ((flags & FLAG_MINUS) != 0) {
// uses C-sprintf, in part, to format numeric output, while
// we'll use Java's numeric formatting code (and our own).
boolean zero;
if (arg instanceof RubyFixnum) {
if (type == ClassIndex.INTEGER) {
negative = ((RubyFixnum)arg).getLongValue() < 0;
zero = ((RubyFixnum)arg).getLongValue() == 0;
if (negative && fchar == 'u') {
Expand Down
38 changes: 26 additions & 12 deletions core/src/main/java/org/jruby/util/io/PopenExecutor.java
Expand Up @@ -1474,7 +1474,10 @@ else if (id.equals("gid") && false) { // TODO
}
break;

case FIXNUM:
case INTEGER:
if (!(val instanceof RubyFixnum)) {
return ST_STOP;
}
case FILE:
case IO:
case ARRAY:
Expand Down Expand Up @@ -1523,9 +1526,14 @@ else if (id.equals("err")) {
case IO:
val = checkExecRedirectFd(runtime, val, false);
/* fall through */
case FIXNUM:
param = val;
eargp.fd_dup2 = checkExecRedirect1(runtime, eargp.fd_dup2, key, param);
case INTEGER:
if (val instanceof RubyFixnum) {
param = val;
eargp.fd_dup2 = checkExecRedirect1(runtime, eargp.fd_dup2, key, param);
break;
}

checkExecRedirectDefault(runtime, key, val, eargp);
break;

case ARRAY:
Expand Down Expand Up @@ -1574,18 +1582,24 @@ else if (flags instanceof RubyString)
break;

default:
tmp = val;
val = TypeConverter.ioCheckIO(runtime, tmp);
if (!val.isNil()) {
val = checkExecRedirectFd(runtime, val, false);
param = val;
eargp.fd_dup2 = checkExecRedirect1(runtime, eargp.fd_dup2, key, param);
}
throw runtime.newArgumentError("wrong exec redirect action");
checkExecRedirectDefault(runtime, key, val, eargp);
}

}

private static void checkExecRedirectDefault(Ruby runtime, IRubyObject key, IRubyObject val, ExecArg eargp) {
IRubyObject tmp;
IRubyObject param;
tmp = val;
val = TypeConverter.ioCheckIO(runtime, tmp);
if (!val.isNil()) {
val = checkExecRedirectFd(runtime, val, false);
param = val;
eargp.fd_dup2 = checkExecRedirect1(runtime, eargp.fd_dup2, key, param);
}
throw runtime.newArgumentError("wrong exec redirect action");
}

// MRI: check_exec_redirect_fd
static IRubyObject checkExecRedirectFd(Ruby runtime, IRubyObject v, boolean iskey) {
IRubyObject tmp;
Expand Down

0 comments on commit a103596

Please sign in to comment.