Skip to content

Commit a3f96bd

Browse files
committed
Narrow simulated Fixnum identity to fixnum range
In CRuby, only integer values within the valid range for fixnum tagged pointers are considered identical. JRuby considered all Fixnum objects identical. This commit narows the JRuby identity logic to only values within the fixnum tagged pointer range.
1 parent f1ec156 commit a3f96bd

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

core/src/main/java/org/jruby/RubyFixnum.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,23 @@ public final boolean eql(IRubyObject other) {
155155

156156
@Override
157157
public IRubyObject equal_p(ThreadContext context, IRubyObject obj) {
158-
return RubyBoolean.newBoolean(context, this == obj || eql(obj));
158+
long value = this.value;
159+
160+
if (fixnumable(value)) {
161+
return RubyBoolean.newBoolean(context, this == obj || eql(obj));
162+
}
163+
164+
return super.equal_p(context, obj);
165+
}
166+
167+
/**
168+
* Determine whether the given long value is in fixnum range.
169+
*
170+
* @param value the value in question
171+
* @return true if the value is in fixnum range, false otherwise
172+
*/
173+
private static boolean fixnumable(long value) {
174+
return value <= Long.MAX_VALUE / 2 && value >= Long.MIN_VALUE / 2;
159175
}
160176

161177
@Override
@@ -1386,7 +1402,9 @@ public IRubyObject bit_length(ThreadContext context) {
13861402

13871403
@Override
13881404
public IRubyObject id() {
1389-
if (value <= Long.MAX_VALUE / 2 && value >= Long.MIN_VALUE / 2) {
1405+
long value = this.value;
1406+
1407+
if (fixnumable(value)) {
13901408
return newFixnum(metaClass.runtime, 2 * value + 1);
13911409
}
13921410

0 commit comments

Comments
 (0)