diff options
author | John Mair <jrmair@gmail.com> | 2011-09-07 04:29:49 +1200 |
---|---|---|
committer | John Mair <jrmair@gmail.com> | 2011-09-07 04:53:50 +1200 |
commit | 3f632e8d1852d6fcaf434b848e85ba2a10741b80 (patch) | |
tree | 2061d92ab99b57ea947a19690dc1a50eaf69fd7b /lib | |
parent | 90628e4154562f8640ca32ff1f897bffa2d03af0 (diff) | |
download | method_source-3f632e8d1852d6fcaf434b848e85ba2a10741b80.tar.gz |
added JRuby source_location hack, also added a LICENSE file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/method_source/source_location.rb | 126 | ||||
-rw-r--r-- | lib/method_source/version.rb | 2 |
2 files changed, 75 insertions, 53 deletions
diff --git a/lib/method_source/source_location.rb b/lib/method_source/source_location.rb index 337e997..5010520 100644 --- a/lib/method_source/source_location.rb +++ b/lib/method_source/source_location.rb @@ -1,31 +1,42 @@ module MethodSource module SourceLocation module MethodExtensions + if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ + require 'java' - def trace_func(event, file, line, id, binding, classname) - return unless event == 'call' - set_trace_func nil + # JRuby version source_location hack + # @return [Array] A two element array containing the source location of the method + def source_location + to_java.source_location(Thread.current.to_java.getContext()) + end + else - @file, @line = file, line - raise :found - end - private :trace_func + def trace_func(event, file, line, id, binding, classname) + return unless event == 'call' + set_trace_func nil + + @file, @line = file, line + raise :found + end - # Return the source location of a method for Ruby 1.8. - # @return [Array] A two element array. First element is the - # file, second element is the line in the file where the - # method definition is found. - def source_location - if @file.nil? - args =[*(1..(arity<-1 ? -arity-1 : arity ))] + private :trace_func - set_trace_func method(:trace_func).to_proc - call(*args) rescue nil - set_trace_func nil - @file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file)) + # Return the source location of a method for Ruby 1.8. + # @return [Array] A two element array. First element is the + # file, second element is the line in the file where the + # method definition is found. + def source_location + if @file.nil? + args =[*(1..(arity<-1 ? -arity-1 : arity ))] + + set_trace_func method(:trace_func).to_proc + call(*args) rescue nil + set_trace_func nil + @file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file)) + end + return [@file, @line] if File.exist?(@file.to_s) end - return [@file, @line] if File.exist?(@file.to_s) end end @@ -56,43 +67,54 @@ module MethodSource end module UnboundMethodExtensions + if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ + require 'java' - # Return the source location of an instance method for Ruby 1.8. - # @return [Array] A two element array. First element is the - # file, second element is the line in the file where the - # method definition is found. - def source_location - klass = case owner - when Class - owner - when Module - method_owner = owner - Class.new { include(method_owner) } - end - - # deal with immediate values - case - when klass == Symbol - return :a.method(name).source_location - when klass == Fixnum - return 0.method(name).source_location - when klass == TrueClass - return true.method(name).source_location - when klass == FalseClass - return false.method(name).source_location - when klass == NilClass - return nil.method(name).source_location + # JRuby version source_location hack + # @return [Array] A two element array containing the source location of the method + def source_location + to_java.source_location(Thread.current.to_java.getContext()) end + else + + + # Return the source location of an instance method for Ruby 1.8. + # @return [Array] A two element array. First element is the + # file, second element is the line in the file where the + # method definition is found. + def source_location + klass = case owner + when Class + owner + when Module + method_owner = owner + Class.new { include(method_owner) } + end + + # deal with immediate values + case + when klass == Symbol + return :a.method(name).source_location + when klass == Fixnum + return 0.method(name).source_location + when klass == TrueClass + return true.method(name).source_location + when klass == FalseClass + return false.method(name).source_location + when klass == NilClass + return nil.method(name).source_location + end - begin - klass.allocate.method(name).source_location - rescue TypeError + begin + klass.allocate.method(name).source_location + rescue TypeError - # Assume we are dealing with a Singleton Class: - # 1. Get the instance object - # 2. Forward the source_location lookup to the instance - instance ||= ObjectSpace.each_object(owner).first - instance.method(name).source_location + # Assume we are dealing with a Singleton Class: + # 1. Get the instance object + # 2. Forward the source_location lookup to the instance + instance ||= ObjectSpace.each_object(owner).first + instance.method(name).source_location + end end end end diff --git a/lib/method_source/version.rb b/lib/method_source/version.rb index ddc524b..dcc81b6 100644 --- a/lib/method_source/version.rb +++ b/lib/method_source/version.rb @@ -1,3 +1,3 @@ module MethodSource - VERSION = "0.6.0" + VERSION = "0.6.5" end |