binding_of_caller 0.4.0 → 0.8.0

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.
@@ -1,15 +1,32 @@
1
1
  /* (c) 2011 John Mair (banisterfiend), MIT license */
2
2
 
3
3
  #include <ruby.h>
4
-
5
- #include <ruby/io.h>
6
- #include <ruby/re.h>
7
4
  #include "vm_core.h"
8
- #include "gc.h"
5
+ #include "rubys_gc.h"
9
6
 
10
7
  typedef enum { false, true } bool;
11
8
 
12
- const int max_frame_errors = 4;
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
+ #define ruby_threadptr_data_type *threadptr_data_type()
28
+
29
+ #define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current()))
13
30
 
14
31
  static size_t
15
32
  binding_memsize(const void *ptr)
@@ -37,7 +54,11 @@ binding_mark(void *ptr)
37
54
  if (ptr) {
38
55
  bind = ptr;
39
56
  RUBY_MARK_UNLESS_NULL(bind->env);
57
+
58
+ #ifdef RUBY_192
40
59
  RUBY_MARK_UNLESS_NULL(bind->filename);
60
+ #endif
61
+
41
62
  }
42
63
  RUBY_MARK_LEAVE("binding");
43
64
  }
@@ -58,32 +79,52 @@ binding_alloc(VALUE klass)
58
79
  return obj;
59
80
  }
60
81
 
61
- static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
62
- if (cfp > limit_cfp)
63
- rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
82
+ static bool ifunc_p(rb_control_frame_t * cfp) {
83
+ return (cfp->flag & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_IFUNC;
84
+ }
64
85
 
65
- return cfp->iseq && !NIL_P(cfp->self);
86
+ static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
87
+ return cfp->iseq && !ifunc_p(cfp) && !NIL_P(cfp->self);
66
88
  }
67
89
 
68
90
  static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) {
69
- int error_count = 0;
70
-
71
- while (error_count <= max_frame_errors) {
91
+ while (cfp < limit_cfp) {
72
92
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
73
93
 
94
+ if (cfp >= limit_cfp)
95
+ return NULL;
96
+
74
97
  if (valid_frame_p(cfp, limit_cfp))
75
98
  return cfp;
76
- else
77
- error_count += 1;
78
99
  }
79
100
 
80
- // never reached
81
- return 0;
101
+ // beyond end of stack
102
+ return NULL;
103
+ }
104
+
105
+ static VALUE
106
+ frametype_name(VALUE flag)
107
+ {
108
+ switch (flag & VM_FRAME_MAGIC_MASK) {
109
+ case VM_FRAME_MAGIC_METHOD: return string2sym("method");
110
+ case VM_FRAME_MAGIC_BLOCK: return string2sym("block");
111
+ case VM_FRAME_MAGIC_CLASS: return string2sym("class");
112
+ case VM_FRAME_MAGIC_TOP: return string2sym("top");
113
+ case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc");
114
+ case VM_FRAME_MAGIC_PROC: return string2sym("proc");
115
+ case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc");
116
+ case VM_FRAME_MAGIC_EVAL: return string2sym("eval");
117
+ case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda");
118
+ default:
119
+ rb_raise(rb_eRuntimeError, "Unknown frame type! got flag: %d", FIX2INT(flag));
120
+ }
82
121
  }
83
122
 
84
123
  static VALUE binding_of_caller(VALUE self, VALUE rb_level)
85
124
  {
86
- rb_thread_t *th = GET_THREAD();
125
+ rb_thread_t *th;
126
+ GetThreadPtr(rb_thread_current(), th);
127
+
87
128
  rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
88
129
  rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
89
130
  int level = FIX2INT(rb_level);
@@ -92,6 +133,9 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
92
133
  for (int i = 0; i < level; i++) {
93
134
  cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
94
135
 
136
+ if (cfp >= limit_cfp)
137
+ rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!");
138
+
95
139
  // skip invalid frames
96
140
  if (!valid_frame_p(cfp, limit_cfp))
97
141
  cfp = find_valid_frame(cfp, limit_cfp);
@@ -100,23 +144,82 @@ static VALUE binding_of_caller(VALUE self, VALUE rb_level)
100
144
  VALUE bindval = binding_alloc(rb_cBinding);
101
145
  rb_binding_t *bind;
102
146
 
103
- if (cfp == 0) {
147
+ if (cfp == 0)
104
148
  rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
105
- }
106
149
 
107
150
  GetBindingPtr(bindval, bind);
151
+
108
152
  bind->env = rb_vm_make_env_object(th, cfp);
109
153
  bind->filename = cfp->iseq->filename;
110
154
  bind->line_no = rb_vm_get_sourceline(cfp);
155
+
156
+ rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag));
157
+ rb_iv_set(bindval, "@frame_description", cfp->iseq->name);
158
+
111
159
  return bindval;
112
160
  }
113
161
 
