Advertisement
Guest User

Untitled

a guest
Feb 29th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.47 KB | None | 0 0
  1. require 'rbconfig'
  2. require 'fileutils'
  3.  
  4. module PryExceptionExplorer
  5.   CompileError = Class.new(StandardError)
  6.  
  7.   module ShimBuilder
  8.     class << self
  9.       attr_reader :dir, :file
  10.     end
  11.  
  12.     @dir = File.expand_path("~/.pry-exception_explorer/#{RUBY_VERSION}")
  13.     @file = File.join(@dir, "raise_shim.c")
  14.  
  15.     if RUBY_PLATFORM =~ /darwin/
  16.       Dyname = "dylib"
  17.     else
  18.       Dyname = "so"
  19.     end
  20.  
  21.     ShimCode = <<-EOF
  22. #include <ruby.h>
  23. #include <stdio.h>
  24. #include <dlfcn.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <stdarg.h>
  28.  
  29. void
  30. rb_raise(VALUE exc, const char *fmt, ...)
  31. {
  32.     va_list args;
  33.     VALUE mesg;
  34.  
  35.     va_start(args, fmt);
  36.     mesg = rb_vsprintf(fmt, args);
  37.     va_end(args);
  38.     rb_funcall(rb_cObject, rb_intern("raise"), 2, exc, mesg);
  39. }
  40.  
  41. void
  42. rb_name_error(ID id, const char *fmt, ...)
  43. {
  44.   rb_funcall(rb_cObject, rb_intern("raise"), 2, rb_eNameError, rb_str_new2("hooked exception (pry)"));
  45. }
  46.  
  47. EOF
  48.  
  49.     def self.create_directory_and_source_file
  50.       FileUtils.mkdir_p(@dir)
  51.       File.open(@file, 'w') do |f|
  52.         f.puts(ShimCode)
  53.       end
  54.     end
  55.  
  56.     def self.compile
  57.       create_directory_and_source_file
  58.  
  59.       # -L
  60.       ldflags = [ ]
  61.       ldflags << "-L#{RbConfig::CONFIG['libdir']}"
  62.       ldflags << "-L#{RbConfig::CONFIG['archdir']}"
  63.       ldflags << RbConfig::CONFIG['LDFLAGS']
  64.       ldflags << RbConfig::CONFIG['LIBS']
  65.       ldflags << RbConfig::CONFIG['DLDLIBS']
  66.       ldflags << RbConfig::CONFIG['LIBRUBYARG']
  67.  
  68.       ldflags = ldflags.join(' ')
  69.  
  70.       # -I
  71.       includes = [ ]
  72.       if RbConfig::CONFIG['rubyhdrdir'] then
  73.         includes << "#{RbConfig::CONFIG['rubyhdrdir']}"
  74.         includes << "#{RbConfig::CONFIG['rubyhdrdir']}/#{RbConfig::CONFIG['arch']}"
  75.         includes << "#{RbConfig::CONFIG['rubyhdrdir']}/ruby/backward"
  76.       else
  77.         includes << "#{RbConfig::CONFIG['archdir']}"
  78.       end
  79.  
  80.       includes = includes.map { |inc| "-I #{inc}" }.join(' ')
  81.  
  82.       if RUBY_PLATFORM =~ /darwin/
  83.         compile_line = "gcc -Wall #{ldflags} #{includes} -o lib_overrides.dylib -dynamiclib #{@file}"
  84.       else
  85.         compile_line = "gcc -Wall -O2 -g #{ldflags} -shared #{includes} -o lib_overrides.so #{@file}"
  86.       end
  87.  
  88.       FileUtils.chdir @dir do
  89.         puts compile_line
  90.         if !system(compile_line)
  91.           raise CompileError, "There was a problem building the shim, aborted!"
  92.         end
  93.       end
  94.  
  95.     end
  96.   end
  97. end
  98.  
  99. PryExceptionExplorer::ShimBuilder.compile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement