/tools/Ruby/lib/ruby/1.8/irb/ext/loader.rb

http://github.com/agross/netopenspace · Ruby · 120 lines · 95 code · 13 blank · 12 comment · 8 complexity · 7a1cf507a53c8a6a9e723ac29882871c MD5 · raw file

  1. #
  2. # loader.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(keiju@ruby-lang.org)
  7. #
  8. # --
  9. #
  10. #
  11. #
  12. module IRB
  13. class LoadAbort < Exception;end
  14. module IrbLoader
  15. @RCS_ID='-$Id: loader.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
  16. alias ruby_load load
  17. alias ruby_require require
  18. def irb_load(fn, priv = nil)
  19. path = search_file_from_ruby_path(fn)
  20. raise LoadError, "No such file to load -- #{fn}" unless path
  21. load_file(path, priv)
  22. end
  23. def search_file_from_ruby_path(fn)
  24. if /^#{Regexp.quote(File::Separator)}/ =~ fn
  25. return fn if File.exist?(fn)
  26. return nil
  27. end
  28. for path in $:
  29. if File.exist?(f = File.join(path, fn))
  30. return f
  31. end
  32. end
  33. return nil
  34. end
  35. def source_file(path)
  36. irb.suspend_name(path, File.basename(path)) do
  37. irb.suspend_input_method(FileInputMethod.new(path)) do
  38. |back_io|
  39. irb.signal_status(:IN_LOAD) do
  40. if back_io.kind_of?(FileInputMethod)
  41. irb.eval_input
  42. else
  43. begin
  44. irb.eval_input
  45. rescue LoadAbort
  46. print "load abort!!\n"
  47. end
  48. end
  49. end
  50. end
  51. end
  52. end
  53. def load_file(path, priv = nil)
  54. irb.suspend_name(path, File.basename(path)) do
  55. if priv
  56. ws = WorkSpace.new(Module.new)
  57. else
  58. ws = WorkSpace.new
  59. end
  60. irb.suspend_workspace(ws) do
  61. irb.suspend_input_method(FileInputMethod.new(path)) do
  62. |back_io|
  63. irb.signal_status(:IN_LOAD) do
  64. # p irb.conf
  65. if back_io.kind_of?(FileInputMethod)
  66. irb.eval_input
  67. else
  68. begin
  69. irb.eval_input
  70. rescue LoadAbort
  71. print "load abort!!\n"
  72. end
  73. end
  74. end
  75. end
  76. end
  77. end
  78. end
  79. def old
  80. back_io = @io
  81. back_path = @irb_path
  82. back_name = @irb_name
  83. back_scanner = @irb.scanner
  84. begin
  85. @io = FileInputMethod.new(path)
  86. @irb_name = File.basename(path)
  87. @irb_path = path
  88. @irb.signal_status(:IN_LOAD) do
  89. if back_io.kind_of?(FileInputMethod)
  90. @irb.eval_input
  91. else
  92. begin
  93. @irb.eval_input
  94. rescue LoadAbort
  95. print "load abort!!\n"
  96. end
  97. end
  98. end
  99. ensure
  100. @io = back_io
  101. @irb_name = back_name
  102. @irb_path = back_path
  103. @irb.scanner = back_scanner
  104. end
  105. end
  106. end
  107. end