binding_of_caller 0.6.3 → 0.7

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f04635fb28639852333bfbee1550220af00a4028
4
+ data.tar.gz: 5badb7913920490c656b9474ce86f7cc4a942b16
5
+ SHA512:
6
+ metadata.gz: e2fac2a7efe9388ba217124dd615d66b4b4732faad53f8211543047315acb0ebea803f9d31d842c990ef34f80fba6792a1f2ae6ab04c78d7cf496f5923c2ed34
7
+ data.tar.gz: 500c220951bb64eb5597b4e5beae0dc1ba5a01e3cd9818012845f313740a5d80f631b0b3b36832ab437c5a856af5cd8b21ae4945c100389778bed2cacc7e24ec
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - ruby-head
5
+
6
+ notifications:
7
+ irc: "irc.freenode.org#pry"
8
+ recipients:
9
+ - jrmair@gmail.com
10
+
11
+ branches:
12
+ only:
13
+ - master
14
+
15
+ #script: rake test --trace
16
+ #
17
+ #before_install:
18
+ # - gem update --system
19
+ # - gem --version
20
+ # - gem install rake bacon
21
+ # - rake gem
22
+ # - gem install pkg/*.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
+ [![Build Status](https://secure.travis-ci.org/banister/binding_of_caller.png)](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.2_
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 and 1.9.3**
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,7 +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 and 1.9.3
55
+ * Only works with MRI 1.9.2, 1.9.3 and RBX (Rubinius)
54
56
  * Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
55
57
 
56
58
  Contact
data/Rakefile CHANGED
@@ -1,112 +1,134 @@
1
- dlext = Config::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. Currently only works for MRI 1.9.2+"
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","~>1.1")
28
- s.homepage = "http://github.com/banister/binding_of_caller"
29
- s.has_rdoc = 'yard'
30
- s.files = `git ls-files`.split("\n")
31
- s.test_files = `git ls-files -- test/*`.split("\n")
32
- end
33
-
34
- desc "Run tests"
35
- task :test do
36
- sh "bacon -Itest -rubygems test.rb -q"
37
- end
38
-
39
- task :pry do
40
- puts "loading binding_of_caller into pry"
41
- sh "pry -r ./lib/binding_of_caller"
42
- end
43
-
44
- desc "generate gemspec"
45
- task :gemspec => "ruby:gemspec"
46
-
47
- namespace :ruby do
48
- spec = Gem::Specification.new do |s|
49
- apply_spec_defaults(s)
50
- s.platform = Gem::Platform::RUBY
51
- s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
52
- end
53
-
54
- Gem::PackageTask.new(spec) do |pkg|
55
- pkg.need_zip = false
56
- pkg.need_tar = false
57
- end
58
-
59
- desc "Generate gemspec file"
60
- task :gemspec do
61
- File.open("#{spec.name}.gemspec", "w") do |f|
62
- f << spec.to_ruby
63
- end
64
- end
65
- end
66
-
67
- namespace :rbx do
68
- spec = Gem::Specification.new do |s|
69
- apply_spec_defaults(s)
70
- s.platform = Gem::Platform.new(["universal", "rubinius"])
71
- end
72
-
73
- Gem::PackageTask.new(spec) do |pkg|
74
- pkg.need_zip = false
75
- pkg.need_tar = false
76
- end
77
- end
78
-
79
- desc "build the binaries"
80
- task :compile do
81
- chdir "./ext/#{PROJECT_NAME}/" do
82
- sh "ruby extconf.rb"
83
- sh "make clean"
84
- sh "make"
85
- sh "cp *.#{dlext} ../../lib/"
86
- end
87
- end
88
-
89
- desc "reinstall gem"
90
- task :reinstall => :gems do
91
- sh "gem uninstall binding_of_caller" rescue nil
92
- sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
93
- end
94
-
95
- desc "build all platform gems at once"
96
- task :gems => [:clean, :rmgems, "ruby:gem", "rbx:gem"]
97
-
98
- task :gem => [:gems]
99
-
100
- task :rbxgem => "rbx:gem"
101
-
102
- desc "remove all platform gems"
103
- task :rmgems => ["ruby:clobber_package"]
104
-
105
- desc "build and push latest gems"
106
- task :pushgems => :gems do
107
- chdir("./pkg") do
108
- Dir["*.gem"].each do |gemfile|
109
- sh "gem push #{gemfile}"
110
- end
111
- end
112
- end
1
+ dlext = RbConfig::CONFIG['DLEXT']
2
+ direc = File.dirname(__FILE__)
3
+
4
+ $:.unshift 'lib'
5
+
6
+ require 'rake/clean'
7
+ require 'rubygems/package_task'
8
+
9
+ require "binding_of_caller/version"
10
+
11
+ CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
12
+ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
13
+ "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
14
+ "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
15
+
16
+ def mri_2?
17
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
18
+ RUBY_VERSION =~ /^2/
19
+ end
20
+
21
+ def apply_spec_defaults(s)
22
+ s.name = "binding_of_caller"
23
+ s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
24
+ s.version = BindingOfCaller::VERSION
25
+ s.date = Time.now.strftime '%Y-%m-%d'
26
+ s.author = "John Mair (banisterfiend)"
27
+ s.email = 'jrmair@gmail.com'
28
+ s.description = s.summary
29
+ s.require_path = 'lib'
30
+ s.add_dependency 'debug_inspector', '>= 0.0.1'
31
+ s.add_development_dependency 'bacon'
32
+ s.add_development_dependency 'rake'
33
+ s.homepage = "http://github.com/banister/binding_of_caller"
34
+ s.has_rdoc = 'yard'
35
+ s.files = `git ls-files`.split("\n")
36
+ s.test_files = `git ls-files -- test/*`.split("\n")
37
+ end
38
+
39
+ desc "Show version"
40
+ task :version do
41
+ puts "BindingOfCaller version: #{BindingOfCaller::VERSION}"
42
+ end
43
+
44
+ desc "run tests"
45
+ task :default => [:test]
46
+
47
+ desc "Run tests"
48
+ task :test do
49
+ unless defined?(Rubinius)
50
+ Rake::Task['compile'].execute
51
+ end
52
+
53
+ $stdout.puts("\033[33m")
54
+ sh "bacon -Itest -rubygems -a -q"
55
+ $stdout.puts("\033[0m")
56
+
57
+ unless defined?(Rubinius)
58
+ Rake::Task['cleanup'].execute
59
+ end
60
+ end
61
+
62
+ task :pry do
63
+ puts "loading binding_of_caller into pry"
64
+ sh "pry -r ./lib/binding_of_caller"
65
+ end
66
+
67
+ desc "generate gemspec"
68
+ task :gemspec => "ruby:gemspec"
69
+
70
+ namespace :ruby do
71
+ spec = Gem::Specification.new do |s|
72
+ apply_spec_defaults(s)
73
+ s.platform = Gem::Platform::RUBY
74
+ s.extensions = ["ext/binding_of_caller/extconf.rb"]
75
+ end
76
+
77
+ Gem::PackageTask.new(spec) do |pkg|
78
+ pkg.need_zip = false
79
+ pkg.need_tar = false
80
+ end
81
+
82
+ desc "Generate gemspec file"
83
+ task :gemspec do
84
+ File.open("#{spec.name}.gemspec", "w") do |f|
85
+ f << spec.to_ruby
86
+ end
87
+ end
88
+ end
89
+
90
+ desc "build the binaries"
91
+ task :compile => :cleanup do
92
+ if !mri_2?
93
+ chdir "./ext/binding_of_caller/" do
94
+ sh "ruby extconf.rb"
95
+ sh "make"
96
+ sh "cp *.#{dlext} ../../lib/"
97
+ end
98
+ end
99
+ end
100
+
101
+ desc 'cleanup the extensions'
102
+ task :cleanup do
103
+ if !mri_2?
104
+ sh 'rm -rf lib/binding_of_caller.so'
105
+ chdir "./ext/binding_of_caller/" do
106
+ sh 'make clean'
107
+ end
108
+ end
109
+ end
110
+
111
+ desc "reinstall gem"
112
+ task :reinstall => :gems do
113
+ sh "gem uninstall binding_of_caller" rescue nil
114
+ sh "gem install #{direc}/pkg/binding_of_caller-#{BindingOfCaller::VERSION}.gem"
115
+ end
116
+
117
+ task :install => :reinstall
118
+
119
+ desc "build all platform gems at once"
120
+ task :gems => [:clean, :rmgems, "ruby:gem"]
121
+
122
+ task :gem => [:gems]
123
+
124
+ desc "remove all platform gems"
125
+ task :rmgems => ["ruby:clobber_package"]
126
+
127
+ desc "build and push latest gems"
128
+ task :pushgems => :gems do
129
+ chdir("./pkg") do
130
+ Dir["*.gem"].each do |gemfile|
131
+ sh "gem push #{gemfile}"
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "binding_of_caller"
5
+ s.version = "0.6.9"
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 = "2013-02-14"
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/mri2.rb", "lib/binding_of_caller/rubinius.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 = "2.0.0.rc.2"
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 = 4
22
+
23
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
24
+ s.add_runtime_dependency(%q<debug_inspector>, [">= 0.0.1"])
25
+ s.add_development_dependency(%q<bacon>, [">= 0"])
26
+ s.add_development_dependency(%q<rake>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<debug_inspector>, [">= 0.0.1"])
29
+ s.add_dependency(%q<bacon>, [">= 0"])
30
+ s.add_dependency(%q<rake>, [">= 0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<debug_inspector>, [">= 0.0.1"])
34
+ s.add_dependency(%q<bacon>, [">= 0"])
35
+ s.add_dependency(%q<rake>, [">= 0"])
36
+ end
37
+ end
@@ -24,6 +24,7 @@ threadptr_data_type(void)
24
24
  }
25
25
 
26
26
  #define ruby_thread_data_type *threadptr_data_type()
27
+ #define ruby_threadptr_data_type *threadptr_data_type()
27
28
 
28
29
  #define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
29
30
 
@@ -78,8 +79,12 @@ binding_alloc(VALUE klass)
78
79
  return obj;
79
80
  }
80
81
 
82
+ static bool ifunc_p(rb_control_frame_t * cfp) {
83
+ return (cfp->flag & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_IFUNC;
84
+ }
85
+
81
86
  static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
82
- return cfp->iseq && !NIL_P(cfp->self);
87
+ return cfp->iseq && !ifunc_p(cfp) && !NIL_P(cfp->self);
83
88
  }
84
89
 
85
90
  static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
@@ -105,7 +110,6 @@ frametype_name(VALUE flag)
105
110
  case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
106
111
  case VM_FRAME_MAGIC_CLASS: return string2sym("class");
107
112
  case VM_FRAME_MAGIC_TOP: return string2sym("top");
108
- case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
109
113
  case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
110
114
  case VM_FRAME_MAGIC_PROC: return string2sym("proc");
111
115
  case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
@@ -144,10 +148,11 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
144
148
  rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
145
149
 
146
150
  GetBindingPtr(bindval, bind);
151
+
147
152
  bind->env = rb_vm_make_env_object(th, cfp);
148
153
  bind->filename = cfp->iseq->filename;
149
154
  bind->line_no = rb_vm_get_sourceline(cfp);
150
-
155
+
151
156
  rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
152
157
  rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
153
158
 
@@ -1,13 +1,33 @@
1
- require 'mkmf'
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
- $CFLAGS += " -O0"
4
- $CFLAGS += " -std=c99"
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
- case RUBY_VERSION
7
- when /1.9.2/
8
- $CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
9
- when /1.9.3/
10
- $CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
31
+ create_makefile('binding_of_caller')
11
32
  end
12
33
 
13
- create_makefile('binding_of_caller')
@@ -0,0 +1,68 @@
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
+ # The description of the frame.
17
+ # @return [String]
18
+ def frame_description
19
+ @frame_description ||= @iseq.label
20
+ end
21
+
22
+ # Return bindings for all caller frames.
23
+ # @return [Array<Binding>]
24
+ def callers
25
+ ary = []
26
+
27
+ RubyVM::DebugInspector.open do |i|
28
+ n = 0
29
+ loop do
30
+ begin
31
+ b = i.frame_binding(n)
32
+
33
+ if b
34
+ iseq = i.frame_iseq(n)
35
+ b.instance_variable_set(:@iseq, iseq)
36
+ ary << b
37
+ end
38
+ rescue ArgumentError
39
+ break
40
+ end
41
+ n += 1
42
+ end
43
+ end
44
+
45
+ ary.drop(1)
46
+ end
47
+
48
+ # Number of parent frames available at the point of call.
49
+ # @return [Fixnum]
50
+ def frame_count
51
+ callers.size - 1
52
+ end
53
+
54
+ # The type of the frame.
55
+ # @return [Symbol]
56
+ def frame_type
57
+ # apparently the 9th element of the iseq array holds the frame type
58
+ # ...not sure how reliable this is.
59
+ @frame_type ||= @iseq.to_a[9]
60
+ end
61
+
62
+ end
63
+ end
64
+
65
+
66
+ class ::Binding
67
+ include BindingOfCaller::BindingExtensions
68
+ end
@@ -0,0 +1,77 @@
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
+ b = Binding.setup(
10
+ bt.variables,
11
+ bt.variables.method,
12
+ bt.static_scope,
13
+ bt.variables.self,
14
+ bt
15
+ )
16
+
17
+ b.instance_variable_set(:@frame_description, bt.describe)
18
+
19
+ b
20
+ rescue
21
+ raise RuntimeError, "Invalid frame, gone beyond end of stack!"
22
+ end
23
+
24
+ # The description of the frame.
25
+ # @return [String]
26
+ def frame_description
27
+ @frame_description
28
+ end
29
+
30
+ # Return bindings for all caller frames.
31
+ # @return [Array<Binding>]
32
+ def callers
33
+ ary = []
34
+ n = 0
35
+ loop do
36
+ begin
37
+ ary << Binding.of_caller(n)
38
+ rescue
39
+ break
40
+ end
41
+ n += 1
42
+ end
43
+ ary.drop_while do |v|
44
+ !(v.frame_type == :method && v.eval("__method__") == :callers)
45
+ end.drop(1)
46
+ end
47
+
48
+ # Number of parent frames available at the point of call.
49
+ # @return [Fixnum]
50
+ def frame_count
51
+ callers.size - 1
52
+ end
53
+
54
+ # The type of the frame.
55
+ # @return [Symbol]
56
+ def frame_type
57
+ case self.variables.method.metadata.to_a.first.to_s
58
+ when /block/
59
+ :block
60
+ when /eval/
61
+ :eval
62
+ else
63
+ if frame_description =~ /__(?:class|module)_init__/
64
+ :class
65
+ else
66
+ :method
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+ end
73
+
74
+ class ::Binding
75
+ include BindingOfCaller::BindingExtensions
76
+ extend BindingOfCaller::BindingExtensions
77
+ end
@@ -1,3 +1,3 @@
1
- module BindingOfCaller
2
- VERSION = "0.6.3"
3
- end
1
+ module BindingOfCaller
2
+ VERSION = "0.7"
3
+ end
@@ -1,82 +1,14 @@
1
- dlext = Config::CONFIG['DLEXT']
1
+ dlext = RbConfig::CONFIG['DLEXT']
2
2
 
3
- if RUBY_ENGINE && RUBY_ENGINE == "ruby"
4
- require "binding_of_caller.#{dlext}"
3
+ def mri_2?
4
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
5
+ RUBY_VERSION =~ /^2/
6
+ end
5
7
 
8
+ if mri_2?
9
+ require 'binding_of_caller/mri2'
10
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
11
+ require "binding_of_caller.#{dlext}"
6
12
  elsif defined?(Rubinius)
7
- module BindingOfCaller
8
- module BindingExtensions
9
-
10
- def of_caller(n)
11
- bt = Rubinius::VM.backtrace(1 + n, true).first
12
-
13
- b = Binding.setup(
14
- bt.variables,
15
- bt.variables.method,
16
- bt.static_scope,
17
- bt.variables.self,
18
- bt
19
- )
20
-
21
- b.instance_variable_set(:@frame_description, bt.describe)
22
-
23
- b
24
- rescue
25
- raise RuntimeError, "Invalid frame, gone beyond end of stack!"
26
- end
27
-
28
- def frame_description
29
- @frame_description
30
- end
31
-
32
- def callers
33
- ary = []
34
- n = 0
35
- loop {
36
- begin
37
- ary << Binding.of_caller(n)
38
- rescue
39
- break
40
- end
41
-
42
- n += 1
43
- }
44
-
45
- ary
46
- end
47
-
48
- def frame_count
49
- ary = []
50
- n = 1
51
- loop {
52
- begin
53
- Binding.of_caller(n)
54
- rescue
55
- break
56
- end
57
-
58
- n += 1
59
- }
60
-
61
- n
62
- end
63
-
64
- def frame_type
65
- case self.variables.method.metadata.to_a.first.to_s
66
- when /block/
67
- :block
68
- when /eval/
69
- :eval
70
- else
71
- :method
72
- end
73
- end
74
-
75
- end
76
- end
77
-
78
- class ::Binding
79
- include BindingOfCaller::BindingExtensions
80
- extend BindingOfCaller::BindingExtensions
81
- end
13
+ require 'binding_of_caller/rubinius'
82
14
  end
@@ -1,91 +1,141 @@
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
-
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 'describes a block frame' do
90
+ binding.of_caller(0).frame_description.should =~ /block/
91
+ end
92
+
93
+ it 'describes a method frame' do
94
+ o = Object.new
95
+ def o.horsey_malone
96
+ binding.of_caller(0).frame_description.should =~ /horsey_malone/
97
+ end
98
+ o.horsey_malone
99
+ end
100
+
101
+ it 'describes a class frame' do
102
+ class HorseyMalone
103
+ binding.of_caller(0).frame_description.should =~ /class/
104
+ end
105
+ Object.remove_const(:HorseyMalone)
106
+ end
107
+ end
108
+
109
+ describe "frame_type" do
110
+ it 'should return the correct frame types' do
111
+ o = Object.new
112
+
113
+ # method frame
114
+ def o.a
115
+ b
116
+ end
117
+
118
+ # method frame
119
+ def o.b
120
+ # block frame
121
+ proc do
122
+ binding.callers
123
+ end.call
124
+ end
125
+ caller_bindings = o.a
126
+ caller_bindings[0].frame_type.should == :block
127
+ caller_bindings[1].frame_type.should == :method
128
+ caller_bindings[2].frame_type.should == :method
129
+ end
130
+
131
+ it 'identifies a class frame' do
132
+ class HorseyMalone
133
+ binding.of_caller(0).frame_type.should == :class
134
+ end
135
+
136
+ Object.remove_const(:HorseyMalone)
137
+ end
138
+
139
+ end
140
+ end
141
+
metadata CHANGED
@@ -1,29 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding_of_caller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
5
- prerelease:
4
+ version: '0.7'
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Mair (banisterfiend)
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-02-29 00:00:00.000000000 Z
11
+ date: 2013-02-16 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: debug_inspector
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: bacon
16
- requirement: &70210333909540 !ruby/object:Gem::Requirement
17
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
18
30
  requirements:
19
- - - ~>
31
+ - - '>='
20
32
  - !ruby/object:Gem::Version
21
- version: '1.1'
33
+ version: '0'
22
34
  type: :development
23
35
  prerelease: false
24
- version_requirements: *70210333909540
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
25
55
  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+
56
+ even further up the stack.
27
57
  email: jrmair@gmail.com
28
58
  executables: []
29
59
  extensions:
@@ -32,11 +62,14 @@ extra_rdoc_files: []
32
62
  files:
33
63
  - .gemtest
34
64
  - .gitignore
65
+ - .travis.yml
35
66
  - .yardopts
67
+ - Gemfile
36
68
  - HISTORY
37
69
  - LICENSE
38
70
  - README.md
39
71
  - Rakefile
72
+ - binding_of_caller.gemspec
40
73
  - examples/example.rb
41
74
  - ext/binding_of_caller/binding_of_caller.c
42
75
  - ext/binding_of_caller/extconf.rb
@@ -88,35 +121,34 @@ files:
88
121
  - ext/binding_of_caller/ruby_headers/193/vm_exec.h
89
122
  - ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
90
123
  - ext/binding_of_caller/ruby_headers/193/vm_opts.h
91
- - lib/binding_of_caller.bundle
92
124
  - lib/binding_of_caller.rb
125
+ - lib/binding_of_caller/mri2.rb
126
+ - lib/binding_of_caller/rubinius.rb
93
127
  - lib/binding_of_caller/version.rb
94
- - lib/tester.rb
95
- - test/test.rb
128
+ - test/test_binding_of_caller.rb
96
129
  homepage: http://github.com/banister/binding_of_caller
97
130
  licenses: []
131
+ metadata: {}
98
132
  post_install_message:
99
133
  rdoc_options: []
100
134
  require_paths:
101
135
  - lib
102
136
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
137
  requirements:
105
- - - ! '>='
138
+ - - '>='
106
139
  - !ruby/object:Gem::Version
107
140
  version: '0'
108
141
  required_rubygems_version: !ruby/object:Gem::Requirement
109
- none: false
110
142
  requirements:
111
- - - ! '>='
143
+ - - '>='
112
144
  - !ruby/object:Gem::Version
113
145
  version: '0'
114
146
  requirements: []
115
147
  rubyforge_project:
116
- rubygems_version: 1.8.16
148
+ rubygems_version: 2.0.0.rc.2
117
149
  signing_key:
118
- specification_version: 3
150
+ specification_version: 4
119
151
  summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
120
- further up the stack. Currently only works for MRI 1.9.2+
152
+ further up the stack.
121
153
  test_files:
122
- - test/test.rb
154
+ - test/test_binding_of_caller.rb
Binary file
data/lib/tester.rb DELETED
@@ -1,15 +0,0 @@
1
- require './binding_of_caller'
2
-
3
- def a
4
- x = 1
5
- y = 2
6
- b()
7
- end
8
-
9
- def b
10
- proc do
11
- puts binding.of_caller(2).eval('__method__')
12
- end.call
13
- end
14
-
15
- a