Skip to content

Commit 3f632e8

Browse files
committed
added JRuby source_location hack, also added a LICENSE file
1 parent 90628e4 commit 3f632e8

File tree

4 files changed

+128
-54
lines changed

4 files changed

+128
-54
lines changed

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
License
2+
-------
3+
4+
(The MIT License)
5+
6+
Copyright (c) 2011 John Mair (banisterfiend)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining
9+
a copy of this software and associated documentation files (the
10+
'Software'), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be
17+
included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
20+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.markdown

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Method comments can also be extracted using the `comment` method.
1515

1616
It is written in pure Ruby (no C).
1717

18-
* Some Ruby 1.8 support now available.
18+
* Some Ruby 1.8 support now available.
19+
* Support for MRI, RBX, JRuby.
1920

2021
`method_source` provides the `source` and `comment` methods to the `Method` and
2122
`UnboundMethod` and `Proc` classes.
@@ -60,3 +61,29 @@ Special Thanks
6061
[Adam Sanderson](https://github.com/adamsanderson) for `comment` functionality.
6162

6263
[Dmitry Elastic](https://github.com/dmitryelastic) for the brilliant Ruby 1.8 `source_location` hack.
64+
65+
License
66+
-------
67+
68+
(The MIT License)
69+
70+
Copyright (c) 2011 John Mair (banisterfiend)
71+
72+
Permission is hereby granted, free of charge, to any person obtaining
73+
a copy of this software and associated documentation files (the
74+
'Software'), to deal in the Software without restriction, including
75+
without limitation the rights to use, copy, modify, merge, publish,
76+
distribute, sublicense, and/or sell copies of the Software, and to
77+
permit persons to whom the Software is furnished to do so, subject to
78+
the following conditions:
79+
80+
The above copyright notice and this permission notice shall be
81+
included in all copies or substantial portions of the Software.
82+
83+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
84+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
85+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
86+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
87+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
88+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
89+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lib/method_source/source_location.rb

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
module MethodSource
22
module SourceLocation
33
module MethodExtensions
4+
if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
5+
require 'java'
46

5-
def trace_func(event, file, line, id, binding, classname)
6-
return unless event == 'call'
7-
set_trace_func nil
7+
# JRuby version source_location hack
8+
# @return [Array] A two element array containing the source location of the method
9+
def source_location
10+
to_java.source_location(Thread.current.to_java.getContext())
11+
end
12+
else
813

9-
@file, @line = file, line
10-
raise :found
11-
end
1214

13-
private :trace_func
15+
def trace_func(event, file, line, id, binding, classname)
16+
return unless event == 'call'
17+
set_trace_func nil
18+
19+
@file, @line = file, line
20+
raise :found
21+
end
1422

15-
# Return the source location of a method for Ruby 1.8.
16-
# @return [Array] A two element array. First element is the
17-
# file, second element is the line in the file where the
18-
# method definition is found.
19-
def source_location
20-
if @file.nil?
21-
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
23+
private :trace_func
2224

23-
set_trace_func method(:trace_func).to_proc
24-
call(*args) rescue nil
25-
set_trace_func nil
26-
@file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
25+
# Return the source location of a method for Ruby 1.8.
26+
# @return [Array] A two element array. First element is the
27+
# file, second element is the line in the file where the
28+
# method definition is found.
29+
def source_location
30+
if @file.nil?
31+
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
32+
33+
set_trace_func method(:trace_func).to_proc
34+
call(*args) rescue nil
35+
set_trace_func nil
36+
@file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
37+
end
38+
return [@file, @line] if File.exist?(@file.to_s)
2739
end
28-
return [@file, @line] if File.exist?(@file.to_s)
2940
end
3041
end
3142

@@ -56,43 +67,54 @@ def source_location
5667
end
5768

5869
module UnboundMethodExtensions
70+
if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
71+
require 'java'
5972

60-
# Return the source location of an instance method for Ruby 1.8.
61-
# @return [Array] A two element array. First element is the
62-
# file, second element is the line in the file where the
63-
# method definition is found.
64-
def source_location
65-
klass = case owner
66-
when Class
67-
owner
68-
when Module
69-
method_owner = owner
70-
Class.new { include(method_owner) }
71-
end
72-
73-
# deal with immediate values
74-
case
75-
when klass == Symbol
76-
return :a.method(name).source_location
77-
when klass == Fixnum
78-
return 0.method(name).source_location
79-
when klass == TrueClass
80-
return true.method(name).source_location
81-
when klass == FalseClass
82-
return false.method(name).source_location
83-
when klass == NilClass
84-
return nil.method(name).source_location
73+
# JRuby version source_location hack
74+
# @return [Array] A two element array containing the source location of the method
75+
def source_location
76+
to_java.source_location(Thread.current.to_java.getContext())
8577
end
78+
else
79+
80+
81+
# Return the source location of an instance method for Ruby 1.8.
82+
# @return [Array] A two element array. First element is the
83+
# file, second element is the line in the file where the
84+
# method definition is found.
85+
def source_location
86+
klass = case owner
87+
when Class
88+
owner
89+
when Module
90+
method_owner = owner
91+
Class.new { include(method_owner) }
92+
end
93+
94+
# deal with immediate values
95+
case
96+
when klass == Symbol
97+
return :a.method(name).source_location
98+
when klass == Fixnum
99+
return 0.method(name).source_location
100+
when klass == TrueClass
101+
return true.method(name).source_location
102+
when klass == FalseClass
103+
return false.method(name).source_location
104+
when klass == NilClass
105+
return nil.method(name).source_location
106+
end
86107

87-
begin
88-
klass.allocate.method(name).source_location
89-
rescue TypeError
108+
begin
109+
klass.allocate.method(name).source_location
110+
rescue TypeError
90111

91-
# Assume we are dealing with a Singleton Class:
92-
# 1. Get the instance object
93-
# 2. Forward the source_location lookup to the instance
94-
instance ||= ObjectSpace.each_object(owner).first
95-
instance.method(name).source_location
112+
# Assume we are dealing with a Singleton Class:
113+
# 1. Get the instance object
114+
# 2. Forward the source_location lookup to the instance
115+
instance ||= ObjectSpace.each_object(owner).first
116+
instance.method(name).source_location
117+
end
96118
end
97119
end
98120
end

lib/method_source/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MethodSource
2-
VERSION = "0.6.0"
2+
VERSION = "0.6.5"
33
end

0 commit comments

Comments
 (0)