binding_of_caller 0.6.6 → 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3fb3c7ee20b9b9ca1237d0540c2768efc657111
4
+ data.tar.gz: be4845aeca79432000b44be789993c23c1955715
5
+ SHA512:
6
+ metadata.gz: 3fd5d2a9ffbda43fcc2d8afa739ec51052f5dacc8632c3e88b60b7be90c2bd67c8a813156dab341e9f3399e46db667f3a1246043d50f1f60b6654d5c07e4e93c
7
+ data.tar.gz: 13fe3b6ab87052403c99be357fb91ea3ed2c9e9e5a8bafadc3123293bbb0a47bd7a235c67f7d25adb020f07bef0dee6d5a00424c7f59bf4c8b59dfc1f691d5c8
data/.gitignore CHANGED
@@ -1,7 +1,8 @@
1
- Makefile
2
- *.so
3
- *.o
4
- *.def
5
- doc/
6
- pkg/
7
- .yardoc/
1
+ Makefile
2
+ *.so
3
+ *.o
4
+ *.def
5
+ doc/
6
+ pkg/
7
+ .yardoc/
8
+ Gemfile.lock
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  rvm:
2
2
  - 1.9.2
3
3
  - 1.9.3
4
- - rbx-18mode
4
+ - 2.0.0
5
5
  - rbx-19mode
6
6
 
7
7
  notifications:
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
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
- (C) John Mair (banisterfiend) 2011
6
+ (C) John Mair (banisterfiend) 2012
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+, MRI 2.0 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, 2.0 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, 2.0 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
@@ -64,7 +66,7 @@ License
64
66
 
65
67
  (The MIT License)
66
68
 
67
- Copyright (c) 2011 (John Mair)
69
+ Copyright (c) 2012 (John Mair)
68
70
 
69
71
  Permission is hereby granted, free of charge, to any person obtaining
70
72
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -3,20 +3,23 @@ direc = File.dirname(__FILE__)
3
3
 
4
4
  $:.unshift 'lib'
5
5
 
6
- PROJECT_NAME = "binding_of_caller"
7
-
8
6
  require 'rake/clean'
9
7
  require 'rubygems/package_task'
10
8
 
11
- require "#{PROJECT_NAME}/version"
9
+ require "binding_of_caller/version"
12
10
 
13
11
  CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
14
12
  CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
15
13
  "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
16
14
  "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
17
15
 
16
+ def mri_2?
17
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
18
+ RUBY_VERSION =~ /^2/
19
+ end
20
+
18
21
  def apply_spec_defaults(s)
19
- s.name = PROJECT_NAME
22
+ s.name = "binding_of_caller"
20
23
  s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
21
24
  s.version = BindingOfCaller::VERSION
22
25
  s.date = Time.now.strftime '%Y-%m-%d'
@@ -24,8 +27,9 @@ def apply_spec_defaults(s)
24
27
  s.email = 'jrmair@gmail.com'
25
28
  s.description = s.summary
26
29
  s.require_path = 'lib'
27
- s.add_development_dependency('bacon')
28
- s.add_development_dependency('rake')
30
+ s.add_dependency 'debug_inspector', '>= 0.0.1'
31
+ s.add_development_dependency 'bacon'
32
+ s.add_development_dependency 'rake'
29
33
  s.homepage = "http://github.com/banister/binding_of_caller"
30
34
  s.has_rdoc = 'yard'
31
35
  s.files = `git ls-files`.split("\n")
@@ -67,7 +71,7 @@ namespace :ruby do
67
71
  spec = Gem::Specification.new do |s|
68
72
  apply_spec_defaults(s)
69
73
  s.platform = Gem::Platform::RUBY
70
- s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
74
+ s.extensions = ["ext/binding_of_caller/extconf.rb"]
71
75
  end
72
76
 
73
77
  Gem::PackageTask.new(spec) do |pkg|
@@ -83,42 +87,35 @@ namespace :ruby do
83
87
  end
84
88
  end
85
89
 
86
- # namespace :rbx do
87
- # spec = Gem::Specification.new do |s|
88
- # apply_spec_defaults(s)
89
- # s.platform = Gem::Platform::RUBY #.new(["universal", "rubinius"])
90
- # end
91
-
92
- # Gem::PackageTask.new(spec) do |pkg|
93
- # pkg.need_zip = false
94
- # pkg.need_tar = false
95
- # end
96
- # end
97
-
98
90
  desc "build the binaries"
99
- task :compile do
100
- chdir "./ext/#{PROJECT_NAME}/" do
101
- sh "ruby extconf.rb"
102
- sh "make clean"
103
- sh "make"
104
- sh "cp *.#{dlext} ../../lib/"
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
105
98
  end
