binding_of_caller-0.7.2/0000755000004100000410000000000012562735515015203 5ustar www-datawww-databinding_of_caller-0.7.2/Rakefile0000755000004100000410000000620512562735515016656 0ustar www-datawww-datadlext = RbConfig::CONFIG['DLEXT'] direc = File.dirname(__FILE__) $:.unshift 'lib' require 'rake/clean' require 'rubygems/package_task' require "binding_of_caller/version" CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o") CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o", "ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*#*", "**/*#*.*", "ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake", "**/*.rbc") def mri_2? defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION =~ /^2/ end def apply_spec_defaults(s) s.name = "binding_of_caller" s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack." s.version = BindingOfCaller::VERSION s.date = Time.now.strftime '%Y-%m-%d' s.author = "John Mair (banisterfiend)" s.email = 'jrmair@gmail.com' s.description = s.summary s.require_path = 'lib' s.add_dependency 'debug_inspector', '>= 0.0.1' s.add_development_dependency 'bacon' s.add_development_dependency 'rake' s.homepage = "http://github.com/banister/binding_of_caller" s.has_rdoc = 'yard' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- test/*`.split("\n") end desc "Show version" task :version do puts "BindingOfCaller version: #{BindingOfCaller::VERSION}" end desc "run tests" task :default => [:test] desc "Run tests" task :test do unless defined?(Rubinius) Rake::Task['compile'].execute end $stdout.puts("\033[33m") sh "bacon -Itest -rubygems -a -q" $stdout.puts("\033[0m") unless defined?(Rubinius) Rake::Task['cleanup'].execute end end task :pry do puts "loading binding_of_caller into pry" sh "pry -r ./lib/binding_of_caller" end desc "generate gemspec" task :gemspec => "ruby:gemspec" namespace :ruby do spec = Gem::Specification.new do |s| apply_spec_defaults(s) s.platform = Gem::Platform::RUBY s.extensions = ["ext/binding_of_caller/extconf.rb"] end Gem::PackageTask.new(spec) do |pkg| pkg.need_zip = false pkg.need_tar = false end desc "Generate gemspec file" task :gemspec do File.open("#{spec.name}.gemspec", "w") do |f| f << spec.to_ruby end end end desc "build the binaries" task :compile => :cleanup do if !mri_2? chdir "./ext/binding_of_caller/" do sh "ruby extconf.rb" sh "make" sh "cp *.#{dlext} ../../lib/" end end end desc 'cleanup the extensions' task :cleanup do if !mri_2? sh 'rm -rf lib/binding_of_caller.so' chdir "./ext/binding_of_caller/" do sh 'make clean' end end end desc "reinstall gem" task :reinstall => :gems do sh "gem uninstall binding_of_caller" rescue nil sh "gem install #{direc}/pkg/binding_of_caller-#{BindingOfCaller::VERSION}.gem" end task :install => :reinstall desc "build all platform gems at once" task :gems => [:clean, :rmgems, "ruby:gem"] task :gem => [:gems] desc "remove all platform gems" task :rmgems => ["ruby:clobber_package"] desc "build and push latest gems" task :pushgems => :gems do chdir("./pkg") do Dir["*.gem"].each do |gemfile| sh "gem push #{gemfile}" end end end binding_of_caller-0.7.2/.gemtest0000755000004100000410000000000012562735515016645 0ustar www-datawww-databinding_of_caller-0.7.2/Gemfile0000644000004100000410000000004612562735515016476 0ustar www-datawww-datasource 'https://rubygems.org' gemspec binding_of_caller-0.7.2/examples/0000755000004100000410000000000012562735515017021 5ustar www-datawww-databinding_of_caller-0.7.2/examples/example.rb0000644000004100000410000000122012562735515020774 0ustar www-datawww-dataunless Object.const_defined? :BindingOfCaller $:.unshift File.expand_path '../../lib', __FILE__ require 'binding_of_caller' require 'binding_of_caller/version' end outer = 10 class Z def z u = 10 A.new.a end end class A def a y = 10 B.new.b end end class B def b x = 10 puts binding.of_caller(0).eval('local_variables') puts binding.of_caller(1).eval('local_variables') puts binding.of_caller(2).eval('local_variables') puts binding.of_caller(3).eval('local_variables') puts binding.of_caller(400).eval('local_variables') end end Z.new.z # output: # => x # => y # => u # => outer # Exception binding_of_caller-0.7.2/.travis.yml0000644000004100000410000000050512562735515017314 0ustar www-datawww-datarvm: - 1.9.2 - 1.9.3 - 2.0.0 - rbx-19mode notifications: irc: "irc.freenode.org#pry" recipients: - jrmair@gmail.com branches: only: - master #script: rake test --trace # #before_install: # - gem update --system # - gem --version # - gem install rake bacon # - rake gem # - gem install pkg/*.gem binding_of_caller-0.7.2/lib/0000755000004100000410000000000012562735515015751 5ustar www-datawww-databinding_of_caller-0.7.2/lib/binding_of_caller.rb0000644000004100000410000000051512562735515021717 0ustar www-datawww-datadlext = RbConfig::CONFIG['DLEXT'] def mri_2? defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION =~ /^2/ end if mri_2? require 'binding_of_caller/mri2' elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" require "binding_of_caller.#{dlext}" elsif defined?(Rubinius) require 'binding_of_caller/rubinius' end binding_of_caller-0.7.2/lib/binding_of_caller/0000755000004100000410000000000012562735515021371 5ustar www-datawww-databinding_of_caller-0.7.2/lib/binding_of_caller/mri2.rb0000644000004100000410000000270312562735515022571 0ustar www-datawww-datarequire 'debug_inspector' module BindingOfCaller module BindingExtensions # Retrieve the binding of the nth caller of the current frame. # @return [Binding] def of_caller(n) c = callers.drop(1) if n > (c.size - 1) raise "No such frame, gone beyond end of stack!" else c[n] end end # Return bindings for all caller frames. # @return [Array] def callers ary = [] RubyVM::DebugInspector.open do |i| n = 0 loop do begin b = i.frame_binding(n) rescue ArgumentError break end if b b.instance_variable_set(:@iseq, i.frame_iseq(n)) ary << b end n += 1 end end ary.drop(1) end # Number of parent frames available at the point of call. # @return [Fixnum] def frame_count callers.size - 1 end # The type of the frame. # @return [Symbol] def frame_type return nil if !@iseq # apparently the 9th element of the iseq array holds the frame type # ...not sure how reliable this is. @frame_type ||= @iseq.to_a[9] end # The description of the frame. # @return [String] def frame_description return nil if !@iseq @frame_description ||= @iseq.label end end end class ::Binding include BindingOfCaller::BindingExtensions end binding_of_caller-0.7.2/lib/binding_of_caller/version.rb0000755000004100000410000000005712562735515023410 0ustar www-datawww-datamodule BindingOfCaller VERSION = "0.7.2" end binding_of_caller-0.7.2/lib/binding_of_caller/rubinius.rb0000644000004100000410000000325712562735515023565 0ustar www-datawww-datamodule BindingOfCaller module BindingExtensions # Retrieve the binding of the nth caller of the current frame. # @return [Binding] def of_caller(n) bt = Rubinius::VM.backtrace(1 + n, true).first raise RuntimeError, "Invalid frame, gone beyond end of stack!" if bt.nil? b = Binding.setup( bt.variables, bt.variables.method, bt.constant_scope, bt.variables.self, bt ) b.instance_variable_set :@frame_description, bt.describe.gsub("{ } in", "block in") b end # The description of the frame. # @return [String] def frame_description @frame_description end # Return bindings for all caller frames. # @return [Array] def callers ary = [] n = 0 loop do begin ary << Binding.of_caller(n) rescue break end n += 1 end ary.drop_while do |v| !(v.frame_type == :method && v.eval("__method__") == :callers) end.drop(1) end # Number of parent frames available at the point of call. # @return [Fixnum] def frame_count callers.size - 1 end # The type of the frame. # @return [Symbol] def frame_type if compiled_code.for_module_body? :class elsif compiled_code.for_eval? :eval elsif compiled_code.is_block? :block else :method end end end end class ::Binding include BindingOfCaller::BindingExtensions extend BindingOfCaller::BindingExtensions end binding_of_caller-0.7.2/metadata.yml0000644000004100000410000001173612562735515017516 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: binding_of_caller version: !ruby/object:Gem::Version version: 0.7.2 platform: ruby authors: - John Mair (banisterfiend) autorequire: bindir: bin cert_chain: [] date: 2013-06-07 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: debug_inspector requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: 0.0.1 type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: 0.0.1 - !ruby/object:Gem::Dependency name: bacon requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' description: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack. email: jrmair@gmail.com executables: [] extensions: - ext/binding_of_caller/extconf.rb extra_rdoc_files: [] 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 homepage: http://github.com/banister/binding_of_caller licenses: [] metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack. test_files: - test/test_binding_of_caller.rb binding_of_caller-0.7.2/test/0000755000004100000410000000000012562735515016162 5ustar www-datawww-databinding_of_caller-0.7.2/test/test_binding_of_caller.rb0000755000004100000410000000702112562735515023171 0ustar www-datawww-dataunless Object.const_defined? :BindingOfCaller $:.unshift File.expand_path '../../lib', __FILE__ require 'binding_of_caller' require 'binding_of_caller/version' end class Module public :remove_const end puts "Testing binding_of_caller version #{BindingOfCaller::VERSION}..." puts "Ruby version: #{RUBY_VERSION}" describe BindingOfCaller do describe "of_caller" do it "should fetch immediate caller's binding when 0 is passed" do o = Object.new def o.a var = 1 binding.of_caller(0).eval('var') end o. a.should == 1 end it "should fetch parent of caller's binding when 1 is passed" do o = Object.new def o.a var = 1 b end def o.b binding.of_caller(1).eval('var') end o.a.should == 1 end it "should modify locals in parent of caller's binding" do o = Object.new def o.a var = 1 b var end def o.b binding.of_caller(1).eval('var = 20') end o.a.should == 20 end it "should raise an exception when retrieving an out of band binding" do o = Object.new def o.a binding.of_caller(100) end lambda { o.a }.should.raise RuntimeError end end describe "callers" do before do @o = Object.new end it 'should return the first non-internal binding when using callers.first' do def @o.meth x = :a_local [binding.callers.first, binding.of_caller(0)] end b1, b2 = @o.meth b1.eval("x").should == :a_local b2.eval("x").should == :a_local end end describe "frame_count" do it 'frame_count should equal callers.count' do binding.frame_count.should == binding.callers.count end end describe "frame_descripton" do it 'can be called on ordinary binding without raising' do lambda { binding.frame_description }.should.not.raise end it 'describes a block frame' do binding.of_caller(0).frame_description.should =~ /block/ end it 'describes a method frame' do o = Object.new def o.horsey_malone binding.of_caller(0).frame_description.should =~ /horsey_malone/ end o.horsey_malone end it 'describes a class frame' do class HorseyMalone binding.of_caller(0).frame_description.should =~ /class/i end Object.remove_const(:HorseyMalone) end end describe "frame_type" do it 'can be called on ordinary binding without raising' do lambda { binding.frame_type }.should.not.raise end describe "when inside a class definition" do before do class HorseyMalone @binding = binding.of_caller(0) def self.binding; @binding; end end @binding = HorseyMalone.binding end it 'returns :class' do @binding.frame_type.should == :class end end describe "when evaluated" do before { @binding = eval("binding.of_caller(0)") } it 'returns :eval' do @binding.frame_type.should == :eval end end describe "when inside a block" do before { @binding = proc { binding.of_caller(0) }.call } it 'returns :block' do @binding.frame_type.should == :block end end describe "when inside an instance method" do before do o = Object.new def o.a; binding.of_caller(0); end @binding = o.a; end it 'returns :method' do @binding.frame_type.should == :method end end end end binding_of_caller-0.7.2/.gitignore0000755000004100000410000000007012562735515017173 0ustar www-datawww-dataMakefile *.so *.o *.def doc/ pkg/ .yardoc/ Gemfile.lock binding_of_caller-0.7.2/.yardopts0000755000004100000410000000002312562735515017047 0ustar www-datawww-data--markup markdown binding_of_caller-0.7.2/HISTORY0000755000004100000410000000000012562735515016260 0ustar www-datawww-databinding_of_caller-0.7.2/LICENSE0000755000004100000410000000215312562735515016214 0ustar www-datawww-dataLicense ------- (The MIT License) Copyright (c) 2011 John Mair (banisterfiend) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. binding_of_caller-0.7.2/binding_of_caller.gemspec0000644000004100000410000001041112562735515022165 0ustar www-datawww-data# -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = "binding_of_caller" s.version = "0.7.2" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["John Mair (banisterfiend)"] s.date = "2013-06-07" s.description = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack." s.email = "jrmair@gmail.com" s.extensions = ["ext/binding_of_caller/extconf.rb"] 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"] s.homepage = "http://github.com/banister/binding_of_caller" s.require_paths = ["lib"] s.rubygems_version = "2.0.3" s.summary = "Retrieve the binding of a method's caller. Can also retrieve bindings even further up the stack." s.test_files = ["test/test_binding_of_caller.rb"] if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q, [">= 0.0.1"]) s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0.0.1"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0.0.1"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end end binding_of_caller-0.7.2/ext/0000755000004100000410000000000012562735515016003 5ustar www-datawww-databinding_of_caller-0.7.2/ext/binding_of_caller/0000755000004100000410000000000012562735515021423 5ustar www-datawww-databinding_of_caller-0.7.2/ext/binding_of_caller/binding_of_caller.c0000644000004100000410000001300312562735515025204 0ustar www-datawww-data/* (c) 2011 John Mair (banisterfiend), MIT license */ #include #include "vm_core.h" #include "rubys_gc.h" typedef enum { false, true } bool; static VALUE string2sym(const char * string) { return ID2SYM(rb_intern(string)); } static inline const rb_data_type_t * threadptr_data_type(void) { static const rb_data_type_t *thread_data_type; if (!thread_data_type) { VALUE current_thread = rb_thread_current(); thread_data_type = RTYPEDDATA_TYPE(current_thread); } return thread_data_type; } #define ruby_thread_data_type *threadptr_data_type() #define ruby_threadptr_data_type *threadptr_data_type() #define ruby_current_thread ((rb_thread_t *)RTYPEDDATA_DATA(rb_thread_current())) static size_t binding_memsize(const void *ptr) { return ptr ? sizeof(rb_binding_t) : 0; } static void binding_free(void *ptr) { rb_binding_t *bind; RUBY_FREE_ENTER("binding"); if (ptr) { bind = ptr; ruby_xfree(ptr); } RUBY_FREE_LEAVE("binding"); } static void binding_mark(void *ptr) { rb_binding_t *bind; RUBY_MARK_ENTER("binding"); if (ptr) { bind = ptr; RUBY_MARK_UNLESS_NULL(bind->env); #ifdef RUBY_192 RUBY_MARK_UNLESS_NULL(bind->filename); #endif } RUBY_MARK_LEAVE("binding"); } static const rb_data_type_t binding_data_type = { "binding", binding_mark, binding_free, binding_memsize, }; static VALUE binding_alloc(VALUE klass) { VALUE obj; rb_binding_t *bind; obj = TypedData_Make_Struct(klass, rb_binding_t, &binding_data_type, bind); return obj; } static bool ifunc_p(rb_control_frame_t * cfp) { return (cfp->flag & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_IFUNC; } static bool valid_frame_p(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) { return cfp->iseq && !ifunc_p(cfp) && !NIL_P(cfp->self); } static rb_control_frame_t * find_valid_frame(rb_control_frame_t * cfp, rb_control_frame_t * limit_cfp) { while (cfp < limit_cfp) { cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); if (cfp >= limit_cfp) return NULL; if (valid_frame_p(cfp, limit_cfp)) return cfp; } // beyond end of stack return NULL; } static VALUE frametype_name(VALUE flag) { switch (flag & VM_FRAME_MAGIC_MASK) { case VM_FRAME_MAGIC_METHOD: return string2sym("method"); case VM_FRAME_MAGIC_BLOCK: return string2sym("block"); case VM_FRAME_MAGIC_CLASS: return string2sym("class"); case VM_FRAME_MAGIC_TOP: return string2sym("top"); case VM_FRAME_MAGIC_CFUNC: return string2sym("cfunc"); case VM_FRAME_MAGIC_PROC: return string2sym("proc"); case VM_FRAME_MAGIC_IFUNC: return string2sym("ifunc"); case VM_FRAME_MAGIC_EVAL: return string2sym("eval"); case VM_FRAME_MAGIC_LAMBDA: return string2sym("lambda"); default: rb_raise(rb_eRuntimeError, "Unknown frame type! got flag: %d", FIX2INT(flag)); } } static VALUE binding_of_caller(VALUE self, VALUE rb_level) { rb_thread_t *th; GetThreadPtr(rb_thread_current(), th); rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size); int level = FIX2INT(rb_level); // attempt to locate the nth parent control frame for (int i = 0; i < level; i++) { cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); if (cfp >= limit_cfp) rb_raise(rb_eRuntimeError, "Invalid frame, gone beyond end of stack!"); // skip invalid frames if (!valid_frame_p(cfp, limit_cfp)) cfp = find_valid_frame(cfp, limit_cfp); } VALUE bindval = binding_alloc(rb_cBinding); rb_binding_t *bind; if (cfp == 0) rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber."); GetBindingPtr(bindval, bind); bind->env = rb_vm_make_env_object(th, cfp); bind->filename = cfp->iseq->filename; bind->line_no = rb_vm_get_sourceline(cfp); rb_iv_set(bindval, "@frame_type", frametype_name(cfp->flag)); rb_iv_set(bindval, "@frame_description", cfp->iseq->name); return bindval; } static VALUE frame_type(VALUE self) { return rb_iv_get(self, "@frame_type"); } static VALUE frame_description(VALUE self) { return rb_iv_get(self, "@frame_description"); } static VALUE frame_count(VALUE self) { rb_thread_t *th; GetThreadPtr(rb_thread_current(), th); rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); rb_control_frame_t *limit_cfp = (void *)(th->stack + th->stack_size); int i = 1; while (cfp < limit_cfp) { cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); if (cfp >= limit_cfp) return INT2FIX(i); // skip invalid frames if (!valid_frame_p(cfp, limit_cfp)) cfp = find_valid_frame(cfp, limit_cfp); if (!cfp) break; i++; } return INT2FIX(i); } static VALUE callers(VALUE self) { VALUE ary = rb_ary_new(); for (int i = 0; i < FIX2INT(frame_count(self)); i++) rb_ary_push(ary, binding_of_caller(self, INT2FIX(i))); return ary; } void Init_binding_of_caller() { VALUE mBindingOfCaller = rb_define_module("BindingOfCaller"); rb_define_method(mBindingOfCaller, "of_caller", binding_of_caller, 1); rb_define_method(mBindingOfCaller, "frame_count", frame_count, 0); rb_define_method(mBindingOfCaller, "frame_type", frame_type, 0); rb_define_method(mBindingOfCaller, "frame_description", frame_description, 0); rb_define_method(mBindingOfCaller, "callers", callers, 0); rb_include_module(rb_cBinding, mBindingOfCaller); } binding_of_caller-0.7.2/ext/binding_of_caller/extconf.rb0000755000004100000410000000113012562735515023414 0ustar www-datawww-datadef fake_makefile File.open(File.join(File.dirname(__FILE__), "Makefile"), "w") do |f| f.puts %[install:\n\techo "Nada."] end end def mri_2? defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION =~ /^2/ end def rbx? defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/ end if mri_2? || rbx? fake_makefile else require 'mkmf' $CFLAGS += " -O0" $CFLAGS += " -std=c99" case RUBY_VERSION when /1.9.2/ $CFLAGS += " -I./ruby_headers/192/ -DRUBY_192" when /1.9.3/ $CFLAGS += " -I./ruby_headers/193/ -DRUBY_193" end create_makefile('binding_of_caller') end binding_of_caller-0.7.2/README.md0000755000004100000410000000507312562735515016472 0ustar www-datawww-data[![Build Status](https://secure.travis-ci.org/banister/binding_of_caller.png)](http://travis-ci.org/banister/binding_of_caller) binding_of_caller =========== (C) John Mair (banisterfiend) 2012 _Retrieve the binding of a method's caller in MRI 1.9.2+, MRI 2.0 and RBX (Rubinius)_ The `binding_of_caller` gem provides the `Binding#of_caller` method. Using `binding_of_caller` we can grab bindings from higher up the call stack and evaluate code in that context. Allows access to bindings arbitrarily far up the call stack, not limited to just the immediate caller. **Recommended for use only in debugging situations. Do not use this in production apps.** **Only works in MRI Ruby 1.9.2, 1.9.3, 2.0 and RBX (Rubinius)** * Install the [gem](https://rubygems.org/gems/binding_of_caller): `gem install binding_of_caller` * See the [source code](http://github.com/banister/binding_of_caller) Example: Modifying a local inside the caller of a caller -------- ```ruby def a var = 10 b puts var end def b c end def c binding.of_caller(2).eval('var = :hello') end a() # OUTPUT # => hello ``` Spinoff project ------- This project is a spinoff from the [Pry REPL project.](http://pry.github.com) Features and limitations ------------------------- * Only works with MRI 1.9.2, 1.9.3, 2.0 and RBX (Rubinius) * Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a `Binding#of_caller` there. Contact ------- Problems or questions contact me at [github](http://github.com/banister) License ------- (The MIT License) Copyright (c) 2012 (John Mair) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.