binding_of_caller 0.2.0 → 0.5.0
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/.gemtest +0 -0
- data/.gitignore +7 -0
- data/.yardopts +1 -0
- data/LICENSE +25 -0
- data/README.md +39 -8
- data/Rakefile +26 -45
- data/examples/example.rb +41 -0
- data/ext/binding_of_caller/binding_of_caller.c +118 -37
- data/lib/binding_of_caller/version.rb +2 -2
- data/test/test.rb +88 -6
- metadata +42 -43
- data/ext/binding_of_caller/compat.h +0 -57
- data/lib/binding_of_caller.rb +0 -22
data/.gemtest
ADDED
|
File without changes
|
data/.gitignore
ADDED
data/.yardopts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--markup markdown
|
data/LICENSE
ADDED
|
@@ -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.
|
data/README.md
CHANGED
|
@@ -3,25 +3,56 @@ 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**
|
|
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
|
|
54
|
+
* Broken in 1.9.3, support will hopefully be provided in the near future.
|
|
55
|
+
* Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
|
|
25
56
|
|
|
26
57
|
Contact
|
|
27
58
|
-------
|
|
@@ -32,7 +63,7 @@ Problems or questions contact me at [github](http://github.com/banister)
|
|
|
32
63
|
License
|
|
33
64
|
-------
|
|
34
65
|
|
|
35
|
-
(The MIT License)
|
|
66
|
+
(The MIT License)
|
|
36
67
|
|
|
37
68
|
Copyright (c) 2011 (John Mair)
|
|
38
69
|
|
data/Rakefile
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
dlext = Config::CONFIG['DLEXT']
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
$:.unshift 'lib'
|
|
3
4
|
|
|
4
5
|
PROJECT_NAME = "binding_of_caller"
|
|
5
6
|
|
|
6
7
|
require 'rake/clean'
|
|
7
8
|
require 'rake/gempackagetask'
|
|
8
|
-
require "#{
|
|
9
|
+
require "#{PROJECT_NAME}/version"
|
|
9
10
|
|
|
10
11
|
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
|
|
11
12
|
CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
|
@@ -14,84 +15,64 @@ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
|
|
14
15
|
|
|
15
16
|
def apply_spec_defaults(s)
|
|
16
17
|
s.name = PROJECT_NAME
|
|
17
|
-
s.summary = "
|
|
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
19
|
s.version = BindingOfCaller::VERSION
|
|
19
20
|
s.date = Time.now.strftime '%Y-%m-%d'
|
|
20
21
|
s.author = "John Mair (banisterfiend)"
|
|
21
22
|
s.email = 'jrmair@gmail.com'
|
|
22
23
|
s.description = s.summary
|
|
23
24
|
s.require_path = 'lib'
|
|
24
|
-
s.add_development_dependency("bacon","
|
|
25
|
-
s.homepage = "http://
|
|
25
|
+
s.add_development_dependency("bacon","~>1.1.0")
|
|
26
|
+
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
26
27
|
s.has_rdoc = 'yard'
|
|
27
|
-
s.files =
|
|
28
|
-
|
|
28
|
+
s.files = `git ls-files`.split("\n")
|
|
29
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
|
29
30
|
end
|
|
30
31
|
|
|
31
|
-
desc "
|
|
32
|
+
desc "Run tests"
|
|
32
33
|
task :test do
|
|
33
|
-
sh "bacon -
|
|
34
|
+
sh "bacon -Itest -rubygems test.rb -q"
|
|
34
35
|
end
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
apply_spec_defaults(s)
|
|
40
|
-
s.platform = "i386-#{v}"
|
|
41
|
-
s.files += FileList["lib/**/*.#{dlext}"].to_a
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
|
45
|
-
pkg.need_zip = false
|
|
46
|
-
pkg.need_tar = false
|
|
47
|
-
end
|
|
48
|
-
end
|
|
37
|
+
task :pry do
|
|
38
|
+
puts "loading binding_of_caller into pry"
|
|
39
|
+
sh "pry -r ./lib/binding_of_caller"
|
|
49
40
|
end
|
|
50
41
|
|
|
51
42
|
namespace :ruby do
|
|
52
43
|
spec = Gem::Specification.new do |s|
|
|
53
|
-
apply_spec_defaults(s)
|
|
44
|
+
apply_spec_defaults(s)
|
|
54
45
|
s.platform = Gem::Platform::RUBY
|
|
55
46
|
s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
|
|
56
47
|
end
|
|
57
|
-
|
|
48
|
+
|
|
58
49
|
Rake::GemPackageTask.new(spec) do |pkg|
|
|
59
50
|
pkg.need_zip = false
|
|
60
51
|
pkg.need_tar = false
|
|
61
52
|
end
|
|
62
53
|
end
|
|
63
54
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
sh
|
|
71
|
-
c:\\devkit\\devkitvars.bat && \
|
|
72
|
-
pik #{pik_ver} && \
|
|
73
|
-
ruby extconf.rb && \
|
|
74
|
-
make clean && \
|
|
75
|
-
make && \
|
|
76
|
-
cp *.so #{direc}/lib/#{ver} \
|
|
77
|
-
}
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
chdir("#{direc}/ext/#{PROJECT_NAME}") do
|
|
81
|
-
build_for.call("187", "1.8")
|
|
82
|
-
build_for.call("192", "1.9")
|
|
55
|
+
desc "build the binaries"
|
|
56
|
+
task :compile do
|
|
57
|
+
chdir "./ext/#{PROJECT_NAME}/" do
|
|
58
|
+
sh "ruby extconf.rb"
|
|
59
|
+
sh "make clean"
|
|
60
|
+
sh "make"
|
|
61
|
+
sh "cp *.#{dlext} ../../lib/"
|
|
83
62
|
end
|
|
84
63
|
end
|
|
85
64
|
|
|
86
65
|
desc "build all platform gems at once"
|
|
87
|
-
task :gems => [:clean, :rmgems, "
|
|
66
|
+
task :gems => [:clean, :rmgems, "ruby:gem"]
|
|
67
|
+
|
|
68
|
+
task :gem => [:gems]
|
|
88
69
|
|
|
89
70
|
desc "remove all platform gems"
|
|
90
71
|
task :rmgems => ["ruby:clobber_package"]
|
|
91
72
|
|
|
92
73
|
desc "build and push latest gems"
|
|
93
74
|
task :pushgems => :gems do
|
|
94
|
-
chdir("
|
|
75
|
+
chdir("./pkg") do
|
|
95
76
|
Dir["*.gem"].each do |gemfile|
|
|
96
77
|
sh "gem push #{gemfile}"
|
|
97
78
|
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,22 +1,31 @@
|
|
|
1
1
|
/* (c) 2011 John Mair (banisterfiend), MIT license */
|
|
2
2
|
|
|
3
3
|
#include <ruby.h>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
#include "vm_core.h"
|
|
5
|
+
#include "gc.h"
|
|
6
|
+
|
|
7
|
+
typedef enum { false, true } bool;
|
|
8
|
+
|
|
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()))
|
|
20
29
|
|
|
21
30
|
static size_t
|
|
22
31
|
binding_memsize(const void *ptr)
|
|
@@ -65,60 +74,132 @@ binding_alloc(VALUE klass)
|
|
|
65
74
|
return obj;
|
|
66
75
|
}
|
|
67
76
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
static bool valid_frame_p(rb_control_frame_t * cfp) {
|
|
77
|
+
static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
71
78
|
return cfp->iseq && !NIL_P(cfp->self);
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
while (error_count <= 4) {
|
|
81
|
+
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
82
|
+
while (cfp < limit_cfp) {
|
|
78
83
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
79
84
|
|
|
80
|
-
if (
|
|
85
|
+
if (cfp >= limit_cfp)
|
|
86
|
+
return NULL;
|
|
87
|
+
|
|
88
|
+
if (valid_frame_p(cfp, limit_cfp))
|
|
81
89
|
return cfp;
|
|
82
|
-
else
|
|
83
|
-
error_count += 1;
|
|
84
90
|
}
|
|
85
91
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// never reached
|
|
89
|
-
return 0;
|
|
92
|
+
// beyond end of stack
|
|
93
|
+
return NULL;
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
93
97
|
{
|
|
94
|
-
rb_thread_t *th
|
|
98
|
+
rb_thread_t *th;
|
|
99
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
100
|
+
|
|
95
101
|
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
102
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
96
103
|
int level = FIX2INT(rb_level);
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
// attempt to locate the nth parent control frame
|
|
106
|
+
for (int i = 0; i < level; i++) {
|
|
99
107
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
100
108
|
|
|
101
|
-
|
|
102
|
-
|
|
109
|
+
if (cfp >= limit_cfp)
|
|
110
|
+
rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
|
|
111
|
+
|
|
112
|
+
// skip invalid frames
|
|
113
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
114
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
115
|
+
}
|
|
103
116
|
|
|
104
117
|
VALUE bindval = binding_alloc(rb_cBinding);
|
|
105
118
|
rb_binding_t *bind;
|
|
106
119
|
|
|
107
|
-
if (cfp == 0)
|
|
120
|
+
if (cfp == 0)
|
|
108
121
|
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
|
|
109
|
-
}
|
|
110
122
|
|
|
111
123
|
GetBindingPtr(bindval, bind);
|
|
112
124
|
bind->env = rb_vm_make_env_object(th, cfp);
|
|
113
125
|
bind->filename = cfp->iseq->filename;
|
|
114
126
|
bind->line_no = rb_vm_get_sourceline(cfp);
|
|
127
|
+
|
|
128
|
+
rb_iv_set(bindval, "@frame_type", cfp->flag);
|
|
115
129
|
return bindval;
|
|
116
130
|
}
|
|
117
131
|
|
|
132
|
+
|
|
133
|
+
static VALUE
|
|
134
|
+
frametype_name(VALUE flag)
|
|
135
|
+
{
|
|
136
|
+
switch (flag & VM_FRAME_MAGIC_MASK) {
|
|
137
|
+
case VM_FRAME_MAGIC_METHOD: return string2sym("method");
|
|
138
|
+
case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
|
|
139
|
+
case VM_FRAME_MAGIC_CLASS: return string2sym("class");
|
|
140
|
+
case VM_FRAME_MAGIC_TOP: return string2sym("top");
|
|
141
|
+
case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
|
|
142
|
+
case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
|
|
143
|
+
case VM_FRAME_MAGIC_PROC: return string2sym("proc");
|
|
144
|
+
case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
|
|
145
|
+
case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
|
|
146
|
+
case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
|
|
147
|
+
default:
|
|
148
|
+
rb_raise(rb_eRuntimeError, "frame_type can only be returned for bindings created with Binding#of_caller().");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static VALUE
|
|
153
|
+
frame_type(VALUE self)
|
|
154
|
+
{
|
|
155
|
+
return frametype_name(rb_iv_get(self, "@frame_type"));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
static VALUE frame_count(VALUE self)
|
|
159
|
+
{
|
|
160
|
+
rb_thread_t *th;
|
|
161
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
162
|
+
|
|
163
|
+
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
164
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
165
|
+
|
|
166
|
+
int i = 1;
|
|
167
|
+
while (cfp < limit_cfp) {
|
|
168
|
+
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
169
|
+
|
|
170
|
+
if (cfp >= limit_cfp)
|
|
171
|
+
return INT2FIX(i);
|
|
172
|
+
|
|
173
|
+
// skip invalid frames
|
|
174
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
175
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
176
|
+
|
|
177
|
+
i++;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return INT2FIX(i);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
static VALUE
|
|
184
|
+
callers(VALUE self)
|
|
185
|
+
{
|
|
186
|
+
VALUE ary = rb_ary_new();
|
|
187
|
+
|
|
188
|
+
for (int i = 0; i < FIX2INT(frame_count(self)); i++)
|
|
189
|
+
rb_ary_push(ary, binding_of_caller(self, INT2FIX(i)));
|
|
190
|
+
|
|
191
|
+
return ary;
|
|
192
|
+
}
|
|
193
|
+
|
|
118
194
|
void
|
|
119
195
|
Init_binding_of_caller()
|
|
120
196
|
{
|
|
121
|
-
|
|
197
|
+
VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
|
|
122
198
|
|
|
199
|
+
rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
|
|
200
|
+
rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
|
|
201
|
+
rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
|
|
202
|
+
rb_define_method(mBindingOfCaller, "callers", callers, 0);
|
|
203
|
+
rb_include_module(rb_cBinding, mBindingOfCaller);
|
|
123
204
|
}
|
|
124
205
|
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
module BindingOfCaller
|
|
2
|
-
VERSION = "0.
|
|
3
|
-
end
|
|
2
|
+
VERSION = "0.5.0"
|
|
3
|
+
end
|
data/test/test.rb
CHANGED
|
@@ -1,12 +1,94 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require '
|
|
4
|
-
require
|
|
5
|
-
|
|
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
6
|
|
|
7
|
-
puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
7
|
+
puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
8
8
|
puts "Ruby version: #{RUBY_VERSION}"
|
|
9
9
|
|
|
10
10
|
describe BindingOfCaller do
|
|
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
|
|
20
|
+
end
|
|
21
|
+
|
|
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
|
|
28
|
+
|
|
29
|
+
def o.b
|
|
30
|
+
binding.of_caller(1).eval('var')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
o.a.should == 1
|
|
34
|
+
end
|
|
35
|
+
|
|
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
|
|
47
|
+
|
|
48
|
+
o.a.should == 20
|
|
49
|
+
end
|
|
50
|
+
|
|
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
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe "frame_count" do
|
|
62
|
+
it 'frame_count should equal callers.count' do
|
|
63
|
+
binding.frame_count.should == binding.callers.count
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
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
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it 'should raise when invoked on an ordinary binding (i.e one not generated through Binding#of_caller)' do
|
|
90
|
+
lambda { binding.frame_type }.should.raise RuntimeError
|
|
91
|
+
end
|
|
92
|
+
end
|
|
11
93
|
end
|
|
12
94
|
|
metadata
CHANGED
|
@@ -1,39 +1,45 @@
|
|
|
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.5.0
|
|
4
5
|
prerelease:
|
|
5
|
-
version: 0.2.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: 2011-11-05 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: &70308421840640 !ruby/object:Gem::Requirement
|
|
19
17
|
none: false
|
|
20
|
-
requirements:
|
|
21
|
-
- -
|
|
22
|
-
- !ruby/object:Gem::Version
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
23
21
|
version: 1.1.0
|
|
24
22
|
type: :development
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70308421840640
|
|
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
|
-
|
|
32
|
+
files:
|
|
33
|
+
- .gemtest
|
|
34
|
+
- .gitignore
|
|
35
|
+
- .yardopts
|
|
36
|
+
- HISTORY
|
|
37
|
+
- LICENSE
|
|
38
|
+
- README.md
|
|
39
|
+
- Rakefile
|
|
40
|
+
- examples/example.rb
|
|
41
|
+
- ext/binding_of_caller/binding_of_caller.c
|
|
35
42
|
- ext/binding_of_caller/extconf.rb
|
|
36
|
-
- ext/binding_of_caller/compat.h
|
|
37
43
|
- ext/binding_of_caller/ruby_headers/192/debug.h
|
|
38
44
|
- ext/binding_of_caller/ruby_headers/192/dln.h
|
|
39
45
|
- ext/binding_of_caller/ruby_headers/192/eval_intern.h
|
|
@@ -82,39 +88,32 @@ files:
|
|
|
82
88
|
- ext/binding_of_caller/ruby_headers/193/vm_exec.h
|
|
83
89
|
- ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
|
|
84
90
|
- ext/binding_of_caller/ruby_headers/193/vm_opts.h
|
|
85
|
-
- ext/binding_of_caller/binding_of_caller.c
|
|
86
91
|
- lib/binding_of_caller/version.rb
|
|
87
|
-
- lib/binding_of_caller.rb
|
|
88
92
|
- test/test.rb
|
|
89
|
-
|
|
90
|
-
- README.md
|
|
91
|
-
- Rakefile
|
|
92
|
-
homepage: http://banisterfiend.wordpress.com
|
|
93
|
+
homepage: http://github.com/banister/binding_of_caller
|
|
93
94
|
licenses: []
|
|
94
|
-
|
|
95
95
|
post_install_message:
|
|
96
96
|
rdoc_options: []
|
|
97
|
-
|
|
98
|
-
require_paths:
|
|
97
|
+
require_paths:
|
|
99
98
|
- lib
|
|
100
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
100
|
none: false
|
|
102
|
-
requirements:
|
|
103
|
-
- -
|
|
104
|
-
- !ruby/object:Gem::Version
|
|
105
|
-
version:
|
|
106
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
|
+
requirements:
|
|
102
|
+
- - ! '>='
|
|
103
|
+
- !ruby/object:Gem::Version
|
|
104
|
+
version: '0'
|
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
106
|
none: false
|
|
108
|
-
requirements:
|
|
109
|
-
- -
|
|
110
|
-
- !ruby/object:Gem::Version
|
|
111
|
-
version:
|
|
107
|
+
requirements:
|
|
108
|
+
- - ! '>='
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
112
111
|
requirements: []
|
|
113
|
-
|
|
114
112
|
rubyforge_project:
|
|
115
113
|
rubygems_version: 1.8.11
|
|
116
114
|
signing_key:
|
|
117
115
|
specification_version: 3
|
|
118
|
-
summary:
|
|
119
|
-
|
|
120
|
-
|
|
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:
|
|
119
|
+
- 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
|
data/lib/binding_of_caller.rb
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# binding_of_caller.rb
|
|
2
|
-
# (C) John Mair (banisterfiend); MIT license
|
|
3
|
-
|
|
4
|
-
direc = File.dirname(__FILE__)
|
|
5
|
-
|
|
6
|
-
require "#{direc}/binding_of_caller/version"
|
|
7
|
-
|
|
8
|
-
begin
|
|
9
|
-
if RUBY_VERSION =~ /1.9/
|
|
10
|
-
require "#{direc}/1.9/binding_of_caller"
|
|
11
|
-
else
|
|
12
|
-
require "#{direc}/1.8/binding_of_caller"
|
|
13
|
-
end
|
|
14
|
-
rescue LoadError => e
|
|
15
|
-
require "rbconfig"
|
|
16
|
-
dlext = Config::CONFIG['DLEXT']
|
|
17
|
-
require "#{direc}/binding_of_caller.#{dlext}"
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
module BindingOfCaller
|
|
21
|
-
end
|
|
22
|
-
|