binding_of_caller 0.2.0 → 0.7.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.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.gitignore +8 -0
- data/.travis.yml +23 -0
- data/.yardopts +1 -0
- data/Gemfile +2 -0
- data/LICENSE +25 -0
- data/README.md +42 -10
- data/Rakefile +134 -99
- data/binding_of_caller.gemspec +37 -0
- data/examples/example.rb +41 -0
- data/ext/binding_of_caller/binding_of_caller.c +138 -37
- data/ext/binding_of_caller/extconf.rb +29 -10
- data/ext/binding_of_caller/ruby_headers/192/version.h +0 -8
- data/lib/binding_of_caller/mri2.rb +69 -0
- data/lib/binding_of_caller/rubinius.rb +74 -0
- data/lib/binding_of_caller/version.rb +3 -3
- data/lib/binding_of_caller.rb +11 -19
- data/test/test_binding_of_caller.rb +161 -0
- metadata +86 -52
- data/ext/binding_of_caller/compat.h +0 -57
- data/test/test.rb +0 -12
- /data/ext/binding_of_caller/ruby_headers/192/{gc.h → rubys_gc.h} +0 -0
- /data/ext/binding_of_caller/ruby_headers/193/{gc.h → rubys_gc.h} +0 -0
|
@@ -1,22 +1,32 @@
|
|
|
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 "rubys_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
|
+
#define ruby_threadptr_data_type *threadptr_data_type()
|
|
28
|
+
|
|
29
|
+
#define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
|
|
20
30
|
|
|
21
31
|
static size_t
|
|
22
32
|
binding_memsize(const void *ptr)
|
|
@@ -44,7 +54,11 @@ binding_mark(void *ptr)
|
|
|
44
54
|
if (ptr) {
|
|
45
55
|
bind = ptr;
|
|
46
56
|
RUBY_MARK_UNLESS_NULL(bind->env);
|
|
57
|
+
|
|
58
|
+
#ifdef RUBY_192
|
|
47
59
|
RUBY_MARK_UNLESS_NULL(bind->filename);
|
|
60
|
+
#endif
|
|
61
|
+
|
|
48
62
|
}
|
|
49
63
|
RUBY_MARK_LEAVE("binding");
|
|
50
64
|
}
|
|
@@ -65,60 +79,147 @@ binding_alloc(VALUE klass)
|
|
|
65
79
|
return obj;
|
|
66
80
|
}
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
static bool valid_frame_p(rb_control_frame_t * cfp) {
|
|
71
|
-
return cfp->iseq && !NIL_P(cfp->self);
|
|
82
|
+
static bool ifunc_p(rb_control_frame_t * cfp) {
|
|
83
|
+
return (cfp->flag & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_IFUNC;
|
|
72
84
|
}
|
|
73
85
|
|
|
74
|
-
static rb_control_frame_t *
|
|
75
|
-
|
|
86
|
+
static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
87
|
+
return cfp->iseq && !ifunc_p(cfp) && !NIL_P(cfp->self);
|
|
88
|
+
}
|
|
76
89
|
|
|
77
|
-
|
|
90
|
+
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
91
|
+
while (cfp < limit_cfp) {
|
|
78
92
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
79
93
|
|
|
80
|
-
if (
|
|
94
|
+
if (cfp >= limit_cfp)
|
|
95
|
+
return NULL;
|
|
96
|
+
|
|
97
|
+
if (valid_frame_p(cfp, limit_cfp))
|
|
81
98
|
return cfp;
|
|
82
|
-
else
|
|
83
|
-
error_count += 1;
|
|
84
99
|
}
|
|
85
100
|
|
|
86
|
-
|
|
101
|
+
// beyond end of stack
|
|
102
|
+
return NULL;
|
|
103
|
+
}
|
|
87
104
|
|
|
88
|
-
|
|
89
|
-
|
|
105
|
+
static VALUE
|
|
106
|
+
frametype_name(VALUE flag)
|
|
107
|
+
{
|
|
108
|
+
switch (flag & VM_FRAME_MAGIC_MASK) {
|
|
109
|
+
case VM_FRAME_MAGIC_METHOD: return string2sym("method");
|
|
110
|
+
case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
|
|
111
|
+
case VM_FRAME_MAGIC_CLASS: return string2sym("class");
|
|
112
|
+
case VM_FRAME_MAGIC_TOP: return string2sym("top");
|
|
113
|
+
case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
|
|
114
|
+
case VM_FRAME_MAGIC_PROC: return string2sym("proc");
|
|
115
|
+
case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
|
|
116
|
+
case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
|
|
117
|
+
case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
|
|
118
|
+
default:
|
|
119
|
+
rb_raise(rb_eRuntimeError, "Unknown frame type! got flag: %d", FIX2INT(flag));
|
|
120
|
+
}
|
|
90
121
|
}
|
|
91
122
|
|
|
92
123
|
static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
93
124
|
{
|
|
94
|
-
rb_thread_t *th
|
|
125
|
+
rb_thread_t *th;
|
|
126
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
127
|
+
|
|
95
128
|
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
129
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
96
130
|
int level = FIX2INT(rb_level);
|
|
97
131
|
|
|
98
|
-
|
|
132
|
+
// attempt to locate the nth parent control frame
|
|
133
|
+
for (int i = 0; i < level; i++) {
|
|
99
134
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
100
135
|
|
|
101
|
-
|
|
102
|
-
|
|
136
|
+
if (cfp >= limit_cfp)
|
|
137
|
+
rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
|
|
138
|
+
|
|
139
|
+
// skip invalid frames
|
|
140
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
141
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
142
|
+
}
|
|
103
143
|
|
|
104
144
|
VALUE bindval = binding_alloc(rb_cBinding);
|
|
105
145
|
rb_binding_t *bind;
|
|
106
146
|
|
|
107
|
-
if (cfp == 0)
|
|
147
|
+
if (cfp == 0)
|
|
108
148
|
rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
|
|
109
|
-
}
|
|
110
149
|
|
|
111
150
|
GetBindingPtr(bindval, bind);
|
|
151
|
+
|
|
112
152
|
bind->env = rb_vm_make_env_object(th, cfp);
|
|
113
153
|
bind->filename = cfp->iseq->filename;
|
|
114
154
|
bind->line_no = rb_vm_get_sourceline(cfp);
|
|
155
|
+
|
|
156
|
+
rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
|
|
157
|
+
rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
|
|
158
|
+
|
|
115
159
|
return bindval;
|
|
116
160
|
}
|
|
117
161
|
|
|
162
|
+
static VALUE
|
|
163
|
+
frame_type(VALUE self)
|
|
164
|
+
{
|
|
165
|
+
return rb_iv_get(self, "@frame_type");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static VALUE
|
|
169
|
+
frame_description(VALUE self)
|
|
170
|
+
{
|
|
171
|
+
return rb_iv_get(self, "@frame_description");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static VALUE frame_count(VALUE self)
|
|
175
|
+
{
|
|
176
|
+
rb_thread_t *th;
|
|
177
|
+
GetThreadPtr(rb_thread_current(), th);
|
|
178
|
+
|
|
179
|
+
rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
|
|
180
|
+
rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
|
|
181
|
+
|
|
182
|
+
int i = 1;
|
|
183
|
+
while (cfp < limit_cfp) {
|
|
184
|
+
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
185
|
+
|
|
186
|
+
if (cfp >= limit_cfp)
|
|
187
|
+
return INT2FIX(i);
|
|
188
|
+
|
|
189
|
+
// skip invalid frames
|
|
190
|
+
if (!valid_frame_p(cfp, limit_cfp))
|
|
191
|
+
cfp = find_valid_frame(cfp, limit_cfp);
|
|
192
|
+
|
|
193
|
+
if (!cfp)
|
|
194
|
+
break;
|
|
195
|
+
|
|
196
|
+
i++;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return INT2FIX(i);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
static VALUE
|
|
203
|
+
callers(VALUE self)
|
|
204
|
+
{
|
|
205
|
+
VALUE ary = rb_ary_new();
|
|
206
|
+
|
|
207
|
+
for (int i = 0; i < FIX2INT(frame_count(self)); i++)
|
|
208
|
+
rb_ary_push(ary, binding_of_caller(self, INT2FIX(i)));
|
|
209
|
+
|
|
210
|
+
return ary;
|
|
211
|
+
}
|
|
212
|
+
|
|
118
213
|
void
|
|
119
214
|
Init_binding_of_caller()
|
|
120
215
|
{
|
|
121
|
-
|
|
122
|
-
|
|
216
|
+
VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
|
|
217
|
+
|
|
218
|
+
rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
|
|
219
|
+
rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
|
|
220
|
+
rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
|
|
221
|
+
rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
|
|
222
|
+
rb_define_method(mBindingOfCaller, "callers", callers, 0);
|
|
223
|
+
rb_include_module(rb_cBinding, mBindingOfCaller);
|
|
123
224
|
}
|
|
124
225
|
|
|
@@ -1,14 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
def fake_makefile
|
|
2
|
+
File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") do |f|
|
|
3
|
+
f.puts %[install:\n\techo "Nada."]
|
|
4
|
+
end
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def mri_2?
|
|
8
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
|
|
9
|
+
RUBY_VERSION =~ /^2/
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def rbx?
|
|
13
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
if mri_2? || rbx?
|
|
17
|
+
fake_makefile
|
|
18
|
+
else
|
|
19
|
+
require 'mkmf'
|
|
20
|
+
|
|
21
|
+
$CFLAGS += " -O0"
|
|
22
|
+
$CFLAGS += " -std=c99"
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
24
|
+
case RUBY_VERSION
|
|
25
|
+
when /1.9.2/
|
|
26
|
+
$CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
|
|
27
|
+
when /1.9.3/
|
|
28
|
+
$CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
|
|
29
|
+
end
|
|
5
30
|
|
|
6
|
-
|
|
7
|
-
when /1.9.2/
|
|
8
|
-
$CFLAGS += " -I./ruby_headers/192/"
|
|
9
|
-
when /1.9.3/
|
|
10
|
-
puts "hit 193"
|
|
11
|
-
$CFLAGS += " -I./ruby_headers/193/"
|
|
31
|
+
create_makefile('binding_of_caller')
|
|
12
32
|
end
|
|
13
33
|
|
|
14
|
-
create_makefile('binding_of_caller')
|
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
#define RUBY_VERSION "1.9.2"
|
|
2
|
-
<<<<<<< HEAD
|
|
3
|
-
<<<<<<< HEAD
|
|
4
|
-
#define RUBY_PATCHLEVEL 34
|
|
5
|
-
=======
|
|
6
|
-
#define RUBY_PATCHLEVEL 27
|
|
7
|
-
>>>>>>> 7f5d559... merges r29155 from trunk into ruby_1_9_2. fixes #3777, #3772 and #3722.
|
|
8
|
-
=======
|
|
9
2
|
#define RUBY_PATCHLEVEL 30
|
|
10
|
-
>>>>>>> 13fdd22... merges r29188 from trunk into ruby_1_9_2.
|
|
11
3
|
#define RUBY_VERSION_MAJOR 1
|
|
12
4
|
#define RUBY_VERSION_MINOR 9
|
|
13
5
|
#define RUBY_VERSION_TEENY 1
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'debug_inspector'
|
|
2
|
+
|
|
3
|
+
module BindingOfCaller
|
|
4
|
+
module BindingExtensions
|
|
5
|
+
# Retrieve the binding of the nth caller of the current frame.
|
|
6
|
+
# @return [Binding]
|
|
7
|
+
def of_caller(n)
|
|
8
|
+
c = callers.drop(1)
|
|
9
|
+
if n > (c.size - 1)
|
|
10
|
+
raise "No such frame, gone beyond end of stack!"
|
|
11
|
+
else
|
|
12
|
+
c[n]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Return bindings for all caller frames.
|
|
17
|
+
# @return [Array<Binding>]
|
|
18
|
+
def callers
|
|
19
|
+
ary = []
|
|
20
|
+
|
|
21
|
+
RubyVM::DebugInspector.open do |i|
|
|
22
|
+
n = 0
|
|
23
|
+
loop do
|
|
24
|
+
begin
|
|
25
|
+
b = i.frame_binding(n)
|
|
26
|
+
rescue ArgumentError
|
|
27
|
+
break
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if b
|
|
31
|
+
b.instance_variable_set(:@iseq, i.frame_iseq(n))
|
|
32
|
+
ary << b
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
n += 1
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
ary.drop(1)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Number of parent frames available at the point of call.
|
|
43
|
+
# @return [Fixnum]
|
|
44
|
+
def frame_count
|
|
45
|
+
callers.size - 1
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The type of the frame.
|
|
49
|
+
# @return [Symbol]
|
|
50
|
+
def frame_type
|
|
51
|
+
return nil if !@iseq
|
|
52
|
+
|
|
53
|
+
# apparently the 9th element of the iseq array holds the frame type
|
|
54
|
+
# ...not sure how reliable this is.
|
|
55
|
+
@frame_type ||= @iseq.to_a[9]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The description of the frame.
|
|
59
|
+
# @return [String]
|
|
60
|
+
def frame_description
|
|
61
|
+
return nil if !@iseq
|
|
62
|
+
@frame_description ||= @iseq.label
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class ::Binding
|
|
68
|
+
include BindingOfCaller::BindingExtensions
|
|
69
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module BindingOfCaller
|
|
2
|
+
module BindingExtensions
|
|
3
|
+
|
|
4
|
+
# Retrieve the binding of the nth caller of the current frame.
|
|
5
|
+
# @return [Binding]
|
|
6
|
+
def of_caller(n)
|
|
7
|
+
bt = Rubinius::VM.backtrace(1 + n, true).first
|
|
8
|
+
|
|
9
|
+
raise RuntimeError, "Invalid frame, gone beyond end of stack!" if bt.nil?
|
|
10
|
+
|
|
11
|
+
b = Binding.setup(
|
|
12
|
+
bt.variables,
|
|
13
|
+
bt.variables.method,
|
|
14
|
+
bt.constant_scope,
|
|
15
|
+
bt.variables.self,
|
|
16
|
+
bt
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
b.instance_variable_set :@frame_description,
|
|
20
|
+
bt.describe.gsub("{ } in", "block in")
|
|
21
|
+
|
|
22
|
+
b
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The description of the frame.
|
|
26
|
+
# @return [String]
|
|
27
|
+
def frame_description
|
|
28
|
+
@frame_description
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Return bindings for all caller frames.
|
|
32
|
+
# @return [Array<Binding>]
|
|
33
|
+
def callers
|
|
34
|
+
ary = []
|
|
35
|
+
n = 0
|
|
36
|
+
loop do
|
|
37
|
+
begin
|
|
38
|
+
ary << Binding.of_caller(n)
|
|
39
|
+
rescue
|
|
40
|
+
break
|
|
41
|
+
end
|
|
42
|
+
n += 1
|
|
43
|
+
end
|
|
44
|
+
ary.drop_while do |v|
|
|
45
|
+
!(v.frame_type == :method && v.eval("__method__") == :callers)
|
|
46
|
+
end.drop(1)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Number of parent frames available at the point of call.
|
|
50
|
+
# @return [Fixnum]
|
|
51
|
+
def frame_count
|
|
52
|
+
callers.size - 1
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The type of the frame.
|
|
56
|
+
# @return [Symbol]
|
|
57
|
+
def frame_type
|
|
58
|
+
if compiled_code.for_module_body?
|
|
59
|
+
:class
|
|
60
|
+
elsif compiled_code.for_eval?
|
|
61
|
+
:eval
|
|
62
|
+
elsif compiled_code.is_block?
|
|
63
|
+
:block
|
|
64
|
+
else
|
|
65
|
+
:method
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class ::Binding
|
|
72
|
+
include BindingOfCaller::BindingExtensions
|
|
73
|
+
extend BindingOfCaller::BindingExtensions
|
|
74
|
+
end
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
module BindingOfCaller
|
|
2
|
-
VERSION = "0.2
|
|
3
|
-
end
|
|
1
|
+
module BindingOfCaller
|
|
2
|
+
VERSION = "0.7.2"
|
|
3
|
+
end
|
data/lib/binding_of_caller.rb
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
# (C) John Mair (banisterfiend); MIT license
|
|
1
|
+
dlext = RbConfig::CONFIG['DLEXT']
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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}"
|
|
3
|
+
def mri_2?
|
|
4
|
+
defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
|
|
5
|
+
RUBY_VERSION =~ /^2/
|
|
18
6
|
end
|
|
19
7
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
if mri_2?
|
|
9
|
+
require 'binding_of_caller/mri2'
|
|
10
|
+
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
|
|
11
|
+
require "binding_of_caller.#{dlext}"
|
|
12
|
+
elsif defined?(Rubinius)
|
|
13
|
+
require 'binding_of_caller/rubinius'
|
|
14
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
class Module
|
|
8
|
+
public :remove_const
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
12
|
+
puts "Ruby version: #{RUBY_VERSION}"
|
|
13
|
+
|
|
14
|
+
describe BindingOfCaller do
|
|
15
|
+
describe "of_caller" do
|
|
16
|
+
it "should fetch immediate caller's binding when 0 is passed" do
|
|
17
|
+
o = Object.new
|
|
18
|
+
def o.a
|
|
19
|
+
var = 1
|
|
20
|
+
binding.of_caller(0).eval('var')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
o. a.should == 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "should fetch parent of caller's binding when 1 is passed" do
|
|
27
|
+
o = Object.new
|
|
28
|
+
def o.a
|
|
29
|
+
var = 1
|
|
30
|
+
b
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def o.b
|
|
34
|
+
binding.of_caller(1).eval('var')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
o.a.should == 1
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should modify locals in parent of caller's binding" do
|
|
41
|
+
o = Object.new
|
|
42
|
+
def o.a
|
|
43
|
+
var = 1
|
|
44
|
+
b
|
|
45
|
+
var
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def o.b
|
|
49
|
+
binding.of_caller(1).eval('var = 20')
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
o.a.should == 20
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "should raise an exception when retrieving an out of band binding" do
|
|
56
|
+
o = Object.new
|
|
57
|
+
def o.a
|
|
58
|
+
binding.of_caller(100)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
lambda { o.a }.should.raise RuntimeError
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
describe "callers" do
|
|
66
|
+
before do
|
|
67
|
+
@o = Object.new
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'should return the first non-internal binding when using callers.first' do
|
|
71
|
+
def @o.meth
|
|
72
|
+
x = :a_local
|
|
73
|
+
[binding.callers.first, binding.of_caller(0)]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
b1, b2 = @o.meth
|
|
77
|
+
b1.eval("x").should == :a_local
|
|
78
|
+
b2.eval("x").should == :a_local
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
describe "frame_count" do
|
|
83
|
+
it 'frame_count should equal callers.count' do
|
|
84
|
+
binding.frame_count.should == binding.callers.count
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "frame_descripton" do
|
|
89
|
+
it 'can be called on ordinary binding without raising' do
|
|
90
|
+
lambda { binding.frame_description }.should.not.raise
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'describes a block frame' do
|
|
94
|
+
binding.of_caller(0).frame_description.should =~ /block/
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'describes a method frame' do
|
|
98
|
+
o = Object.new
|
|
99
|
+
def o.horsey_malone
|
|
100
|
+
binding.of_caller(0).frame_description.should =~ /horsey_malone/
|
|
101
|
+
end
|
|
102
|
+
o.horsey_malone
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'describes a class frame' do
|
|
106
|
+
class HorseyMalone
|
|
107
|
+
binding.of_caller(0).frame_description.should =~ /class/i
|
|
108
|
+
end
|
|
109
|
+
Object.remove_const(:HorseyMalone)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
describe "frame_type" do
|
|
114
|
+
it 'can be called on ordinary binding without raising' do
|
|
115
|
+
lambda { binding.frame_type }.should.not.raise
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
describe "when inside a class definition" do
|
|
119
|
+
before do
|
|
120
|
+
class HorseyMalone
|
|
121
|
+
@binding = binding.of_caller(0)
|
|
122
|
+
def self.binding; @binding; end
|
|
123
|
+
end
|
|
124
|
+
@binding = HorseyMalone.binding
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'returns :class' do
|
|
128
|
+
@binding.frame_type.should == :class
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
describe "when evaluated" do
|
|
133
|
+
before { @binding = eval("binding.of_caller(0)") }
|
|
134
|
+
|
|
135
|
+
it 'returns :eval' do
|
|
136
|
+
@binding.frame_type.should == :eval
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
describe "when inside a block" do
|
|
141
|
+
before { @binding = proc { binding.of_caller(0) }.call }
|
|
142
|
+
|
|
143
|
+
it 'returns :block' do
|
|
144
|
+
@binding.frame_type.should == :block
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
describe "when inside an instance method" do
|
|
149
|
+
before do
|
|
150
|
+
o = Object.new
|
|
151
|
+
def o.a; binding.of_caller(0); end
|
|
152
|
+
@binding = o.a;
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it 'returns :method' do
|
|
156
|
+
@binding.frame_type.should == :method
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|