1 33 package org.jruby; 34 35 import org.jruby.runtime.CallbackFactory; 36 import org.jruby.runtime.ClassIndex; 37 import org.jruby.runtime.ObjectAllocator; 38 import org.jruby.runtime.builtin.IRubyObject; 39 40 44 public class RubyNil extends RubyObject { 45 private final Ruby runtime; 46 47 public RubyNil(Ruby runtime) { 48 super(runtime, null); 49 this.runtime = runtime; 50 } 51 52 public Ruby getRuntime() { 53 return runtime; 54 } 55 56 public static ObjectAllocator NIL_ALLOCATOR = new ObjectAllocator() { 57 public IRubyObject allocate(Ruby runtime, RubyClass klass) { 58 return runtime.getNil(); 59 } 60 }; 61 62 public static RubyClass createNilClass(Ruby runtime) { 63 RubyClass nilClass = runtime.defineClass("NilClass", runtime.getObject(), NIL_ALLOCATOR); 64 nilClass.index = ClassIndex.NIL; 65 66 CallbackFactory callbackFactory = runtime.callbackFactory(RubyNil.class); 67 nilClass.defineFastMethod("type", callbackFactory.getFastSingletonMethod("type")); 68 nilClass.defineFastMethod("to_i", callbackFactory.getFastSingletonMethod("to_i")); 69 nilClass.defineFastMethod("to_s", callbackFactory.getFastSingletonMethod("to_s")); 70 nilClass.defineFastMethod("to_a", callbackFactory.getFastSingletonMethod("to_a")); 71 nilClass.defineFastMethod("to_f", callbackFactory.getFastSingletonMethod("to_f")); 72 nilClass.defineFastMethod("inspect", callbackFactory.getFastSingletonMethod("inspect")); 73 74 nilClass.defineFastMethod("&", callbackFactory.getFastSingletonMethod("op_and", RubyKernel.IRUBY_OBJECT)); 75 nilClass.defineFastMethod("|", callbackFactory.getFastSingletonMethod("op_or", RubyKernel.IRUBY_OBJECT)); 76 nilClass.defineFastMethod("^", callbackFactory.getFastSingletonMethod("op_xor", RubyKernel.IRUBY_OBJECT)); 77 nilClass.defineFastMethod("nil?", callbackFactory.getFastMethod("nil_p")); 78 nilClass.defineFastMethod("id", callbackFactory.getFastSingletonMethod("id")); 79 nilClass.defineFastMethod("taint", callbackFactory.getFastMethod("taint")); 80 nilClass.defineFastMethod("freeze", callbackFactory.getFastMethod("freeze")); 81 82 nilClass.getMetaClass().undefineMethod("new"); 83 84 runtime.defineGlobalConstant("NIL", runtime.getNil()); 85 86 return nilClass; 87 } 88 89 public int getNativeTypeIndex() { 90 return ClassIndex.NIL; 91 } 92 93 public RubyClass getMetaClass() { 94 return runtime.getNilClass(); 95 } 96 97 public boolean isImmediate() { 98 return true; 99 } 100 101 public boolean safeHasInstanceVariables() { 102 return false; 103 } 104 105 107 110 public static RubyFixnum to_i(IRubyObject recv) { 111 return RubyFixnum.zero(recv.getRuntime()); 112 } 113 114 118 public static RubyFloat to_f(IRubyObject recv) { 119 return RubyFloat.newFloat(recv.getRuntime(), 0.0D); 120 } 121 122 125 public static RubyString to_s(IRubyObject recv) { 126 return recv.getRuntime().newString(""); 127 } 128 129 132 public static RubyArray to_a(IRubyObject recv) { 133 return recv.getRuntime().newArray(0); 134 } 135 136 139 public static RubyString inspect(IRubyObject recv) { 140 return recv.getRuntime().newString("nil"); 141 } 142 143 146 public static RubyClass type(IRubyObject recv) { 147 return recv.getRuntime().getClass("NilClass"); 148 } 149 150 153 public static RubyBoolean op_and(IRubyObject recv, IRubyObject obj) { 154 return recv.getRuntime().getFalse(); 155 } 156 157 160 public static RubyBoolean op_or(IRubyObject recv, IRubyObject obj) { 161 return recv.getRuntime().newBoolean(obj.isTrue()); 162 } 163 164 167 public static RubyBoolean op_xor(IRubyObject recv, IRubyObject obj) { 168 return recv.getRuntime().newBoolean(obj.isTrue()); 169 } 170 171 public static RubyFixnum id(IRubyObject recv) { 172 return recv.getRuntime().newFixnum(4); 173 } 174 175 public boolean isNil() { 176 return true; 177 } 178 179 public boolean isFalse() { 180 return true; 181 } 182 183 public boolean isTrue() { 184 return false; 185 } 186 187 public IRubyObject freeze() { 188 return this; 189 } 190 191 public IRubyObject nil_p() { 192 return getRuntime().getTrue(); 193 } 194 195 public IRubyObject taint() { 196 return this; 197 } 198 199 public RubyFixnum id() { 200 return getRuntime().newFixnum(4); 201 } 202 } 203 | Popular Tags |