binding_of_caller 0.2.0 → 0.6.6

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/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ Makefile
2
+ *.so
3
+ *.o
4
+ *.def
5
+ doc/
6
+ pkg/
7
+ .yardoc/
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/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ License
2
+ -------
3
+
4
+ (The MIT License)
5
+
6
+ Copyright (c) 2011 John Mair (banisterfiend)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining
9
+ a copy of this software and associated documentation files (the
10
+ 'Software'), to deal in the Software without restriction, including
11
+ without limitation the rights to use, copy, modify, merge, publish,
12
+ distribute, sublicense, and/or sell copies of the Software, and to
13
+ permit persons to whom the Software is furnished to do so, subject to
14
+ the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -3,25 +3,55 @@ binding_of_caller
3
3
 
4
4
  (C) John Mair (banisterfiend) 2011
5
5
 
6
- FIXME: _tagline_
6
+ _Retrieve the binding of a method's caller in MRI 1.9.2+_
7
7
 
8
- FIXME: _description goes here_
8
+ The `binding_of_caller` gem provides the `Binding#of_caller` method.
9
+
10
+ Using `binding_of_caller` we can grab bindings from higher up the call
11
+ stack and evaluate code in that context. Allows access to bindings arbitrarily far up the
12
+ call stack, not limited to just the immediate caller.
13
+
14
+ **Recommended for use only in debugging situations. Do not use this in production apps.**
15
+
16
+ **Only works in MRI Ruby 1.9.2 and 1.9.3**
9
17
 
10
18
  * Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