106
99
  end
107
100
 
108
101
  desc 'cleanup the extensions'
109
102
  task :cleanup do
110
- sh 'rm -rf lib/binding_of_caller.so'
111
- chdir "./ext/#{PROJECT_NAME}/" do
112
- sh 'make clean'
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
113
108
  end
114
109
  end
115
110
 
116
111
  desc "reinstall gem"
117
112
  task :reinstall => :gems do
118
113
  sh "gem uninstall binding_of_caller" rescue nil
119
- sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
114
+ sh "gem install #{direc}/pkg/binding_of_caller-#{BindingOfCaller::VERSION}.gem"
120
115
  end
121
116
 
117
+ task :install => :reinstall
118
+
122
119
  desc "build all platform gems at once"
123
120
  task :gems => [:clean, :rmgems, "ruby:gem"]
124
121
 
@@ -2,32 +2,35 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "binding_of_caller"
5
- s.version = "0.6.6"
5
+ s.version = "0.7.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Mair (banisterfiend)"]
9
- s.date = "2012-03-01"
9
+ s.date = "2013-06-07"
10
10
  s.description = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
11
11
  s.email = "jrmair@gmail.com"
12
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"]
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/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/rubys_gc.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/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/rubys_gc.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
14
  s.homepage = "http://github.com/banister/binding_of_caller"
15
15
  s.require_paths = ["lib"]
16
- s.rubygems_version = "1.8.12"
16
+ s.rubygems_version = "2.0.3"
17
17
  s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
18
18
  s.test_files = ["test/test_binding_of_caller.rb"]
19
19
 
20
20
  if s.respond_to? :specification_version then
21
- s.specification_version = 3
21
+ s.specification_version = 4
22
22
 
23
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"])
24
25
  s.add_development_dependency(%q<bacon>, [">= 0"])
25
26
  s.add_development_dependency(%q<rake>, [">= 0"])
26
27
  else
28
+ s.add_dependency(%q<debug_inspector>, [">= 0.0.1"])
27
29
  s.add_dependency(%q<bacon>, [">= 0"])
28
30
  s.add_dependency(%q<rake>, [">= 0"])
29
31
  end
30
32
  else
33
+ s.add_dependency(%q<debug_inspector>, [">= 0.0.1"])
31
34
  s.add_dependency(%q<bacon>, [">= 0"])
32
35
  s.add_dependency(%q<rake>, [">= 0"])
33
36
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  #include <ruby.h>
4
4
  #include "vm_core.h"
5
- #include "gc.h"
5
+ #include "rubys_gc.h"
6
6
 
7
7
  typedef enum { false, true } bool;
8
8
 
@@ -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,10 +1,19 @@
1
1
  def fake_makefile
2
- File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") {|f|
2
+ File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") do |f|
3
3
  f.puts %[install:\n\techo "Nada."]
4
- }
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/
5
14
  end
6
15
 
7
- if RUBY_ENGINE && RUBY_ENGINE =~ /rbx/
16
+ if mri_2? || rbx?
8
17
  fake_makefile
9
18
  else
10
19
  require 'mkmf'
@@ -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
1
  module BindingOfCaller
2
- VERSION = "0.6.6"
2
+ VERSION = "0.7.2"
3
3
  end
@@ -1,86 +1,14 @@
1
1
  dlext = RbConfig::CONFIG['DLEXT']
2
- direc = File.dirname(__FILE__)
3
2
 
4
- if RUBY_ENGINE && RUBY_ENGINE == "ruby"
5
- require "binding_of_caller.#{dlext}"
3
+ def mri_2?
4
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
5
+ RUBY_VERSION =~ /^2/
6
+ end
6
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}"
7
12
  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
13
+ require 'binding_of_caller/rubinius'
86
14
  end
@@ -1,91 +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
- 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 '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
+
metadata CHANGED
@@ -1,59 +1,65 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: binding_of_caller
3
- version: !ruby/object:Gem::Version
4
- hash: 1509392978297545075
5
- prerelease:
6
- segments:
7
- - 0
8
- - 6
9
- - 6
10
- version: 0.6.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.2
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - John Mair (banisterfiend)
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-03-01 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: bacon
11
+ date: 2013-06-07 00:00:00.000000000 Z
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
22
21
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 2002549777813010636
29
- segments:
30
- - 0
31
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bacon
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
32
34
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rake
36
35
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 2002549777813010636
43
- segments:
44
- - 0
45
- version: "0"
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'
46
48
  type: :development
