binding_of_caller 0.5.0 → 0.6.8
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 +23 -0
- data/Gemfile +2 -0
- data/README.md +5 -4
- data/Rakefile +125 -80
- data/binding_of_caller.gemspec +34 -0
- data/ext/binding_of_caller/binding_of_caller.c +39 -20
- data/ext/binding_of_caller/extconf.rb +20 -10
- data/lib/binding_of_caller/version.rb +3 -3
- data/lib/binding_of_caller.rb +85 -0
- data/test/{test.rb → test_binding_of_caller.rb} +108 -94
- metadata +25 -10
data/.travis.yml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
rvm:
|
|
2
|
+
- 1.9.2
|
|
3
|
+
- 1.9.3
|
|
4
|
+
- rbx-18mode
|
|
5
|
+
- rbx-19mode
|
|
6
|
+
|
|
7
|
+
notifications:
|
|
8
|
+
irc: "irc.freenode.org#pry"
|
|
9
|
+
recipients:
|
|
10
|
+
- jrmair@gmail.com
|
|
11
|
+
|
|
12
|
+
branches:
|
|
13
|
+
only:
|
|
14
|
+
- master
|
|
15
|
+
|
|
16
|
+
#script: rake test --trace
|
|
17
|
+
#
|
|
18
|
+
#before_install:
|
|
19
|
+
# - gem update --system
|
|
20
|
+
# - gem --version
|
|
21
|
+
# - gem install rake bacon
|
|
22
|
+
# - rake gem
|
|
23
|
+
# - gem install pkg/*.gem
|
data/Gemfile
ADDED
data/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
[](http://travis-ci.org/banister/binding_of_caller)
|
|
2
|
+
|
|
1
3
|
binding_of_caller
|
|
2
4
|
===========
|
|
3
5
|
|
|
4
6
|
(C) John Mair (banisterfiend) 2011
|
|
5
7
|
|
|
6
|
-
_Retrieve the binding of a method's caller in MRI 1.9.
|
|
8
|
+
_Retrieve the binding of a method's caller in MRI 1.9.2+, and RBX (Rubinius)_
|
|
7
9
|
|
|
8
10
|
The `binding_of_caller` gem provides the `Binding#of_caller` method.
|
|
9
11
|
|
|
@@ -13,7 +15,7 @@ call stack, not limited to just the immediate caller.
|
|
|
13
15
|
|
|
14
16
|
**Recommended for use only in debugging situations. Do not use this in production apps.**
|
|
15
17
|
|
|
16
|
-
**Only works in MRI Ruby 1.9.2**
|
|
18
|
+
**Only works in MRI Ruby 1.9.2, 1.9.3 and RBX (Rubinius)**
|
|
17
19
|
|
|
18
20
|
* Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
|
|
19
21
|
* See the [source code](http://github.com/banister/binding_of_caller)
|
|
@@ -50,8 +52,7 @@ This project is a spinoff from the [Pry REPL project.](http://pry.github.com)
|
|
|
50
52
|
Features and limitations
|
|
51
53
|
-------------------------
|
|
52
54
|
|
|
53
|
-
* Only works with MRI 1.9.2
|
|
54
|
-
* Broken in 1.9.3, support will hopefully be provided in the near future.
|
|
55
|
+
* Only works with MRI 1.9.2, 1.9.3 and RBX (Rubinius)
|
|
55
56
|
* Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
|
|
56
57
|
|
|
57
58
|
Contact
|
data/Rakefile
CHANGED
|
@@ -1,80 +1,125 @@
|
|
|
1
|
-
dlext =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
require 'rake/
|
|
9
|
-
require
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
s.
|
|
20
|
-
s.
|
|
21
|
-
s.
|
|
22
|
-
s.
|
|
23
|
-
s.
|
|
24
|
-
s.
|
|
25
|
-
s.
|
|
26
|
-
s.
|
|
27
|
-
s.
|
|
28
|
-
s.
|
|
29
|
-
s.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
dlext = RbConfig::CONFIG['DLEXT']
|
|
2
|
+
direc = File.dirname(__FILE__)
|
|
3
|
+
|
|
4
|
+
$:.unshift 'lib'
|
|
5
|
+
|
|
6
|
+
PROJECT_NAME = "binding_of_caller"
|
|
7
|
+
|
|
8
|
+
require 'rake/clean'
|
|
9
|
+
require 'rubygems/package_task'
|
|
10
|
+
|
|
11
|
+
require "#{PROJECT_NAME}/version"
|
|
12
|
+
|
|
13
|
+
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
|
|
14
|
+
CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
|
15
|
+
"ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
|
|
16
|
+
"ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
|
|
17
|
+
|
|
18
|
+
def apply_spec_defaults(s)
|
|
19
|
+
s.name = PROJECT_NAME
|
|
20
|
+
s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
|
|
21
|
+
s.version = BindingOfCaller::VERSION
|
|
22
|
+
s.date = Time.now.strftime '%Y-%m-%d'
|
|
23
|
+
s.author = "John Mair (banisterfiend)"
|
|
24
|
+
s.email = 'jrmair@gmail.com'
|
|
25
|
+
s.description = s.summary
|
|
26
|
+
s.require_path = 'lib'
|
|
27
|
+
s.add_development_dependency('bacon')
|
|
28
|
+
s.add_development_dependency('rake')
|
|
29
|
+
s.homepage = "http://github.com/banister/binding_of_caller"
|
|
30
|
+
s.has_rdoc = 'yard'
|
|
31
|
+
s.files = `git ls-files`.split("\n")
|
|
32
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
|
33
|
+
end
|
|
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
|
+
|
|
43
|
+
desc "Run tests"
|
|
44
|
+
task :test do
|
|
45
|
+
unless defined?(Rubinius)
|
|
46
|
+
Rake::Task['compile'].execute
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
$stdout.puts("\033[33m")
|
|
50
|
+
sh "bacon -Itest -rubygems -a -q"
|
|
51
|
+
$stdout.puts("\033[0m")
|
|
52
|
+
|
|
53
|
+
unless defined?(Rubinius)
|
|
54
|
+
Rake::Task['cleanup'].execute
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
task :pry do
|
|
59
|
+
puts "loading binding_of_caller into pry"
|
|
60
|
+
sh "pry -r ./lib/binding_of_caller"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
desc "generate gemspec"
|
|
64
|
+
task :gemspec => "ruby:gemspec"
|
|
65
|
+
|
|
66
|
+
namespace :ruby do
|
|
67
|
+
spec = Gem::Specification.new do |s|
|
|
68
|
+
apply_spec_defaults(s)
|
|
69
|
+
s.platform = Gem::Platform::RUBY
|
|
70
|
+
s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
Gem::PackageTask.new(spec) do |pkg|
|
|
74
|
+
pkg.need_zip = false
|
|
75
|
+
pkg.need_tar = false
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
desc "Generate gemspec file"
|
|
79
|
+
task :gemspec do
|
|
80
|
+
File.open("#{spec.name}.gemspec", "w") do |f|
|
|
81
|
+
f << spec.to_ruby
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
desc "build the binaries"
|
|
87
|
+
task :compile do
|
|
88
|
+
chdir "./ext/#{PROJECT_NAME}/" do
|
|
89
|
+
sh "ruby extconf.rb"
|
|
90
|
+
sh "make clean"
|
|
91
|
+
sh "make"
|
|
92
|
+
sh "cp *.#{dlext} ../../lib/"
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
desc 'cleanup the extensions'
|
|
97
|
+
task :cleanup do
|
|
98
|
+
sh 'rm -rf lib/binding_of_caller.so'
|
|
99
|
+
chdir "./ext/#{PROJECT_NAME}/" do
|
|
100
|
+
sh 'make clean'
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
desc "reinstall gem"
|
|
105
|
+
task :reinstall => :gems do
|
|
106
|
+
sh "gem uninstall binding_of_caller" rescue nil
|
|
107
|
+
sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
desc "build all platform gems at once"
|
|
111
|
+
task :gems => [:clean, :rmgems, "ruby:gem"]
|
|
112
|
+
|
|
113
|
+
task :gem => [:gems]
|
|
114
|
+
|
|
115
|
+
desc "remove all platform gems"
|
|
116
|
+
task :rmgems => ["ruby:clobber_package"]
|
|
117
|
+
|
|
118
|
+
desc "build and push latest gems"
|
|
119
|
+
task :pushgems => :gems do
|
|
120
|
+
chdir("./pkg") do
|
|
121
|
+
Dir["*.gem"].each do |gemfile|
|
|
122
|
+
sh "gem push #{gemfile}"
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -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.8"
|
|
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-09-12"
|
|
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.rb", "lib/binding_of_caller/version.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.11"
|
|
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>, [">= 0"])
|
|
25
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
|
26
|
+
else
|
|
27
|
+
s.add_dependency(%q<bacon>, [">= 0"])
|
|
28
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
s.add_dependency(%q<bacon>, [">= 0"])
|
|
32
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -53,7 +53,11 @@ binding_mark(void *ptr)
|
|
|
53
53
|
if (ptr) {
|
|
54
54
|
bind = ptr;
|
|
55
55
|
RUBY_MARK_UNLESS_NULL(bind->env);
|
|
56
|
+
|
|
57
|
+
#ifdef RUBY_192
|
|
56
58
|
RUBY_MARK_UNLESS_NULL(bind->filename);
|
|
59
|
+
#endif
|
|
60
|
+
|
|
57
61
|
}
|
|
58
62
|
RUBY_MARK_LEAVE("binding");
|
|
59
63
|
}
|
|
@@ -74,8 +78,12 @@ binding_alloc(VALUE klass)
|
|
|
74
78
|
return obj;
|
|
75
79
|
}
|
|
76
80
|
|
|
81
|
+
static bool ifunc_p(rb_control_frame_t * cfp) {
|
|
82
|
+
return (cfp->flag & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_IFUNC;
|
|
83
|
+
}
|
|
84
|
+
|
|
77
85
|
static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
78
|
-
return cfp->iseq && !NIL_P(cfp->self);
|
|
86
|
+
return cfp->iseq && !ifunc_p(cfp) && !NIL_P(cfp->self);
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
|
|
@@ -93,6 +101,25 @@ static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_contro
|
|
|
93
101
|
return NULL;
|
|
94
102
|
}
|
|
95
103
|
|
|
104
|
+
static VALUE
|
|
105
|
+
frametype_name(VALUE flag)
|
|
106
|
+
{
|
|
107
|
+
switch (flag & VM_FRAME_MAGIC_MASK) {
|
|
108
|
+
case VM_FRAME_MAGIC_METHOD: return string2sym("method");
|
|
109
|
+
case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
|
|
110
|
+
case VM_FRAME_MAGIC_CLASS: return string2sym("class");
|
|
111
|
+
case VM_FRAME_MAGIC_TOP: return string2sym("top");
|
|
112
|
+
case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
|
|
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
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
96
123
|
static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
97
124
|
{
|
|
98
125
|
rb_thread_t *th;
|
|
@@ -125,34 +152,22 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
|
|
|
125
152
|
bind->filename = cfp->iseq->filename;
|
|
126
153
|
bind->line_no = rb_vm_get_sourceline(cfp);
|
|
127
154
|
|
|
128
|
-
rb_iv_set(bindval, "@frame_type", cfp->flag);
|
|
155
|
+
rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
|
|
156
|
+
rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
|
|
157
|
+
|
|
129
158
|
return bindval;
|
|
130
159
|
}
|
|
131
160
|
|
|
132
|
-
|
|
133
161
|
static VALUE
|
|
134
|
-
|
|
162
|
+
frame_type(VALUE self)
|
|
135
163
|
{
|
|
136
|
-
|
|
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
|
-
}
|
|
164
|
+
return rb_iv_get(self, "@frame_type");
|
|
150
165
|
}
|
|
151
166
|
|
|
152
167
|
static VALUE
|
|
153
|
-
|
|
168
|
+
frame_description(VALUE self)
|
|
154
169
|
{
|
|
155
|
-
return
|
|
170
|
+
return rb_iv_get(self, "@frame_description");
|
|
156
171
|
}
|
|
157
172
|
|
|
158
173
|
static VALUE frame_count(VALUE self)
|
|
@@ -174,6 +189,9 @@ static VALUE frame_count(VALUE self)
|
|
|
174
189
|
if (!valid_frame_p(cfp, limit_cfp))
|
|
175
190
|
cfp = find_valid_frame(cfp, limit_cfp);
|
|
176
191
|
|
|
192
|
+
if (!cfp)
|
|
193
|
+
break;
|
|
194
|
+
|
|
177
195
|
i++;
|
|
178
196
|
}
|
|
179
197
|
|
|
@@ -199,6 +217,7 @@ Init_binding_of_caller()
|
|
|
199
217
|
rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
|
|
200
218
|
rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
|
|
201
219
|
rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
|
|
220
|
+
rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
|
|
202
221
|
rb_define_method(mBindingOfCaller, "callers", callers, 0);
|
|
203
222
|
rb_include_module(rb_cBinding, mBindingOfCaller);
|
|
204
223
|
}
|
|
@@ -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.
|
|
3
|
-
end
|
|
1
|
+
module BindingOfCaller
|
|
2
|
+
VERSION = "0.6.8"
|
|
3
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
dlext = RbConfig::CONFIG['DLEXT']
|
|
2
|
+
direc = File.dirname(__FILE__)
|
|
3
|
+
|
|
4
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
|
|
5
|
+
require "binding_of_caller.#{dlext}"
|
|
6
|
+
|
|
7
|
+
elsif defined?(Rubinius)
|
|
8
|
+
module BindingOfCaller
|
|
9
|
+
module BindingExtensions
|
|
10
|
+
|
|
11
|
+
# Retrieve the binding of the nth caller of the current frame.
|
|
12
|
+
# @return [Binding]
|
|
13
|
+
def of_caller(n)
|
|
14
|
+
bt = Rubinius::VM.backtrace(1 + n, true).first
|
|
15
|
+
|
|
16
|
+
b = Binding.setup(
|
|
17
|
+
bt.variables,
|
|
18
|
+
bt.variables.method,
|
|
19
|
+
bt.static_scope,
|
|
20
|
+
bt.variables.self,
|
|
21
|
+
bt
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
b.instance_variable_set(:@frame_description, bt.describe)
|
|
25
|
+
|
|
26
|
+
b
|
|
27
|
+
rescue
|
|
28
|
+
raise RuntimeError, "Invalid frame, gone beyond end of stack!"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The description of the frame.
|
|
32
|
+
# @return [String]
|
|
33
|
+
def frame_description
|
|
34
|
+
@frame_description
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Return bindings for all caller frames.
|
|
38
|
+
# @return [Array<Binding>]
|
|
39
|
+
def callers
|
|
40
|
+
ary = []
|
|
41
|
+
n = 0
|
|
42
|
+
loop do
|
|
43
|
+
begin
|
|
44
|
+
ary << Binding.of_caller(n)
|
|
45
|
+
rescue
|
|
46
|
+
break
|
|
47
|
+
end
|
|
48
|
+
n += 1
|
|
49
|
+
end
|
|
50
|
+
ary.drop_while do |v|
|
|
51
|
+
!(v.frame_type == :method && v.eval("__method__") == :callers)
|
|
52
|
+
end.drop(1)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Number of parent frames available at the point of call.
|
|
56
|
+
# @return [Fixnum]
|
|
57
|
+
def frame_count
|
|
58
|
+
callers.size - 1
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# The type of the frame.
|
|
62
|
+
# @return [Symbol]
|
|
63
|
+
def frame_type
|
|
64
|
+
case self.variables.method.metadata.to_a.first.to_s
|
|
65
|
+
when /block/
|
|
66
|
+
:block
|
|
67
|
+
when /eval/
|
|
68
|
+
:eval
|
|
69
|
+
else
|
|
70
|
+
if frame_description =~ /__(?:class|module)_init__/
|
|
71
|
+
:class
|
|
72
|
+
else
|
|
73
|
+
:method
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
class ::Binding
|
|
82
|
+
include BindingOfCaller::BindingExtensions
|
|
83
|
+
extend BindingOfCaller::BindingExtensions
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -1,94 +1,108 @@
|
|
|
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 "
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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 "callers" do
|
|
62
|
+
before do
|
|
63
|
+
@o = Object.new
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'should return the first non-internal binding when using callers.first' do
|
|
67
|
+
def @o.meth
|
|
68
|
+
x = :a_local
|
|
69
|
+
[binding.callers.first, binding.of_caller(0)]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
b1, b2 = @o.meth
|
|
73
|
+
b1.eval("x").should == :a_local
|
|
74
|
+
b2.eval("x").should == :a_local
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
describe "frame_count" do
|
|
79
|
+
it 'frame_count should equal callers.count' do
|
|
80
|
+
binding.frame_count.should == binding.callers.count
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
describe "frame_type" do
|
|
85
|
+
it 'should return the correct frame types' do
|
|
86
|
+
o = Object.new
|
|
87
|
+
|
|
88
|
+
# method frame
|
|
89
|
+
def o.a
|
|
90
|
+
b
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# method frame
|
|
94
|
+
def o.b
|
|
95
|
+
# block frame
|
|
96
|
+
proc do
|
|
97
|
+
binding.callers
|
|
98
|
+
end.call
|
|
99
|
+
end
|
|
100
|
+
caller_bindings = o.a
|
|
101
|
+
caller_bindings[0].frame_type.should == :block
|
|
102
|
+
caller_bindings[1].frame_type.should == :method
|
|
103
|
+
caller_bindings[2].frame_type.should == :method
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: binding_of_caller
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.8
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,21 +9,32 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date:
|
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: bacon
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &70130738847880 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
|
-
- -
|
|
19
|
+
- - ! '>='
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version:
|
|
21
|
+
version: '0'
|
|
22
22
|
type: :development
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *70130738847880
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rake
|
|
27
|
+
requirement: &70130738846740 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *70130738846740
|
|
25
36
|
description: Retrieve the binding of a method's caller. Can also retrieve bindings
|
|
26
|
-
even further up the stack.
|
|
37
|
+
even further up the stack.
|
|
27
38
|
email: jrmair@gmail.com
|
|
28
39
|
executables: []
|
|
29
40
|
extensions:
|
|
@@ -32,11 +43,14 @@ extra_rdoc_files: []
|
|
|
32
43
|
files:
|
|
33
44
|
- .gemtest
|
|
34
45
|
- .gitignore
|
|
46
|
+
- .travis.yml
|
|
35
47
|
- .yardopts
|
|
48
|
+
- Gemfile
|
|
36
49
|
- HISTORY
|
|
37
50
|
- LICENSE
|
|
38
51
|
- README.md
|
|
39
52
|
- Rakefile
|
|
53
|
+
- binding_of_caller.gemspec
|
|
40
54
|
- examples/example.rb
|
|
41
55
|
- ext/binding_of_caller/binding_of_caller.c
|
|
42
56
|
- ext/binding_of_caller/extconf.rb
|
|
@@ -88,8 +102,9 @@ files:
|
|
|
88
102
|
- ext/binding_of_caller/ruby_headers/193/vm_exec.h
|
|
89
103
|
- ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
|
|
90
104
|
- ext/binding_of_caller/ruby_headers/193/vm_opts.h
|
|
105
|
+
- lib/binding_of_caller.rb
|
|
91
106
|
- lib/binding_of_caller/version.rb
|
|
92
|
-
- test/
|
|
107
|
+
- test/test_binding_of_caller.rb
|
|
93
108
|
homepage: http://github.com/banister/binding_of_caller
|
|
94
109
|
licenses: []
|
|
95
110
|
post_install_message:
|
|
@@ -114,6 +129,6 @@ rubygems_version: 1.8.11
|
|
|
114
129
|
signing_key:
|
|
115
130
|
specification_version: 3
|
|
116
131
|
summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
|
|
117
|
-
further up the stack.
|
|
132
|
+
further up the stack.
|
|
118
133
|
test_files:
|
|
119
|
-
- test/
|
|
134
|
+
- test/test_binding_of_caller.rb
|