2007年 10月 14日

Ruby で、メソッドがどこで定義されたか外から知る方法

「あるクラス・モジュール・メソッドがどこで定義されたものなのか簡単に知る方法がない」と書いたけど、ホントにホント?という切っ掛けで、メソッド内側の binding なら caller つかえば簡単にとれるけど、外から名前を指定してはとれないよなぁと思うので考えてみた。

class Object
	def location_of_method(name)
		c = nil
		ret = callcc {|c| false }
		unless ret
			m = self.method(name)
			args = [nil] * m.arity.abs
			set_trace_func Proc.new {|event, file, line, id, binding, klass|
				case event
				when "c-call"
					if id == name
						set_trace_func(nil)
						c.call([:native, nil])
					end
				when "call"
					set_trace_func(nil)
					c.call([file, line])
				end
			}
			m.call(*args)
			set_trace_func(nil) # attr_* 系で定義されたメソッドは call が trace できない?
		end
		ret
	end
end

class Class
	def location_of_instance_method(name)
		self.allocate.location_of_method(name)
	end
end

p Object.location_of_method(:new) #=> [:native, nil]
p Object.location_of_instance_method(:instance_eval) #=> [:native, nil]

require "pathname"
p Pathname.location_of_instance_method(:absolute?)
#=> ["/usr/lib/ruby/1.8/pathname.rb", 404]

require "ostruct"
p OpenStruct.location_of_instance_method(:initialize)
#=> ["/usr/lib/ruby/1.8/ostruct.rb", 46]

class OpenStruct
	def initialize
	end
end
# 直前の
p OpenStruct.location_of_instance_method(:initialize)
#=> ["test.rb", 44]

o = Object.new
def o.singleton_method_foo
end
# 直前の
p o.location_of_method(:singleton_method_foo)
#=> ["test.rb", 51]

require "webrick"
p WEBrick::HTTPServer.location_of_instance_method(:mount)
#=> ["/usr/lib/ruby/1.8/webrick/httpserver.rb", 111]

set_trace_func と callcc (継続) を使ってる。activesupport の Binding#of_caller の実装を少し前にみて「うわきめー」って思って使う機会があったら使ってみたい、とか考えていたら意外にホイホイあった。やってみてわかったことは set_trace_func 内のデバッグがすごくむずかしいということだった。
もっと簡単にやる方法があるかなぁ……

しかしモジュール定義とかはどうやってとるかがわからなすぎる。現在のファイルと require してるファイルを全部見てくしかないよねたぶん……

Delegater 使ってるときにうまくいかないなぁ。そもそも call がよばれてこない。なんでだろう。

require "tempfile"
p Tempfile.location_of_instance_method(:unlink)
__END__
(eval):3:in `__send__': undefined method `unlink' for class `NilClass' (NameError)
        from (eval):3:in `location_of_method'
        from test.rb:32:in `location_of_instance_method'
        from test.rb:85

Tempfile にも unlink が定義されているはずなのに、それが呼ばれない?で、スーパークラスのデリゲートが呼ばれているように見える。どういうことだろう……

なぜか Tempfile.allocate が nil になる

むーどうして allocate が nil になるかわからない。ちゃんと Class.allocate がよばれてるみたいなのに……

もうちょい改良

  • エラーができるだけでないように
  • スレッドセーフ
  • DelegateClass みたいなのに対応
class Object
	def location_of_method(name)
		old_state = Thread.critical
		Thread.critical = true
		name = name.to_sym
		c = nil
		ret = callcc {|c| false }
		unless ret
			m = self.method(name)
			args = [nil] * m.arity.abs
			set_trace_func Proc.new {|event, file, line, id, binding, klass|
				case event
				when "c-call"
					if id == name
						set_trace_func(nil)
						c.call([:native, nil, binding, klass])
					end
				when "call"
					set_trace_func(nil)
					c.call([file, line, binding, klass])
				end
			}
			begin
				m.call(*args)
			rescue Exception
			end
			set_trace_func(nil)
		end
		ret
	rescue ArgumentError
		false
	ensure
		Thread.critical = old_state
	end
end

class Class
	def location_of_instance_method(name)
		ret = nil
		old_state = Thread.critical
		Thread.critical = true
		self.ancestors.each do |c|
			break if c == Object
			[:location_of_method, :method_missing, :allocate].each do |m|
				[c, (class <<c; self; end)].each do |klass|
					begin
						klass.module_eval <<-EOC
							alias __location_temp_#{m} #{m}
							remove_method :#{m}
						EOC
					rescue NameError
					end
				end
			end
		end
		begin
			ret = self.__send__(:allocate).location_of_method(name)
		rescue NotImplementedError, TypeError, NoMethodError => e
			p e
		end
		self.ancestors.each do |c|
			break if c == Object
			[:location_of_method, :method_missing, :allocate].each do |m|
				[c, (class <<c; self; end)].each do |klass|
					begin
						klass.module_eval <<-EOC
							alias #{m}  __location_temp_#{m}
							remove_method :__location_temp_#{m}
						EOC
					rescue NameError
					end
				end
			end
		end
		ret
	ensure
		Thread.critical = old_state
	end
end

class Module
	def location_of_instance_method(name)
		c = Class.new
		# included を実行させない
		self.__send__ :append_features, c
		c.location_of_instance_method(name)
	end
end

#-- Test

def __modules
	ret = []
	ObjectSpace.each_object(Module) do |o|
		ret << o
	end
	ret
end
before = __modules

require "rubygems"
require "active_support"
require "active_record"

module_files = {}
(__modules - before).each do |o|
	next if o.name.nil? || o.name.empty?
	o.instance_methods(false).each do |m|
		r = o.location_of_instance_method(m.to_sym)
		if r
			(module_files[o.to_s] ||= []) << r.first unless r.first == :native
		end
		module_files[o.to_s] &&= module_files[o.to_s].uniq
	end
end

require "pp"
pp module_files