Blob Blame History Raw
Thu Jul 23 15:04:20 2009  Keiju Ishitsuka  <keiju@ruby-lang.org>

ruby* lib/irb.rb, lib/irb/init.rb, lib/irb/ext/save-history.rb: add
ruby  IRB::irb_at_exit. no use finalizer saving history. [ruby-dev-38563]

Index: lib/irb/ext/save-history.rb
===================================================================
--- lib/irb/ext/save-history.rb	(revision 24225)
+++ lib/irb/ext/save-history.rb	(revision 24254)
@@ -50,23 +50,24 @@
   module HistorySavingAbility
     include Readline
 
-    def HistorySavingAbility.create_finalizer
-      proc do
-	if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
-	  if hf = IRB.conf[:HISTORY_FILE]
-	    file = File.expand_path(hf)
-	  end
-	  file = IRB.rc_file("_history") unless file
-	  open(file, 'w' ) do |f|
-	    hist = HISTORY.to_a
-	    f.puts(hist[-num..-1] || hist)
-	  end
-	end
-      end
-    end
+#     def HistorySavingAbility.create_finalizer
+#       proc do
+# 	if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
+# 	  if hf = IRB.conf[:HISTORY_FILE]
+# 	    file = File.expand_path(hf)
+# 	  end
+# 	  file = IRB.rc_file("_history") unless file
+# 	  open(file, 'w' ) do |f|
+# 	    hist = HISTORY.to_a
+# 	    f.puts(hist[-num..-1] || hist)
+# 	  end
+# 	end
+#       end
+#     end
 
     def HistorySavingAbility.extended(obj)
-      ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
+#      ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
+      IRB.conf[:AT_EXIT].push proc{obj.save_history}
       obj.load_history
       obj
     end
@@ -80,6 +81,19 @@
 	end
       end
     end
+
+    def save_history
+      if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
+	if history_file = IRB.conf[:HISTORY_FILE]
+	  history_file = File.expand_path(history_file)
+	end
+	history_file = IRB.rc_file("_history") unless history_file
+	open(history_file, 'w' ) do |f|
+	  hist = HISTORY.to_a
+	  f.puts(hist[-num..-1] || hist)
+	end
+      end
+    end
   end
 end
 
Index: lib/irb/init.rb
===================================================================
--- lib/irb/init.rb	(revision 24225)
+++ lib/irb/init.rb	(revision 24254)
@@ -114,6 +114,8 @@
 #    @CONF[:LC_MESSAGES] = "en"
     @CONF[:LC_MESSAGES] = Locale.new
     
+    @CONF[:AT_EXIT] = []
+    
     @CONF[:DEBUG_LEVEL] = 1
   end
 
Index: lib/irb.rb
===================================================================
--- lib/irb.rb	(revision 24225)
+++ lib/irb.rb	(revision 24254)
@@ -65,13 +65,21 @@
     trap("SIGINT") do
       irb.signal_handle
     end
-    
-    catch(:IRB_EXIT) do
-      irb.eval_input
+
+    begin
+      catch(:IRB_EXIT) do
+	irb.eval_input
+      end
+    ensure
+      irb_at_exit
     end
 #    print "\n"
   end
 
+  def IRB.irb_at_exit
+    @CONF[:AT_EXIT].each{|hook| hook.call}
+  end
+
   def IRB.irb_exit(irb, ret)
     throw :IRB_EXIT, ret
   end