binding_of_caller 0.5.0 → 0.6.3

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/README.md CHANGED
@@ -13,7 +13,7 @@ call stack, not limited to just the immediate caller.
13
13
 
14
14
  **Recommended for use only in debugging situations. Do not use this in production apps.**
15
15
 
16
- **Only works in MRI Ruby 1.9.2**
16
+ **Only works in MRI Ruby 1.9.2 and 1.9.3**
17
17
 
18
18
  * Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller`
19
19
  * See the [source code](http://github.com/banister/binding_of_caller)
@@ -50,8 +50,7 @@ This project is a spinoff from the [Pry REPL project.](http://pry.github.com)
50
50
  Features and limitations
51
51
  -------------------------
52
52
 
53
- * Only works with MRI 1.9.2
54
- * Broken in 1.9.3, support will hopefully be provided in the near future.
53
+ * Only works with MRI 1.9.2 and 1.9.3
55
54
  * Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there.
56
55
 
57
56
  Contact
data/Rakefile CHANGED
@@ -1,28 +1,30 @@
1
1
  dlext = Config::CONFIG['DLEXT']
2
+ direc = File.dirname(__FILE__)
2
3
 
3
4
  $:.unshift 'lib'
4
5
 
5
6
  PROJECT_NAME = "binding_of_caller"
6
7
 
7
8
  require 'rake/clean'
8
- require 'rake/gempackagetask'
9
+ require 'rubygems/package_task'
10
+
9
11
  require "#{PROJECT_NAME}/version"
10
12
 
11
13
  CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
12
14
  CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
13
15
  "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*",
14
- "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
16
+ "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc")
15
17
 
16
18
  def apply_spec_defaults(s)
17
19
  s.name = PROJECT_NAME
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."
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+"
19
21
  s.version = BindingOfCaller::VERSION
20
22
  s.date = Time.now.strftime '%Y-%m-%d'
21
23
  s.author = "John Mair (banisterfiend)"
22
24
  s.email = 'jrmair@gmail.com'
23
25
  s.description = s.summary
24
26
  s.require_path = 'lib'
25
- s.add_development_dependency("bacon","~>1.1.0")
27
+ s.add_development_dependency("bacon","~>1.1")
26
28
  s.homepage = "http://github.com/banister/binding_of_caller"
27
29
  s.has_rdoc = 'yard'
28
30
  s.files = `git ls-files`.split("\n")
@@ -39,6 +41,9 @@ task :pry do
39
41
  sh "pry -r ./lib/binding_of_caller"
40
42
  end
41
43
 
44
+ desc "generate gemspec"
45
+ task :gemspec => "ruby:gemspec"
46
+
42
47
  namespace :ruby do
43
48
  spec = Gem::Specification.new do |s|
44
49
  apply_spec_defaults(s)
@@ -46,7 +51,26 @@ namespace :ruby do
46
51
  s.extensions = ["ext/#{PROJECT_NAME}/extconf.rb"]
47
52
  end
48
53
 
49
- Rake::GemPackageTask.new(spec) do |pkg|
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|
50
74
  pkg.need_zip = false
51
75
  pkg.need_tar = false
52
76
  end
@@ -62,11 +86,19 @@ task :compile do
62
86
  end
63
87
  end
64
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
+
65
95
  desc "build all platform gems at once"
66
- task :gems => [:clean, :rmgems, "ruby:gem"]
96
+ task :gems => [:clean, :rmgems, "ruby:gem", "rbx:gem"]
67
97
 
68
98
  task :gem => [:gems]
69
99
 
100
+ task :rbxgem => "rbx:gem"
101
+
70
102
  desc "remove all platform gems"
71
103
  task :rmgems => ["ruby:clobber_package"]
72
104
 
@@ -53,7 +53,11 @@ binding_mark(void *ptr)
53
53
  if (ptr) {
54
54
  bind = ptr;
55
55
  RUBY_MARK_UNLESS_NULL(bind->env);
56
+
57
+ #ifdef RUBY_192
56
58
  RUBY_MARK_UNLESS_NULL(bind->filename);
59
+ #endif
60
+
57
61
  }
58
62
  RUBY_MARK_LEAVE("binding");
59
63
  }
@@ -93,6 +97,25 @@ static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_contro
93
97
  return NULL;
94
98
  }
95
99
 
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
+ }
117
+ }
118
+
96
119
  static VALUE binding_of_caller(VALUE self, VALUE rb_level)
97
120
  {
98
121
  rb_thread_t *th;
@@ -125,34 +148,22 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
125
148
  bind->filename = cfp->iseq->filename;
126
149
  bind->line_no = rb_vm_get_sourceline(cfp);
127
150
 
128
- rb_iv_set(bindval, "@frame_type", cfp->flag);
151
+ rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
152
+ rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
153
+
129
154
  return bindval;
130
155
  }
131
156
 
132
-
133
157
  static VALUE
134
- frametype_name(VALUE flag)
158
+ frame_type(VALUE self)
135
159
  {
136
- switch (flag & VM_FRAME_MAGIC_MASK) {
137
- case VM_FRAME_MAGIC_METHOD: return string2sym("method");
138
- case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
139
- case VM_FRAME_MAGIC_CLASS: return string2sym("class");
140
- case VM_FRAME_MAGIC_TOP: return string2sym("top");
141
- case VM_FRAME_MAGIC_FINISH: return string2sym("finish");
142
- case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
143
- case VM_FRAME_MAGIC_PROC: return string2sym("proc");
144
- case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
145
- case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
146
- case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
147
- default:
148
- rb_raise(rb_eRuntimeError, "frame_type can only be returned for bindings created with Binding#of_caller().");
149
- }
160
+ return rb_iv_get(self, "@frame_type");
150
161
  }
151
162
 
152
163
  static VALUE
153
- frame_type(VALUE self)
164
+ frame_description(VALUE self)
154
165
  {
155
- return frametype_name(rb_iv_get(self, "@frame_type"));
166
+ return rb_iv_get(self, "@frame_description");
156
167
  }
157
168
 
158
169
  static VALUE frame_count(VALUE self)
@@ -174,6 +185,9 @@ static VALUE frame_count(VALUE self)
174
185
  if (!valid_frame_p(cfp, limit_cfp))
175
186
  cfp = find_valid_frame(cfp, limit_cfp);
176
187
 
188
+ if (!cfp)
189
+ break;
190
+
177
191
  i++;
178
192
  }
179
193
 
@@ -199,6 +213,7 @@ Init_binding_of_caller()
199
213
  rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
200
214
  rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
201
215
  rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
216
+ rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
202
217
  rb_define_method(mBindingOfCaller, "callers", callers, 0);
203
218
  rb_include_module(rb_cBinding, mBindingOfCaller);
204
219
  }
@@ -5,10 +5,9 @@ $CFLAGS += " -std=c99"
5
5
 
6
6
  case RUBY_VERSION
7
7
  when /1.9.2/
8
- $CFLAGS += " -I./ruby_headers/192/"
8
+ $CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
9
9
  when /1.9.3/
10
- puts "hit 193"
11
- $CFLAGS += " -I./ruby_headers/193/"
10
+ $CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
12
11
  end
13
12
 
14
13
  create_makefile('binding_of_caller')
@@ -1,3 +1,3 @@
1
1
  module BindingOfCaller
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.3"
3
3
  end
Binary file
@@ -0,0 +1,82 @@
1
+ dlext = Config::CONFIG['DLEXT']
2
+
3
+ if RUBY_ENGINE && RUBY_ENGINE == "ruby"
4
+ require "binding_of_caller.#{dlext}"
5
+
6
+ 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
82
+ end
data/lib/tester.rb ADDED
@@ -0,0 +1,15 @@
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
data/test/test.rb CHANGED
@@ -86,9 +86,6 @@ describe BindingOfCaller do
86
86
  caller_bindings[2].frame_type.should == :method
87
87
  end
88
88
 
89
- it 'should raise when invoked on an ordinary binding (i.e one not generated through Binding#of_caller)' do
90
- lambda { binding.frame_type }.should.raise RuntimeError
91
- end
92
89
  end
93
90
  end
94
91
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding_of_caller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,21 +9,21 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-05 00:00:00.000000000 Z
12
+ date: 2012-02-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
16
- requirement: &70308421840640 !ruby/object:Gem::Requirement
16
+ requirement: &70210333909540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.1.0
21
+ version: '1.1'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70308421840640
24
+ version_requirements: *70210333909540
25
25
  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.
26
+ even further up the stack. Currently only works for MRI 1.9.2+
27
27
  email: jrmair@gmail.com
28
28
  executables: []
29
29
  extensions:
@@ -88,7 +88,10 @@ files:
88
88
  - ext/binding_of_caller/ruby_headers/193/vm_exec.h
89
89
  - ext/binding_of_caller/ruby_headers/193/vm_insnhelper.h
90
90
  - ext/binding_of_caller/ruby_headers/193/vm_opts.h
91
+ - lib/binding_of_caller.bundle
92
+ - lib/binding_of_caller.rb
91
93
  - lib/binding_of_caller/version.rb
94
+ - lib/tester.rb
92
95
  - test/test.rb
93
96
  homepage: http://github.com/banister/binding_of_caller
94
97
  licenses: []
@@ -110,10 +113,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
113
  version: '0'
111
114
  requirements: []
112
115
  rubyforge_project:
113
- rubygems_version: 1.8.11
116
+ rubygems_version: 1.8.16
114
117
  signing_key:
115
118
  specification_version: 3
116
119
  summary: Retrieve the binding of a method's caller. Can also retrieve bindings even
117
- further up the stack. Currently only works for MRI 1.9.2.
120
+ further up the stack. Currently only works for MRI 1.9.2+
118
121
  test_files:
119
122
  - test/test.rb