BooleanCastNode.java

  1. /*
  2.  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
  3.  * code is released under a tri EPL/GPL/LGPL license. You can use it,
  4.  * redistribute it and/or modify it under the terms of the:
  5.  *
  6.  * Eclipse Public License version 1.0
  7.  * GNU General Public License version 2
  8.  * GNU Lesser General Public License version 2.1
  9.  */
  10. package org.jruby.truffle.nodes.cast;

  11. import com.oracle.truffle.api.dsl.NodeChild;
  12. import com.oracle.truffle.api.dsl.Specialization;
  13. import com.oracle.truffle.api.frame.VirtualFrame;
  14. import com.oracle.truffle.api.source.SourceSection;
  15. import org.jruby.truffle.nodes.RubyNode;
  16. import org.jruby.truffle.runtime.RubyContext;
  17. import org.jruby.truffle.runtime.core.RubyBasicObject;
  18. import org.jruby.truffle.runtime.core.RubyNilClass;

  19. /**
  20.  * Casts a value into a boolean.
  21.  */
  22. @NodeChild(value = "child", type = RubyNode.class)
  23. public abstract class BooleanCastNode extends RubyNode {

  24.     public BooleanCastNode(RubyContext context, SourceSection sourceSection) {
  25.         super(context, sourceSection);
  26.     }

  27.     public BooleanCastNode(BooleanCastNode copy) {
  28.         super(copy.getContext(), copy.getSourceSection());
  29.     }

  30.     @Specialization
  31.     public boolean doNil(@SuppressWarnings("unused") RubyNilClass nil) {
  32.         return false;
  33.     }

  34.     @Specialization
  35.     public boolean doBoolean(boolean value) {
  36.         return value;
  37.     }

  38.     @Specialization
  39.     public boolean doIntegerFixnum(int value) {
  40.         return true;
  41.     }

  42.     @Specialization
  43.     public boolean doLongFixnum(long value) {
  44.         return true;
  45.     }

  46.     @Specialization
  47.     public boolean doFloat(double value) {
  48.         return true;
  49.     }

  50.     @Specialization(guards = "!isRubyNilClass")
  51.     public boolean doBasicObject(RubyBasicObject object) {
  52.         return true;
  53.     }

  54.     @Override
  55.     public abstract boolean executeBoolean(VirtualFrame frame);

  56.     public abstract boolean executeBoolean(VirtualFrame frame, Object value);

  57. }