binding_of_caller 0.2.0 → 0.3.1

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/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
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,50 @@ 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**
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
+ def a
25
+ var = 10
26
+ b
27
+ puts var
28
+ end
29
+
30
+ def b
31
+ c
32
+ end
33
+
34
+ def c
35
+ binding.of_caller(2).eval('var = :hello')
36
+ end
37
+
38
+ a()
18
39
 
19
- puts "example code"
40
+ # OUTPUT
41
+ # => hello
20
42
 
21
43
  Features and limitations
22
44
  -------------------------
23
45
 
24
- Feature List Preamble
46
+ * Only works with MRI 1.9.2
47
+ * Broken in 1.9.3, support will hopefully be provided in the near
48
+ * future.
49
+ * Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
25
50
 
26
51
  Contact
27
52
  -------
@@ -32,7 +57,7 @@ Problems or questions contact me at [github](http://github.com/banister)
32
57
  License
33
58
  -------
34
59
 
35
- (The MIT License)
60
+ (The MIT License)
36
61
 
37
62
  Copyright (c) 2011 (John Mair)
38
63
 
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
1
  dlext = Config::CONFIG['DLEXT']
2
- direc = File.dirname(__FILE__)
2
+
3
+ $:.unshift 'lib'
3
4
 
4
5
  PROJECT_NAME = "binding_of_caller"
5
6
 
6
7
  require 'rake/clean'
7
8
  require 'rake/gempackagetask'
8
- require "#{direc}/lib/#{PROJECT_NAME}/version"
9
+ require "#{PROJECT_NAME}/version"
9
10
 
10
11
  CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
11
12
  CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
@@ -14,84 +15,64 @@ CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
14
15
 
15
16
  def apply_spec_defaults(s)
16
17
  s.name = PROJECT_NAME
17
- s.summary = "FIX ME"
18
+ 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."
18
19
  s.version = BindingOfCaller::VERSION
19
20
  s.date = Time.now.strftime '%Y-%m-%d'
20
21
  s.author = "John Mair (banisterfiend)"
21
22
  s.email = 'jrmair@gmail.com'
22
23
  s.description = s.summary
23
24
  s.require_path = 'lib'
24
- s.add_development_dependency("bacon",">=1.1.0")
25
- s.homepage = "http://banisterfiend.wordpress.com"
25
+ s.add_development_dependency("bacon","~>1.1.0")
26
+ s.homepage = "http://github.com/banister/binding_of_caller"
26
27
  s.has_rdoc = 'yard'
27
- s.files = Dir["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c", "lib/**/*.rb",
28
- "test/*.rb", "HISTORY", "README.md", "Rakefile"]
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- test/*`.split("\n")
29
30
  end
30
31
 
31
- desc "run tests"
32
+ desc "Run tests"
32
33
  task :test do
33
- sh "bacon -k #{direc}/test/test.rb"
34
+ sh "bacon -Itest -rubygems test.rb -q"
34
35
  end
35
36
 
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
37
+ task :pry do
38
+ puts "loading binding_of_caller into pry"
39
+ sh "pry -r ./lib/binding_of_caller"
49
40
  end
50
41
 
51
42
  namespace :ruby do
52
43
  spec = Gem::Specification.new do |s|
53
- apply_spec_defaults(s)
44
+ apply_spec_defaults(s)
54
45
  s.platform = Gem::Platform::RUBY
55
46
  s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
56
47
  end
57
-
48
+
58
49
  Rake::GemPackageTask.new(spec) do |pkg|
59
50
  pkg.need_zip = false
60
51
  pkg.need_tar = false
61
52
  end
62
53
  end
63
54
 
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")
55
+ desc "build the binaries"
56
+ task :compile do
57
+ chdir "./ext/#{PROJECT_NAME}/" do
58
+ sh "ruby extconf.rb"
59
+ sh "make clean"
60
+ sh "make"
61
+ sh "cp *.#{dlext} ../../lib/"
83
62
  end
84
63
  end
85
64
 
86
65
  desc "build all platform gems at once"
87
- task :gems => [:clean, :rmgems, "mingw32:gem", "mswin32:gem", "ruby:gem"]
66
+ task :gems => [:clean, :rmgems, "ruby:gem"]
67
+
68
+ task :gem => [:gems]
88
69
 
89
70
  desc "remove all platform gems"
90
71
  task :rmgems => ["ruby:clobber_package"]
91
72
 
92
73
  desc "build and push latest gems"
93
74
  task :pushgems => :gems do
94
- chdir("#{direc}/pkg") do
75
+ chdir("./pkg") do
95
76
  Dir["*.gem"].each do |gemfile|
96
77
  sh "gem push #{gemfile}"
97
78
  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,15 @@
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
+
5
+ #include <ruby/io.h>
6
+ #include <ruby/re.h>
7
+ #include "vm_core.h"
8
+ #include "gc.h"
9
+
10
+ typedef enum { false, true } bool;
11
+
12
+ const int max_frame_errors = 4;
20
13
 
21
14
  static size_t
22
15
  binding_memsize(const void *ptr)
@@ -65,8 +58,6 @@ binding_alloc(VALUE klass)
65
58
  return obj;
66
59
  }
67
60
 
68
- typedef enum { false, true } bool;
69
-
70
61
  static bool valid_frame_p(rb_control_frame_t * cfp) {
71
62
  return cfp->iseq && !NIL_P(cfp->self);
72
63
  }
@@ -74,7 +65,7 @@ static bool valid_frame_p(rb_control_frame_t * cfp) {
74
65
  static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp) {
75
66
  int error_count = 0;
76
67
 
77
- while (error_count <= 4) {
68
+ while (error_count <= max_frame_errors) {
78
69
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
79
70
 
80
71
  if (valid_frame_p(cfp))
@@ -95,9 +86,11 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
95
86
  rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
96
87
  int level = FIX2INT(rb_level);
97
88
 
89
+ // attempt to locate the nth parent control frame
98
90
  for (int i = 0; i < level; i++)
99
91
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
100
92
 
93
+ // if did not find a valid one, then search for a valid one
101
94
  if (!valid_frame_p(cfp))
102
95
  cfp = find_valid_frame(cfp);
103
96
 
@@ -118,7 +111,9 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
118
111
  void
119
112
  Init_binding_of_caller()
120
113
  {
121
- rb_define_method(rb_cObject, "binding_of_caller", binding_of_caller, 1);
114
+ VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
122
115
 
116
+ rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
117
+ rb_include_module(rb_cBinding, mBindingOfCaller);
123
118
  }
124
119
 
@@ -1,3 +1,3 @@
1
1
  module BindingOfCaller
2
- VERSION = "0.2.0"
3
- end
2
+ VERSION = "0.3.1"
3
+ end
data/test/test.rb CHANGED
@@ -1,12 +1,59 @@
1
- direc = File.dirname(__FILE__)
2
-
3
- require 'rubygems'
4
- require "#{direc}/../lib/binding_of_caller"
5
- require 'bacon'
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
6
 
7
- puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
7
+ puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..."
8
8
  puts "Ruby version: #{RUBY_VERSION}"
9
9
 
10
10
  describe BindingOfCaller do
11
+ it "should fetch immediate caller's binding when 0 is passed" do
12
+ o = Object.new
13
+ def o.a
14
+ var = 1
15
+ binding.of_caller(0).eval('var')
16
+ end
17
+
18
+ o. a.should == 1
19
+ end
20
+
21
+ it "should fetch parent of caller's binding when 1 is passed" do
22
+ o = Object.new
23
+ def o.a
24
+ var = 1
25
+ b
26
+ end
27
+
28
+ def o.b
29
+ binding.of_caller(1).eval('var')
30
+ end
31
+
32
+ o.a.should == 1
33
+ end
34
+
35
+ it "should modify locals in parent of caller's binding" do
36
+ o = Object.new
37
+ def o.a
38
+ var = 1
39
+ b
40
+ var
41
+ end
42
+
43
+ def o.b
44
+ binding.of_caller(1).eval('var = 20')
45
+ end
46
+
47
+ o.a.should == 20
48
+ end
49
+
50
+ it "should raise an exception when retrieving an out of band binding" do
51
+ o = Object.new
52
+ def o.a
53
+ binding.of_caller(100)
54
+ end
55
+
56
+ lambda { o.a }.should.raise RuntimeError
57
+ end
11
58
  end
12
59
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: binding_of_caller
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - John Mair (banisterfiend)
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-17 00:00:00 Z
13
+ date: 2011-10-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bacon
@@ -18,12 +18,12 @@ dependencies:
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - ">="
21
+ - - ~>
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.1.0
24
24
  type: :development
25
25
  version_requirements: *id001
26
- description: FIX ME
26
+ description: 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.
27
27
  email: jrmair@gmail.com
28
28
  executables: []
29
29
 
@@ -32,8 +32,16 @@ extensions:
32
32
  extra_rdoc_files: []
33
33
 
34
34
  files:
35
+ - .gemtest
36
+ - .gitignore
37
+ - .yardopts
38
+ - HISTORY
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - examples/example.rb
43
+ - ext/binding_of_caller/binding_of_caller.c
35
44
  - ext/binding_of_caller/extconf.rb
36
- - ext/binding_of_caller/compat.h
37
45
  - ext/binding_of_caller/ruby_headers/192/debug.h
38
46
  - ext/binding_of_caller/ruby_headers/192/dln.h
39
47
  - ext/binding_of_caller/ruby_headers/192/eval_intern.h
@@ -82,14 +90,9 @@ files:
82
90
  - ext/binding_of_caller/ruby_headers/193/vm_exec.h
83
91
  - ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
84
92
  - ext/binding_of_caller/ruby_headers/193/vm_opts.h
85
- - ext/binding_of_caller/binding_of_caller.c
86
93
  - lib/binding_of_caller/version.rb
87
- - lib/binding_of_caller.rb
88
94
  - test/test.rb
89
- - HISTORY
90
- - README.md
91
- - Rakefile
92
- homepage: http://banisterfiend.wordpress.com
95
+ homepage: http://github.com/banister/binding_of_caller
93
96
  licenses: []
94
97
 
95
98
  post_install_message:
@@ -115,6 +118,6 @@ rubyforge_project:
115
118
  rubygems_version: 1.8.11
116
119
  signing_key:
117
120
  specification_version: 3
118
- summary: FIX ME
119
- test_files: []
120
-
121
+ 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.
122
+ test_files:
123
+ - test/test.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
@@ -1,22 +0,0 @@
1
- # binding_of_caller.rb
2
- # (C) John Mair (banisterfiend); MIT license
3
-
4
- direc = File.dirname(__FILE__)
5
-
6
- require "#{direc}/binding_of_caller/version"
7
-
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
19
-
20
- module BindingOfCaller
21
- end
22
-