2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
# irb/workspace-binding.rb -
|
2009-07-07 15:36:20 +04:00
|
|
|
# $Release Version: 0.9.6$
|
2001-04-30 21:38:21 +04:00
|
|
|
# $Revision$
|
2005-04-13 19:27:09 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
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
|
|
|
|
2014-08-09 05:36:49 +04:00
|
|
|
when 3 # binding in function on TOPLEVEL_BINDING(default)
|
2019-08-25 03:16:11 +03:00
|
|
|
@binding = eval("self.class.send(: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)
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
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]
|
|
|
|
IRB.conf[:__MAIN__] = @main
|
|
|
|
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
|
|
|
|
IRB.fail CantChangeBinding, @main.inspect
|
|
|
|
end
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
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
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Evaluate the given +statements+ within the context of this workspace.
|
2002-07-09 15:17:17 +04:00
|
|
|
def evaluate(context, statements, file = __FILE__, line = __LINE__)
|
|
|
|
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
|
2001-04-30 21:38:21 +04:00
|
|
|
def filter_backtrace(bt)
|
|
|
|
case IRB.conf[:CONTEXT_MODE]
|
|
|
|
when 0
|
2014-08-09 05:36:49 +04:00
|
|
|
return nil if bt =~ /\(irb_local_binding\)/
|
2001-04-30 21:38:21 +04:00
|
|
|
when 1
|
2014-08-09 05:36:49 +04:00
|
|
|
if(bt =~ %r!/tmp/irb-binding! or
|
|
|
|
bt =~ %r!irb/.*\.rb! or
|
|
|
|
bt =~ /irb\.rb/)
|
|
|
|
return nil
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
when 2
|
2014-08-09 05:36:49 +04:00
|
|
|
return nil if bt =~ /irb\/.*\.rb/
|
|
|
|
return nil if bt =~ /irb\.rb/
|
2001-04-30 21:38:21 +04:00
|
|
|
when 3
|
2014-08-09 05:36:49 +04:00
|
|
|
return nil if bt =~ /irb\/.*\.rb/
|
|
|
|
return nil if bt =~ /irb\.rb/
|
|
|
|
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
|
2019-04-26 12:28:54 +03:00
|
|
|
if @binding.respond_to?(:source_location)
|
|
|
|
file, pos = @binding.source_location
|
|
|
|
else
|
|
|
|
file, pos = @binding.eval('[__FILE__, __LINE__]')
|
|
|
|
end
|
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
|
|
|
|
|
|
|
# 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-21 03:14:08 +03:00
|
|
|
lines = Color.colorize_code(code).lines
|
|
|
|
else
|
|
|
|
lines = code.lines
|
|
|
|
end
|
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
|
|
|
|
|
2019-05-30 09:49:48 +03:00
|
|
|
if use_colorize
|
2019-05-15 10:36:33 +03: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-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("")
|
2019-04-25 15:16:21 +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
|
|
|
def IRB.delete_caller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|