public IRubyObject trap(final Ruby runtime, BlockCallback blk, String sig) {
return trap(runtime, new JRubySignalHandler(runtime, blk, sig));
}
private IRubyObject trap(final Ruby runtime, final JRubySignalHandler handler) {
final SignalHandler oldHandler;
final Signal signal;
try {
signal = new Signal(handler.signal);
} catch (Throwable e) {
return runtime.getNil();
}
try {
oldHandler = Signal.handle(signal, handler);
} catch (Exception e) {
throw runtime.newArgumentError(e.getMessage());
}
BlockCallback callback = null;
if (oldHandler instanceof JRubySignalHandler) {
JRubySignalHandler jsHandler = (JRubySignalHandler) oldHandler;
if (jsHandler.blockCallback != null) {
callback = jsHandler.blockCallback;
} else {
return jsHandler.block;
}
}
if (callback == null) {
callback = new BlockCallback() {
public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block) {
oldHandler.handle(new Signal(handler.signal));
return runtime.getNil();
}
};
}
final RubyModule signalModule = runtime.getModule("Signal");