2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2001-04-30 13:38:21 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# irb/workspace-binding.rb -
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2001-04-30 13:38:21 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 13:38:21 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-04-30 13:38:21 -04:00
|
|
|
#
|
2019-08-25 02:18:23 -04:00
|
|
|
|
|
|
|
require "delegate"
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
module IRB # :nodoc:
|
2001-04-30 13:38:21 -04:00
|
|
|
class WorkSpace
|
2012-12-13 00:22:30 -05:00
|
|
|
# Creates a new workspace.
|
|
|
|
#
|
|
|
|
# set self to main if specified, otherwise
|
2002-07-09 07:17:17 -04:00
|
|
|
# inherit main from TOPLEVEL_BINDING.
|
2001-04-30 13:38:21 -04:00
|
|
|
def initialize(*main)
|
2002-07-09 07:17:17 -04:00
|
|
|
if main[0].kind_of?(Binding)
|
2014-08-08 21:36:49 -04:00
|
|
|
@binding = main.shift
|
2002-07-09 07:17:17 -04:00
|
|
|
elsif IRB.conf[:SINGLE_IRB]
|
2014-08-08 21:36:49 -04:00
|
|
|
@binding = TOPLEVEL_BINDING
|
2001-04-30 13:38:21 -04:00
|
|
|
else
|
2014-08-08 21: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 13:38:21 -04:00
|
|
|
EOF
|
2014-08-08 21:36:49 -04:00
|
|
|
f.close
|
|
|
|
load f.path
|
|
|
|
@binding = $binding
|
2001-04-30 13:38:21 -04:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
when 2 # binding in loaded file(thread use)
|
|
|
|
unless defined? BINDING_QUEUE
|
2016-08-30 02:22:30 -04:00
|
|
|
IRB.const_set(:BINDING_QUEUE, Thread::SizedQueue.new(1))
|
2014-08-08 21: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 13:10:46 -05:00
|
|
|
|
2020-11-20 16:58:02 -05:00
|
|
|
when 3 # binding in function on TOPLEVEL_BINDING
|
2020-10-27 00:42:52 -04:00
|
|
|
@binding = eval("self.class.remove_method(:irb_binding) if defined?(irb_binding); private; def irb_binding; binding; end; irb_binding",
|
2014-08-08 21:36:49 -04:00
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
__FILE__,
|
|
|
|
__LINE__ - 3)
|
2020-11-20 16:58:02 -05:00
|
|
|
when 4 # binding is a copy of TOPLEVEL_BINDING (default)
|
|
|
|
@binding = TOPLEVEL_BINDING.dup
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
2019-08-25 02:18:23 -04:00
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
if main.empty?
|
2014-08-08 21:36:49 -04:00
|
|
|
@main = eval("self", @binding)
|
2001-04-30 13:38:21 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@main = main[0]
|
2019-08-25 02:18:23 -04:00
|
|
|
end
|
|
|
|
IRB.conf[:__MAIN__] = @main
|
|
|
|
|
|
|
|
unless main.empty?
|
2014-08-08 21: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 15:38:09 -05:00
|
|
|
fail CantChangeBinding, @main.inspect
|
2014-08-08 21:36:49 -04:00
|
|
|
end
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
2019-08-25 02:18:23 -04: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(:exit) do |*a, &b|
|
|
|
|
# Do nothing, will be overridden
|
|
|
|
end
|
|
|
|
define_method(:binding, Kernel.instance_method(:binding))
|
|
|
|
define_method(:local_variables, Kernel.instance_method(:local_variables))
|
|
|
|
end
|
|
|
|
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location)
|
|
|
|
end
|
|
|
|
|
2018-04-14 08:49:30 -04:00
|
|
|
@binding.local_variable_set(:_, nil)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# The Binding of this workspace
|
2001-04-30 13:38:21 -04:00
|
|
|
attr_reader :binding
|
2012-12-21 00:45:50 -05:00
|
|
|
# The top-level workspace of this context, also available as
|
|
|
|
# <code>IRB.conf[:__MAIN__]</code>
|
2001-04-30 13:38:21 -04:00
|
|
|
attr_reader :main
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Evaluate the given +statements+ within the context of this workspace.
|
2002-07-09 07:17:17 -04:00
|
|
|
def evaluate(context, statements, file = __FILE__, line = __LINE__)
|
|
|
|
eval(statements, @binding, file, line)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-04-14 08:49:30 -04: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-18 19:19:47 -04:00
|
|
|
# error message manipulator
|
2001-04-30 13:38:21 -04:00
|
|
|
def filter_backtrace(bt)
|
2019-11-21 09:10:30 -05:00
|
|
|
return nil if bt =~ /\/irb\/.*\.rb/
|
|
|
|
return nil if bt =~ /\/irb\.rb/
|
2021-01-07 22:56:36 -05:00
|
|
|
return nil if bt =~ /tool\/lib\/.*\.rb|runner\.rb/ # for tests in Ruby repository
|
2001-04-30 13:38:21 -04:00
|
|
|
case IRB.conf[:CONTEXT_MODE]
|
|
|
|
when 1
|
2019-10-29 19:49:03 -04:00
|
|
|
return nil if bt =~ %r!/tmp/irb-binding!
|
2001-04-30 13:38:21 -04:00
|
|
|
when 3
|
2014-08-08 21:36:49 -04:00
|
|
|
bt = bt.sub(/:\s*in `irb_binding'/, '')
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
bt
|
|
|
|
end
|
|
|
|
|
2017-11-23 22:53:27 -05:00
|
|
|
def code_around_binding
|
2019-04-26 05:28:54 -04:00
|
|
|
if @binding.respond_to?(:source_location)
|
|
|
|
file, pos = @binding.source_location
|
|
|
|
else
|
|
|
|
file, pos = @binding.eval('[__FILE__, __LINE__]')
|
|
|
|
end
|
2017-11-23 22:53:27 -05:00
|
|
|
|
2019-04-25 08:16:21 -04:00
|
|
|
if defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file]
|
|
|
|
code = ::SCRIPT_LINES__[file].join('')
|
|
|
|
else
|
2017-11-24 06:00:10 -05:00
|
|
|
begin
|
2019-04-25 08:16:21 -04:00
|
|
|
code = File.read(file)
|
2017-11-24 06:00:10 -05:00
|
|
|
rescue SystemCallError
|
|
|
|
return
|
|
|
|
end
|
2017-11-23 22:53:27 -05:00
|
|
|
end
|
2019-05-30 02:49:48 -04:00
|
|
|
|
|
|
|
# NOT using #use_colorize? of IRB.conf[:MAIN_CONTEXT] because this method may be called before IRB::Irb#run
|
|
|
|
use_colorize = IRB.conf.fetch(:USE_COLORIZE, true)
|
|
|
|
if use_colorize
|
2019-05-20 20:14:08 -04:00
|
|
|
lines = Color.colorize_code(code).lines
|
|
|
|
else
|
|
|
|
lines = code.lines
|
|
|
|
end
|
2017-11-24 00:00:56 -05:00
|
|
|
pos -= 1
|
2017-11-23 22:53:27 -05:00
|
|
|
|
|
|
|
start_pos = [pos - 5, 0].max
|
|
|
|
end_pos = [pos + 5, lines.size - 1].min
|
|
|
|
|
2019-05-30 02:49:48 -04:00
|
|
|
if use_colorize
|
2019-05-15 03:36:33 -04:00
|
|
|
fmt = " %2s #{Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD])}: %s"
|
|
|
|
else
|
|
|
|
fmt = " %2s %#{end_pos.to_s.length}d: %s"
|
|
|
|
end
|
2017-11-23 22:53:27 -05:00
|
|
|
body = (start_pos..end_pos).map do |current_pos|
|
2017-11-24 00:00:56 -05:00
|
|
|
sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos])
|
|
|
|
end.join("")
|
2021-02-07 21:08:57 -05:00
|
|
|
"\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear if use_colorize}\n"
|
2017-11-23 22:53:27 -05:00
|
|
|
end
|
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
def IRB.delete_caller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|