binding_of_caller 0.4.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -3
- data/Rakefile +3 -2
- data/ext/binding_of_caller/binding_of_caller.c +115 -17
- data/ext/binding_of_caller/extconf.rb +2 -3
- data/lib/binding_of_caller/version.rb +1 -1
- data/test/test.rb +63 -31
- metadata +32 -36
data/README.md
CHANGED
|
@@ -13,7 +13,7 @@ call stack, not limited to just the immediate caller.
|
|
|
13
13
|
|
|
14
14
|
**Recommended for use only in debugging situations. Do not use this in production apps.**
|
|
15
15
|
|
|
16
|
-
**Only works in MRI Ruby 1.9.2**
|
|
16
|
+
**Only works in MRI Ruby 1.9.2 and 1.9.3**
|
|
17
17
|
|
|
18
18
|
* Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
|
|
19
19
|
* See the [source code](http://github.com/banister/binding_of_caller)
|
|
@@ -50,8 +50,7 @@ This project is a spinoff from the [Pry REPL project.](http://pry.github.com)
|
|
|
50
50
|
Features and limitations
|
|
51
51
|
-------------------------
|
|
52
52
|
|
|
53
|
-
* Only works with MRI 1.9.2
|
|
54
|
-
* Broken in 1.9.3, support will hopefully be provided in the near future.
|
|
53
|
+
* Only works with MRI 1.9.2 and 1.9.3
|
|
55
54
|
* Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
|
|
56
55
|
|
|
57
56
|
Contact
|
data/Rakefile
CHANGED
|
@@ -15,16 +15,17 @@ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
|
|
15
15
|
|
|
16
16
|
def apply_spec_defaults(s)
|
|
17
17
|
s.name = PROJECT_NAME
|
|
18
|
-
s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack. Currently only works for MRI 1.9.2
|
|
18
|
+
s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack. Currently only works for MRI 1.9.2+"
|
|
19
19
|
s.version = BindingOfCaller::VERSION
|
|
20
20
|
s.date = Time.now.strftime '%Y-%m-%d'
|
|
21
21
|
s.author = "John Mair (banisterfiend)"
|
|
22
22
|
s.email = 'jrmair@gmail.com'
|
|
23
23
|
s.description = s.summary
|
|
24
24
|
s.require_path = 'lib'
|
|
25
|
-
s.add_development_dependency("bacon","~>1.1
|
|
25
|
+
s.add_development_dependency("bacon","~>1.1")
|
|
26
26
|
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
27
27
|
s.has_rdoc = 'yard'
|
|
28
|
+
s.required_ruby_version = '>= 1.9.2'
|
|
28
29
|
s.files = `git ls-files`.split("\n")
|
|
29
30
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
|
30
31
|
end
|
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
/* (c) 2011 John Mair (banisterfiend), MIT license */
|
|
2
2
|
|
|
3
3
|
#include <ruby.h>
|
|
4
|
-
|
|
5
|
-
#include <ruby/io.h>
|
|
6
|
-
#include <ruby/re.h>
|
|
7
4
|
#include "vm_core.h"
|
|
8
5
|
#include "gc.h"
|
|
9
6
|
|
|
10
7
|
typedef enum { false, true } bool;
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
static VALUE
|
|
10
|
+
string2sym(const char * string)
|
|
11
|
+
{
|
|
12
|
+
return ID2SYM(rb_intern(string));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static inline const rb_data_type_t *
|
|
16
|
+
threadptr_data_type(void)
|
|
17
|
+
{
|
|
18
|
+
static const rb_data_type_t *thread_data_type;
|
|
19
|
+
if (!thread_data_type) {
|
|
20
|
+
VALUE current_thread = rb_thread_current();
|
|
21
|
+
thread_data_type = RTYPEDDATA_TYPE(current_thread);
|
|
22
|
+
}
|
|
23
|
+
return thread_data_type;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#define ruby_thread_data_type *threadptr_data_type()
|
|
27
|
+
|
|
28
|
+
#define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
|
|
13
29
|
|
|
14
30
|
static size_t
|
|
15
31
|
binding_memsize(const void *ptr)
|
|
@@ -37,7 +53,11 @@ binding_mark(void *ptr)
|
|
|
37
53
|
if (ptr) {
|
|
38
54
|
bind = ptr;
|
|
39
55
|
RUBY_MARK_UNLESS_NULL(bind->env);
|
|
56
|
+
|
|
57
|
+
#ifdef RUBY_192
|
|
40
58
|
RUBY_MARK_UNLESS_NULL(bind->filename);
|
|
59
|
+
#endif
|
|
60
|
+
|
|
41
61
|
}
|
|
42
62
|
RUBY_MARK_LEAVE("binding");
|
|
43
63
|
}
|
|
@@ -59,31 +79,48 @@ binding_alloc(VALUE klass)
|
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
62
|
-
if (cfp > limit_cfp)
|
|
63
|
-
rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
|
|
64
|
-
|
|
65
82
|
return cfp->iseq && !NIL_P(cfp->self);
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
while (error_count <= max_frame_errors) {
|
|
86
|
+
while (cfp < limit_cfp) {
|
|
72
87
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
73
88
|
|
|
89
|
+
if (cfp >= limit_cfp)
|
|
90
|
+
return NULL;
|
|
91
|
+
|
|
74
92
|
if (valid_frame_p(cfp, limit_cfp))
|
|
75
93
|
return cfp;
|
|
76
|
-
else
|
|
77
|
-
error_count += 1;
|
|
78
94
|
}
|
|
79
95
|
|
|
80
|
-
//
|
|
81
|
-
return
|
|
96
|
+
// beyond end of stack
|
|
97
|
+
return NULL;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static VALUE
|
|
101
|
+
frametype_name(VALUE flag)
|
|
102
|
+
{
|
|
103
|
+
switch (flag & VM_FRAME_MAGIC_MASK) {
|
|
104
|
+
case VM_FRAME_MAGIC_METHOD: return string2sym("method");
|
|
105
|
+
case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
|
|
106
|
+
case VM_FRAME_MAGIC_CLASS: return string2sym("class");
|
|
107
|
+
case VM_FRAME_MAGIC_TOP: return string2sym("top");
|
|
108
|
+
case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
|
|
109
|
+
case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
|
|
110
|
+
case VM_FRAME_MAGIC_PROC: return string2sym("proc");
|
|
111
|
+
case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
|
|
112
|
+
case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
|
|
113
|
+
case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
|
|
114
|
+
default:
|
|
115
|
+
rb_raise(rb_eRuntimeError, "Unknown frame type! got flag: %d", FIX2INT(flag));
|
|
116
|
+
}
|
|
82
117
|
}
|
|
83
118
|
|
|
84
119
|
static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
85
120
|
{
|
|
86
|
-
rb_thread_t *th
|
|
121
|
+
rb_thread_t *th;
|
|
122
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
123
|
+
|
|
87
124
|
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
88
125
|
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
89
126
|
int level = FIX2INT(rb_level);
|
|
@@ -92,6 +129,9 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
|
92
129
|
for (int i = 0; i < level; i++) {
|
|
93
130
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
94
131
|
|
|
132
|
+
if (cfp >= limit_cfp)
|
|
133
|
+
rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
|
|
134
|
+
|
|
95
135
|
// skip invalid frames
|
|
96
136
|
if (!valid_frame_p(cfp, limit_cfp))
|
|
97
137
|
cfp = find_valid_frame(cfp, limit_cfp);
|
|
@@ -100,23 +140,81 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
|
100
140
|
VALUE bindval = binding_alloc(rb_cBinding);
|
|
101
141
|
rb_binding_t *bind;
|
|
102
142
|
|
|
103
|
-
if (cfp == 0)
|
|
143
|
+
if (cfp == 0)
|
|
104
144
|
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
|
|
105
|
-
}
|
|
106
145
|
|
|
107
146
|
GetBindingPtr(bindval, bind);
|
|
108
147
|
bind->env = rb_vm_make_env_object(th, cfp);
|
|
109
148
|
bind->filename = cfp->iseq->filename;
|
|
110
149
|
bind->line_no = rb_vm_get_sourceline(cfp);
|
|
150
|
+
|
|
151
|
+
rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
|
|
152
|
+
rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
|
|
153
|
+
|
|
111
154
|
return bindval;
|
|
112
155
|
}
|
|
113
156
|
|
|
157
|
+
static VALUE
|
|
158
|
+
frame_type(VALUE self)
|
|
159
|
+
{
|
|
160
|
+
return rb_iv_get(self, "@frame_type");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static VALUE
|
|
164
|
+
frame_description(VALUE self)
|
|
165
|
+
{
|
|
166
|
+
return rb_iv_get(self, "@frame_description");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static VALUE frame_count(VALUE self)
|
|
170
|
+
{
|
|
171
|
+
rb_thread_t *th;
|
|
172
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
173
|
+
|
|
174
|
+
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
175
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
176
|
+
|
|
177
|
+
int i = 1;
|
|
178
|
+
while (cfp < limit_cfp) {
|
|
179
|
+
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
180
|
+
|
|
181
|
+
if (cfp >= limit_cfp)
|
|
182
|
+
return INT2FIX(i);
|
|
183
|
+
|
|
184
|
+
// skip invalid frames
|
|
185
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
186
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
187
|
+
|
|
188
|
+
if (!cfp)
|
|
189
|
+
break;
|
|
190
|
+
|
|
191
|
+
i++;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return INT2FIX(i);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static VALUE
|
|
198
|
+
callers(VALUE self)
|
|
199
|
+
{
|
|
200
|
+
VALUE ary = rb_ary_new();
|
|
201
|
+
|
|
202
|
+
for (int i = 0; i < FIX2INT(frame_count(self)); i++)
|
|
203
|
+
rb_ary_push(ary, binding_of_caller(self, INT2FIX(i)));
|
|
204
|
+
|
|
205
|
+
return ary;
|
|
206
|
+
}
|
|
207
|
+
|
|
114
208
|
void
|
|
115
209
|
Init_binding_of_caller()
|
|
116
210
|
{
|
|
117
211
|
VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
|
|
118
212
|
|
|
119
213
|
rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
|
|
214
|
+
rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
|
|
215
|
+
rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
|
|
216
|
+
rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
|
|
217
|
+
rb_define_method(mBindingOfCaller, "callers", callers, 0);
|
|
120
218
|
rb_include_module(rb_cBinding, mBindingOfCaller);
|
|
121
219
|
}
|
|
122
220
|
|
|
@@ -5,10 +5,9 @@ $CFLAGS += " -std=c99"
|
|
|
5
5
|
|
|
6
6
|
case RUBY_VERSION
|
|
7
7
|
when /1.9.2/
|
|
8
|
-
$CFLAGS += " -I./ruby_headers/192/"
|
|
8
|
+
$CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
|
|
9
9
|
when /1.9.3/
|
|
10
|
-
|
|
11
|
-
$CFLAGS += " -I./ruby_headers/193/"
|
|
10
|
+
$CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
|
|
12
11
|
end
|
|
13
12
|
|
|
14
13
|
create_makefile('binding_of_caller')
|
data/test/test.rb
CHANGED
|
@@ -8,52 +8,84 @@ puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
|
8
8
|
puts "Ruby version: #{RUBY_VERSION}"
|
|
9
9
|
|
|
10
10
|
describe BindingOfCaller do
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
describe "of_caller" do
|
|
12
|
+
it "should fetch immediate caller's binding when 0 is passed" do
|
|
13
|
+
o = Object.new
|
|
14
|
+
def o.a
|
|
15
|
+
var = 1
|
|
16
|
+
binding.of_caller(0).eval('var')
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
o. a.should == 1
|
|
16
20
|
end
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
it "should fetch parent of caller's binding when 1 is passed" do
|
|
23
|
+
o = Object.new
|
|
24
|
+
def o.a
|
|
25
|
+
var = 1
|
|
26
|
+
b
|
|
27
|
+
end
|
|
20
28
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
var = 1
|
|
25
|
-
b
|
|
26
|
-
end
|
|
29
|
+
def o.b
|
|
30
|
+
binding.of_caller(1).eval('var')
|
|
31
|
+
end
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
binding.of_caller(1).eval('var')
|
|
33
|
+
o.a.should == 1
|
|
30
34
|
end
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
it "should modify locals in parent of caller's binding" do
|
|
37
|
+
o = Object.new
|
|
38
|
+
def o.a
|
|
39
|
+
var = 1
|
|
40
|
+
b
|
|
41
|
+
var
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def o.b
|
|
45
|
+
binding.of_caller(1).eval('var = 20')
|
|
46
|
+
end
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
o = Object.new
|
|
37
|
-
def o.a
|
|
38
|
-
var = 1
|
|
39
|
-
b
|
|
40
|
-
var
|
|
48
|
+
o.a.should == 20
|
|
41
49
|
end
|
|
42
50
|
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
it "should raise an exception when retrieving an out of band binding" do
|
|
52
|
+
o = Object.new
|
|
53
|
+
def o.a
|
|
54
|
+
binding.of_caller(100)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
lambda { o.a }.should.raise RuntimeError
|
|
45
58
|
end
|
|
59
|
+
end
|
|
46
60
|
|
|
47
|
-
|
|
61
|
+
describe "frame_count" do
|
|
62
|
+
it 'frame_count should equal callers.count' do
|
|
63
|
+
binding.frame_count.should == binding.callers.count
|
|
64
|
+
end
|
|
48
65
|
end
|
|
49
66
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
67
|
+
describe "frame_type" do
|
|
68
|
+
it 'should return the correct frame types' do
|
|
69
|
+
o = Object.new
|
|
70
|
+
|
|
71
|
+
# method frame
|
|
72
|
+
def o.a
|
|
73
|
+
b
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# method frame
|
|
77
|
+
def o.b
|
|
78
|
+
# block frame
|
|
79
|
+
proc do
|
|
80
|
+
binding.callers
|
|
81
|
+
end.call
|
|
82
|
+
end
|
|
83
|
+
caller_bindings = o.a
|
|
84
|
+
caller_bindings[0].frame_type.should == :block
|
|
85
|
+
caller_bindings[1].frame_type.should == :method
|
|
86
|
+
caller_bindings[2].frame_type.should == :method
|
|
54
87
|
end
|
|
55
88
|
|
|
56
|
-
lambda { o.a }.should.raise RuntimeError
|
|
57
89
|
end
|
|
58
90
|
end
|
|
59
91
|
|
metadata
CHANGED
|
@@ -1,37 +1,35 @@
|
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: binding_of_caller
|
|
3
|
-
version: !ruby/object:Gem::Version
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.6.2
|
|
4
5
|
prerelease:
|
|
5
|
-
version: 0.4.0
|
|
6
6
|
platform: ruby
|
|
7
|
-
authors:
|
|
7
|
+
authors:
|
|
8
8
|
- John Mair (banisterfiend)
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
- !ruby/object:Gem::Dependency
|
|
12
|
+
date: 2012-02-02 00:00:00.000000000Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: bacon
|
|
17
|
-
|
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
|
16
|
+
requirement: &70103760947940 !ruby/object:Gem::Requirement
|
|
19
17
|
none: false
|
|
20
|
-
requirements:
|
|
18
|
+
requirements:
|
|
21
19
|
- - ~>
|
|
22
|
-
- !ruby/object:Gem::Version
|
|
23
|
-
version: 1.1
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.1'
|
|
24
22
|
type: :development
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70103760947940
|
|
25
|
+
description: Retrieve the binding of a method's caller. Can also retrieve bindings
|
|
26
|
+
even further up the stack. Currently only works for MRI 1.9.2+
|
|
27
27
|
email: jrmair@gmail.com
|
|
28
28
|
executables: []
|
|
29
|
-
|
|
30
|
-
extensions:
|
|
29
|
+
extensions:
|
|
31
30
|
- ext/binding_of_caller/extconf.rb
|
|
32
31
|
extra_rdoc_files: []
|
|
33
|
-
|
|
34
|
-
files:
|
|
32
|
+
files:
|
|
35
33
|
- .gemtest
|
|
36
34
|
- .gitignore
|
|
37
35
|
- .yardopts
|
|
@@ -94,30 +92,28 @@ files:
|
|
|
94
92
|
- test/test.rb
|
|
95
93
|
homepage: http://github.com/banister/binding_of_caller
|
|
96
94
|
licenses: []
|
|
97
|
-
|
|
98
95
|
post_install_message:
|
|
99
96
|
rdoc_options: []
|
|
100
|
-
|
|
101
|
-
require_paths:
|
|
97
|
+
require_paths:
|
|
102
98
|
- lib
|
|
103
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
100
|
none: false
|
|
105
|
-
requirements:
|
|
106
|
-
- -
|
|
107
|
-
- !ruby/object:Gem::Version
|
|
108
|
-
version:
|
|
109
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ! '>='
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: 1.9.2
|
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
106
|
none: false
|
|
111
|
-
requirements:
|
|
112
|
-
- -
|
|
113
|
-
- !ruby/object:Gem::Version
|
|
114
|
-
version:
|
|
107
|
+
requirements:
|
|
108
|
+
- - ! '>='
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
115
111
|
requirements: []
|
|
116
|
-
|
|
117
112
|
rubyforge_project:
|
|
118
|
-
rubygems_version: 1.8.
|
|
113
|
+
rubygems_version: 1.8.6
|
|
119
114
|
signing_key:
|
|
120
115
|
specification_version: 3
|
|
121
|
-
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
|
|
122
|
-
|
|
116
|
+
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
|
|
117
|
+
further up the stack. Currently only works for MRI 1.9.2+
|
|
118
|
+
test_files:
|
|
123
119
|
- test/test.rb
|