Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Some final cleaning of the hacking spree, before speccing

  • Loading branch information...
commit abfd9dfebcc5649b225f4fc2b9676e36ada7bcd7 1 parent d3513a6
Eloy Durán authored July 14, 2010
4  bin/dietrb
@@ -32,7 +32,9 @@ end
32 32
 IRB.formatter.filter_from_backtrace << /^#{__FILE__}/
33 33
 
34 34
 if ARGV.empty?
35  
-  require 'irb/driver/readline'
  35
+  # require 'irb/driver/readline'
  36
+  # require 'irb/driver/tty'
  37
+  require 'irb/driver/socket'
36 38
 else
37 39
   path = ARGV.shift
38 40
   require 'irb/driver/file'
14  lib/irb/context.rb
@@ -15,7 +15,7 @@ def processors
15 15
     end
16 16
     
17 17
     attr_reader :object, :binding, :line, :source, :processors
18  
-    attr_accessor :driver, :formatter
  18
+    attr_accessor :formatter
19 19
     
20 20
     def initialize(object, explicit_binding = nil)
21 21
       @object  = object
@@ -34,11 +34,11 @@ def __evaluate__(source, file = __FILE__, line = __LINE__)
34 34
     def evaluate(source)
35 35
       result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
36 36
       store_result(result)
37  
-      output.puts(formatter.result(result))
  37
+      puts(formatter.result(result))
38 38
       result
39 39
     rescue Exception => e
40 40
       store_exception(e)
41  
-      output.puts(formatter.exception(e))
  41
+      puts(formatter.exception(e))
42 42
     end
43 43
     
44 44
     # Returns whether or not the user wants to continue the current runloop.
@@ -61,7 +61,7 @@ def process_line(line)
61 61
       return false if @source.terminate?
62 62
       
63 63
       if @source.syntax_error?
64  
-        output.puts(formatter.syntax_error(@line, @source.syntax_error))
  64
+        puts(formatter.syntax_error(@line, @source.syntax_error))
65 65
         @source.pop
66 66
       elsif @source.code_block?
67 67
         evaluate(@source)
@@ -77,14 +77,10 @@ def prompt
77 77
     end
78 78
     
79 79
     def input_line(line)
80  
-      output.puts(formatter.prompt(self) + line)
  80
+      puts(formatter.prompt(self) + line)
81 81
       process_line(line)
82 82
     end
83 83
     
84  
-    def output
85  
-      @driver || $stdout
86  
-    end
87  
-    
88 84
     def formatter
89 85
       @formatter ||= IRB.formatter
90 86
     end
60  lib/irb/driver.rb
... ...
@@ -0,0 +1,60 @@
  1
+module IRB
  2
+  class << self
  3
+    attr_accessor :driver_class
  4
+    
  5
+    def driver=(driver)
  6
+      Thread.current[:irb_driver] = driver
  7
+    end
  8
+    
  9
+    def driver
  10
+      current_thread = Thread.current
  11
+      current_thread[:irb_driver] ||= begin
  12
+        if group = current_thread.group
  13
+          group.list.each do |thread|
  14
+            break(driver) if driver = thread[:irb_driver]
  15
+          end
  16
+        end
  17
+      end
  18
+    end
  19
+  end
  20
+  
  21
+  module Driver
  22
+    class OutputRedirector
  23
+      def self.target
  24
+        if driver = IRB.driver
  25
+          driver.output
  26
+        else
  27
+          $stderr
  28
+        end
  29
+      end
  30
+      
  31
+      # A standard output object has only one mandatory method: write.
  32
+      # It returns the number of characters written
  33
+      def write(object)
  34
+        string = object.respond_to?(:to_str) ? object : object.to_s
  35
+        send_to_target :write, string
  36
+        string.length
  37
+      end
  38
+      
  39
+      # if puts is not there, Ruby will automatically use the write
  40
+      # method when calling Kernel#puts, but defining it has 2 advantages:
  41
+      # - if puts is not defined, you cannot of course use $stdout.puts directly
  42
+      # - (objc) when Ruby emulates puts, it calls write twice
  43
+      #   (once for the string and once for the carriage return)
  44
+      #   but here we send the calls to another thread so it's nice
  45
+      #   to be able to save up one (slow) interthread call
  46
+      def puts(*args)
  47
+        send_to_target :puts, *args
  48
+        nil
  49
+      end
  50
+      
  51
+      # Override this if for your situation you need to dispatch from a thread
  52
+      # in a special manner.
  53
+      #
  54
+      # TODO: for macruby send to main thread
  55
+      def send_to_target(method, *args)
  56
+        self.class.target.__send__(method, *args)
  57
+      end
  58
+    end
  59
+  end
  60
+end
12  lib/irb/driver/socket.rb
@@ -3,16 +3,6 @@
3 3
 
4 4
 module IRB
5 5
   module Driver
6  
-    class SocketTTY < TTY
7  
-      # We don't want to put the real standard output object on the
8  
-      # OutputRedirector target stack.
9  
-      def assign_output_redirector!
10  
-        before  = $stdout
11  
-        $stdout = IRB::Driver::OutputRedirector.new
12  
-        before
13  
-      end
14  
-    end
15  
-    
16 6
     class Socket
17 7
       # Initializes with the object and binding that each new connection will
18 8
       # get as Context. The binding is shared, so local variables will stay
@@ -34,7 +24,7 @@ def run
34 24
           connection = @server.accept
