1 29 package org.jruby; 30 31 import org.jruby.runtime.Block; 32 import org.jruby.runtime.CallbackFactory; 33 import org.jruby.runtime.ObjectAllocator; 34 import org.jruby.runtime.builtin.IRubyObject; 35 36 37 41 public class RubyProcess { 42 43 public static RubyModule createProcessModule(Ruby runtime) { 44 RubyModule process = runtime.defineModule("Process"); 45 46 RubyModule process_status = process.defineClassUnder("Status", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR); 48 49 CallbackFactory processCallbackFactory = runtime.callbackFactory(RubyProcess.class); 50 CallbackFactory process_statusCallbackFactory = runtime.callbackFactory(RubyProcess.RubyStatus.class); 51 52 76 82 process.defineModuleFunction("times", processCallbackFactory.getSingletonMethod("times")); 96 97 process_status.defineMethod("==", process_statusCallbackFactory.getMethod("op_eq", RubyKernel.IRUBY_OBJECT)); 99 process_status.defineMethod(">>", process_statusCallbackFactory.getMethod("rightshift_op", RubyKernel.IRUBY_OBJECT)); 101 process_status.defineMethod("to_i", process_statusCallbackFactory.getMethod("to_i")); 102 process_status.defineMethod("to_s", process_statusCallbackFactory.getMethod("to_s")); 104 process_status.defineMethod("inspect", process_statusCallbackFactory.getMethod("inspect")); 105 process_status.defineMethod("exitstatus", process_statusCallbackFactory.getMethod("exitstatus")); 112 process_status.defineMethod("success?", process_statusCallbackFactory.getMethod("success_p")); 113 115 return process; 116 } 117 118 public static class RubyStatus extends RubyObject { 119 private long status = 0L; 120 121 private static final long EXIT_SUCCESS = 0L; 122 public RubyStatus(Ruby runtime, RubyClass metaClass, long status) { 123 super(runtime, metaClass); 124 125 this.status = status; 126 } 127 128 public static RubyStatus newProcessStatus(Ruby runtime, long status) { 129 return new RubyStatus(runtime, runtime.getModule("Process").getClass("Status"), status); 130 } 131 132 public IRubyObject exitstatus(Block block) { 133 return getRuntime().newFixnum(status); 134 } 135 136 public IRubyObject rightshift_op(IRubyObject other, Block block) { 137 long shiftValue = other.convertToInteger().getLongValue(); 138 139 140 return getRuntime().newFixnum(status >> shiftValue); 141 } 142 143 public IRubyObject op_eq(IRubyObject other, Block block) { 144 return other.callMethod(getRuntime().getCurrentContext(), "==", this.to_i(block)); 145 } 146 147 public IRubyObject to_i(Block unusedBlock) { 148 return exitstatus(null); 149 } 150 151 public IRubyObject to_s(Block unusedBlock) { 152 return getRuntime().newString(String.valueOf(status)); 153 } 154 155 public IRubyObject inspect(Block unusedBlock) { 156 return to_s(); 157 } 158 159 public IRubyObject success_p(Block unusedBlock) { 160 return getRuntime().newBoolean(status == EXIT_SUCCESS); 161 } 162 } 163 164 public static IRubyObject times(IRubyObject recv, Block unusedBlock) { 165 Ruby runtime = recv.getRuntime(); 166 double currentTime = System.currentTimeMillis() / 1000.0; 167 double startTime = runtime.getStartTime() / 1000.0; 168 RubyFloat zero = runtime.newFloat(0.0); 169 return RubyStruct.newStruct(runtime.getTmsStruct(), 170 new IRubyObject[] { runtime.newFloat(currentTime - startTime), zero, zero, zero }, 171 Block.NULL_BLOCK); 172 } 173 174 public static IRubyObject pid(IRubyObject recv) { 175 return recv.getRuntime().newFixnum(0); 176 } 177 178 public static IRubyObject kill(IRubyObject recv, IRubyObject[] args) throws Exception { 179 return recv.getRuntime().getNil(); 180 } 181 } 182 | Popular Tags |