Skip to content

Commit

Permalink
[Truffle] Use the lazy initialization pattern for CompareNode.boolean…
Browse files Browse the repository at this point in the history
…CastNode.
  • Loading branch information
eregon committed Oct 24, 2014
1 parent 229038c commit 108afd1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java
Expand Up @@ -85,12 +85,10 @@ public abstract static class CompareNode extends CoreMethodNode {

public CompareNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
booleanCastNode = BooleanCastNodeFactory.create(context, sourceSection, null);
}

public CompareNode(CompareNode prev) {
super(prev);
booleanCastNode = prev.booleanCastNode;
}

private Object isSubclass(VirtualFrame frame, RubyModule self, RubyModule other) {
Expand All @@ -101,6 +99,14 @@ private Object isSubclass(VirtualFrame frame, RubyModule self, RubyModule other)
return subclassNode.executeIsSubclassOf(frame, self, other);
}

private boolean booleanCast(VirtualFrame frame, Object value) {
if (booleanCastNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
booleanCastNode = insert(BooleanCastNodeFactory.create(getContext(), getSourceSection(), null));
}
return booleanCastNode.executeBoolean(frame, value);
}

@Specialization
public Object compare(VirtualFrame frame, RubyModule self, RubyModule other) {
notDesignedForCompilation();
Expand All @@ -113,7 +119,7 @@ public Object compare(VirtualFrame frame, RubyModule self, RubyModule other) {

if (isSubclass instanceof RubyNilClass) {
return getContext().getCoreLibrary().getNilObject();
} else if (booleanCastNode.executeBoolean(frame, isSubclass)) {
} else if (booleanCast(frame, isSubclass)) {
return -1;
}
return 1;
Expand Down

0 comments on commit 108afd1

Please sign in to comment.