35 25
           Thread.new do
36 26
             # assign driver with connection to current thread and start runloop
37  
-            IRB.driver = SocketTTY.new(connection, connection)
  27
+            IRB.driver = TTY.new(connection, connection)
38 28
             irb(@object, @binding)
39 29
             connection.close
40 30
           end
87  lib/irb/driver/tty.rb
... ...
@@ -1,80 +1,13 @@
  1
+require 'irb/driver'
  2
+
1 3
 module IRB
2  
-  class << self
3  
-    attr_accessor :driver_class
4  
-    
5  
-    def driver=(driver)
6  
-      Thread.current[:irb_driver] = driver
7  
-    end
8  
-    
9  
-    def driver
10  
-      current_thread = Thread.current
11  
-      current_thread[:irb_driver] ||= begin
12  
-        driver = nil
13  
-        if group = current_thread.group
14  
-          group.list.each do |thread|
15  
-            break if driver = thread[:irb_driver]
16  
-          end
17  
-        end
18  
-        driver || driver_class.new
19  
-      end
20  
-    end
21  
-  end
22  
-  
23 4
   module Driver
24  
-    class OutputRedirector
25  
-      # The output object for the current thread.
26  
-      def self.target=(output)
27  
-        Thread.current[:irb_stdout_target] = output
28  
-      end
29  
-      
30  
-      # TODO cache, or not to cache?
31  
-      def self.target
32  
-        current_thread = Thread.current
33  
-        if target = current_thread[:irb_stdout_target]
34  
-        elsif group = current_thread.group
35  
-          group.list.each do |thread|
36  
-            break if target = thread[:irb_stdout_target]
37  
-          end
38  
-        end
39  
-        target || $stderr
40  
-      end
41  
-      
42  
-      # A standard output object has only one mandatory method: write.
43  
-      # It returns the number of characters written
44  
-      def write(object)
45  
-        string = object.respond_to?(:to_str) ? object : object.to_s
46  
-        send_to_target :write, string
47  
-        string.length
48  
-      end
49  
-      
50  
-      # if puts is not there, Ruby will automatically use the write
51  
-      # method when calling Kernel#puts, but defining it has 2 advantages:
52  
-      # - if puts is not defined, you cannot of course use $stdout.puts directly
53  
-      # - (objc) when Ruby emulates puts, it calls write twice
54  
-      #   (once for the string and once for the carriage return)
55  
-      #   but here we send the calls to another thread so it's nice
56  
-      #   to be able to save up one (slow) interthread call
57  
-      def puts(*args)
58  
-        send_to_target :puts, *args
59  
-        nil
60  
-      end
61  
-      
62  
-      # Override this if for your situation you need to dispatch from a thread
63  
-      # in a special manner.
64  
-      #
65  
-      # TODO: for macruby send to main thread
66  
-      def send_to_target(method, *args)
67  
-        self.class.target.__send__(method, *args)
68  
-      end
69  
-    end
70  
-    
71 5
     class TTY
72  
-      attr_accessor :current_context
  6
+      attr_reader :input, :output
73 7
       
74 8
       def initialize(input = $stdin, output = $stdout)
75 9
         @input  = input
76 10
         @output = output
77  
-        OutputRedirector.target = output
78 11
         
79 12
         @thread_group = ThreadGroup.new
80 13
         @thread_group.add(Thread.current)
@@ -104,7 +37,6 @@ def print(*args)
104 37
       
105 38
       def run(context)
106 39
         ensure_output_redirector do
107  
-          context.driver = self
108 40
           while line = consume(context)
109 41
             continue = context.process_line(line)
110 42
             break unless continue
@@ -115,27 +47,22 @@ def run(context)
115 47
       # Ensure that the standard output object is a OutputRedirector. If it's
116 48
       # already a OutputRedirector, do nothing.
117 49
       def ensure_output_redirector
118  
-        before = assign_output_redirector! unless $stdout.is_a?(IRB::Driver::OutputRedirector)
  50
+        unless $stdout.is_a?(IRB::Driver::OutputRedirector)
  51
+          before, $stdout = $stdout, IRB::Driver::OutputRedirector.new
  52
+        end
119 53
         yield
120 54
       ensure
121 55
         $stdout = before if before
122 56
       end
123  
-      
124  
-      def assign_output_redirector!
125  
-        before  = IRB::Driver::OutputRedirector.target = $stdout
126  
-        $stdout = IRB::Driver::OutputRedirector.new
127  
-        before
128  
-      end
129 57
     end
130 58
   end
131 59
 end
132 60
 
133  
-IRB.driver_class = IRB::Driver::TTY
  61
+IRB.driver = IRB::Driver::TTY.new
134 62
 
135 63
 module Kernel
136 64
   # Creates a new IRB::Context with the given +object+ and runs it.
137 65
   def irb(object, binding = nil)
138  
-    # IRB.driver.run(IRB::Context.new(IRB.driver, object, binding))
139 66
     IRB.driver.run(IRB::Context.new(object, binding))
140 67
   end
141 68
   
5  spec/driver/tty_spec.rb
... ...
@@ -0,0 +1,5 @@
  1
+require File.expand_path('../../spec_helper', __FILE__)
  2
+
  3
+describe "IRB::Driver::TTY" do
  4
+  
  5
+end

0 notes on commit abfd9df

Please sign in to comment.
Something went wrong with that request. Please try again.