47
- version_requirements: *id002
48
- description: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Retrieve the binding of a method's caller. Can also retrieve bindings
56
+ even further up the stack.
49
57
  email: jrmair@gmail.com
50
58
  executables: []
51
-
52
- extensions:
59
+ extensions:
53
60
  - ext/binding_of_caller/extconf.rb
54
61
  extra_rdoc_files: []
55
-
56
- files:
62
+ files:
57
63
  - .gemtest
58
64
  - .gitignore
59
65
  - .travis.yml
@@ -70,7 +76,6 @@ files:
70
76
  - ext/binding_of_caller/ruby_headers/192/debug.h
71
77
  - ext/binding_of_caller/ruby_headers/192/dln.h
72
78
  - ext/binding_of_caller/ruby_headers/192/eval_intern.h
73
- - ext/binding_of_caller/ruby_headers/192/gc.h
74
79
  - ext/binding_of_caller/ruby_headers/192/id.h
75
80
  - ext/binding_of_caller/ruby_headers/192/iseq.h
76
81
  - ext/binding_of_caller/ruby_headers/192/method.h
@@ -78,6 +83,7 @@ files:
78
83
  - ext/binding_of_caller/ruby_headers/192/regenc.h
79
84
  - ext/binding_of_caller/ruby_headers/192/regint.h
80
85
  - ext/binding_of_caller/ruby_headers/192/regparse.h
86
+ - ext/binding_of_caller/ruby_headers/192/rubys_gc.h
81
87
  - ext/binding_of_caller/ruby_headers/192/thread_pthread.h
82
88
  - ext/binding_of_caller/ruby_headers/192/thread_win32.h
83
89
  - ext/binding_of_caller/ruby_headers/192/timev.h
@@ -94,7 +100,6 @@ files:
94
100
  - ext/binding_of_caller/ruby_headers/193/dln.h
95
101
  - ext/binding_of_caller/ruby_headers/193/encdb.h
96
102
  - ext/binding_of_caller/ruby_headers/193/eval_intern.h
97
- - ext/binding_of_caller/ruby_headers/193/gc.h
98
103
  - ext/binding_of_caller/ruby_headers/193/id.h
99
104
  - ext/binding_of_caller/ruby_headers/193/internal.h
100
105
  - ext/binding_of_caller/ruby_headers/193/iseq.h
@@ -105,6 +110,7 @@ files:
105
110
  - ext/binding_of_caller/ruby_headers/193/regint.h
106
111
  - ext/binding_of_caller/ruby_headers/193/regparse.h
107
112
  - ext/binding_of_caller/ruby_headers/193/revision.h
113
+ - ext/binding_of_caller/ruby_headers/193/rubys_gc.h
108
114
  - ext/binding_of_caller/ruby_headers/193/thread_pthread.h
109
115
  - ext/binding_of_caller/ruby_headers/193/thread_win32.h
110
116
  - ext/binding_of_caller/ruby_headers/193/timev.h
@@ -116,40 +122,33 @@ files:
116
122
  - ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
117
123
  - ext/binding_of_caller/ruby_headers/193/vm_opts.h
118
124
  - lib/binding_of_caller.rb
125
+ - lib/binding_of_caller/mri2.rb
126
+ - lib/binding_of_caller/rubinius.rb
119
127
  - lib/binding_of_caller/version.rb
120
128
  - test/test_binding_of_caller.rb
121
129
  homepage: http://github.com/banister/binding_of_caller
122
130
  licenses: []
123
-
131
+ metadata: {}
124
132
  post_install_message:
125
133
  rdoc_options: []
126
-
127
- require_paths:
134
+ require_paths:
128
135
  - lib
129
- required_ruby_version: !ruby/object:Gem::Requirement
130
- none: false
131
- requirements:
132
- - - ">="
133
- - !ruby/object:Gem::Version
134
- hash: 2002549777813010636
135
- segments:
136
- - 0
137
- version: "0"
138
- required_rubygems_version: !ruby/object:Gem::Requirement
139
- none: false
140
- requirements:
141
- - - ">="
142
- - !ruby/object:Gem::Version
143
- hash: 2002549777813010636
144
- segments:
145
- - 0
146
- version: "0"
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
147
146
  requirements: []
148
-
149
147
  rubyforge_project:
150
- rubygems_version: 1.8.12
148
+ rubygems_version: 2.0.3
151
149
  signing_key:
152
- specification_version: 3
153
- summary: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
154
- test_files:
150
+ specification_version: 4
151
+ summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
152
+ further up the stack.
153
+ test_files:
155
154
  - test/test_binding_of_caller.rb