1 | #
|
---|
2 | # irb/workspace-binding.rb -
|
---|
3 | # $Release Version: 0.9.5$
|
---|
4 | # $Revision: 11708 $
|
---|
5 | # $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
---|
6 | # by Keiju ISHITSUKA([email protected])
|
---|
7 | #
|
---|
8 | # --
|
---|
9 | #
|
---|
10 | #
|
---|
11 | #
|
---|
12 | module IRB
|
---|
13 | class WorkSpace
|
---|
14 | # create new workspace. set self to main if specified, otherwise
|
---|
15 | # inherit main from TOPLEVEL_BINDING.
|
---|
16 | def initialize(*main)
|
---|
17 | if main[0].kind_of?(Binding)
|
---|
18 | @binding = main.shift
|
---|
19 | elsif IRB.conf[:SINGLE_IRB]
|
---|
20 | @binding = TOPLEVEL_BINDING
|
---|
21 | else
|
---|
22 | case IRB.conf[:CONTEXT_MODE]
|
---|
23 | when 0 # binding in proc on TOPLEVEL_BINDING
|
---|
24 | @binding = eval("proc{binding}.call",
|
---|
25 | TOPLEVEL_BINDING,
|
---|
26 | __FILE__,
|
---|
27 | __LINE__)
|
---|
28 | when 1 # binding in loaded file
|
---|
29 | require "tempfile"
|
---|
30 | f = Tempfile.open("irb-binding")
|
---|
31 | f.print <<EOF
|
---|
32 | $binding = binding
|
---|
33 | EOF
|
---|
34 | f.close
|
---|
35 | load f.path
|
---|
36 | @binding = $binding
|
---|
37 |
|
---|
38 | when 2 # binding in loaded file(thread use)
|
---|
39 | unless defined? BINDING_QUEUE
|
---|
40 | require "thread"
|
---|
41 |
|
---|
42 | IRB.const_set("BINDING_QUEUE", SizedQueue.new(1))
|
---|
43 | Thread.abort_on_exception = true
|
---|
44 | Thread.start do
|
---|
45 | eval "require \"irb/ws-for-case-2\"", TOPLEVEL_BINDING, __FILE__, __LINE__
|
---|
46 | end
|
---|
47 | Thread.pass
|
---|
48 | end
|
---|
49 | @binding = BINDING_QUEUE.pop
|
---|
50 |
|
---|
51 | when 3 # binging in function on TOPLEVEL_BINDING(default)
|
---|
52 | @binding = eval("def irb_binding; binding; end; irb_binding",
|
---|
53 | TOPLEVEL_BINDING,
|
---|
54 | __FILE__,
|
---|
55 | __LINE__ - 3)
|
---|
56 | end
|
---|
57 | end
|
---|
58 | if main.empty?
|
---|
59 | @main = eval("self", @binding)
|
---|
60 | else
|
---|
61 | @main = main[0]
|
---|
62 | IRB.conf[:__MAIN__] = @main
|
---|
63 | case @main
|
---|
64 | when Module
|
---|
65 | @binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
---|
66 | else
|
---|
67 | begin
|
---|
68 | @binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
|
---|
69 | rescue TypeError
|
---|
70 | IRB.fail CantChangeBinding, @main.inspect
|
---|
71 | end
|
---|
72 | end
|
---|
73 | end
|
---|
74 | eval("_=nil", @binding)
|
---|
75 | end
|
---|
76 |
|
---|
77 | attr_reader :binding
|
---|
78 | attr_reader :main
|
---|
79 |
|
---|
80 | def evaluate(context, statements, file = __FILE__, line = __LINE__)
|
---|
81 | eval(statements, @binding, file, line)
|
---|
82 | end
|
---|
83 |
|
---|
84 | # error message manipulator
|
---|
85 | def filter_backtrace(bt)
|
---|
86 | case IRB.conf[:CONTEXT_MODE]
|
---|
87 | when 0
|
---|
88 | return nil if bt =~ /\(irb_local_binding\)/
|
---|
89 | when 1
|
---|
90 | if(bt =~ %r!/tmp/irb-binding! or
|
---|
91 | bt =~ %r!irb/.*\.rb! or
|
---|
92 | bt =~ /irb\.rb/)
|
---|
93 | return nil
|
---|
94 | end
|
---|
95 | when 2
|
---|
96 | return nil if bt =~ /irb\/.*\.rb/
|
---|
97 | when 3
|
---|
98 | return nil if bt =~ /irb\/.*\.rb/
|
---|
99 | bt.sub!(/:\s*in `irb_binding'/){""}
|
---|
100 | end
|
---|
101 | bt
|
---|
102 | end
|
---|
103 |
|
---|
104 | def IRB.delete_caller
|
---|
105 | end
|
---|
106 | end
|
---|
107 | end
|
---|