Skip to content

Commit e8ef793

Browse files
committedFeb 23, 2018
Move Fixnum#remainder up to Numeric and clean up isNeg/Pos.
1 parent 0f3746b commit e8ef793

File tree

6 files changed

+45
-43
lines changed

6 files changed

+45
-43
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyFixnum.java

-16
Original file line numberDiff line numberDiff line change
@@ -1383,22 +1383,6 @@ public RubyRational convertToRational() {
13831383
return RubyRational.newRationalRaw(runtime, this, one(runtime));
13841384
}
13851385

1386-
@Override
1387-
public IRubyObject remainder(ThreadContext context, IRubyObject y) {
1388-
RubyFixnum x = this;
1389-
JavaSites.FixnumSites sites = sites(context);
1390-
IRubyObject z = sites.op_mod.call(context, this, this, y);
1391-
1392-
if ((!Helpers.rbEqual(context, z, RubyFixnum.zero(context.runtime), sites.op_equal).isTrue()) &&
1393-
((x.isNegative() &&
1394-
((RubyInteger) y).isPositive()) ||
1395-
(x.isPositive() &&
1396-
((RubyInteger) y).isNegative()))) {
1397-
return sites.op_minus.call(context, z, z, y);
1398-
}
1399-
return z;
1400-
}
1401-
14021386
private static JavaSites.FixnumSites sites(ThreadContext context) {
14031387
return context.sites.Fixnum;
14041388
}

Diff for: ‎core/src/main/java/org/jruby/RubyFloat.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,23 @@ public int signum() {
200200
@Override
201201
@JRubyMethod(name = "negative?")
202202
public IRubyObject isNegative(ThreadContext context) {
203-
return context.runtime.newBoolean(signum() < 0);
203+
return context.runtime.newBoolean(isNegative());
204204
}
205205

206206
@Override
207207
@JRubyMethod(name = "positive?")
208208
public IRubyObject isPositive(ThreadContext context) {
209-
return context.runtime.newBoolean(signum() > 0);
209+
return context.runtime.newBoolean(isPositive());
210+
}
211+
212+
@Override
213+
public boolean isNegative() {
214+
return signum() < 0;
215+
}
216+
217+
@Override
218+
public boolean isPositive() {
219+
return signum() > 0;
210220
}
211221

212222
public static RubyFloat newFloat(Ruby runtime, double value) {

Diff for: ‎core/src/main/java/org/jruby/RubyInteger.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ public IRubyObject isPositive(ThreadContext context) {
123123
return context.runtime.newBoolean(signum() > 0);
124124
}
125125

126-
// @Override // NOTE: could be public + defined on Numeric
127-
final boolean isNegative() {
126+
@Override
127+
public boolean isNegative() {
128128
return signum() < 0;
129129
}
130130

131-
// @Override // NOTE: could be public + defined on Numeric
132-
final boolean isPositive() {
131+
@Override
132+
public boolean isPositive() {
133133
return signum() > 0;
134134
}
135135

Diff for: ‎core/src/main/java/org/jruby/RubyNumeric.java

+18-20
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,20 @@ public IRubyObject modulo(ThreadContext context, IRubyObject other) {
809809
*
810810
*/
811811
@JRubyMethod(name = "remainder")
812-
public IRubyObject remainder(ThreadContext context, IRubyObject dividend) {
813-
IRubyObject z = numFuncall(context, this, sites(context).op_mod, dividend);
814-
RubyFixnum zero = RubyFixnum.zero(context.runtime);
812+
public IRubyObject remainder(ThreadContext context, IRubyObject y) {
813+
RubyNumeric x = this;
814+
JavaSites.NumericSites sites = sites(context);
815+
IRubyObject z = sites.op_mod.call(context, this, this, y);
816+
817+
// non-numeric would error out in % call
818+
RubyNumeric yNum = (RubyNumeric) y;
815819

816-
if (!equalInternal(context, z, zero) &&
817-
((isNegative(context).isTrue() &&
818-
positiveIntP(context, dividend).isTrue()) ||
819-
(isPositive(context).isTrue() &&
820-
negativeIntP(context, dividend).isTrue()))) {
821-
return sites(context).op_minus.call(context, z, z, dividend);
820+
if ((!Helpers.rbEqual(context, z, RubyFixnum.zero(context.runtime), sites.op_equal).isTrue()) &&
821+
((x.isNegative() &&
822+
yNum.isPositive()) ||
823+
(x.isPositive() &&
824+
yNum.isNegative()))) {
825+
return sites.op_minus.call(context, z, z, y);
822826
}
823827
return z;
824828
}
@@ -1374,8 +1378,8 @@ public Throwable fillInStackTrace() {
13741378
@JRubyMethod(name = "negative?")
13751379
public IRubyObject isNegative(ThreadContext context) {
13761380
return compareWithZero(context, this, sites(context).op_lt_checked);
1377-
13781381
}
1382+
13791383
/** num_positive_p
13801384
*
13811385
*/
@@ -1384,18 +1388,12 @@ public IRubyObject isPositive(ThreadContext context) {
13841388
return compareWithZero(context, this, sites(context).op_gt_checked);
13851389
}
13861390

1387-
protected static IRubyObject negativeIntP(ThreadContext context, IRubyObject obj) {
1388-
if (obj instanceof RubyNumeric) {
1389-
return ((RubyNumeric) obj).isNegative(context);
1390-
}
1391-
return compareWithZero(context, obj, sites(context).op_lt_checked);
1391+
public boolean isNegative() {
1392+
return isNegative(getRuntime().getCurrentContext()).isTrue();
13921393
}
13931394

1394-
protected static IRubyObject positiveIntP(ThreadContext context, IRubyObject obj) {
1395-
if (obj instanceof RubyNumeric) {
1396-
return ((RubyNumeric) obj).isPositive(context);
1397-
}
1398-
return compareWithZero(context, obj, sites(context).op_gt_checked);
1395+
public boolean isPositive() {
1396+
return isPositive(getRuntime().getCurrentContext()).isTrue();
13991397
}
14001398

14011399
protected static IRubyObject compareWithZero(ThreadContext context, IRubyObject num, JavaSites.CheckedSites site) {

Diff for: ‎core/src/main/java/org/jruby/RubyRational.java

+10
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,16 @@ public IRubyObject isPositive(ThreadContext context) {
471471
return context.runtime.newBoolean(signum() > 0);
472472
}
473473

474+
@Override
475+
public boolean isNegative() {
476+
return signum() < 0;
477+
}
478+
479+
@Override
480+
public boolean isPositive() {
481+
return signum() > 0;
482+
}
483+
474484
public final int signum() { return num.signum(); }
475485

476486
/** f_imul

Diff for: ‎core/src/main/java/org/jruby/runtime/JavaSites.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public static class NumericSites {
198198
public final CallSite op_rshift = new FunctionalCachingCallSite(">>");
199199
public final CallSite size = new FunctionalCachingCallSite("size");
200200
public final CallSite ceil = new FunctionalCachingCallSite("ceil");
201+
public final CallSite op_equal = new FunctionalCachingCallSite("==");
201202
}
202203

203204
public static class IntegerSites {
@@ -236,7 +237,6 @@ public static class FixnumSites {
236237
public final CheckedSites checked_op_and = new CheckedSites("&");
237238
public final CheckedSites checked_op_or = new CheckedSites("|");
238239
public final CheckedSites checked_op_xor = new CheckedSites("^");
239-
public final CallSite op_equal = new FunctionalCachingCallSite("==");
240240
}
241241

242242
public static class BignumSites {

0 commit comments

Comments
 (0)
Please sign in to comment.