162
+ static VALUE
163
+ frame_type(VALUE self)
164
+ {
165
+ return rb_iv_get(self, "@frame_type");
166
+ }
167
+
168
+ static VALUE
169
+ frame_description(VALUE self)
170
+ {
171
+ return rb_iv_get(self, "@frame_description");
172
+ }
173
+
174
+ static VALUE frame_count(VALUE self)
175
+ {
176
+ rb_thread_t *th;
177
+ GetThreadPtr(rb_thread_current(), th);
178
+
179
+ rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
180
+ rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size);
181
+
182
+ int i = 1;
183
+ while (cfp < limit_cfp) {
184
+ cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
185
+
186
+ if (cfp >= limit_cfp)
187
+ return INT2FIX(i);
188
+
189
+ // skip invalid frames
190
+ if (!valid_frame_p(cfp, limit_cfp))
191
+ cfp = find_valid_frame(cfp, limit_cfp);
192
+
193
+ if (!cfp)
194
+ break;
195
+
196
+ i++;
197
+ }
198
+
199
+ return INT2FIX(i);
200
+ }
201
+
202
+ static VALUE
203
+ callers(VALUE self)
204
+ {
205
+ VALUE ary = rb_ary_new();
206
+
207
+ for (int i = 0; i < FIX2INT(frame_count(self)); i++)
208
+ rb_ary_push(ary, binding_of_caller(self, INT2FIX(i)));
209
+
210
+ return ary;
211
+ }
212
+
114
213
  void
115
214
  Init_binding_of_caller()
116
215
  {
117
216
  VALUE mBindingOfCaller = rb_define_module("BindingOfCaller");
118
217
 
119
218
  rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1);
219
+ rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0);
220
+ rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0);
221
+ rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0);
222
+ rb_define_method(mBindingOfCaller, "callers", callers, 0);
120
223
  rb_include_module(rb_cBinding, mBindingOfCaller);
121
224
  }
122
225
 
@@ -1,14 +1,28 @@
1
- require 'mkmf'
2
-
3
- $CFLAGS += " -O0"
4
- $CFLAGS += " -std=c99"
1
+ def fake_makefile
2
+ File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") do |f|
3
+ f.puts %[install:\n\techo "Nada."]
4
+ end
5
+ end
5
6
 
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/"
7
+ def mri_1_9?
8
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
9
+ RUBY_VERSION =~ /^1\.9/
12
10
  end
13
11
 
