Skip to content

Commit

Permalink
[Truffle] Implement a ToHexStringNode for better specializations.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Dec 12, 2014
1 parent 5e508ab commit 852995c
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -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);
Expand All @@ -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 + ">");
}
Expand Down

0 comments on commit 852995c

Please sign in to comment.