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
|
|
|
#
|
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
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
when 3 # binding in function on TOPLEVEL_BINDING(default)
|
|
|
|
@binding = eval("def irb_binding; private; binding; end; irb_binding",
|
|
|
|
TOPLEVEL_BINDING,
|
|
|
|
__FILE__,
|
|
|
|
__LINE__ - 3)
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
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]
|
|
|
|
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 13:38:21 -04:00
|
|
|
end
|
|
|
|
eval("_=nil", @binding)
|
|
|
|
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
|
|
|
|
* 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)
|
|
|
|
case IRB.conf[:CONTEXT_MODE]
|
|
|
|
when 0
|
2014-08-08 21:36:49 -04:00
|
|
|
return nil if bt =~ /\(irb_local_binding\)/
|
2001-04-30 13:38:21 -04:00
|
|
|
when 1
|
2014-08-08 21: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 13:38:21 -04:00
|
|
|
when 2
|
2014-08-08 21:36:49 -04:00
|
|
|
return nil if bt =~ /irb\/.*\.rb/
|
|
|
|
return nil if bt =~ /irb\.rb/
|
2001-04-30 13:38:21 -04:00
|
|
|
when 3
|
2014-08-08 21: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 13:38:21 -04:00
|
|
|
end
|
|
|
|
bt
|
|
|
|
end
|
|
|
|
|
2017-11-23 22:53:27 -05:00
|
|
|
def code_around_binding
|
|
|
|
file = @binding.eval('__FILE__')
|
|
|
|
pos = @binding.eval('__LINE__') - 1
|
|
|
|
return nil unless File.exist?(file)
|
|
|
|
|
|
|
|
if defined?(SCRIPT_LINES__) && SCRIPT_LINES__.key?(file)
|
|
|
|
lines = SCRIPT_LINES__[file].split(/^/)
|
|
|
|
else
|
|
|
|
lines = File.readlines(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
start_pos = [pos - 5, 0].max
|
|
|
|
end_pos = [pos + 5, lines.size - 1].min
|
|
|
|
|
|
|
|
body = (start_pos..end_pos).map do |current_pos|
|
|
|
|
lineno = "%#{end_pos.to_s.length}d" % (current_pos + 1)
|
|
|
|
" #{pos == current_pos ? '=>' : ' '} #{lineno}: #{lines[current_pos]}"
|
|
|
|
end.join
|
|
|
|
"\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}\n"
|
|
|
|
end
|
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
def IRB.delete_caller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|