Skip to content
Permalink
Browse files
[Truffle] Implement a ToHexStringNode for better specializations.
  • Loading branch information
eregon committed Dec 12, 2014
1 parent 5e508ab commit 852995c0c9dd1332cac2d7325c5db832714d1666
Showing with 32 additions and 9 deletions.
  1. +32 −9 core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
@@ -1980,16 +1980,47 @@ public Object doThrow(Object tag, Object value) {

}

public abstract static class ToHexStringNode extends CoreMethodNode {

public ToHexStringNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ToHexStringNode(ToHexStringNode prev) {
super(prev);
}

public abstract String executeToHexString(Object value);

@Specialization
public String toHexString(int value) {
return toHexString((long) value);
}

@Specialization
public String toHexString(long value) {
return Long.toHexString(value);
}

@Specialization
public String toHexString(RubyBignum value) {
return value.toHexString();
}

}

@CoreMethod(names = {"to_s", "inspect"})
public abstract static class ToSNode extends CoreMethodNode {

@Child protected ClassNode classNode;
@Child protected BasicObjectNodes.IDNode idNode;
@Child protected ToHexStringNode toHexStringNode;

public ToSNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
classNode = KernelNodesFactory.ClassNodeFactory.create(context, sourceSection, new RubyNode[]{null});
idNode = BasicObjectNodesFactory.IDNodeFactory.create(context, sourceSection, new RubyNode[]{null});
toHexStringNode = KernelNodesFactory.ToHexStringNodeFactory.create(context, sourceSection, new RubyNode[]{null});
}

public abstract RubyString executeToS(VirtualFrame frame, Object self);
@@ -1999,16 +2030,8 @@ public RubyString toS(VirtualFrame frame, Object self) {
notDesignedForCompilation();

String className = classNode.executeGetClass(frame, self).getName();

Object id = idNode.executeObjectID(frame, self);
String hexID;
if (id instanceof Integer || id instanceof Long) {
hexID = Long.toHexString((long) id);
} else if (id instanceof RubyBignum) {
hexID = ((RubyBignum) id).toHexString();
} else {
throw new UnsupportedOperationException();
}
String hexID = toHexStringNode.executeToHexString(id);

return getContext().makeString("#<" + className + ":0x" + hexID + ">");
}

0 comments on commit 852995c

Please sign in to comment.