getsource 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1 +1,3 @@
1
+ 0.2.0: Implemented getsource methods in pure ruby for ruby 1.9 using source_location
2
+
1
3
  0.1.0: implemented getsource with code extracted from rallhook
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
 
7
7
  spec = Gem::Specification.new do |s|
8
8
  s.name = 'getsource'
9
- s.version = '0.1.0'
9
+ s.version = '0.2.0'
10
10
  s.author = 'Dario Seminara'
11
11
  s.email = 'robertodarioseminara@gmail.com'
12
12
  s.platform = Gem::Platform::RUBY
@@ -20,28 +20,10 @@ along with getsource. if not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
21
 
22
22
  #include <ruby.h>
23
-
24
23
  #ifdef RUBY1_8
25
- #include <node.h>
26
- #endif
27
-
28
- #ifdef RUBY1_9
29
- #include <node_defs.h>
30
- #endif
31
-
32
- #ifndef __USE_GNU
33
- #define __USE_GNU
34
- #endif
35
- #include <dlfcn.h>
36
24
 
25
+ #include <node.h>
37
26
 
38
- VALUE rb_cNode;
39
- ID intern_owner;
40
- ID intern_name;
41
- ID intern_sym;
42
-
43
- /// from eval.c
44
- #ifdef RUBY1_8
45
27
  struct METHOD {
46
28
  VALUE klass, rklass;
47
29
 
@@ -53,33 +35,17 @@ struct METHOD {
53
35
  unsigned char* base__;
54
36
 
55
37
  #define nd_file(n) n->nd_file
38
+ #include <dlfcn.h>
56
39
 
57
- #endif
58
-
59
- #ifdef RUBY1_9
60
-
61
- // from proc.c
62
- struct METHOD {
63
- VALUE oclass; /* class that holds the method */
64
- VALUE rclass; /* class of the receiver */
65
- VALUE recv;
66
- ID id, oid;
67
- NODE *body;
68
- };
69
-
70
- typedef struct rb_iseq_struct__ {
71
- VALUE type; // instruction sequence type
72
- VALUE name; // String: iseq name
73
- VALUE filename; // file information where this sequence from
74
- } rb_iseq_t__;
75
-
40
+ VALUE rb_cNode;
41
+ ID intern_owner;
42
+ ID intern_name;
43
+ ID intern_sym;
76
44
 
77
45
  typedef VALUE (*MNEW)(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope);
78
46
  MNEW mnew_;
79
47
  unsigned char* base__;
80
48
 
81
- #endif
82
-
83
49
  /*
84
50
  The node that acts as body of the method
85
51
  */
@@ -95,12 +61,10 @@ VALUE rb_method_body(VALUE self) {
95
61
 
96
62
  if (method->body == 0) return Qnil;
97
63
 
98
- #ifdef RUBY1_8
99
64
  // nd_defn is only present in ruby_1.8
100
65
  if (method->body->nd_defn != 0) {
101
66
  return Data_Wrap_Struct(rb_cNode, 0, 0, method->body->nd_defn);
102
67
  }
103
- #endif
104
68
 
105
69
  return Data_Wrap_Struct(rb_cNode, 0, 0, method->body);
106
70
  }
@@ -112,15 +76,11 @@ VALUE rb_node_line(VALUE self) {
112
76
  NODE* _node;
113
77
  Data_Get_Struct(self,NODE,_node);
114
78
 
115
- #ifdef RUBY1_8
116
79
  return INT2FIX(nd_line(_node));
117
- #endif
118
80
 
119
- #ifdef RUBY1_9
120
- return INT2FIX(0);
121
- #endif
122
81
  }
123
82
 
83
+
124
84
  /*
125
85
  The name of the ruby source file where the code associated with the node are defined
126
86
  */
@@ -128,46 +88,20 @@ VALUE rb_node_file(VALUE self) {
128
88
  NODE* _node;
129
89
  Data_Get_Struct(self,NODE,_node);
130
90
 
131
- #ifdef RUBY1_8
132
91
  if (nd_file(_node) == NULL ) {
133
92
  return rb_str_new2("");
134
93
  }
135
94
  return rb_str_new2(nd_file(_node) );
136
- #endif
137
-
138
- #ifdef RUBY1_9
139
-
140
- if (nd_type(_node) == RUBY_VM_METHOD_NODE) {
141
- VALUE iseqval = (VALUE)(_node->nd_body);
142
- rb_iseq_t__* ptr;
143
- Data_Get_Struct(iseqval, rb_iseq_t__, ptr);
144
-
145
- return ptr->filename;
146
- }
147
-
148
- return rb_str_new2("");
149
-
150
- #endif
151
-
152
95
  }
153
96
 
154
97
 
155
98
  static void
156
99
  bm_mark(struct METHOD *data)
157
100
  {
158
- #ifdef RUBY1_8
159
101
  rb_gc_mark(data->klass);
160
102
  rb_gc_mark(data->rklass);
161
103
  rb_gc_mark(data->recv);
162
104
  rb_gc_mark((VALUE)data->body);
163
- #endif
164
- #ifdef RUBY1_9
165
- rb_gc_mark(data->rclass);
166
- rb_gc_mark(data->oclass);
167
- rb_gc_mark(data->recv);
168
- rb_gc_mark((VALUE)data->body);
169
- #endif
170
-
171
105
  }
172
106
 
173
107
  static VALUE
@@ -180,12 +114,7 @@ umethod_unchecked_bind(VALUE method, VALUE recv)
180
114
  method = Data_Make_Struct(rb_cMethod, struct METHOD, bm_mark, -1, bound);
181
115
  *bound = *data;
182
116
  bound->recv = recv;
183
- #ifdef RUBY1_8
184
117
  bound->rklass = CLASS_OF(recv);
185
- #endif
186
- #ifdef RUBY1_9
187
- bound->rclass = CLASS_OF(recv);
188
- #endif
189
118
 
190
119
  return method;
191
120
  }
@@ -202,11 +131,18 @@ void Init_getsource_base() {
202
131
  the ruby source code and may be associated with a file and line where thats node are defined
203
132
  */
204
133
  rb_cNode = rb_define_class("Node", rb_cObject);
205
-
206
134
  rb_define_method(rb_cNode, "line", rb_node_line, 0);
207
135
  rb_define_method(rb_cNode, "file", rb_node_file, 0);
208
136
 
137
+
209
138
  intern_owner = rb_intern("owner");
210
139
  intern_name = rb_intern("name");
211
140
  intern_sym = rb_intern("to_sym");
212
141
  }
142
+ #else
143
+
144
+ void Init_getsource_base() {
145
+ }
146
+
147
+ #endif
148
+
@@ -20,7 +20,41 @@ along with getsource. if not, see <http://www.gnu.org/licenses/>.
20
20
  =end
21
21
  require "getsource_base"
22
22
 
23
+ class Method
24
+
25
+ class Node
26
+
27
+ attr_reader :file
28
+ attr_reader :line
29
+
30
+ def initialize(file_,line_)
31
+ @file = file_
32
+ @line = line_
33
+ end
34
+ end
35
+
36
+ begin
37
+ instance_method("body")
38
+ rescue
39
+ def body
40
+ Method::Node.new(source_location[0], source_location[1])
41
+ end
42
+ end
43
+ end
44
+
45
+ class UnboundMethod
46
+ begin
47
+ instance_method("body")
48
+ rescue
49
+ def body
50
+ Method::Node.new(source_location[0], source_location[1])
51
+ end
52
+ end
53
+ end
54
+
55
+
23
56
  class Object
57
+
24
58
  def specific_method(arg1, arg2=nil)
25
59
  if arg2
26
60
  method_name = arg2
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getsource
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dario Seminara
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-30 00:00:00 -03:00
18
+ date: 2011-04-27 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21