diff options
Diffstat (limited to 'lib/irb/ext')
-rw-r--r-- | lib/irb/ext/change-ws.rb | 20 | ||||
-rw-r--r-- | lib/irb/ext/eval_history.rb (renamed from lib/irb/ext/history.rb) | 20 | ||||
-rw-r--r-- | lib/irb/ext/loader.rb | 42 | ||||
-rw-r--r-- | lib/irb/ext/multi-irb.rb | 17 | ||||
-rw-r--r-- | lib/irb/ext/save-history.rb | 130 | ||||
-rw-r--r-- | lib/irb/ext/tracer.rb | 73 | ||||
-rw-r--r-- | lib/irb/ext/use-loader.rb | 20 | ||||
-rw-r--r-- | lib/irb/ext/workspaces.rb | 50 |
8 files changed, 55 insertions, 317 deletions
diff --git a/lib/irb/ext/change-ws.rb b/lib/irb/ext/change-ws.rb index 4c57e44eab..60e8afe31f 100644 --- a/lib/irb/ext/change-ws.rb +++ b/lib/irb/ext/change-ws.rb @@ -1,14 +1,8 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # irb/ext/cb.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# module IRB # :nodoc: class Context @@ -18,7 +12,7 @@ module IRB # :nodoc: if defined? @home_workspace @home_workspace else - @home_workspace = @workspace + @home_workspace = workspace end end @@ -31,15 +25,13 @@ module IRB # :nodoc: # See IRB::WorkSpace.new for more information. def change_workspace(*_main) if _main.empty? - @workspace = home_workspace + replace_workspace(home_workspace) return main end - @workspace = WorkSpace.new(_main[0]) - - if !(class<<main;ancestors;end).include?(ExtendCommandBundle) - main.extend ExtendCommandBundle - end + workspace = WorkSpace.new(_main[0]) + replace_workspace(workspace) + workspace.load_helper_methods_to_main end end end diff --git a/lib/irb/ext/history.rb b/lib/irb/ext/eval_history.rb index fc304c6f6c..6c21ff00ee 100644 --- a/lib/irb/ext/history.rb +++ b/lib/irb/ext/eval_history.rb @@ -1,14 +1,8 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # history.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# module IRB # :nodoc: @@ -24,7 +18,7 @@ module IRB # :nodoc: if defined?(@eval_history) && @eval_history @eval_history_values.push @line_no, @last_value - @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}" + workspace.evaluate "__ = IRB.CurrentContext.instance_eval{@eval_history_values}" end @last_value @@ -46,16 +40,16 @@ module IRB # :nodoc: # # If +no+ is +nil+, execution result history isn't used (default). # - # History values are available via <code>__</code> variable, see - # IRB::History. + # EvalHistory values are available via <code>__</code> variable, see + # IRB::EvalHistory. def eval_history=(no) if no if defined?(@eval_history) && @eval_history @eval_history_values.size(no) else - @eval_history_values = History.new(no) + @eval_history_values = EvalHistory.new(no) IRB.conf[:__TMP__EHV__] = @eval_history_values - @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]") + workspace.evaluate("__ = IRB.conf[:__TMP__EHV__]") IRB.conf.delete(:__TMP_EHV__) end else @@ -95,7 +89,7 @@ module IRB # :nodoc: # __[1] # # => 10 # - class History + class EvalHistory def initialize(size = 16) # :nodoc: @size = size diff --git a/lib/irb/ext/loader.rb b/lib/irb/ext/loader.rb index af028996e7..df5aaa8e5a 100644 --- a/lib/irb/ext/loader.rb +++ b/lib/irb/ext/loader.rb @@ -1,15 +1,8 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # loader.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# - module IRB # :nodoc: # Raised in the event of an exception in a file loaded from an Irb session @@ -31,31 +24,8 @@ module IRB # :nodoc: load_file(path, priv) end - if File.respond_to?(:absolute_path?) - def absolute_path?(path) - File.absolute_path?(path) - end - else - separator = - if File::ALT_SEPARATOR - "[#{Regexp.quote(File::SEPARATOR + File::ALT_SEPARATOR)}]" - else - File::SEPARATOR - end - ABSOLUTE_PATH_PATTERN = # :nodoc: - case Dir.pwd - when /\A\w:/, /\A#{separator}{2}/ - /\A(?:\w:|#{separator})#{separator}/ - else - /\A#{separator}/ - end - def absolute_path?(path) - ABSOLUTE_PATH_PATTERN =~ path - end - end - def search_file_from_ruby_path(fn) # :nodoc: - if absolute_path?(fn) + if File.absolute_path?(fn) return fn if File.exist?(fn) return nil end @@ -72,6 +42,7 @@ module IRB # :nodoc: # # See Irb#suspend_input_method for more information. def source_file(path) + irb = irb_context.irb irb.suspend_name(path, File.basename(path)) do FileInputMethod.open(path) do |io| irb.suspend_input_method(io) do @@ -96,6 +67,7 @@ module IRB # :nodoc: # # See Irb#suspend_input_method for more information. def load_file(path, priv = nil) + irb = irb_context.irb irb.suspend_name(path, File.basename(path)) do if priv @@ -126,13 +98,13 @@ module IRB # :nodoc: def old # :nodoc: back_io = @io - back_path = @irb_path + back_path = irb_path back_name = @irb_name back_scanner = @irb.scanner begin @io = FileInputMethod.new(path) @irb_name = File.basename(path) - @irb_path = path + self.irb_path = path @irb.signal_status(:IN_LOAD) do if back_io.kind_of?(FileInputMethod) @irb.eval_input @@ -147,7 +119,7 @@ module IRB # :nodoc: ensure @io = back_io @irb_name = back_name - @irb_path = back_path + self.irb_path = back_path @irb.scanner = back_scanner end end diff --git a/lib/irb/ext/multi-irb.rb b/lib/irb/ext/multi-irb.rb index 74de1ecde5..9f234f0cdc 100644 --- a/lib/irb/ext/multi-irb.rb +++ b/lib/irb/ext/multi-irb.rb @@ -1,18 +1,11 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # irb/multi-irb.rb - multiple irb module -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# -fail CantShiftToMultiIrbMode unless defined?(Thread) module IRB - class JobManager + class JobManager # :nodoc: # Creates a new JobManager object def initialize @@ -173,12 +166,12 @@ module IRB @JobManager = JobManager.new # The current JobManager in the session - def IRB.JobManager + def IRB.JobManager # :nodoc: @JobManager end # The current Context in this session - def IRB.CurrentContext + def IRB.CurrentContext # :nodoc: IRB.JobManager.irb(Thread.current).context end @@ -186,7 +179,7 @@ module IRB # # The optional +file+ argument is given to Context.new, along with the # workspace created with the remaining arguments, see WorkSpace.new - def IRB.irb(file = nil, *main) + def IRB.irb(file = nil, *main) # :nodoc: workspace = WorkSpace.new(*main) parent_thread = Thread.current Thread.start do diff --git a/lib/irb/ext/save-history.rb b/lib/irb/ext/save-history.rb deleted file mode 100644 index 2135184dba..0000000000 --- a/lib/irb/ext/save-history.rb +++ /dev/null @@ -1,130 +0,0 @@ -# frozen_string_literal: false -# save-history.rb - -# $Release Version: 0.9.6$ -# $Revision$ -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# -# -- -# -# -# - -module IRB - module HistorySavingAbility # :nodoc: - end - - class Context - def init_save_history# :nodoc: - unless (class<<@io;self;end).include?(HistorySavingAbility) - @io.extend(HistorySavingAbility) - end - end - - # A copy of the default <code>IRB.conf[:SAVE_HISTORY]</code> - def save_history - IRB.conf[:SAVE_HISTORY] - end - - remove_method(:save_history=) if method_defined?(:save_history=) - # Sets <code>IRB.conf[:SAVE_HISTORY]</code> to the given +val+ and calls - # #init_save_history with this context. - # - # Will store the number of +val+ entries of history in the #history_file - # - # Add the following to your +.irbrc+ to change the number of history - # entries stored to 1000: - # - # IRB.conf[:SAVE_HISTORY] = 1000 - def save_history=(val) - IRB.conf[:SAVE_HISTORY] = val - if val - main_context = IRB.conf[:MAIN_CONTEXT] - main_context = self unless main_context - main_context.init_save_history - end - end - - # A copy of the default <code>IRB.conf[:HISTORY_FILE]</code> - def history_file - IRB.conf[:HISTORY_FILE] - end - - # Set <code>IRB.conf[:HISTORY_FILE]</code> to the given +hist+. - def history_file=(hist) - IRB.conf[:HISTORY_FILE] = hist - end - end - - module HistorySavingAbility # :nodoc: - def HistorySavingAbility.extended(obj) - IRB.conf[:AT_EXIT].push proc{obj.save_history} - obj.load_history - obj - end - - def load_history - return unless self.class.const_defined?(:HISTORY) - history = self.class::HISTORY - if history_file = IRB.conf[:HISTORY_FILE] - history_file = File.expand_path(history_file) - end - history_file = IRB.rc_file("_history") unless history_file - if File.exist?(history_file) - open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f| - f.each { |l| - l = l.chomp - if self.class == ReidlineInputMethod and history.last&.end_with?("\\") - history.last.delete_suffix!("\\") - history.last << "\n" << l - else - history << l - end - } - end - @loaded_history_lines = history.size - @loaded_history_mtime = File.mtime(history_file) - end - end - - def save_history - return unless self.class.const_defined?(:HISTORY) - history = self.class::HISTORY - if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0 - if history_file = IRB.conf[:HISTORY_FILE] - history_file = File.expand_path(history_file) - end - history_file = IRB.rc_file("_history") unless history_file - - # Change the permission of a file that already exists[BUG #7694] - begin - if File.stat(history_file).mode & 066 != 0 - File.chmod(0600, history_file) - end - rescue Errno::ENOENT - rescue Errno::EPERM - return - rescue - raise - end - - if File.exist?(history_file) && - File.mtime(history_file) != @loaded_history_mtime - history = history[@loaded_history_lines..-1] if @loaded_history_lines - append_history = true - end - - File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f| - hist = history.map{ |l| l.split("\n").join("\\\n") } - unless append_history - begin - hist = hist.last(num) if hist.size > num and num > 0 - rescue RangeError # bignum too big to convert into `long' - # Do nothing because the bignum should be treated as inifinity - end - end - f.puts(hist) - end - end - end - end -end diff --git a/lib/irb/ext/tracer.rb b/lib/irb/ext/tracer.rb index 67ac4bb965..fd6daa88ae 100644 --- a/lib/irb/ext/tracer.rb +++ b/lib/irb/ext/tracer.rb @@ -1,84 +1,39 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # irb/lib/tracer.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# +# Loading the gem "tracer" will cause it to extend IRB commands with: +# https://github.com/ruby/tracer/blob/v0.2.2/lib/tracer/irb.rb begin require "tracer" rescue LoadError - $stderr.puts "Tracer extension of IRB is enabled but tracer gem doesn't found." - module IRB - TracerLoadError = true - class Context - def use_tracer=(opt) - # do nothing - end - end - end + $stderr.puts "Tracer extension of IRB is enabled but tracer gem wasn't found." return # This is about to disable loading below end module IRB + class CallTracer < ::CallTracer + IRB_DIR = File.expand_path('../..', __dir__) - # initialize tracing function - def IRB.initialize_tracer - Tracer.verbose = false - Tracer.add_filter { - |event, file, line, id, binding, *rests| - /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and - File::basename(file) != "irb.rb" - } - end - - class Context - # Whether Tracer is used when evaluating statements in this context. - # - # See +lib/tracer.rb+ for more information. - attr_reader :use_tracer - alias use_tracer? use_tracer - - # Sets whether or not to use the Tracer library when evaluating statements - # in this context. - # - # See +lib/tracer.rb+ for more information. - def use_tracer=(opt) - if opt - Tracer.set_get_line_procs(@irb_path) { - |line_no, *rests| - @io.line(line_no) - } - elsif !opt && @use_tracer - Tracer.off - end - @use_tracer=opt + def skip?(tp) + super || tp.path.match?(IRB_DIR) || tp.path.match?('<internal:prelude>') end end - class WorkSpace alias __evaluate__ evaluate # Evaluate the context of this workspace and use the Tracer library to # output the exact lines of code are being executed in chronological order. # - # See +lib/tracer.rb+ for more information. - def evaluate(context, statements, file = nil, line = nil) - if context.use_tracer? && file != nil && line != nil - Tracer.on - begin - __evaluate__(context, statements, file, line) - ensure - Tracer.off + # See https://github.com/ruby/tracer for more information. + def evaluate(statements, file = __FILE__, line = __LINE__) + if IRB.conf[:USE_TRACER] == true + CallTracer.new(colorize: Color.colorable?).start do + __evaluate__(statements, file, line) end else - __evaluate__(context, statements, file || __FILE__, line || __LINE__) + __evaluate__(statements, file, line) end end end - - IRB.initialize_tracer end diff --git a/lib/irb/ext/use-loader.rb b/lib/irb/ext/use-loader.rb index 1897bc89e0..c8a3ea1fe8 100644 --- a/lib/irb/ext/use-loader.rb +++ b/lib/irb/ext/use-loader.rb @@ -1,16 +1,10 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # use-loader.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# -require_relative "../cmd/load" +require_relative "../command/load" require_relative "loader" class Object @@ -23,12 +17,12 @@ module IRB remove_method :irb_load if method_defined?(:irb_load) # Loads the given file similarly to Kernel#load, see IrbLoader#irb_load def irb_load(*opts, &b) - ExtendCommand::Load.execute(irb_context, *opts, &b) + Command::Load.execute(irb_context, *opts, &b) end remove_method :irb_require if method_defined?(:irb_require) # Loads the given file similarly to Kernel#require def irb_require(*opts, &b) - ExtendCommand::Require.execute(irb_context, *opts, &b) + Command::Require.execute(irb_context, *opts, &b) end end @@ -55,14 +49,12 @@ module IRB if IRB.conf[:USE_LOADER] != opt IRB.conf[:USE_LOADER] = opt if opt - if !$".include?("irb/cmd/load") - end - (class<<@workspace.main;self;end).instance_eval { + (class<<workspace.main;self;end).instance_eval { alias_method :load, :irb_load alias_method :require, :irb_require } else - (class<<@workspace.main;self;end).instance_eval { + (class<<workspace.main;self;end).instance_eval { alias_method :load, :__original__load__IRB_use_loader__ alias_method :require, :__original__require__IRB_use_loader__ } diff --git a/lib/irb/ext/workspaces.rb b/lib/irb/ext/workspaces.rb index 730b58e64d..da09faa83e 100644 --- a/lib/irb/ext/workspaces.rb +++ b/lib/irb/ext/workspaces.rb @@ -1,32 +1,11 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # push-ws.rb - -# $Release Version: 0.9.6$ -# $Revision$ # by Keiju ISHITSUKA(keiju@ruby-lang.org) # -# -- -# -# -# module IRB # :nodoc: class Context - - # Size of the current WorkSpace stack - def irb_level - workspace_stack.size - end - - # WorkSpaces in the current stack - def workspaces - if defined? @workspaces - @workspaces - else - @workspaces = [] - end - end - # Creates a new workspace with the given object or binding, and appends it # onto the current #workspaces stack. # @@ -34,20 +13,15 @@ module IRB # :nodoc: # information. def push_workspace(*_main) if _main.empty? - if workspaces.empty? - print "No other workspace\n" - return nil + if @workspace_stack.size > 1 + # swap the top two workspaces + previous_workspace, current_workspace = @workspace_stack.pop(2) + @workspace_stack.push current_workspace, previous_workspace end - ws = workspaces.pop - workspaces.push @workspace - @workspace = ws - return workspaces - end - - workspaces.push @workspace - @workspace = WorkSpace.new(@workspace.binding, _main[0]) - if !(class<<main;ancestors;end).include?(ExtendCommandBundle) - main.extend ExtendCommandBundle + else + new_workspace = WorkSpace.new(workspace.binding, _main[0]) + @workspace_stack.push new_workspace + new_workspace.load_helper_methods_to_main end end @@ -56,11 +30,7 @@ module IRB # :nodoc: # # Also, see #push_workspace. def pop_workspace - if workspaces.empty? - print "workspace stack empty\n" - return - end - @workspace = workspaces.pop + @workspace_stack.pop if @workspace_stack.size > 1 end end end |