2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2001-05-03 04:56:49 -04:00
|
|
|
# irb/multi-irb.rb - multiple irb module
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
2019-11-24 15:38:09 -05:00
|
|
|
fail CantShiftToMultiIrbMode unless defined?(Thread)
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
module IRB
|
|
|
|
class JobManager
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new JobManager object
|
2000-05-12 05:07:57 -04:00
|
|
|
def initialize
|
|
|
|
@jobs = []
|
|
|
|
@current_job = nil
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The active irb session
|
2001-04-30 13:54:55 -04:00
|
|
|
attr_accessor :current_job
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The total number of irb sessions, used to set +irb_name+ of the current
|
|
|
|
# Context.
|
2000-05-12 05:07:57 -04:00
|
|
|
def n_jobs
|
|
|
|
@jobs.size
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the thread for the given +key+ object, see #search for more
|
|
|
|
# information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def thread(key)
|
2010-11-08 15:59:01 -05:00
|
|
|
th, = search(key)
|
2001-05-08 05:28:19 -04:00
|
|
|
th
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the irb session for the given +key+ object, see #search for more
|
|
|
|
# information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def irb(key)
|
2010-11-08 15:59:01 -05:00
|
|
|
_, irb = search(key)
|
2000-05-12 05:07:57 -04:00
|
|
|
irb
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the top level thread.
|
2000-05-12 05:07:57 -04:00
|
|
|
def main_thread
|
|
|
|
@jobs[0][0]
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the top level irb session.
|
2000-05-12 05:07:57 -04:00
|
|
|
def main_irb
|
|
|
|
@jobs[0][1]
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Add the given +irb+ session to the jobs Array.
|
2000-05-12 05:07:57 -04:00
|
|
|
def insert(irb)
|
|
|
|
@jobs.push [Thread.current, irb]
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Changes the current active irb session to the given +key+ in the jobs
|
|
|
|
# Array.
|
|
|
|
#
|
|
|
|
# Raises an IrbAlreadyDead exception if the given +key+ is no longer alive.
|
|
|
|
#
|
|
|
|
# If the given irb session is already active, an IrbSwitchedToCurrentThread
|
|
|
|
# exception is raised.
|
2000-05-12 05:07:57 -04:00
|
|
|
def switch(key)
|
|
|
|
th, irb = search(key)
|
2019-11-24 15:38:09 -05:00
|
|
|
fail IrbAlreadyDead unless th.alive?
|
|
|
|
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
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Terminates the irb sessions specified by the given +keys+.
|
|
|
|
#
|
|
|
|
# Raises an IrbAlreadyDead exception if one of the given +keys+ is already
|
|
|
|
# terminated.
|
|
|
|
#
|
|
|
|
# See Thread#exit for more information.
|
2000-05-12 05:07:57 -04:00
|
|
|
def kill(*keys)
|
|
|
|
for key in keys
|
2014-08-09 21:05:08 -04:00
|
|
|
th, _ = search(key)
|
2019-11-24 15:38:09 -05:00
|
|
|
fail IrbAlreadyDead unless th.alive?
|
2014-08-09 21:05:08 -04:00
|
|
|
th.exit
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Returns the associated job for the given +key+.
|
|
|
|
#
|
|
|
|
# If given an Integer, it will return the +key+ index for the jobs Array.
|
|
|
|
#
|
|
|
|
# When an instance of Irb is given, it will return the irb session
|
|
|
|
# associated with +key+.
|
|
|
|
#
|
|
|
|
# If given an instance of Thread, it will return the associated thread
|
|
|
|
# +key+ using Object#=== on the jobs Array.
|
|
|
|
#
|
|
|
|
# Otherwise returns the irb session with the same top-level binding as the
|
|
|
|
# given +key+.
|
|
|
|
#
|
|
|
|
# Raises a NoSuchJob exception if no job can be found with the given +key+.
|
2000-05-12 05:07:57 -04:00
|
|
|
def search(key)
|
2008-08-25 09:41:11 -04:00
|
|
|
job = case key
|
2014-08-09 21:05:08 -04:00
|
|
|
when Integer
|
|
|
|
@jobs[key]
|
|
|
|
when Irb
|
|
|
|
@jobs.find{|k, v| v.equal?(key)}
|
|
|
|
when Thread
|
|
|
|
@jobs.assoc(key)
|
|
|
|
else
|
|
|
|
@jobs.find{|k, v| v.context.main.equal?(key)}
|
|
|
|
end
|
2019-11-24 15:38:09 -05:00
|
|
|
fail NoSuchJob, key if job.nil?
|
2008-08-25 09:41:11 -04:00
|
|
|
job
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Deletes the job at the given +key+.
|
2000-05-12 05:07:57 -04:00
|
|
|
def delete(key)
|
|
|
|
case key
|
|
|
|
when Integer
|
2019-11-24 15:38:09 -05:00
|
|
|
fail NoSuchJob, key unless @jobs[key]
|
2014-08-09 21:05:08 -04:00
|
|
|
@jobs[key] = nil
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-09 21:05:08 -04:00
|
|
|
catch(:EXISTS) do
|
|
|
|
@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
|
2019-11-24 15:38:09 -05:00
|
|
|
fail NoSuchJob, key
|
2014-08-09 21:05:08 -04:00
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
until assoc = @jobs.pop; end unless @jobs.empty?
|
|
|
|
@jobs.push assoc
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Outputs a list of jobs, see the irb command +irb_jobs+, or +jobs+.
|
2000-05-12 05:07:57 -04:00
|
|
|
def inspect
|
|
|
|
ary = []
|
|
|
|
@jobs.each_index do
|
2014-08-09 21:05:08 -04:00
|
|
|
|i|
|
|
|
|
th, irb = @jobs[i]
|
|
|
|
next if th.nil?
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2014-08-09 21:05:08 -04:00
|
|
|
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)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
ary.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@JobManager = JobManager.new
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The current JobManager in the session
|
2000-05-12 05:07:57 -04:00
|
|
|
def IRB.JobManager
|
|
|
|
@JobManager
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The current Context in this session
|
2002-07-09 07:17:17 -04:00
|
|
|
def IRB.CurrentContext
|
|
|
|
IRB.JobManager.irb(Thread.current).context
|
|
|
|
end
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
# Creates a new IRB session, see Irb.new.
|
|
|
|
#
|
|
|
|
# The optional +file+ argument is given to Context.new, along with the
|
|
|
|
# workspace created with the remaining arguments, see WorkSpace.new
|
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
|
2014-08-09 21:05:08 -04:00
|
|
|
irb = Irb.new(workspace, file)
|
2009-03-05 22:56:38 -05:00
|
|
|
rescue
|
2014-08-09 21:05:08 -04:00
|
|
|
print "Subirb can't start with context(self): ", workspace.main.inspect, "\n"
|
|
|
|
print "return to main irb\n"
|
|
|
|
Thread.pass
|
|
|
|
Thread.main.wakeup
|
|
|
|
Thread.exit
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-09 21:05:08 -04:00
|
|
|
system_exit = false
|
|
|
|
catch(:IRB_EXIT) do
|
|
|
|
irb.eval_input
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
rescue SystemExit
|
2014-08-09 21:05:08 -04:00
|
|
|
system_exit = true
|
|
|
|
raise
|
|
|
|
#fail
|
2000-05-12 05:07:57 -04:00
|
|
|
ensure
|
2014-08-09 21:05:08 -04:00
|
|
|
unless system_exit
|
|
|
|
@JobManager.delete(irb)
|
|
|
|
if @JobManager.current_job == 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
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
Thread.stop
|
|
|
|
@JobManager.current_job = @JobManager.irb(Thread.current)
|
|
|
|
end
|
|
|
|
|
|
|
|
@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?
|
2014-08-08 21:36:49 -04:00
|
|
|
print "\nabort!!\n" if @context.verbose?
|
|
|
|
exit
|
2001-05-08 05:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
case @signal_status
|
|
|
|
when :IN_INPUT
|
2014-08-08 21:36:49 -04:00
|
|
|
print "^C\n"
|
|
|
|
IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput
|
2001-05-08 05:28:19 -04:00
|
|
|
when :IN_EVAL
|
2014-08-08 21:36:49 -04:00
|
|
|
IRB.irb_abort(self)
|
2001-05-08 05:28:19 -04:00
|
|
|
when :IN_LOAD
|
2014-08-08 21:36:49 -04:00
|
|
|
IRB.irb_abort(self, LoadAbort)
|
2001-05-08 05:28:19 -04:00
|
|
|
when :IN_IRB
|
2014-08-08 21:36:49 -04:00
|
|
|
# ignore
|
2001-05-08 05:28:19 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -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
|