2000-05-12 05:07:57 -04:00
|
|
|
#
|
2001-05-03 04:56:49 -04:00
|
|
|
# irb/multi-irb.rb - multiple irb module
|
2005-04-13 11:27:09 -04:00
|
|
|
# $Release Version: 0.9.5$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
2003-02-07 14:00:21 -05:00
|
|
|
IRB.fail CantShiftToMultiIrbMode unless defined?(Thread)
|
2000-05-12 05:07:57 -04:00
|
|
|
require "thread"
|
|
|
|
|
|
|
|
module IRB
|
|
|
|
# job management class
|
|
|
|
class JobManager
|
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
# @jobs = [[thread, irb],...]
|
|
|
|
@jobs = []
|
|
|
|
@current_job = nil
|
|
|
|
end
|
|
|
|
|
2001-04-30 13:54:55 -04:00
|
|
|
attr_accessor :current_job
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
def n_jobs
|
|
|
|
@jobs.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def thread(key)
|
|
|
|
th, irb = search(key)
|
2001-05-08 05:28:19 -04:00
|
|
|
th
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def irb(key)
|
|
|
|
th, irb = search(key)
|
|
|
|
irb
|
|
|
|
end
|
|
|
|
|
|
|
|
def main_thread
|
|
|
|
@jobs[0][0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def main_irb
|
|
|
|
@jobs[0][1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert(irb)
|
|
|
|
@jobs.push [Thread.current, irb]
|
|
|
|
end
|
|
|
|
|
|
|
|
def switch(key)
|
|
|
|
th, irb = search(key)
|
|
|
|
IRB.fail IrbAlreadyDead unless th.alive?
|
2003-02-07 14:00:21 -05:00
|
|
|
IRB.fail IrbSwitchedToCurrentThread if th == Thread.current
|
2000-05-12 05:07:57 -04:00
|
|
|
@current_job = irb
|
|
|
|
th.run
|
|
|
|
Thread.stop
|
|
|
|
@current_job = irb(Thread.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
def kill(*keys)
|
|
|
|
for key in keys
|
|
|
|
th, irb = search(key)
|
|
|
|
IRB.fail IrbAlreadyDead unless th.alive?
|
|
|
|
th.exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(key)
|
|
|
|
case key
|
|
|
|
when Integer
|
|
|
|
@jobs[key]
|
|
|
|
when Irb
|
2001-05-08 05:28:19 -04:00
|
|
|
@jobs.find{|k, v| v.equal?(key)}
|
2000-05-12 05:07:57 -04:00
|
|
|
when Thread
|
|
|
|
@jobs.assoc(key)
|
|
|
|
else
|
|
|
|
assoc = @jobs.find{|k, v| v.context.main.equal?(key)}
|
|
|
|
IRB.fail NoSuchJob, key if assoc.nil?
|
|
|
|
assoc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key)
|
|
|
|
case key
|
|
|
|
when Integer
|
|
|
|
IRB.fail NoSuchJob, key unless @jobs[key]
|
|
|
|
@jobs[key] = nil
|
|
|
|
else
|
2001-06-27 11:35:04 -04:00
|
|
|
catch(:EXISTS) do
|
2000-05-12 05:07:57 -04:00
|
|
|
@jobs.each_index do
|
|
|
|
|i|
|
|
|
|
if @jobs[i] and (@jobs[i][0] == key ||
|
|
|
|
@jobs[i][1] == key ||
|
|
|
|
@jobs[i][1].context.main.equal?(key))
|
|
|
|
@jobs[i] = nil
|
|
|
|
throw :EXISTS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
IRB.fail NoSuchJob, key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
until assoc = @jobs.pop; end unless @jobs.empty?
|
|
|
|
@jobs.push assoc
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
ary = []
|
|
|
|
@jobs.each_index do
|
|
|
|
|i|
|
|
|
|
th, irb = @jobs[i]
|
|
|
|
next if th.nil?
|
|
|
|
|
|
|
|
if th.alive?
|
|
|
|
if th.stop?
|
|
|
|
t_status = "stop"
|
|
|
|
else
|
|
|
|
t_status = "running"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
t_status = "exited"
|
|
|
|
end
|
|
|
|
ary.push format("#%d->%s on %s (%s: %s)",
|
|
|
|
i,
|
|
|
|
irb.context.irb_name,
|
|
|
|
irb.context.main,
|
|
|
|
th,
|
|
|
|
t_status)
|
|
|
|
end
|
|
|
|
ary.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@JobManager = JobManager.new
|
|
|
|
|
|
|
|
def IRB.JobManager
|
|
|
|
@JobManager
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
def IRB.CurrentContext
|
|
|
|
IRB.JobManager.irb(Thread.current).context
|
|
|
|
end
|
|
|
|
|
2001-04-30 13:54:55 -04:00
|
|
|
# invoke multi-irb
|
2000-05-12 05:07:57 -04:00
|
|
|
def IRB.irb(file = nil, *main)
|
2001-04-30 13:54:55 -04:00
|
|
|
workspace = WorkSpace.new(*main)
|
2000-05-12 05:07:57 -04:00
|
|
|
parent_thread = Thread.current
|
|
|
|
Thread.start do
|
|
|
|
begin
|
2001-04-30 13:54:55 -04:00
|
|
|
irb = Irb.new(workspace, file)
|
2000-05-12 05:07:57 -04:00
|
|
|
rescue
|
2001-04-30 13:54:55 -04:00
|
|
|
print "Subirb can't start with context(self): ", workspace.main.inspect, "\n"
|
2000-05-12 05:07:57 -04:00
|
|
|
print "return to main irb\n"
|
|
|
|
Thread.pass
|
|
|
|
Thread.main.wakeup
|
|
|
|
Thread.exit
|
|
|
|
end
|
|
|
|
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
|
|
|
|
@JobManager.insert(irb)
|
2001-05-08 05:28:19 -04:00
|
|
|
@JobManager.current_job = irb
|
2000-05-12 05:07:57 -04:00
|
|
|
begin
|
|
|
|
system_exit = false
|
|
|
|
catch(:IRB_EXIT) do
|
|
|
|
irb.eval_input
|
|
|
|
end
|
|
|
|
rescue SystemExit
|
|
|
|
system_exit = true
|
|
|
|
raise
|
|
|
|
#fail
|
|
|
|
ensure
|
|
|
|
unless system_exit
|
|
|
|
@JobManager.delete(irb)
|
|
|
|
if parent_thread.alive?
|
|
|
|
@JobManager.current_job = @JobManager.irb(parent_thread)
|
|
|
|
parent_thread.run
|
|
|
|
else
|
|
|
|
@JobManager.current_job = @JobManager.main_irb
|
|
|
|
@JobManager.main_thread.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Thread.stop
|
|
|
|
@JobManager.current_job = @JobManager.irb(Thread.current)
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
# class Context
|
|
|
|
# def set_last_value(value)
|
|
|
|
# @last_value = value
|
|
|
|
# @workspace.evaluate "_ = IRB.JobManager.irb(Thread.current).context.last_value"
|
|
|
|
# if @eval_history #and !@__.equal?(@last_value)
|
|
|
|
# @eval_history_values.push @line_no, @last_value
|
|
|
|
# @workspace.evaluate "__ = IRB.JobManager.irb(Thread.current).context.instance_eval{@eval_history_values}"
|
|
|
|
# end
|
|
|
|
# @last_value
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# module ExtendCommand
|
|
|
|
# def irb_context
|
|
|
|
# IRB.JobManager.irb(Thread.current).context
|
|
|
|
# end
|
|
|
|
# # alias conf irb_context
|
|
|
|
# end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
@CONF[:SINGLE_IRB_MODE] = false
|
|
|
|
@JobManager.insert(@CONF[:MAIN_CONTEXT].irb)
|
|
|
|
@JobManager.current_job = @CONF[:MAIN_CONTEXT].irb
|
|
|
|
|
2001-05-08 05:28:19 -04:00
|
|
|
class Irb
|
|
|
|
def signal_handle
|
|
|
|
unless @context.ignore_sigint?
|
|
|
|
print "\nabort!!\n" if @context.verbose?
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
case @signal_status
|
|
|
|
when :IN_INPUT
|
|
|
|
print "^C\n"
|
|
|
|
IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput
|
|
|
|
when :IN_EVAL
|
|
|
|
IRB.irb_abort(self)
|
|
|
|
when :IN_LOAD
|
|
|
|
IRB.irb_abort(self, LoadAbort)
|
|
|
|
when :IN_IRB
|
|
|
|
# ignore
|
|
|
|
else
|
2002-07-09 07:17:17 -04:00
|
|
|
# ignore other cases as well
|
2001-05-08 05:28:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
trap("SIGINT") do
|
|
|
|
@JobManager.current_job.signal_handle
|
2001-05-08 05:28:19 -04:00
|
|
|
Thread.stop
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|