Skip to content

Commit

Permalink
[Truffle] Improve BooleanCastNode so to better reflect its cousin Rub…
Browse files Browse the repository at this point in the history
…yContext.isTruthy().

* They do the same but the node might be more efficient.
  • Loading branch information
eregon committed Nov 7, 2014
1 parent a8eb2bf commit 5d93cdc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
Expand Up @@ -21,8 +21,7 @@
import java.math.BigInteger;

/**
* Casts a value into a boolean. Works at the language level, so doesn't call any Ruby methods to
* cast non-core or monkey-patched objects.
* Casts a value into a boolean.
*/
@NodeChild(value = "child", type = RubyNode.class)
public abstract class BooleanCastNode extends RubyNode {
Expand All @@ -40,6 +39,11 @@ public boolean doNil(@SuppressWarnings("unused") RubyNilClass nil) {
return false;
}

@Specialization
public boolean doFalse(@SuppressWarnings("unused") RubyFalseClass falseObject) {
return false;
}

@Specialization
public boolean doBoolean(boolean value) {
return value;
Expand All @@ -65,9 +69,14 @@ public boolean doFloat(double value) {
return true;
}

@Specialization
@Specialization(guards = "neitherNilNorFalse")
public boolean doBasicObject(RubyBasicObject object) {
return object.isTrue();
return true;
}

protected boolean neitherNilNorFalse(RubyBasicObject object) {
return object != getContext().getCoreLibrary().getNilObject() &&
object != getContext().getCoreLibrary().getFalseObject();
}

@Override
Expand Down
Expand Up @@ -161,10 +161,6 @@ public boolean hasPrivateLayout() {
return hasPrivateLayout;
}

public boolean isTrue() {
return true;
}

public void visitObjectGraph(ObjectSpaceManager.ObjectGraphVisitor visitor) {
if (visitor.visit(this)) {
metaClass.visitObjectGraph(visitor);
Expand Down
Expand Up @@ -36,11 +36,6 @@ public int hashCode() {
return Boolean.FALSE.hashCode();
}

@Override
public boolean isTrue() {
return false;
}

@Override
public boolean hasClassAsSingleton() {
return true;
Expand Down
Expand Up @@ -26,11 +26,6 @@ public int hashCode() {
return 0;
}

@Override
public boolean isTrue() {
return false;
}

@Override
public boolean hasClassAsSingleton() {
return true;
Expand Down

0 comments on commit 5d93cdc

Please sign in to comment.