binding_of_caller 0.4.0 → 0.6.4
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/.travis.yml +14 -0
- data/Gemfile +2 -0
- data/README.md +3 -4
- data/Rakefile +46 -7
- data/binding_of_caller.gemspec +34 -0
- data/ext/binding_of_caller/binding_of_caller.c +115 -17
- data/ext/binding_of_caller/extconf.rb +20 -10
- data/lib/binding_of_caller/version.rb +3 -3
- data/lib/binding_of_caller.bundle +0 -0
- data/lib/binding_of_caller.rb +86 -0
- data/lib/tester.rb +15 -0
- data/test/test_binding_of_caller.rb +91 -0
- metadata +44 -8
- data/test/test.rb +0 -59
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
CHANGED
|
@@ -3,7 +3,7 @@ binding_of_caller
|
|
|
3
3
|
|
|
4
4
|
(C) John Mair (banisterfiend) 2011
|
|
5
5
|
|
|
6
|
-
_Retrieve the binding of a method's caller in MRI 1.9.
|
|
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
9
|
|
|
@@ -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
|
@@ -1,37 +1,48 @@
|
|
|
1
|
-
dlext =
|
|
1
|
+
dlext = RbConfig::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.
|
|
20
|
+
s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
|
|
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")
|
|
28
|
+
s.add_development_dependency('rake', '~> 0.9')
|
|
26
29
|
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
27
30
|
s.has_rdoc = 'yard'
|
|
28
31
|
s.files = `git ls-files`.split("\n")
|
|
29
32
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
|
30
33
|
end
|
|
31
34
|
|
|
35
|
+
desc "Show version"
|
|
36
|
+
task :version do
|
|
37
|
+
puts "BindingOfCaller version: #{BindingOfCaller::VERSION}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
desc "run tests"
|
|
41
|
+
task :default => :test
|
|
42
|
+
|
|
32
43
|
desc "Run tests"
|
|
33
44
|
task :test do
|
|
34
|
-
sh "bacon -Itest -rubygems
|
|
45
|
+
sh "bacon -Itest -rubygems -a -q"
|
|
35
46
|
end
|
|
36
47
|
|
|
37
48
|
task :pry do
|
|
@@ -39,6 +50,9 @@ task :pry do
|
|
|
39
50
|
sh "pry -r ./lib/binding_of_caller"
|
|
40
51
|
end
|
|
41
52
|
|
|
53
|
+
desc "generate gemspec"
|
|
54
|
+
task :gemspec => "ruby:gemspec"
|
|
55
|
+
|
|
42
56
|
namespace :ruby do
|
|
43
57
|
spec = Gem::Specification.new do |s|
|
|
44
58
|
apply_spec_defaults(s)
|
|
@@ -46,12 +60,31 @@ namespace :ruby do
|
|
|
46
60
|
s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
|
|
47
61
|
end
|
|
48
62
|
|
|
49
|
-
|
|
63
|
+
Gem::PackageTask.new(spec) do |pkg|
|
|
50
64
|
pkg.need_zip = false
|
|
51
65
|
pkg.need_tar = false
|
|
52
66
|
end
|
|
67
|
+
|
|
68
|
+
desc "Generate gemspec file"
|
|
69
|
+
task :gemspec do
|
|
70
|
+
File.open("#{spec.name}.gemspec", "w") do |f|
|
|
71
|
+
f << spec.to_ruby
|
|
72
|
+
end
|
|
73
|
+
end
|
|
53
74
|
end
|
|
54
75
|
|
|
76
|
+
# namespace :rbx do
|
|
77
|
+
# spec = Gem::Specification.new do |s|
|
|
78
|
+
# apply_spec_defaults(s)
|
|
79
|
+
# s.platform = Gem::Platform::RUBY #.new(["universal", "rubinius"])
|
|
80
|
+
# end
|
|
81
|
+
|
|
82
|
+
# Gem::PackageTask.new(spec) do |pkg|
|
|
83
|
+
# pkg.need_zip = false
|
|
84
|
+
# pkg.need_tar = false
|
|
85
|
+
# end
|
|
86
|
+
# end
|
|
87
|
+
|
|
55
88
|
desc "build the binaries"
|
|
56
89
|
task :compile do
|
|
57
90
|
chdir "./ext/#{PROJECT_NAME}/" do
|
|
@@ -62,6 +95,12 @@ task :compile do
|
|
|
62
95
|
end
|
|
63
96
|
end
|
|
64
97
|
|
|
98
|
+
desc "reinstall gem"
|
|
99
|
+
task :reinstall => :gems do
|
|
100
|
+
sh "gem uninstall binding_of_caller" rescue nil
|
|
101
|
+
sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
|
|
102
|
+
end
|
|
103
|
+
|
|
65
104
|
desc "build all platform gems at once"
|
|
66
105
|
task :gems => [:clean, :rmgems, "ruby:gem"]
|
|
67
106
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = "binding_of_caller"
|
|
5
|
+
s.version = "0.6.4"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["John Mair (banisterfiend)"]
|
|
9
|
+
s.date = "2012-02-29"
|
|
10
|
+
s.description = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
|
|
11
|
+
s.email = "jrmair@gmail.com"
|
|
12
|
+
s.extensions = ["ext/binding_of_caller/extconf.rb"]
|
|
13
|
+
s.files = [".gemtest", ".gitignore", ".travis.yml", ".yardopts", "Gemfile", "HISTORY", "LICENSE", "README.md", "Rakefile", "binding_of_caller.gemspec", "examples/example.rb", "ext/binding_of_caller/binding_of_caller.c", "ext/binding_of_caller/extconf.rb", "ext/binding_of_caller/ruby_headers/192/debug.h", "ext/binding_of_caller/ruby_headers/192/dln.h", "ext/binding_of_caller/ruby_headers/192/eval_intern.h", "ext/binding_of_caller/ruby_headers/192/gc.h", "ext/binding_of_caller/ruby_headers/192/id.h", "ext/binding_of_caller/ruby_headers/192/iseq.h", "ext/binding_of_caller/ruby_headers/192/method.h", "ext/binding_of_caller/ruby_headers/192/node.h", "ext/binding_of_caller/ruby_headers/192/regenc.h", "ext/binding_of_caller/ruby_headers/192/regint.h", "ext/binding_of_caller/ruby_headers/192/regparse.h", "ext/binding_of_caller/ruby_headers/192/thread_pthread.h", "ext/binding_of_caller/ruby_headers/192/thread_win32.h", "ext/binding_of_caller/ruby_headers/192/timev.h", "ext/binding_of_caller/ruby_headers/192/transcode_data.h", "ext/binding_of_caller/ruby_headers/192/version.h", "ext/binding_of_caller/ruby_headers/192/vm_core.h", "ext/binding_of_caller/ruby_headers/192/vm_exec.h", "ext/binding_of_caller/ruby_headers/192/vm_insnhelper.h", "ext/binding_of_caller/ruby_headers/192/vm_opts.h", "ext/binding_of_caller/ruby_headers/193/addr2line.h", "ext/binding_of_caller/ruby_headers/193/atomic.h", "ext/binding_of_caller/ruby_headers/193/constant.h", "ext/binding_of_caller/ruby_headers/193/debug.h", "ext/binding_of_caller/ruby_headers/193/dln.h", "ext/binding_of_caller/ruby_headers/193/encdb.h", "ext/binding_of_caller/ruby_headers/193/eval_intern.h", "ext/binding_of_caller/ruby_headers/193/gc.h", "ext/binding_of_caller/ruby_headers/193/id.h", "ext/binding_of_caller/ruby_headers/193/internal.h", "ext/binding_of_caller/ruby_headers/193/iseq.h", "ext/binding_of_caller/ruby_headers/193/method.h", "ext/binding_of_caller/ruby_headers/193/node.h", "ext/binding_of_caller/ruby_headers/193/parse.h", "ext/binding_of_caller/ruby_headers/193/regenc.h", "ext/binding_of_caller/ruby_headers/193/regint.h", "ext/binding_of_caller/ruby_headers/193/regparse.h", "ext/binding_of_caller/ruby_headers/193/revision.h", "ext/binding_of_caller/ruby_headers/193/thread_pthread.h", "ext/binding_of_caller/ruby_headers/193/thread_win32.h", "ext/binding_of_caller/ruby_headers/193/timev.h", "ext/binding_of_caller/ruby_headers/193/transcode_data.h", "ext/binding_of_caller/ruby_headers/193/transdb.h", "ext/binding_of_caller/ruby_headers/193/version.h", "ext/binding_of_caller/ruby_headers/193/vm_core.h", "ext/binding_of_caller/ruby_headers/193/vm_exec.h", "ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h", "ext/binding_of_caller/ruby_headers/193/vm_opts.h", "lib/binding_of_caller.bundle", "lib/binding_of_caller.rb", "lib/binding_of_caller/version.rb", "lib/tester.rb", "test/test_binding_of_caller.rb"]
|
|
14
|
+
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
15
|
+
s.require_paths = ["lib"]
|
|
16
|
+
s.rubygems_version = "1.8.12"
|
|
17
|
+
s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
|
|
18
|
+
s.test_files = ["test/test_binding_of_caller.rb"]
|
|
19
|
+
|
|
20
|
+
if s.respond_to? :specification_version then
|
|
21
|
+
s.specification_version = 3
|
|
22
|
+
|
|
23
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
24
|
+
s.add_development_dependency(%q<bacon>, ["~> 1.1"])
|
|
25
|
+
s.add_development_dependency(%q<rake>, ["~> 0.9"])
|
|
26
|
+
else
|
|
27
|
+
s.add_dependency(%q<bacon>, ["~> 1.1"])
|
|
28
|
+
s.add_dependency(%q<rake>, ["~> 0.9"])
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
s.add_dependency(%q<bacon>, ["~> 1.1"])
|
|
32
|
+
s.add_dependency(%q<rake>, ["~> 0.9"])
|
|
33
|
+
end
|
|
34
|
+
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
|
|
|
@@ -1,14 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
def fake_makefile
|
|
2
|
+
File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") {|f|
|
|
3
|
+
f.puts %[install:\n\techo "Nada."]
|
|
4
|
+
}
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
if RUBY_ENGINE && RUBY_ENGINE =~ /rbx/
|
|
8
|
+
fake_makefile
|
|
9
|
+
else
|
|
10
|
+
require 'mkmf'
|
|
11
|
+
|
|
12
|
+
$CFLAGS += " -O0"
|
|
13
|
+
$CFLAGS += " -std=c99"
|
|
2
14
|
|
|
3
|
-
|
|
4
|
-
|
|
15
|
+
case RUBY_VERSION
|
|
16
|
+
when /1.9.2/
|
|
17
|
+
$CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
|
|
18
|
+
when /1.9.3/
|
|
19
|
+
$CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
|
|
20
|
+
end
|
|
5
21
|
|
|
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/"
|
|
22
|
+
create_makefile('binding_of_caller')
|
|
12
23
|
end
|
|
13
24
|
|
|
14
|
-
create_makefile('binding_of_caller')
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
module BindingOfCaller
|
|
2
|
-
VERSION = "0.4
|
|
3
|
-
end
|
|
1
|
+
module BindingOfCaller
|
|
2
|
+
VERSION = "0.6.4"
|
|
3
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
dlext = Config::CONFIG['DLEXT']
|
|
2
|
+
direc = File.dirname(__FILE__)
|
|
3
|
+
|
|
4
|
+
if RUBY_ENGINE && RUBY_ENGINE == "ruby"
|
|
5
|
+
require File.join direc, "binding_of_caller.#{dlext}"
|
|
6
|
+
|
|
7
|
+
elsif defined?(Rubinius)
|
|
8
|
+
module BindingOfCaller
|
|
9
|
+
module BindingExtensions
|
|
10
|
+
|
|
11
|
+
def of_caller(n)
|
|
12
|
+
bt = Rubinius::VM.backtrace(1 + n, true).first
|
|
13
|
+
|
|
14
|
+
b = Binding.setup(
|
|
15
|
+
bt.variables,
|
|
16
|
+
bt.variables.method,
|
|
17
|
+
bt.static_scope,
|
|
18
|
+
bt.variables.self,
|
|
19
|
+
bt
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
b.instance_variable_set(:@frame_description, bt.describe)
|
|
23
|
+
|
|
24
|
+
b
|
|
25
|
+
rescue
|
|
26
|
+
raise RuntimeError, "Invalid frame, gone beyond end of stack!"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def frame_description
|
|
30
|
+
@frame_description
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def callers
|
|
34
|
+
ary = []
|
|
35
|
+
n = 0
|
|
36
|
+
loop {
|
|
37
|
+
begin
|
|
38
|
+
ary << Binding.of_caller(n)
|
|
39
|
+
rescue
|
|
40
|
+
break
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
n += 1
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
ary
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def frame_count
|
|
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
|
+
if frame_description =~ /__(?:class|module)_init__/
|
|
72
|
+
:class
|
|
73
|
+
else
|
|
74
|
+
:method
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
class ::Binding
|
|
83
|
+
include BindingOfCaller::BindingExtensions
|
|
84
|
+
extend BindingOfCaller::BindingExtensions
|
|
85
|
+
end
|
|
86
|
+
end
|
data/lib/tester.rb
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
8
|
+
puts "Ruby version: #{RUBY_VERSION}"
|
|
9
|
+
|
|
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
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
metadata
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: binding_of_caller
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 550684568609937877
|
|
4
5
|
prerelease:
|
|
5
|
-
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 6
|
|
9
|
+
- 4
|
|
10
|
+
version: 0.6.4
|
|
6
11
|
platform: ruby
|
|
7
12
|
authors:
|
|
8
13
|
- John Mair (banisterfiend)
|
|
@@ -10,7 +15,7 @@ autorequire:
|
|
|
10
15
|
bindir: bin
|
|
11
16
|
cert_chain: []
|
|
12
17
|
|
|
13
|
-
date:
|
|
18
|
+
date: 2012-03-01 00:00:00 Z
|
|
14
19
|
dependencies:
|
|
15
20
|
- !ruby/object:Gem::Dependency
|
|
16
21
|
name: bacon
|
|
@@ -20,10 +25,29 @@ dependencies:
|
|
|
20
25
|
requirements:
|
|
21
26
|
- - ~>
|
|
22
27
|
- !ruby/object:Gem::Version
|
|
23
|
-
|
|
28
|
+
hash: 3882215148898798703
|
|
29
|
+
segments:
|
|
30
|
+
- 1
|
|
31
|
+
- 1
|
|
32
|
+
version: "1.1"
|
|
24
33
|
type: :development
|
|
25
34
|
version_requirements: *id001
|
|
26
|
-
|
|
35
|
+
- !ruby/object:Gem::Dependency
|
|
36
|
+
name: rake
|
|
37
|
+
prerelease: false
|
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ~>
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
hash: 2854635824043747355
|
|
44
|
+
segments:
|
|
45
|
+
- 0
|
|
46
|
+
- 9
|
|
47
|
+
version: "0.9"
|
|
48
|
+
type: :development
|
|
49
|
+
version_requirements: *id002
|
|
50
|
+
description: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
|
|
27
51
|
email: jrmair@gmail.com
|
|
28
52
|
executables: []
|
|
29
53
|
|
|
@@ -34,11 +58,14 @@ extra_rdoc_files: []
|
|
|
34
58
|
files:
|
|
35
59
|
- .gemtest
|
|
36
60
|
- .gitignore
|
|
61
|
+
- .travis.yml
|
|
37
62
|
- .yardopts
|
|
63
|
+
- Gemfile
|
|
38
64
|
- HISTORY
|
|
39
65
|
- LICENSE
|
|
40
66
|
- README.md
|
|
41
67
|
- Rakefile
|
|
68
|
+
- binding_of_caller.gemspec
|
|
42
69
|
- examples/example.rb
|
|
43
70
|
- ext/binding_of_caller/binding_of_caller.c
|
|
44
71
|
- ext/binding_of_caller/extconf.rb
|
|
@@ -90,8 +117,11 @@ files:
|
|
|
90
117
|
- ext/binding_of_caller/ruby_headers/193/vm_exec.h
|
|
91
118
|
- ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
|
|
92
119
|
- ext/binding_of_caller/ruby_headers/193/vm_opts.h
|
|
120
|
+
- lib/binding_of_caller.bundle
|
|
121
|
+
- lib/binding_of_caller.rb
|
|
93
122
|
- lib/binding_of_caller/version.rb
|
|
94
|
-
-
|
|
123
|
+
- lib/tester.rb
|
|
124
|
+
- test/test_binding_of_caller.rb
|
|
95
125
|
homepage: http://github.com/banister/binding_of_caller
|
|
96
126
|
licenses: []
|
|
97
127
|
|
|
@@ -105,19 +135,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
105
135
|
requirements:
|
|
106
136
|
- - ">="
|
|
107
137
|
- !ruby/object:Gem::Version
|
|
138
|
+
hash: 2002549777813010636
|
|
139
|
+
segments:
|
|
140
|
+
- 0
|
|
108
141
|
version: "0"
|
|
109
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
143
|
none: false
|
|
111
144
|
requirements:
|
|
112
145
|
- - ">="
|
|
113
146
|
- !ruby/object:Gem::Version
|
|
147
|
+
hash: 2002549777813010636
|
|
148
|
+
segments:
|
|
149
|
+
- 0
|
|
114
150
|
version: "0"
|
|
115
151
|
requirements: []
|
|
116
152
|
|
|
117
153
|
rubyforge_project:
|
|
118
|
-
rubygems_version: 1.8.
|
|
154
|
+
rubygems_version: 1.8.12
|
|
119
155
|
signing_key:
|
|
120
156
|
specification_version: 3
|
|
121
|
-
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
|
|
157
|
+
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
|
|
122
158
|
test_files:
|
|
123
|
-
- test/
|
|
159
|
+
- test/test_binding_of_caller.rb
|
data/test/test.rb
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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
|
-
puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
|
|
8
|
-
puts "Ruby version: #{RUBY_VERSION}"
|
|
9
|
-
|
|
10
|
-
describe BindingOfCaller do
|
|
11
|
-
it "should fetch immediate caller's binding when 0 is passed" do
|
|
12
|
-
o = Object.new
|
|
13
|
-
def o.a
|
|
14
|
-
var = 1
|
|
15
|
-
binding.of_caller(0).eval('var')
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
o. a.should == 1
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
it "should fetch parent of caller's binding when 1 is passed" do
|
|
22
|
-
o = Object.new
|
|
23
|
-
def o.a
|
|
24
|
-
var = 1
|
|
25
|
-
b
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def o.b
|
|
29
|
-
binding.of_caller(1).eval('var')
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
o.a.should == 1
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
it "should modify locals in parent of caller's binding" do
|
|
36
|
-
o = Object.new
|
|
37
|
-
def o.a
|
|
38
|
-
var = 1
|
|
39
|
-
b
|
|
40
|
-
var
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def o.b
|
|
44
|
-
binding.of_caller(1).eval('var = 20')
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
o.a.should == 20
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it "should raise an exception when retrieving an out of band binding" do
|
|
51
|
-
o = Object.new
|
|
52
|
-
def o.a
|
|
53
|
-
binding.of_caller(100)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
lambda { o.a }.should.raise RuntimeError
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|