2024-05-29 01:54:39 +03:00
|
|
|
# frozen_string_literal: true
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# irb/workspace-binding.rb -
|
2005-04-13 19:27:09 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2019-08-25 09:18:23 +03:00
|
|
|
|
|
|
|
require "delegate"
|
|
|
|
|
2024-05-29 01:54:39 +03:00
|
|
|
require_relative "helper_method"
|
|
|
|
|
2021-09-10 00:08:56 +03:00
|
|
|
IRB::TOPLEVEL_BINDING = binding
|
2012-12-13 09:22:30 +04:00
|
|
|
module IRB # :nodoc:
|
2001-04-30 21:38:21 +04:00
|
|
|
class WorkSpace
|
2012-12-13 09:22:30 +04:00
|
|
|
# Creates a new workspace.
|
|
|
|
#
|
|
|
|
# set self to main if specified, otherwise
|
2002-07-09 15:17:17 +04:00
|
|
|
# inherit main from TOPLEVEL_BINDING.
|
2001-04-30 21:38:21 +04:00
|
|
|
def initialize(*main)
|
2002-07-09 15:17:17 +04:00
|
|
|
if main[0].kind_of?(Binding)
|
2014-08-09 05:36:49 +04:00
|
|
|
@binding = main.shift
|
2002-07-09 15:17:17 +04:00
|
|
|
elsif IRB.conf[:SINGLE_IRB]
|
2014-08-09 05:36:49 +04:00
|
|
|
@binding = TOPLEVEL_BINDING
|
2001-04-30 21:38:21 +04:00
|
|
|
else
|
2014-08-09 05:36:49 +04:00
|
|
|
case IRB.conf[:CONTEXT_MODE]
|
|
|
|
when 0 # binding in proc on TOPLEVEL_BINDING
|
|
|
|
@binding = eval("proc{binding}.call",
|
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
__FILE__,
|
|
|
|
__LINE__)
|
|
|
|
when 1 # binding in loaded file
|
|
|
|
require "tempfile"
|
|
|
|
f = Tempfile.open("irb-binding")
|
|
|
|
f.print <<EOF
|
|
|
|
$binding = binding
|
2001-04-30 21:38:21 +04:00
|
|
|
EOF
|
2014-08-09 05:36:49 +04:00
|
|
|
f.close
|
|
|
|
load f.path
|
|
|
|
@binding = $binding
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2014-08-09 05:36:49 +04:00
|
|
|
when 2 # binding in loaded file(thread use)
|
|
|
|
unless defined? BINDING_QUEUE
|
2016-08-30 09:22:30 +03:00
|
|
|
IRB.const_set(:BINDING_QUEUE, Thread::SizedQueue.new(1))
|
2014-08-09 05:36:49 +04:00
|
|
|
Thread.abort_on_exception = true
|
|
|
|
Thread.start do
|
|
|
|
eval "require \"irb/ws-for-case-2\"", TOPLEVEL_BINDING, __FILE__, __LINE__
|
|
|
|
end
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
@binding = BINDING_QUEUE.pop
|
2012-12-25 22:10:46 +04:00
|
|
|
|
2020-11-21 00:58:02 +03:00
|
|
|
when 3 # binding in function on TOPLEVEL_BINDING
|
2020-10-27 07:42:52 +03:00
|
|
|
@binding = eval("self.class.remove_method(:irb_binding) if defined?(irb_binding); private; def irb_binding; binding; end; irb_binding",
|
2014-08-09 05:36:49 +04:00
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
__FILE__,
|
|
|
|
__LINE__ - 3)
|
2020-11-21 00:58:02 +03:00
|
|
|
when 4 # binding is a copy of TOPLEVEL_BINDING (default)
|
2021-09-10 00:08:56 +03:00
|
|
|
# Note that this will typically be IRB::TOPLEVEL_BINDING
|
2021-03-18 16:44:42 +03:00
|
|
|
# This is to avoid RubyGems' local variables (see issue #17623)
|
2020-11-21 00:58:02 +03:00
|
|
|
@binding = TOPLEVEL_BINDING.dup
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
2019-08-25 09:18:23 +03:00
|
|
|
|
2001-04-30 21:38:21 +04:00
|
|
|
if main.empty?
|
2014-08-09 05:36:49 +04:00
|
|
|
@main = eval("self", @binding)
|
2001-04-30 21:38:21 +04:00
|
|
|
else
|
2014-08-09 05:36:49 +04:00
|
|
|
@main = main[0]
|
2019-08-25 09:18:23 +03:00
|
|
|
end
|
|
|
|
IRB.conf[:__MAIN__] = @main
|
|
|
|
|
|
|
|
unless main.empty?
|
2014-08-09 05:36:49 +04:00
|
|
|
case @main
|
|
|
|
when Module
|
|
|
|
@binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
|
|
|
rescue TypeError
|
2019-11-24 23:38:09 +03:00
|
|
|
fail CantChangeBinding, @main.inspect
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
2019-08-25 09:18:23 +03:00
|
|
|
|
|
|
|
case @main
|
|
|
|
when Object
|
|
|
|
use_delegator = @main.frozen?
|
|
|
|
else
|
|
|
|
use_delegator = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if use_delegator
|
|
|
|
@main = SimpleDelegator.new(@main)
|
|
|
|
IRB.conf[:__MAIN__] = @main
|
|
|
|
@main.singleton_class.class_eval do
|
|
|
|
private
|
|
|
|
define_method(:binding, Kernel.instance_method(:binding))
|
|
|
|
define_method(:local_variables, Kernel.instance_method(:local_variables))
|
2024-05-29 01:54:39 +03:00
|
|
|
# Define empty method to avoid delegator warning, will be overridden.
|
|
|
|
define_method(:exit) {|*a, &b| }
|
|
|
|
define_method(:exit!) {|*a, &b| }
|
2019-08-25 09:18:23 +03:00
|
|
|
end
|
|
|
|
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location)
|
|
|
|
end
|
|
|
|
|
2018-04-14 15:49:30 +03:00
|
|
|
@binding.local_variable_set(:_, nil)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# The Binding of this workspace
|
2001-04-30 21:38:21 +04:00
|
|
|
attr_reader :binding
|
2012-12-21 09:45:50 +04:00
|
|
|
# The top-level workspace of this context, also available as
|
|
|
|
# <code>IRB.conf[:__MAIN__]</code>
|
2001-04-30 21:38:21 +04:00
|
|
|
attr_reader :main
|
|
|
|
|
2024-05-29 01:54:39 +03:00
|
|
|
def load_helper_methods_to_main
|
|
|
|
ancestors = class<<main;ancestors;end
|
|
|
|
main.extend ExtendCommandBundle if !ancestors.include?(ExtendCommandBundle)
|
|
|
|
main.extend HelpersContainer if !ancestors.include?(HelpersContainer)
|
2023-08-13 21:30:30 +03:00
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Evaluate the given +statements+ within the context of this workspace.
|
2023-02-18 14:34:23 +03:00
|
|
|
def evaluate(statements, file = __FILE__, line = __LINE__)
|
2002-07-09 15:17:17 +04:00
|
|
|
eval(statements, @binding, file, line)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2018-04-14 15:49:30 +03:00
|
|
|
def local_variable_set(name, value)
|
|
|
|
@binding.local_variable_set(name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_variable_get(name)
|
|
|
|
@binding.local_variable_get(name)
|
|
|
|
end
|
|
|
|
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-19 03:19:47 +04:00
|
|
|
# error message manipulator
|
2023-02-27 18:23:26 +03:00
|
|
|
# WARN: Rails patches this method to filter its own backtrace. Be cautious when changing it.
|
|
|
|
# See: https://github.com/rails/rails/blob/main/railties/lib/rails/commands/console/console_command.rb#L8:~:text=def,filter_backtrace
|
2001-04-30 21:38:21 +04:00
|
|
|
def filter_backtrace(bt)
|
2019-11-21 17:10:30 +03:00
|
|
|
return nil if bt =~ /\/irb\/.*\.rb/
|
|
|
|
return nil if bt =~ /\/irb\.rb/
|
2021-01-08 06:56:36 +03:00
|
|
|
return nil if bt =~ /tool\/lib\/.*\.rb|runner\.rb/ # for tests in Ruby repository
|
2001-04-30 21:38:21 +04:00
|
|
|
case IRB.conf[:CONTEXT_MODE]
|
|
|
|
when 1
|
2019-10-30 02:49:03 +03:00
|
|
|
return nil if bt =~ %r!/tmp/irb-binding!
|
2001-04-30 21:38:21 +04:00
|
|
|
when 3
|
2014-08-09 05:36:49 +04:00
|
|
|
bt = bt.sub(/:\s*in `irb_binding'/, '')
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
bt
|
|
|
|
end
|
|
|
|
|
2017-11-24 06:53:27 +03:00
|
|
|
def code_around_binding
|
2023-01-04 12:35:52 +03:00
|
|
|
file, pos = @binding.source_location
|
2017-11-24 06:53:27 +03:00
|
|
|
|
2019-04-25 15:16:21 +03:00
|
|
|
if defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file]
|
|
|
|
code = ::SCRIPT_LINES__[file].join('')
|
|
|
|
else
|
2017-11-24 14:00:10 +03:00
|
|
|
begin
|
2019-04-25 15:16:21 +03:00
|
|
|
code = File.read(file)
|
2017-11-24 14:00:10 +03:00
|
|
|
rescue SystemCallError
|
|
|
|
return
|
|
|
|
end
|
2017-11-24 06:53:27 +03:00
|
|
|
end
|
2019-05-30 09:49:48 +03:00
|
|
|
|
2022-06-28 16:30:36 +03:00
|
|
|
lines = Color.colorize_code(code).lines
|
2017-11-24 08:00:56 +03:00
|
|
|
pos -= 1
|
2017-11-24 06:53:27 +03:00
|
|
|
|
|
|
|
start_pos = [pos - 5, 0].max
|
|
|
|
end_pos = [pos + 5, lines.size - 1].min
|
|
|
|
|
2022-06-28 16:30:36 +03:00
|
|
|
line_number_fmt = Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD])
|
|
|
|
fmt = " %2s #{line_number_fmt}: %s"
|
|
|
|
|
2017-11-24 06:53:27 +03:00
|
|
|
body = (start_pos..end_pos).map do |current_pos|
|
2017-11-24 08:00:56 +03:00
|
|
|
sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos])
|
|
|
|
end.join("")
|
2022-06-28 16:30:36 +03:00
|
|
|
|
|
|
|
"\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear}\n"
|
2017-11-24 06:53:27 +03:00
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
2024-05-29 01:54:39 +03:00
|
|
|
|
|
|
|
module HelpersContainer
|
|
|
|
def self.install_helper_methods
|
|
|
|
HelperMethod.helper_methods.each do |name, helper_method_class|
|
|
|
|
define_method name do |*args, **opts, &block|
|
|
|
|
helper_method_class.instance.execute(*args, **opts, &block)
|
|
|
|
end unless method_defined?(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
install_helper_methods
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|