binding_of_caller 0.3.0 → 0.6.3
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 +38 -8
- data/Rakefile +40 -8
- data/examples/example.rb +41 -0
- data/ext/binding_of_caller/binding_of_caller.c +125 -24
- data/ext/binding_of_caller/extconf.rb +2 -3
- data/lib/binding_of_caller/version.rb +1 -1
- data/lib/binding_of_caller.bundle +0 -0
- data/lib/binding_of_caller.rb +82 -0
- data/lib/tester.rb +15 -0
- data/test/test.rb +63 -31
- metadata +36 -38
- data/ext/binding_of_caller/compat.h +0 -57
- data/ext/binding_of_caller/example.rb +0 -39
data/README.md
CHANGED
|
@@ -3,25 +3,55 @@ binding_of_caller
|
|
|
3
3
|
|
|
4
4
|
(C) John Mair (banisterfiend) 2011
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
_Retrieve the binding of a method's caller in MRI 1.9.2_
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
The `binding_of_caller` gem provides the `Binding#of_caller` method.
|
|
9
|
+
|
|
10
|
+
Using `binding_of_caller` we can grab bindings from higher up the call
|
|
11
|
+
stack and evaluate code in that context. Allows access to bindings arbitrarily far up the
|
|
12
|
+
call stack, not limited to just the immediate caller.
|
|
13
|
+
|
|
14
|
+
**Recommended for use only in debugging situations. Do not use this in production apps.**
|
|
15
|
+
|
|
16
|
+
**Only works in MRI Ruby 1.9.2 and 1.9.3**
|
|
9
17
|
|
|
10
18
|
* Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
|
|
11
|
-
* Read the [documentation](http://rdoc.info/github/banister/binding_of_caller/master/file/README.markdown)
|
|
12
19
|
* See the [source code](http://github.com/banister/binding_of_caller)
|
|
13
20
|
|
|
14
|
-
Example:
|
|
21
|
+
Example: Modifying a local inside the caller of a caller
|
|
15
22
|
--------
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
```ruby
|
|
25
|
+
def a
|
|
26
|
+
var = 10
|
|
27
|
+
b
|
|
28
|
+
puts var
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def b
|
|
32
|
+
c
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def c
|
|
36
|
+
binding.of_caller(2).eval('var = :hello')
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
a()
|
|
40
|
+
|
|
41
|
+
# OUTPUT
|
|
42
|
+
# => hello
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Spinoff project
|
|
46
|
+
-------
|
|
18
47
|
|
|
19
|
-
|
|
48
|
+
This project is a spinoff from the [Pry REPL project.](http://pry.github.com)
|
|
20
49
|
|
|
21
50
|
Features and limitations
|
|
22
51
|
-------------------------
|
|
23
52
|
|
|
24
|
-
|
|
53
|
+
* Only works with MRI 1.9.2 and 1.9.3
|
|
54
|
+
* Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
|
|
25
55
|
|
|
26
56
|
Contact
|
|
27
57
|
-------
|
|
@@ -32,7 +62,7 @@ Problems or questions contact me at [github](http://github.com/banister)
|
|
|
32
62
|
License
|
|
33
63
|
-------
|
|
34
64
|
|
|
35
|
-
(The MIT License)
|
|
65
|
+
(The MIT License)
|
|
36
66
|
|
|
37
67
|
Copyright (c) 2011 (John Mair)
|
|
38
68
|
|
data/Rakefile
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
dlext = Config::CONFIG['DLEXT']
|
|
2
|
+
direc = File.dirname(__FILE__)
|
|
2
3
|
|
|
3
4
|
$:.unshift 'lib'
|
|
4
5
|
|
|
5
6
|
PROJECT_NAME = "binding_of_caller"
|
|
6
7
|
|
|
7
8
|
require 'rake/clean'
|
|
8
|
-
require '
|
|
9
|
+
require 'rubygems/package_task'
|
|
10
|
+
|
|
9
11
|
require "#{PROJECT_NAME}/version"
|
|
10
12
|
|
|
11
13
|
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
|
|
12
14
|
CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
|
13
15
|
"ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
|
|
14
|
-
"ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
|
|
16
|
+
"ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
|
|
15
17
|
|
|
16
18
|
def apply_spec_defaults(s)
|
|
17
19
|
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
|
|
20
|
+
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
21
|
s.version = BindingOfCaller::VERSION
|
|
20
22
|
s.date = Time.now.strftime '%Y-%m-%d'
|
|
21
23
|
s.author = "John Mair (banisterfiend)"
|
|
22
24
|
s.email = 'jrmair@gmail.com'
|
|
23
25
|
s.description = s.summary
|
|
24
26
|
s.require_path = 'lib'
|
|
25
|
-
s.add_development_dependency("bacon","~>1.1
|
|
27
|
+
s.add_development_dependency("bacon","~>1.1")
|
|
26
28
|
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
27
29
|
s.has_rdoc = 'yard'
|
|
28
30
|
s.files = `git ls-files`.split("\n")
|
|
@@ -39,6 +41,9 @@ task :pry do
|
|
|
39
41
|
sh "pry -r ./lib/binding_of_caller"
|
|
40
42
|
end
|
|
41
43
|
|
|
44
|
+
desc "generate gemspec"
|
|
45
|
+
task :gemspec => "ruby:gemspec"
|
|
46
|
+
|
|
42
47
|
namespace :ruby do
|
|
43
48
|
spec = Gem::Specification.new do |s|
|
|
44
49
|
apply_spec_defaults(s)
|
|
@@ -46,13 +51,32 @@ namespace :ruby do
|
|
|
46
51
|
s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
|
|
47
52
|
end
|
|
48
53
|
|
|
49
|
-
|
|
54
|
+
Gem::PackageTask.new(spec) do |pkg|
|
|
50
55
|
pkg.need_zip = false
|
|
51
56
|
pkg.need_tar = false
|
|
52
57
|
end
|
|
58
|
+
|
|
59
|
+
desc "Generate gemspec file"
|
|
60
|
+
task :gemspec do
|
|
61
|
+
File.open("#{spec.name}.gemspec", "w") do |f|
|
|
62
|
+
f << spec.to_ruby
|
|
63
|
+
end
|
|
64
|
+
end
|
|
53
65
|
end
|
|
54
66
|
|
|
55
|
-
|
|
67
|
+
namespace :rbx do
|
|
68
|
+
spec = Gem::Specification.new do |s|
|
|
69
|
+
apply_spec_defaults(s)
|
|
70
|
+
s.platform = Gem::Platform.new(["universal", "rubinius"])
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
Gem::PackageTask.new(spec) do |pkg|
|
|
74
|
+
pkg.need_zip = false
|
|
75
|
+
pkg.need_tar = false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
desc "build the binaries"
|
|
56
80
|
task :compile do
|
|
57
81
|
chdir "./ext/#{PROJECT_NAME}/" do
|
|
58
82
|
sh "ruby extconf.rb"
|
|
@@ -62,17 +86,25 @@ task :compile do
|
|
|
62
86
|
end
|
|
63
87
|
end
|
|
64
88
|
|
|
89
|
+
desc "reinstall gem"
|
|
90
|
+
task :reinstall => :gems do
|
|
91
|
+
sh "gem uninstall binding_of_caller" rescue nil
|
|
92
|
+
sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
|
|
93
|
+
end
|
|
94
|
+
|
|
65
95
|
desc "build all platform gems at once"
|
|
66
|
-
task :gems => [:clean, :rmgems, "ruby:gem"]
|
|
96
|
+
task :gems => [:clean, :rmgems, "ruby:gem", "rbx:gem"]
|
|
67
97
|
|
|
68
98
|
task :gem => [:gems]
|
|
69
99
|
|
|
100
|
+
task :rbxgem => "rbx:gem"
|
|
101
|
+
|
|
70
102
|
desc "remove all platform gems"
|
|
71
103
|
task :rmgems => ["ruby:clobber_package"]
|
|
72
104
|
|
|
73
105
|
desc "build and push latest gems"
|
|
74
106
|
task :pushgems => :gems do
|
|
75
|
-
chdir("
|
|
107
|
+
chdir("./pkg") do
|
|
76
108
|
Dir["*.gem"].each do |gemfile|
|
|
77
109
|
sh "gem push #{gemfile}"
|
|
78
110
|
end
|
data/examples/example.rb
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
unless Object.const_defined? :BindingOfCaller
|
|
2
|
+
$:.unshift File.expand_path '../../lib', __FILE__
|
|
3
|
+
require 'binding_of_caller'
|
|
4
|
+
require 'binding_of_caller/version'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
outer = 10
|
|
8
|
+
|
|
9
|
+
class Z
|
|
10
|
+
def z
|
|
11
|
+
u = 10
|
|
12
|
+
A.new.a
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class A
|
|
17
|
+
def a
|
|
18
|
+
y = 10
|
|
19
|
+
B.new.b
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class B
|
|
24
|
+
def b
|
|
25
|
+
x = 10
|
|
26
|
+
puts binding.of_caller(0).eval('local_variables')
|
|
27
|
+
puts binding.of_caller(1).eval('local_variables')
|
|
28
|
+
puts binding.of_caller(2).eval('local_variables')
|
|
29
|
+
puts binding.of_caller(3).eval('local_variables')
|
|
30
|
+
puts binding.of_caller(400).eval('local_variables')
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
Z.new.z
|
|
35
|
+
|
|
36
|
+
# output:
|
|
37
|
+
# => x
|
|
38
|
+
# => y
|
|
39
|
+
# => u
|
|
40
|
+
# => outer
|
|
41
|
+
# Exception
|
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
/* (c) 2011 John Mair (banisterfiend), MIT license */
|
|
2
2
|
|
|
3
3
|
#include <ruby.h>
|
|
4
|
-
|
|
5
|
-
#
|
|
6
|
-
# include <ruby/re.h>
|
|
7
|
-
# include "vm_core.h"
|
|
8
|
-
# include "gc.h"
|
|
4
|
+
#include "vm_core.h"
|
|
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
|
}
|
|
@@ -58,62 +78,143 @@ binding_alloc(VALUE klass)
|
|
|
58
78
|
return obj;
|
|
59
79
|
}
|
|
60
80
|
|
|
61
|
-
static bool valid_frame_p(rb_control_frame_t * cfp) {
|
|
81
|
+
static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
62
82
|
return cfp->iseq && !NIL_P(cfp->self);
|
|
63
83
|
}
|
|
64
84
|
|
|
65
|
-
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
while (error_count <= max_frame_errors) {
|
|
85
|
+
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
86
|
+
while (cfp < limit_cfp) {
|
|
69
87
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
70
88
|
|
|
71
|
-
if (
|
|
89
|
+
if (cfp >= limit_cfp)
|
|
90
|
+
return NULL;
|
|
91
|
+
|
|
92
|
+
if (valid_frame_p(cfp, limit_cfp))
|
|
72
93
|
return cfp;
|
|
73
|
-
else
|
|
74
|
-
error_count += 1;
|
|
75
94
|
}
|
|
76
95
|
|
|
77
|
-
|
|
96
|
+
// beyond end of stack
|
|
97
|
+
return NULL;
|
|
98
|
+
}
|
|
78
99
|
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
}
|
|
81
117
|
}
|
|
82
118
|
|
|
83
119
|
static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
84
120
|
{
|
|
85
|
-
rb_thread_t *th
|
|
121
|
+
rb_thread_t *th;
|
|
122
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
123
|
+
|
|
86
124
|
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
125
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
87
126
|
int level = FIX2INT(rb_level);
|
|
88
127
|
|
|
89
128
|
// attempt to locate the nth parent control frame
|
|
90
|
-
for (int i = 0; i < level; i++)
|
|
129
|
+
for (int i = 0; i < level; i++) {
|
|
91
130
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
92
131
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
132
|
+
if (cfp >= limit_cfp)
|
|
133
|
+
rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
|
|
134
|
+
|
|
135
|
+
// skip invalid frames
|
|
136
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
137
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
138
|
+
}
|
|
96
139
|
|
|
97
140
|
VALUE bindval = binding_alloc(rb_cBinding);
|
|
98
141
|
rb_binding_t *bind;
|
|
99
142
|
|
|
100
|
-
if (cfp == 0)
|
|
143
|
+
if (cfp == 0)
|
|
101
144
|
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
|
|
102
|
-
}
|
|
103
145
|
|
|
104
146
|
GetBindingPtr(bindval, bind);
|
|
105
147
|
bind->env = rb_vm_make_env_object(th, cfp);
|
|
106
148
|
bind->filename = cfp->iseq->filename;
|
|
107
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
|
+
|
|
108
154
|
return bindval;
|
|
109
155
|
}
|
|
110
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
|
+
|
|
111
208
|
void
|
|
112
209
|
Init_binding_of_caller()
|
|
113
210
|
{
|
|
114
211
|
VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
|
|
115
212
|
|
|
116
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);
|
|
117
218
|
rb_include_module(rb_cBinding, mBindingOfCaller);
|
|
118
219
|
}
|
|
119
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')
|
|
Binary file
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
dlext = Config::CONFIG['DLEXT']
|
|
2
|
+
|
|
3
|
+
if RUBY_ENGINE && RUBY_ENGINE == "ruby"
|
|
4
|
+
require "binding_of_caller.#{dlext}"
|
|
5
|
+
|
|
6
|
+
elsif defined?(Rubinius)
|
|
7
|
+
module BindingOfCaller
|
|
8
|
+
module BindingExtensions
|
|
9
|
+
|
|
10
|
+
def of_caller(n)
|
|
11
|
+
bt = Rubinius::VM.backtrace(1 + n, true).first
|
|
12
|
+
|
|
13
|
+
b = Binding.setup(
|
|
14
|
+
bt.variables,
|
|
15
|
+
bt.variables.method,
|
|
16
|
+
bt.static_scope,
|
|
17
|
+
bt.variables.self,
|
|
18
|
+
bt
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
b.instance_variable_set(:@frame_description, bt.describe)
|
|
22
|
+
|
|
23
|
+
b
|
|
24
|
+
rescue
|
|
25
|
+
raise RuntimeError, "Invalid frame, gone beyond end of stack!"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def frame_description
|
|
29
|
+
@frame_description
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def callers
|
|
33
|
+
ary = []
|
|
34
|
+
n = 0
|
|
35
|
+
loop {
|
|
36
|
+
begin
|
|
37
|
+
ary << Binding.of_caller(n)
|
|
38
|
+
rescue
|
|
39
|
+
break
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
n += 1
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
ary
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def frame_count
|
|
49
|
+
ary = []
|
|
50
|
+
n = 1
|
|
51
|
+
loop {
|
|
52
|
+
begin
|
|
53
|
+
Binding.of_caller(n)
|
|
54
|
+
rescue
|
|
55
|
+
break
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
n += 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
n
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def frame_type
|
|
65
|
+
case self.variables.method.metadata.to_a.first.to_s
|
|
66
|
+
when /block/
|
|
67
|
+
:block
|
|
68
|
+
when /eval/
|
|
69
|
+
:eval
|
|
70
|
+
else
|
|
71
|
+
:method
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
class ::Binding
|
|
79
|
+
include BindingOfCaller::BindingExtensions
|
|
80
|
+
extend BindingOfCaller::BindingExtensions
|
|
81
|
+
end
|
|
82
|
+
end
|
data/lib/tester.rb
ADDED
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.3
|
|
4
5
|
prerelease:
|
|
5
|
-
version: 0.3.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-29 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: bacon
|
|
17
|
-
|
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
|
16
|
+
requirement: &70210333909540 !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: *70210333909540
|
|
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
|
|
@@ -39,9 +37,8 @@ files:
|
|
|
39
37
|
- LICENSE
|
|
40
38
|
- README.md
|
|
41
39
|
- Rakefile
|
|
40
|
+
- examples/example.rb
|
|
42
41
|
- ext/binding_of_caller/binding_of_caller.c
|
|
43
|
-
- ext/binding_of_caller/compat.h
|
|
44
|
-
- ext/binding_of_caller/example.rb
|
|
45
42
|
- ext/binding_of_caller/extconf.rb
|
|
46
43
|
- ext/binding_of_caller/ruby_headers/192/debug.h
|
|
47
44
|
- ext/binding_of_caller/ruby_headers/192/dln.h
|
|
@@ -91,34 +88,35 @@ files:
|
|
|
91
88
|
- ext/binding_of_caller/ruby_headers/193/vm_exec.h
|
|
92
89
|
- ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
|
|
93
90
|
- ext/binding_of_caller/ruby_headers/193/vm_opts.h
|
|
91
|
+
- lib/binding_of_caller.bundle
|
|
92
|
+
- lib/binding_of_caller.rb
|
|
94
93
|
- lib/binding_of_caller/version.rb
|
|
94
|
+
- lib/tester.rb
|
|
95
95
|
- test/test.rb
|
|
96
96
|
homepage: http://github.com/banister/binding_of_caller
|
|
97
97
|
licenses: []
|
|
98
|
-
|
|
99
98
|
post_install_message:
|
|
100
99
|
rdoc_options: []
|
|
101
|
-
|
|
102
|
-
require_paths:
|
|
100
|
+
require_paths:
|
|
103
101
|
- lib
|
|
104
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
103
|
none: false
|
|
106
|
-
requirements:
|
|
107
|
-
- -
|
|
108
|
-
- !ruby/object:Gem::Version
|
|
109
|
-
version:
|
|
110
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ! '>='
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '0'
|
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
109
|
none: false
|
|
112
|
-
requirements:
|
|
113
|
-
- -
|
|
114
|
-
- !ruby/object:Gem::Version
|
|
115
|
-
version:
|
|
110
|
+
requirements:
|
|
111
|
+
- - ! '>='
|
|
112
|
+
- !ruby/object:Gem::Version
|
|
113
|
+
version: '0'
|
|
116
114
|
requirements: []
|
|
117
|
-
|
|
118
115
|
rubyforge_project:
|
|
119
|
-
rubygems_version: 1.8.
|
|
116
|
+
rubygems_version: 1.8.16
|
|
120
117
|
signing_key:
|
|
121
118
|
specification_version: 3
|
|
122
|
-
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
|
|
123
|
-
|
|
119
|
+
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
|
|
120
|
+
further up the stack. Currently only works for MRI 1.9.2+
|
|
121
|
+
test_files:
|
|
124
122
|
- test/test.rb
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
|
|
2
|
-
|
|
3
|
-
#ifndef GUARD_COMPAT_H
|
|
4
|
-
#define GUARD_COMPAT_H
|
|
5
|
-
|
|
6
|
-
#include <ruby.h>
|
|
7
|
-
|
|
8
|
-
/* test for 1.9 */
|
|
9
|
-
#if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
|
|
10
|
-
# define RUBY_19
|
|
11
|
-
#endif
|
|
12
|
-
|
|
13
|
-
/* macros for backwards compatibility with 1.8 */
|
|
14
|
-
#ifndef RUBY_19
|
|
15
|
-
# define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
|
|
16
|
-
# define RCLASS_SUPER(c) (RCLASS(c)->super)
|
|
17
|
-
# define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
|
|
18
|
-
# define OBJ_UNTRUSTED OBJ_TAINTED
|
|
19
|
-
# include "st.h"
|
|
20
|
-
#endif
|
|
21
|
-
|
|
22
|
-
#ifdef RUBY_19
|
|
23
|
-
inline static VALUE
|
|
24
|
-
class_alloc(VALUE flags, VALUE klass)
|
|
25
|
-
{
|
|
26
|
-
rb_classext_t *ext = ALLOC(rb_classext_t);
|
|
27
|
-
NEWOBJ(obj, struct RClass);
|
|
28
|
-
OBJSETUP(obj, klass, flags);
|
|
29
|
-
obj->ptr = ext;
|
|
30
|
-
RCLASS_IV_TBL(obj) = 0;
|
|
31
|
-
RCLASS_M_TBL(obj) = 0;
|
|
32
|
-
RCLASS_SUPER(obj) = 0;
|
|
33
|
-
RCLASS_IV_INDEX_TBL(obj) = 0;
|
|
34
|
-
return (VALUE)obj;
|
|
35
|
-
}
|
|
36
|
-
#endif
|
|
37
|
-
|
|
38
|
-
inline static VALUE
|
|
39
|
-
create_class(VALUE flags, VALUE klass)
|
|
40
|
-
{
|
|
41
|
-
#ifdef RUBY_19
|
|
42
|
-
VALUE new_klass = class_alloc(flags, klass);
|
|
43
|
-
#else
|
|
44
|
-
NEWOBJ(new_klass, struct RClass);
|
|
45
|
-
OBJSETUP(new_klass, klass, flags);
|
|
46
|
-
#endif
|
|
47
|
-
|
|
48
|
-
return (VALUE)new_klass;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
# define FALSE 0
|
|
52
|
-
# define TRUE 1
|
|
53
|
-
|
|
54
|
-
/* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
|
|
55
|
-
#define KLASS_OF(c) (RBASIC(c)->klass)
|
|
56
|
-
|
|
57
|
-
#endif
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
require './binding_of_caller'
|
|
2
|
-
|
|
3
|
-
outer = 10
|
|
4
|
-
|
|
5
|
-
class Z
|
|
6
|
-
def z
|
|
7
|
-
u = 10
|
|
8
|
-
A.new.a
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
class A
|
|
13
|
-
def a
|
|
14
|
-
y = 10
|
|
15
|
-
B.new.b
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
class B
|
|
20
|
-
def b
|
|
21
|
-
x = 10
|
|
22
|
-
puts binding_of_caller(0).eval('local_variables')
|
|
23
|
-
puts binding_of_caller(1).eval('local_variables')
|
|
24
|
-
puts binding_of_caller(2).eval('local_variables')
|
|
25
|
-
puts binding_of_caller(3).eval('local_variables')
|
|
26
|
-
puts binding_of_caller(400).eval('local_variables')
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def a; b; end; def b; binding_of_caller(10); end; a;
|
|
31
|
-
|
|
32
|
-
# Z.new.z
|
|
33
|
-
|
|
34
|
-
# output:
|
|
35
|
-
# => x
|
|
36
|
-
# => y
|
|
37
|
-
# => u
|
|
38
|
-
# => outer
|
|
39
|
-
# Exception
|