11
- * Read the [documentation](http://rdoc.info/github/banister/binding_of_caller/master/file/README.markdown)
12
19
  * See the [source code](http://github.com/banister/binding_of_caller)
13
20
 
14
- Example: Example description
21
+ Example: Modifying a local inside the caller of a caller
15
22
  --------
16
23
 
17
- Example preamble
24
+ ```ruby
25
+ def a
26
+ var = 10
27
+ b
28
+ puts var
29
+ end
30
+
31
+ def b
32
+ c
33
+ end
34
+
35
+ def c
36
+ binding.of_caller(2).eval('var = :hello')
37
+ end
38
+
39
+ a()
40
+
41
+ # OUTPUT
42
+ # => hello
43
+ ```
44
+
45
+ Spinoff project
46
+ -------
18
47
 
19
- puts "example code"
48
+ This project is a spinoff from the [Pry REPL project.](http://pry.github.com)
20
49
 
21
50
  Features and limitations
22
51
  -------------------------
23
52
 
24
- Feature List Preamble
53
+ * Only works with MRI 1.9.2 and 1.9.3
54
+ * Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
25
55
 
26
56
  Contact
27
57
  -------
@@ -32,7 +62,7 @@ Problems or questions contact me at [github](http://github.com/banister)
32
62
  License
33
63
  -------
34
64
 
35
- (The MIT License)
65
+ (The MIT License)
36
66
 
37
67
  Copyright (c) 2011 (John Mair)
38
68
 
data/Rakefile CHANGED
@@ -1,99 +1,137 @@
1
- dlext = Config::CONFIG['DLEXT']
2
- direc = File.dirname(__FILE__)
3
-
4
- PROJECT_NAME = "binding_of_caller"
5
-
6
- require 'rake/clean'
7
- require 'rake/gempackagetask'
8
- require "#{direc}/lib/#{PROJECT_NAME}/version"
9
-
10
- CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
11
- CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
12
- "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
13
- "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
14
-
15
- def apply_spec_defaults(s)
16
- s.name = PROJECT_NAME
17
- s.summary = "FIX ME"
18
- s.version = BindingOfCaller::VERSION
19
- s.date = Time.now.strftime '%Y-%m-%d'
20
- s.author = "John Mair (banisterfiend)"
21
- s.email = 'jrmair@gmail.com'
22
- s.description = s.summary
23
- s.require_path = 'lib'
24
- s.add_development_dependency("bacon",">=1.1.0")
25
- s.homepage = "http://banisterfiend.wordpress.com"
26
- s.has_rdoc = 'yard'
27
- s.files = Dir["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "lib/**/*.rb",
28
- "test/*.rb", "HISTORY", "README.md", "Rakefile"]
29
- end
30
-
31
- desc "run tests"
32
- task :test do
33
- sh "bacon -k #{direc}/test/test.rb"
34
- end
35
-
36
- [:mingw32, :mswin32].each do |v|
37
- namespace v do
38
- spec = Gem::Specification.new do |s|
39
- apply_spec_defaults(s)
40
- s.platform = "i386-#{v}"
41
- s.files += FileList["lib/**/*.#{dlext}"].to_a
42
- end
43
-
44
- Rake::GemPackageTask.new(spec) do |pkg|
45
- pkg.need_zip = false
46
- pkg.need_tar = false
47
- end
48
- end
49
- end
50
-
51
- namespace :ruby do
52
- spec = Gem::Specification.new do |s|
53
- apply_spec_defaults(s)
54
- s.platform = Gem::Platform::RUBY
55
- s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
56
- end
57
-
58
- Rake::GemPackageTask.new(spec) do |pkg|
59
- pkg.need_zip = false
60
- pkg.need_tar = false
61
- end
62
- end
63
-
64
- directories = ["#{direc}/lib/1.8", "#{direc}/lib/1.9"]
65
- directories.each { |d| directory d }
66
-
67
- desc "build the 1.8 and 1.9 binaries from source and copy to lib/"
68
- task :compile => directories do
69
- build_for = proc do |pik_ver, ver|
70
- sh %{ \
71
- c:\\devkit\\devkitvars.bat && \
72
- pik #{pik_ver} && \
73
- ruby extconf.rb && \
74
- make clean && \
75
- make && \
76
- cp *.so #{direc}/lib/#{ver} \
77
- }
78
- end
79
-
80
- chdir("#{direc}/ext/#{PROJECT_NAME}") do
81
- build_for.call("187", "1.8")
82
- build_for.call("192", "1.9")
83
- end
84
- end
85
-
86
- desc "build all platform gems at once"
87
- task :gems => [:clean, :rmgems, "mingw32:gem", "mswin32:gem", "ruby:gem"]
88
-
89
- desc "remove all platform gems"
90
- task :rmgems => ["ruby:clobber_package"]
91
-
92
- desc "build and push latest gems"
93
- task :pushgems => :gems do
94
- chdir("#{direc}/pkg") do
95
- Dir["*.gem"].each do |gemfile|
96
- sh "gem push #{gemfile}"
97
- end
98
- end
99
- end
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
+ # 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
+ 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/"
105
+ end
106
+ end
107
+
108
+ desc 'cleanup the extensions'
109
+ task :cleanup do
110
+ sh 'rm -rf lib/binding_of_caller.so'
111
+ chdir "./ext/#{PROJECT_NAME}/" do
112
+ sh 'make clean'
113
+ end
114
+ end
115
+
116
+ desc "reinstall gem"
117
+ task :reinstall => :gems do
118
+ sh "gem uninstall binding_of_caller" rescue nil
119
+ sh "gem install #{direc}/pkg/#{PROJECT_NAME}-#{BindingOfCaller::VERSION}.gem"
120
+ end
121
+
122
+ desc "build all platform gems at once"
123
+ task :gems => [:clean, :rmgems, "ruby:gem"]
124
+
125
+ task :gem => [:gems]
126
+
127
+ desc "remove all platform gems"
128
+ task :rmgems => ["ruby:clobber_package"]
129
+
130
+ desc "build and push latest gems"
131
+ task :pushgems => :gems do
132
+ chdir("./pkg") do
133
+ Dir["*.gem"].each do |gemfile|
134
+ sh "gem push #{gemfile}"
135
+ end
136
+ end
137
+ 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.6"
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-03-01"
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.12"
17
+ s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack."
18
+ s.test_files = ["test/test_binding_of_caller.rb"]
19
+
20
+ if s.respond_to? :specification_version then
21
+ s.specification_version = 3
22
+
23
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
24
+ s.add_development_dependency(%q<bacon>, [">= 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
@@ -0,0 +1,41 @@
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
+ outer = 10
8
+
9
+ class Z
10
+ def z
11
+ u = 10
12
+ A.new.a
13
+ end
14
+ end
15
+
16
+ class A
17
+ def a
18
+ y = 10
19
+ B.new.b
20
+ end
21
+ end
22
+
23
+ class B
24
+ def b
25
+ x = 10
26
+ puts binding.of_caller(0).eval('local_variables')
27
+ puts binding.of_caller(1).eval('local_variables')
28
+ puts binding.of_caller(2).eval('local_variables')
29
+ puts binding.of_caller(3).eval('local_variables')
30
+ puts binding.of_caller(400).eval('local_variables')
31
+ end
32
+ end
33
+
34
+ Z.new.z
35
+
36
+ # output:
37
+ # => x
38
+ # => y
39
+ # => u
40
+ # => outer
41
+ # Exception
@@ -1,22 +1,31 @@
1
1
  /* (c) 2011 John Mair (banisterfiend), MIT license */
2
2
 
3
3
  #include <ruby.h>
4
- //#include "compat.h"
5
-
6
- //#ifdef RUBY_19
7
- # include <ruby/io.h>
8
- # include <ruby/re.h>
9
- # include "vm_core.h"
10
- # include "gc.h"
11
- /* #else */
12
- /* # include "re.h" */
13
- /* # include "env.h" */
14
- /* # include "node.h" */
15
- /* # include "rubysig.h" */
16
- /* # include "rubyio.h" */
17
- /* #endif */
18
-
19
- extern rb_thread_t *ruby_current_thread;
4
+ #include "vm_core.h"
5
+ #include "gc.h"
6
+
7
+ typedef enum { false, true } bool;
8
+
9
+ static VALUE
10
+ string2sym(const char * string)
11
+ {
12
+ return ID2SYM(rb_intern(string));
13
+ }
14
+
15
+ static inline const rb_data_type_t *
16
+ threadptr_data_type(void)
17
+ {
18
+ static const rb_data_type_t *thread_data_type;
19
+ if (!thread_data_type) {
20
+ VALUE current_thread = rb_thread_current();
21
+ thread_data_type = RTYPEDDATA_TYPE(current_thread);
22
+ }
23
+ return thread_data_type;
24
+ }
25
+
26
+ #define ruby_thread_data_type *threadptr_data_type()
27
+
28
+ #define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
20
29
 
21
30
  static size_t
22
31
  binding_memsize(const void *ptr)
@@ -44,7 +53,11 @@ binding_mark(void *ptr)
44
53
  if (ptr) {
45
54
  bind = ptr;
46
55
  RUBY_MARK_UNLESS_NULL(bind->env);
56
+
57
+ #ifdef RUBY_192
47
58
  RUBY_MARK_UNLESS_NULL(bind->filename);
59
+ #endif
60
+
48
61
  }
49
62
  RUBY_MARK_LEAVE("binding");
50
63
  }
@@ -65,60 +78,143 @@ binding_alloc(VALUE klass)
65
78
  return obj;
66
79
  }
67
80
 
68
- typedef enum { false, true } bool;
69
-
70
- static bool valid_frame_p(rb_control_frame_t * cfp) {
81
+ static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
71
82
  return cfp->iseq && !NIL_P(cfp->self);
72
83
  }
73
84
 
74
- static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp) {
75
- int error_count = 0;
76
-
77
- while (error_count <= 4) {
85
+ static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
86
+ while (cfp < limit_cfp) {
78
87
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
79
88
 
80
- if (valid_frame_p(cfp))
89
+ if (cfp >= limit_cfp)
90
+ return NULL;
91
+
92
+ if (valid_frame_p(cfp, limit_cfp))
81
93
  return cfp;
82
- else
83
- error_count += 1;
84
94
  }
85
95
 
86
- rb_raise(rb_eRuntimeError, "No valid stack frame found.");
96
+ // beyond end of stack
97
+ return NULL;
98
+ }
87
99
 
88
- // never reached
89
- return 0;
100
+ static VALUE
101
+ frametype_name(VALUE flag)
102
+ {
103
+ switch (flag & VM_FRAME_MAGIC_MASK) {
104
+ case VM_FRAME_MAGIC_METHOD: return string2sym("method");
105
+ case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
106
+ case VM_FRAME_MAGIC_CLASS: return string2sym("class");
107
+ case VM_FRAME_MAGIC_TOP: return string2sym("top");
108
+ case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
109
+ case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
110
+ case VM_FRAME_MAGIC_PROC: return string2sym("proc");
111
+ case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
112
+ case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
113
+ case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
114
+ default:
115
+ rb_raise(rb_eRuntimeError, "Unknown frame type! got flag: %d", FIX2INT(flag));
116
+ }
90
117
  }
91
118
 
92
119
  static VALUE binding_of_caller(VALUE self, VALUE rb_level)
93
120
  {
94
- rb_thread_t *th = GET_THREAD();
121
+ rb_thread_t *th;
122
+ GetThreadPtr(rb_thread_current(), th);
123
+
95
124
  rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
125
+ rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
96
126
  int level = FIX2INT(rb_level);
97
127
 
98
- for (int i = 0; i < level; i++)
128
+ // attempt to locate the nth parent control frame
129
+ for (int i = 0; i < level; i++) {
99
130
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
100
131
 
101
- if (!valid_frame_p(cfp))
102
- cfp = find_valid_frame(cfp);
132
+ if (cfp >= limit_cfp)
133
+ rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
134
+
135
+ // skip invalid frames
136
+ if (!valid_frame_p(cfp, limit_cfp))
137
+ cfp = find_valid_frame(cfp, limit_cfp);
138
+ }
103
139
 
104
140
  VALUE bindval = binding_alloc(rb_cBinding);
105
141
  rb_binding_t *bind;
106
142
 
107
- if (cfp == 0) {
143
+ if (cfp == 0)
108
144
  rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
109
- }
110
145
 
111
146
  GetBindingPtr(bindval, bind);
112
147
  bind->env = rb_vm_make_env_object(th, cfp);
113
148
  bind->filename = cfp->iseq->filename;
114
149
  bind->line_no = rb_vm_get_sourceline(cfp);
150
+
151
+ rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
152
+ rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
153
+
115
154
  return bindval;
116
155
  }
117
156
 
157
+ static VALUE
158
+ frame_type(VALUE self)
159
+ {
160
+ return rb_iv_get(self, "@frame_type");
161
+ }
162
+
163
+ static VALUE
164
+ frame_description(VALUE self)
165
+ {
166
+ return rb_iv_get(self, "@frame_description");
167
+ }
168
+
169
+ static VALUE frame_count(VALUE self)
170
+ {
171
+ rb_thread_t *th;
172
+ GetThreadPtr(rb_thread_current(), th);
173
+
174
+ rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
175
+ rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
176
+
177
+ int i = 1;
178
+ while (cfp < limit_cfp) {
179
+ cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
180
+
181
+ if (cfp >= limit_cfp)
182
+ return INT2FIX(i);
183
+
184
+ // skip invalid frames
185
+ if (!valid_frame_p(cfp, limit_cfp))
186
+ cfp = find_valid_frame(cfp, limit_cfp);
187
+
188
+ if (!cfp)
189
+ break;
190
+
191
+ i++;
192
+ }
193
+
194
+ return INT2FIX(i);
195
+ }
196
+
197
+ static VALUE
198
+ callers(VALUE self)
199
+ {
200
+ VALUE ary = rb_ary_new();
201
+
202
+ for (int i = 0; i < FIX2INT(frame_count(self)); i++)
203
+ rb_ary_push(ary, binding_of_caller(self, INT2FIX(i)));
204
+
205
+ return ary;
206
+ }
207
+
118
208
  void
119
209
  Init_binding_of_caller()
120
210
  {
121
- rb_define_method(rb_cObject, "binding_of_caller", binding_of_caller, 1);
122
-
211
+ VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
212
+
213
+ rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
214
+ rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
215
+ rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
216
+ rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
217
+ rb_define_method(mBindingOfCaller, "callers", callers, 0);
218
+ rb_include_module(rb_cBinding, mBindingOfCaller);
123
219
  }
124
220
 
@@ -1,14 +1,24 @@
1
- require 'mkmf'
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
- $CFLAGS += " -O0"
4
- $CFLAGS += " -std=c99"
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
- case RUBY_VERSION
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.2.0"
3
- end
1
+ module BindingOfCaller
2
+ VERSION = "0.6.6"
3
+ end
@@ -1,22 +1,86 @@
1
- # binding_of_caller.rb
2
- # (C) John Mair (banisterfiend); MIT license
3
-
1
+ dlext = RbConfig::CONFIG['DLEXT']
4
2
  direc = File.dirname(__FILE__)
5
3
 
6
- require "#{direc}/binding_of_caller/version"
4
+ if RUBY_ENGINE && RUBY_ENGINE == "ruby"
5
+ require "binding_of_caller.#{dlext}"
7
6
 
8
- begin
9
- if RUBY_VERSION =~ /1.9/
10
- require "#{direc}/1.9/binding_of_caller"
11
- else
12
- require "#{direc}/1.8/binding_of_caller"
13
- end
14
- rescue LoadError => e
15
- require "rbconfig"
16
- dlext = Config::CONFIG['DLEXT']
17
- require "#{direc}/binding_of_caller.#{dlext}"
18
- end
7
+ elsif defined?(Rubinius)
8
+ module BindingOfCaller
9
+ module BindingExtensions
10
+
11
+ def of_caller(n)
12
+ bt = Rubinius::VM.backtrace(1 + n, true).first
13
+
14
+ b = Binding.setup(
15
+ bt.variables,
16
+ bt.variables.method,
17
+ bt.static_scope,
18
+ bt.variables.self,
19
+ bt
20
+ )
21
+
22
+ b.instance_variable_set(:@frame_description, bt.describe)
23
+
24
+ b
25
+ rescue
26
+ raise RuntimeError, "Invalid frame, gone beyond end of stack!"
27
+ end
28
+
29
+ def frame_description
30
+ @frame_description
31
+ end
32
+
33
+ def callers
34
+ ary = []
35
+ n = 0
36
+ loop {
37
+ begin
38
+ ary << Binding.of_caller(n)
39
+ rescue
40
+ break
41
+ end
19
42
 
20
- module BindingOfCaller
21
- end
43
+ n += 1
44
+ }
22
45
 
46
+ ary
47
+ end
48
+
49
+ def frame_count
50
+ n = 1
51
+ loop {
52
+ begin
53
+ Binding.of_caller(n)
54
+ rescue
55
+ break
56
+ end
57
+
58
+ n += 1
59
+ }
60
+
61
+ n
62
+ end
63
+
64
+ def frame_type
65
+ case self.variables.method.metadata.to_a.first.to_s
66
+ when /block/
67
+ :block
68
+ when /eval/
69
+ :eval
70
+ else
71
+ if frame_description =~ /__(?:class|module)_init__/
72
+ :class
73
+ else
74
+ :method
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ class ::Binding
83
+ include BindingOfCaller::BindingExtensions
84
+ extend BindingOfCaller::BindingExtensions
85
+ end
86
+ end
@@ -0,0 +1,91 @@
1
+ unless Object.const_defined? :BindingOfCaller
2
+ $:.unshift File.expand_path '../../lib', __FILE__
3
+ require 'binding_of_caller'
4
+ require 'binding_of_caller/version'
5
+ end
6
+
7
+ puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
8
+ puts "Ruby version: #{RUBY_VERSION}"
9
+
10
+ describe BindingOfCaller do
11
+ describe "of_caller" do
12
+ it "should fetch immediate caller's binding when 0 is passed" do
13
+ o = Object.new
14
+ def o.a
15
+ var = 1
16
+ binding.of_caller(0).eval('var')
17
+ end
18
+
19
+ o. a.should == 1
20
+ end
21
+
22
+ it "should fetch parent of caller's binding when 1 is passed" do
23
+ o = Object.new
24
+ def o.a
25
+ var = 1
26
+ b
27
+ end
28
+
29
+ def o.b
30
+ binding.of_caller(1).eval('var')
31
+ end
32
+
33
+ o.a.should == 1
34
+ end
35
+
36
+ it "should modify locals in parent of caller's binding" do
37
+ o = Object.new
38
+ def o.a
39
+ var = 1
40
+ b
41
+ var
42
+ end
43
+
44
+ def o.b
45
+ binding.of_caller(1).eval('var = 20')
46
+ end
47
+
48
+ o.a.should == 20
49
+ end
50
+
51
+ it "should raise an exception when retrieving an out of band binding" do
52
+ o = Object.new
53
+ def o.a
54
+ binding.of_caller(100)
55
+ end
56
+
57
+ lambda { o.a }.should.raise RuntimeError
58
+ end
59
+ end
60
+
61
+ describe "frame_count" do
62
+ it 'frame_count should equal callers.count' do
63
+ binding.frame_count.should == binding.callers.count
64
+ end
65
+ end
66
+
67
+ describe "frame_type" do
68
+ it 'should return the correct frame types' do
69
+ o = Object.new
70
+
71
+ # method frame
72
+ def o.a
73
+ b
74
+ end
75
+
76
+ # method frame
77
+ def o.b
78
+ # block frame
79
+ proc do
80
+ binding.callers
81
+ end.call
82
+ end
83
+ caller_bindings = o.a
84
+ caller_bindings[0].frame_type.should == :block
85
+ caller_bindings[1].frame_type.should == :method
86
+ caller_bindings[2].frame_type.should == :method
87
+ end
88
+
89
+ end
90
+ end
91
+
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding_of_caller
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 1509392978297545075
4
5
  prerelease:
5
- version: 0.2.0
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 6
10
+ version: 0.6.6
6
11
  platform: ruby
7
12
  authors:
8
13
  - John Mair (banisterfiend)
@@ -10,7 +15,7 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-10-17 00:00:00 Z
18
+ date: 2012-03-01 00:00:00 Z
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: bacon
@@ -20,10 +25,27 @@ dependencies:
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
23
- version: 1.1.0
28
+ hash: 2002549777813010636
29
+ segments:
30
+ - 0
31
+ version: "0"
24
32
  type: :development
25
33
  version_requirements: *id001
26
- description: FIX ME
34
+ - !ruby/object:Gem::Dependency
35
+ name: rake
36
+ 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"
46
+ 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.
27
49
  email: jrmair@gmail.com
28
50
  executables: []
29
51
 
@@ -32,8 +54,19 @@ extensions:
32
54
  extra_rdoc_files: []
33
55
 
34
56
  files:
57
+ - .gemtest
58
+ - .gitignore
59
+ - .travis.yml
60
+ - .yardopts
61
+ - Gemfile
62
+ - HISTORY
63
+ - LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - binding_of_caller.gemspec
67
+ - examples/example.rb
68
+ - ext/binding_of_caller/binding_of_caller.c
35
69
  - ext/binding_of_caller/extconf.rb
36
- - ext/binding_of_caller/compat.h
37
70
  - ext/binding_of_caller/ruby_headers/192/debug.h
38
71
  - ext/binding_of_caller/ruby_headers/192/dln.h
39
72
  - ext/binding_of_caller/ruby_headers/192/eval_intern.h
@@ -82,14 +115,10 @@ files:
82
115
  - ext/binding_of_caller/ruby_headers/193/vm_exec.h
83
116
  - ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
84
117
  - ext/binding_of_caller/ruby_headers/193/vm_opts.h
85
- - ext/binding_of_caller/binding_of_caller.c
86
- - lib/binding_of_caller/version.rb
87
118
  - lib/binding_of_caller.rb
88
- - test/test.rb
89
- - HISTORY
90
- - README.md
91
- - Rakefile
92
- homepage: http://banisterfiend.wordpress.com
119
+ - lib/binding_of_caller/version.rb
120
+ - test/test_binding_of_caller.rb
121
+ homepage: http://github.com/banister/binding_of_caller
93
122
  licenses: []
94
123
 
95
124
  post_install_message:
@@ -102,19 +131,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
131
  requirements:
103
132
  - - ">="
104
133
  - !ruby/object:Gem::Version
134
+ hash: 2002549777813010636
135
+ segments:
136
+ - 0
105
137
  version: "0"
106
138
  required_rubygems_version: !ruby/object:Gem::Requirement
107
139
  none: false
108
140
  requirements:
109
141
  - - ">="
110
142
  - !ruby/object:Gem::Version
143
+ hash: 2002549777813010636
144
+ segments:
145
+ - 0
111
146
  version: "0"
112
147
  requirements: []
113
148
 
114
149
  rubyforge_project:
115
- rubygems_version: 1.8.11
150
+ rubygems_version: 1.8.12
116
151
  signing_key:
117
152
  specification_version: 3
118
- summary: FIX ME
119
- test_files: []
120
-
153
+ summary: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack.
154
+ test_files:
155
+ - test/test_binding_of_caller.rb
@@ -1,57 +0,0 @@
1
- /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
-
3
- #ifndef GUARD_COMPAT_H
4
- #define GUARD_COMPAT_H
5
-
6
- #include <ruby.h>
7
-
8
- /* test for 1.9 */
9
- #if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
10
- # define RUBY_19
11
- #endif
12
-
13
- /* macros for backwards compatibility with 1.8 */
14
- #ifndef RUBY_19
15
- # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
16
- # define RCLASS_SUPER(c) (RCLASS(c)->super)
17
- # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
18
- # define OBJ_UNTRUSTED OBJ_TAINTED
19
- # include "st.h"
20
- #endif
21
-
22
- #ifdef RUBY_19
23
- inline static VALUE
24
- class_alloc(VALUE flags, VALUE klass)
25
- {
26
- rb_classext_t *ext = ALLOC(rb_classext_t);
27
- NEWOBJ(obj, struct RClass);
28
- OBJSETUP(obj, klass, flags);
29
- obj->ptr = ext;
30
- RCLASS_IV_TBL(obj) = 0;
31
- RCLASS_M_TBL(obj) = 0;
32
- RCLASS_SUPER(obj) = 0;
33
- RCLASS_IV_INDEX_TBL(obj) = 0;
34
- return (VALUE)obj;
35
- }
36
- #endif
37
-
38
- inline static VALUE
39
- create_class(VALUE flags, VALUE klass)
40
- {
41
- #ifdef RUBY_19
42
- VALUE new_klass = class_alloc(flags, klass);
43
- #else
44
- NEWOBJ(new_klass, struct RClass);
45
- OBJSETUP(new_klass, klass, flags);
46
- #endif
47
-
48
- return (VALUE)new_klass;
49
- }
50
-
51
- # define FALSE 0
52
- # define TRUE 1
53
-
54
- /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
55
- #define KLASS_OF(c) (RBASIC(c)->klass)
56
-
57
- #endif
data/test/test.rb DELETED
@@ -1,12 +0,0 @@
1
- direc = File.dirname(__FILE__)
2
-
3
- require 'rubygems'
4
- require "#{direc}/../lib/binding_of_caller"
5
- require 'bacon'
6
-
7
- puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
8
- puts "Ruby version: #{RUBY_VERSION}"
9
-
10
- describe BindingOfCaller do
11
- end
12
-