1#
2#   irb/lib/tracer.rb -
3#   	$Release Version: 0.9.6$
4#   	$Revision: 38515 $
5#   	by Keiju ISHITSUKA(keiju@ruby-lang.org)
6#
7# --
8#
9#
10#
11require "tracer"
12
13module IRB
14
15  # initialize tracing function
16  def IRB.initialize_tracer
17    Tracer.verbose = false
18    Tracer.add_filter {
19      |event, file, line, id, binding, *rests|
20      /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and
21	File::basename(file) != "irb.rb"
22    }
23  end
24
25  class Context
26    # Whether Tracer is used when evaluating statements in this context.
27    #
28    # See +lib/tracer.rb+ for more information.
29    attr_reader :use_tracer
30    alias use_tracer? use_tracer
31
32    # Sets whether or not to use the Tracer library when evaluating statements
33    # in this context.
34    #
35    # See +lib/tracer.rb+ for more information.
36    def use_tracer=(opt)
37      if opt
38	Tracer.set_get_line_procs(@irb_path) {
39	  |line_no, *rests|
40	  @io.line(line_no)
41	}
42      elsif !opt && @use_tracer
43	Tracer.off
44      end
45      @use_tracer=opt
46    end
47  end
48
49  class WorkSpace
50    alias __evaluate__ evaluate
51    # Evaluate the context of this workspace and use the Tracer library to
52    # output the exact lines of code are being executed in chronological order.
53    #
54    # See +lib/tracer.rb+ for more information.
55    def evaluate(context, statements, file = nil, line = nil)
56      if context.use_tracer? && file != nil && line != nil
57	Tracer.on
58	begin
59	  __evaluate__(context, statements, file, line)
60	ensure
61	  Tracer.off
62	end
63      else
64	__evaluate__(context, statements, file || __FILE__, line || __LINE__)
65      end
66    end
67  end
68
69  IRB.initialize_tracer
70end
71
72