1 32 package org.jruby; 33 34 import org.jruby.runtime.CallbackFactory; 35 import org.jruby.runtime.ClassIndex; 36 import org.jruby.runtime.ObjectAllocator; 37 import org.jruby.runtime.builtin.IRubyObject; 38 import org.jruby.runtime.marshal.MarshalStream; 39 40 44 public class RubyBoolean extends RubyObject { 45 private final Ruby runtime; 46 47 private final boolean value; 48 49 public RubyBoolean(Ruby runtime, boolean value) { 50 super(runtime, null, false); this.value = value; 53 this.runtime = runtime; 54 } 55 56 public int getNativeTypeIndex() { 57 return value ? ClassIndex.TRUE : ClassIndex.FALSE; 58 } 59 60 public Ruby getRuntime() { 61 return runtime; 62 } 63 64 public boolean isImmediate() { 65 return true; 66 } 67 68 public Class getJavaClass() { 69 return Boolean.TYPE; 70 } 71 72 public RubyClass getMetaClass() { 73 return value 74 ? getRuntime().getClass("TrueClass") 75 : getRuntime().getClass("FalseClass"); 76 } 77 78 public boolean isTrue() { 79 return value; 80 } 81 82 public boolean isFalse() { 83 return !value; 84 } 85 86 public RubyFixnum id() { 87 return getRuntime().newFixnum(value ? 2 : 0); 88 } 89 90 public static RubyClass createFalseClass(Ruby runtime) { 91 RubyClass falseClass = runtime.defineClass("FalseClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 92 falseClass.index = ClassIndex.FALSE; 93 94 CallbackFactory fact = runtime.callbackFactory(RubyBoolean.class); 95 96 falseClass.defineFastMethod("type", fact.getFastMethod("type")); 97 falseClass.defineFastMethod("&", fact.getFastMethod("false_and", RubyKernel.IRUBY_OBJECT)); 98 falseClass.defineFastMethod("|", fact.getFastMethod("false_or", RubyKernel.IRUBY_OBJECT)); 99 falseClass.defineFastMethod("^", fact.getFastMethod("false_xor", RubyKernel.IRUBY_OBJECT)); 100 falseClass.defineFastMethod("id", fact.getFastMethod("false_id")); 101 falseClass.defineFastMethod("to_s", fact.getFastMethod("false_to_s")); 102 103 runtime.defineGlobalConstant("FALSE", runtime.getFalse()); 104 105 return falseClass; 106 } 107 108 public static RubyClass createTrueClass(Ruby runtime) { 109 RubyClass trueClass = runtime.defineClass("TrueClass", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 110 trueClass.index = ClassIndex.TRUE; 111 112 CallbackFactory fact = runtime.callbackFactory(RubyBoolean.class); 113 114 trueClass.defineFastMethod("type", fact.getFastMethod("type")); 115 trueClass.defineFastMethod("&", fact.getFastMethod("true_and", RubyKernel.IRUBY_OBJECT)); 116 trueClass.defineFastMethod("|", fact.getFastMethod("true_or", RubyKernel.IRUBY_OBJECT)); 117 trueClass.defineFastMethod("^", fact.getFastMethod("true_xor", RubyKernel.IRUBY_OBJECT)); 118 trueClass.defineFastMethod("id", fact.getFastMethod("true_id")); 119 trueClass.defineFastMethod("to_s", fact.getFastMethod("true_to_s")); 120 121 runtime.defineGlobalConstant("TRUE", runtime.getTrue()); 122 123 return trueClass; 124 } 125 126 public static RubyBoolean newBoolean(Ruby runtime, boolean value) { 127 return value ? runtime.getTrue() : runtime.getFalse(); 128 } 129 130 134 public RubyClass type() { 135 return getMetaClass(); 136 } 137 138 public IRubyObject false_and(IRubyObject oth) { 139 return this; 140 } 141 142 public IRubyObject false_or(IRubyObject oth) { 143 return oth.isTrue() ? getRuntime().getTrue() : this; 144 } 145 146 public IRubyObject false_xor(IRubyObject oth) { 147 return oth.isTrue() ? getRuntime().getTrue() : this; 148 } 149 150 public IRubyObject false_id() { 151 return RubyFixnum.zero(getRuntime()); 152 } 153 154 public IRubyObject false_to_s() { 155 return getRuntime().newString("false"); 156 } 157 158 public IRubyObject true_and(IRubyObject oth) { 159 return oth.isTrue() ? this : getRuntime().getFalse(); 160 } 161 162 public IRubyObject true_or(IRubyObject oth) { 163 return this; 164 } 165 166 public IRubyObject true_xor(IRubyObject oth) { 167 return oth.isTrue() ? getRuntime().getFalse() : this; 168 } 169 170 public IRubyObject true_id() { 171 return getRuntime().newFixnum(2); 172 } 173 174 public IRubyObject true_to_s() { 175 return getRuntime().newString("true"); 176 } 177 178 public void marshalTo(MarshalStream output) throws java.io.IOException { 179 output.write(isTrue() ? 'T' : 'F'); 180 } 181 } 182 183 | Popular Tags |