Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2c20b8

Browse files
committed
[Truffle] Remove all instanceof for RubyBignum.
1 parent f07a4b7 commit d2c20b8

File tree

8 files changed

+27
-13
lines changed

8 files changed

+27
-13
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/RubyGuards.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
package org.jruby.truffle.nodes;
1111

1212
import com.oracle.truffle.api.interop.TruffleObject;
13-
import org.jruby.truffle.nodes.core.BigDecimalNodes;
14-
import org.jruby.truffle.nodes.core.MethodNodes;
15-
import org.jruby.truffle.nodes.core.StringNodes;
16-
import org.jruby.truffle.nodes.core.UnboundMethodNodes;
13+
import org.jruby.truffle.nodes.core.*;
1714
import org.jruby.truffle.nodes.core.array.ArrayNodes;
1815
import org.jruby.truffle.nodes.core.hash.HashNodes;
1916
import org.jruby.truffle.runtime.NotProvided;
@@ -43,7 +40,11 @@ public static boolean isDouble(Object value) {
4340
// Ruby types
4441

4542
public static boolean isRubyBignum(Object value) {
46-
return value instanceof RubyBignum;
43+
return (value instanceof RubyBasicObject) && isRubyBignum((RubyBasicObject) value);
44+
}
45+
46+
public static boolean isRubyBignum(RubyBasicObject value) {
47+
return value.getDynamicObject().getShape().getObjectType() == BignumNodes.BIGNUM_TYPE;
4748
}
4849

4950
public static boolean isRubyBigDecimal(RubyBasicObject value) {
@@ -66,6 +67,7 @@ public static boolean isRubyArray(RubyBasicObject value) {
6667
//return value.getDynamicObject().getShape().getObjectType() == ArrayNodes.ARRAY_TYPE;
6768
return value instanceof RubyArray;
6869
}
70+
6971
public static boolean isRubyBinding(Object value) {
7072
return value instanceof RubyBinding;
7173
}

truffle/src/main/java/org/jruby/truffle/nodes/RubyNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public long executeLong(VirtualFrame frame) throws UnexpectedResultException {
112112
public RubyBignum executeRubyBignum(VirtualFrame frame) throws UnexpectedResultException {
113113
final Object value = execute(frame);
114114

115-
if (value instanceof RubyBignum) {
115+
if (RubyGuards.isRubyBignum(value)) {
116116
return (RubyBignum) value;
117117
} else {
118118
throw new UnexpectedResultException(value);

truffle/src/main/java/org/jruby/truffle/nodes/core/BignumNodes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.jruby.truffle.runtime.core.RubyBignum;
2929
import org.jruby.truffle.runtime.core.RubyClass;
3030
import org.jruby.truffle.runtime.core.RubyString;
31+
import org.jruby.truffle.runtime.object.BasicObjectType;
3132

3233
import java.math.BigInteger;
3334
import java.util.EnumSet;
@@ -38,14 +39,20 @@ public abstract class BignumNodes {
3839
public static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
3940
public static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
4041

42+
public static class BignumType extends BasicObjectType {
43+
44+
}
45+
46+
public static final BignumType BIGNUM_TYPE = new BignumType();
47+
4148
private static final HiddenKey VALUE_IDENTIFIER = new HiddenKey("value");
4249
public static final Property VALUE_PROPERTY;
4350
private static final DynamicObjectFactory BIGNUM_FACTORY;
4451

4552
static {
4653
final Shape.Allocator allocator = RubyBasicObject.LAYOUT.createAllocator();
4754
VALUE_PROPERTY = Property.create(VALUE_IDENTIFIER, allocator.locationForType(BigInteger.class, EnumSet.of(LocationModifier.Final, LocationModifier.NonNull)), 0);
48-
final Shape shape = RubyBasicObject.EMPTY_SHAPE.addProperty(VALUE_PROPERTY);
55+
final Shape shape = RubyBasicObject.LAYOUT.createShape(BIGNUM_TYPE).addProperty(VALUE_PROPERTY);
4956
BIGNUM_FACTORY = shape.createFactory();
5057
}
5158

truffle/src/main/java/org/jruby/truffle/pack/nodes/PackGuards.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package org.jruby.truffle.pack.nodes;
1111

12+
import org.jruby.truffle.nodes.RubyGuards;
1213
import org.jruby.truffle.runtime.core.RubyArray;
1314
import org.jruby.truffle.runtime.core.RubyBignum;
1415
import org.jruby.truffle.runtime.core.RubyString;
@@ -38,7 +39,7 @@ public static boolean isBigInteger(Object object) {
3839
}
3940

4041
public static boolean isRubyBignum(Object object) {
41-
return object instanceof RubyBignum;
42+
return RubyGuards.isRubyBignum(object);
4243
}
4344

4445
public static boolean isRubyString(Object object) {

truffle/src/main/java/org/jruby/truffle/pack/nodes/read/ReadLongOrBigIntegerNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.oracle.truffle.api.dsl.Specialization;
1616
import com.oracle.truffle.api.frame.VirtualFrame;
1717
import com.oracle.truffle.api.utilities.ConditionProfile;
18+
import org.jruby.truffle.pack.nodes.PackGuards;
1819
import org.jruby.truffle.pack.nodes.PackNode;
1920
import org.jruby.truffle.pack.nodes.SourceNode;
2021
import org.jruby.truffle.pack.nodes.type.ToLongNode;
@@ -66,7 +67,7 @@ public long read(VirtualFrame frame, long[] source) {
6667
public Object read(VirtualFrame frame, Object[] source) {
6768
final Object value = source[advanceSourcePosition(frame)];
6869

69-
if (bignumProfile.profile(value instanceof RubyBignum)) {
70+
if (bignumProfile.profile(PackGuards.isRubyBignum(value))) {
7071
return value;
7172
} else {
7273
if (toLongNode == null) {

truffle/src/main/java/org/jruby/truffle/pack/nodes/type/ToLongNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
2020
import org.jruby.truffle.nodes.dispatch.DispatchNode;
2121
import org.jruby.truffle.nodes.dispatch.MissingBehavior;
22+
import org.jruby.truffle.pack.nodes.PackGuards;
2223
import org.jruby.truffle.pack.nodes.PackNode;
2324
import org.jruby.truffle.pack.runtime.exceptions.CantConvertException;
2425
import org.jruby.truffle.pack.runtime.exceptions.NoImplicitConversionException;
@@ -91,7 +92,7 @@ public long toLong(VirtualFrame frame, Object object) {
9192
return toLong(frame, (long) value);
9293
}
9394

94-
if (seenBignum && value instanceof RubyBignum) {
95+
if (seenBignum && PackGuards.isRubyBignum(value)) {
9596
return toLong(frame, (RubyBignum) value);
9697
}
9798

@@ -111,7 +112,7 @@ public long toLong(VirtualFrame frame, Object object) {
111112
return toLong(frame, (long) value);
112113
}
113114

114-
if (value instanceof RubyBignum) {
115+
if (PackGuards.isRubyBignum(value)) {
115116
seenBignum = true;
116117
return toLong(frame, (RubyBignum) value);
117118
}

truffle/src/main/java/org/jruby/truffle/pack/nodes/write/WriteBERNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.oracle.truffle.api.dsl.Specialization;
1717
import com.oracle.truffle.api.frame.VirtualFrame;
1818
import org.jruby.truffle.nodes.core.BignumNodes;
19+
import org.jruby.truffle.pack.nodes.PackGuards;
1920
import org.jruby.truffle.pack.nodes.PackNode;
2021
import org.jruby.truffle.pack.runtime.exceptions.CantCompressNegativeException;
2122
import org.jruby.truffle.runtime.RubyContext;
@@ -85,7 +86,7 @@ private ByteList encode(Object from) {
8586

8687
long l;
8788

88-
if (from instanceof RubyBignum) {
89+
if (PackGuards.isRubyBignum(from)) {
8990
BigInteger big128 = BigInteger.valueOf(128);
9091
from = BignumNodes.getBigIntegerValue((RubyBignum) from);
9192
while (true) {

truffle/src/main/java/org/jruby/truffle/runtime/core/CoreLibrary.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jruby.runtime.Constants;
2626
import org.jruby.runtime.encoding.EncodingService;
2727
import org.jruby.runtime.load.LoadServiceResource;
28+
import org.jruby.truffle.nodes.RubyGuards;
2829
import org.jruby.truffle.nodes.RubyNode;
2930
import org.jruby.truffle.nodes.core.*;
3031
import org.jruby.truffle.nodes.core.array.ArrayNodes;
@@ -742,7 +743,7 @@ public static double toDouble(Object value, RubyBasicObject nil) {
742743
return (long) value;
743744
}
744745

745-
if (value instanceof RubyBignum) {
746+
if (RubyGuards.isRubyBignum(value)) {
746747
return BignumNodes.getBigIntegerValue((RubyBignum) value).doubleValue();
747748
}
748749

0 commit comments

Comments
 (0)