14
- create_makefile('binding_of_caller')
12
+ if mri_1_9?
13
+ require 'mkmf'
14
+
15
+ $CFLAGS += " -O0"
16
+ $CFLAGS += " -std=c99"
17
+
18
+ case RUBY_VERSION
19
+ when /1.9.2/
20
+ $CFLAGS += " -I./ruby_headers/192/ -DRUBY_192"
21
+ when /1.9.3/
22
+ $CFLAGS += " -I./ruby_headers/193/ -DRUBY_193"
23
+ end
24
+
25
+ create_makefile('binding_of_caller')
26
+ else
27
+ fake_makefile
28
+ end
@@ -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,123 @@
1
+ module BindingOfCaller
2
+ class JRubyBindingHolder
3
+ java_import org.jruby.RubyBinding
4
+
5
+ def initialize(binding)
6
+ @binding = binding
7
+ end
8
+
9
+ def eval(code, file = nil, line = nil)
10
+ b = JRuby.dereference(RubyBinding.new(JRuby.runtime, Binding, @binding))
11
+ if (file == nil)
12
+ Kernel.eval code, b
13
+ else
14
+ Kernel.eval code, b, file, line
15
+ end
16
+ end
17
+
18
+ def frame_type
19
+ case
20
+ when block?
21
+ :block
22
+ when eval?
23
+ :eval
24
+ when top?
25
+ :top
26
+ else
27
+ :method
28
+ end
29
+ end
30
+
31
+ def frame_description
32
+ "#{block_desc}#{method_desc}"
33
+ end
34
+
35
+ private
36
+
37
+ def block?
38
+ @binding.getDynamicScope().getStaticScope().isBlockScope()
39
+ end
40
+
41
+ def eval?
42
+ @binding.getFrame().getKlazz().nil? && @binding.getLine() != 0
43
+ end
44
+
45
+ def top?
46
+ @binding.getFrame().getKlazz().nil? && @binding.getLine() == 0
47
+ end
48
+
49
+ def block_desc
50
+ if frame_type == :block
51
+ "block in "
52
+ end
53
+ end
54
+
55
+ def method_desc
56
+ @binding.getFrame().getName() || "<main>"
57
+ end
58
+ end
59
+
60
+ module BindingExtensions
61
+ def of_caller(index = 1)
62
+ index += 1 # always omit this frame
63
+ JRuby.runtime.current_context.binding_of_caller(index)
64
+ end
65
+
66
+ def callers
67
+ ary = []
68
+ n = 2
69
+ while binding = of_caller(n)
70
+ ary << binding
71
+ n += 1
72
+ end
73
+ ary
74
+ end
75
+
76
+ def frame_count
77
+ callers.count - 1
78
+ end
79
+
80
+ def frame_type
81
+ nil
82
+ end
83
+
84
+ def frame_description
85
+ nil
86
+ end
87
+ end
88
+ end
89
+
90
+
91
+ class org::jruby::runtime::ThreadContext
92
+ java_import org.jruby.runtime.Binding
93
+ java_import org.jruby.RubyInstanceConfig::CompileMode
94
+
95
+ field_accessor :frameStack, :frameIndex,
96
+ :scopeStack, :scopeIndex,
97
+ :backtrace, :backtraceIndex
98
+
99
+ def binding_of_caller(index)
100
+ unless JRuby.runtime.instance_config.compile_mode == CompileMode::OFF
101
+ raise RuntimeError, "caller binding only supported in interpreter"
102
+ end
103
+
104
+ index += 1 # always omit this frame
105
+
106
+ return nil if index > frameIndex
107
+
108
+ frame = frameStack[frameIndex - index]
109
+
110
+ return binding_of_caller(index - 1) if index > scopeIndex
111
+
112
+ scope = scopeStack[scopeIndex - index]
113
+ element = backtrace[backtraceIndex - index]
114
+
115
+ binding = Binding.new(frame, scope.static_scope.module, scope, element.clone)
116
+
117
+ BindingOfCaller::JRubyBindingHolder.new(binding)
118
+ end
119
+ end
120
+
121
+ class ::Binding
122
+ include BindingOfCaller::BindingExtensions
123
+ end
@@ -0,0 +1,63 @@
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 |dc|
22
+ locs = dc.backtrace_locations
23
+
24
+ locs.size.times do |i|
25
+ b = dc.frame_binding(i)
26
+ if b
27
+ b.instance_variable_set(:@iseq, dc.frame_iseq(i))
28
+ ary << b
29
+ end
30
+ end
31
+ end
32
+
33
+ ary.drop(1)
34
+ end
35
+
36
+ # Number of parent frames available at the point of call.
37
+ # @return [Fixnum]
38
+ def frame_count
39
+ callers.size - 1
40
+ end
41
+
42
+ # The type of the frame.
43
+ # @return [Symbol]
44
+ def frame_type
45
+ return nil if !@iseq
46
+
47
+ # apparently the 9th element of the iseq array holds the frame type
48
+ # ...not sure how reliable this is.
49
+ @frame_type ||= @iseq.to_a[9]
50
+ end
51
+
52
+ # The description of the frame.
53
+ # @return [String]
54
+ def frame_description
55
+ return nil if !@iseq
56
+ @frame_description ||= @iseq.label
57
+ end
58
+ end
59
+ end
60
+
61
+ class ::Binding
62
+ include BindingOfCaller::BindingExtensions
63
+ end
@@ -0,0 +1,67 @@
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
+ location = Rubinius::VM.backtrace(1 + n, true).first
8
+
9
+ raise RuntimeError, "Invalid frame, gone beyond end of stack!" if location.nil?
10
+
11
+ setup_binding_from_location(location)
12
+ end
13
+
14
+ # The description of the frame.
15
+ # @return [String]
16
+ def frame_description
17
+ @frame_description
18
+ end
19
+
20
+ # Return bindings for all caller frames.
21
+ # @return [Array<Binding>]
22
+ def callers
23
+ Rubinius::VM.backtrace(1, true).map &(method(:setup_binding_from_location).
24
+ to_proc)
25
+ end
26
+
27
+ # Number of parent frames available at the point of call.
28
+ # @return [Fixnum]
29
+ def frame_count
30
+ Rubinius::VM.backtrace(1).count
31
+ end
32
+
33
+ # The type of the frame.
34
+ # @return [Symbol]
35
+ def frame_type
36
+ if compiled_code.for_module_body?
37
+ :class
38
+ elsif compiled_code.for_eval?
39
+ :eval
40
+ elsif compiled_code.is_block?
41
+ :block
42
+ else
43
+ :method
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def setup_binding_from_location(location)
50
+ binding = Binding.setup location.variables,
51
+ location.variables.method,
52
+ location.constant_scope,
53
+ location.variables.self,
54
+ location
55
+
56
+ binding.instance_variable_set :@frame_description,
57
+ location.describe.gsub("{ } in", "block in")
58
+
59
+ binding
60
+ end
61
+ end
62
+ end
63
+
64
+ class ::Binding
65
+ include BindingOfCaller::BindingExtensions
66
+ extend BindingOfCaller::BindingExtensions
67
+ end
@@ -1,3 +1,3 @@
1
- module BindingOfCaller
2
- VERSION = "0.4.0"
3
- end
1
+ module BindingOfCaller
2
+ VERSION = "0.8.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ dlext = RbConfig::CONFIG['DLEXT']
2
+
3
+ mri_2 = defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" &&
4
+ RUBY_VERSION =~ /^2/
5
+
6
+ if mri_2
7
+ require 'binding_of_caller/mri2'
8
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
9
+ require "binding_of_caller.#{dlext}"
10
+ elsif defined?(Rubinius)
11
+ require 'binding_of_caller/rubinius'
12
+ elsif defined?(JRuby)
13
+ require 'binding_of_caller/jruby_interpreted'
14
+ end
@@ -0,0 +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
+ 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
+