<commit_before>require 'rbconfig'
require 'fileutils'

module PryExceptionExplorer
  module ShimBuilder
    class << self
      attr_reader :dir, :file
    end

    @dir = File.expand_path("~/.pry-exception_explorer/#{RUBY_VERSION}")
    @file = File.join(@dir, "raise_shim.c")

    if RUBY_PLATFORM =~ /darwin/
      Dyname = "dylib"
    else
      Dyname = "so"
    end

    ShimCode = <<-EOF
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>

void
rb_raise(unsigned long exc, const char *fmt, ...)
{
  static void (*libruby_rb_raise)
    (unsigned long exc, const char *fmt, ...) = NULL;

  void * handle;
  char * error;

  if (!libruby_rb_raise) {
    handle = dlopen("#{RbConfig::CONFIG['libdir']}/libruby.#{Dyname}", RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libruby_rb_raise = dlsym(handle, "rb_raise");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s", error);
      exit(1);
    }
  }

  rb_funcall(rb_cObject, rb_intern("raise"), 2, exc, rb_str_new2("hooked exception (pry)"));
}
EOF

    def self.create_directory_and_source_file
      FileUtils.mkdir_p(@dir)
      File.open(@file, 'w') do |f|
        f.puts(ShimCode)
      end
    end

    def self.compile
      create_directory_and_source_file

      # -L
      lib_dir = RbConfig::CONFIG['libdir']

      # -I
      arch_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1",  RbConfig::CONFIG['arch']
      backward_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1",  "ruby/backward"
      ruby191_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1"

      if RUBY_PLATFORM =~ /darwin/
        compile_line = "gcc -Wall -L#{lib_dir} -lruby -I#{arch_include}  -I#{backward_include} -I#{ruby191_include} -o lib_overrides.dylib -dynamiclib #{@file}"
      else
        compile_line = "gcc -Wall -O2 -fpic -shared -ldl -g -I#{arch_include}  -I#{backward_include} -I#{ruby191_include} -o lib_overrides.so #{@file}"
      end

      FileUtils.chdir @dir do
        system(compile_line)
      end

    end
  end
end
<commit_msg>added better error checking for shim compilation<commit_after>require 'rbconfig'
require 'fileutils'

module PryExceptionExplorer
  CompileError = Class.new(StandardError)

  module ShimBuilder
    class << self
      attr_reader :dir, :file
    end

    @dir = File.expand_path("~/.pry-exception_explorer/#{RUBY_VERSION}")
    @file = File.join(@dir, "raise_shim.c")

    if RUBY_PLATFORM =~ /darwin/
      Dyname = "dylib"
    else
      Dyname = "so"
    end

    ShimCode = <<-EOF
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <ruby.h>

void
rb_raise(unsigned long exc, const char *fmt, ...)
{
  rb_funcall(rb_cObject, rb_intern("raise"), 2, exc, rb_str_new2("hooked exception (pry)"));
}

void
rb_name_error(ID id, const char *fmt, ...)
{
  rb_funcall(rb_cObject, rb_intern("raise"), 2, rb_eNameError, rb_str_new2("hooked exception (pry)"));
}

EOF

    def self.create_directory_and_source_file
      FileUtils.mkdir_p(@dir)
      File.open(@file, 'w') do |f|
        f.puts(ShimCode)
      end
    end

    def self.compile
      create_directory_and_source_file

      # -L
      lib_dir = RbConfig::CONFIG['libdir']

      # -I
      arch_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1",  RbConfig::CONFIG['arch']
      backward_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1",  "ruby/backward"
      ruby191_include = File.join RbConfig::CONFIG['includedir'], "ruby-1.9.1"

      if RUBY_PLATFORM =~ /darwin/
        compile_line = "gcc -Wall -L#{lib_dir} -lruby -I#{arch_include}  -I#{backward_include} -I#{ruby191_include} -o lib_overrides.dylib -dynamiclib #{@file}"
      else
        compile_line = "gcc -Wall -O2 -fpic -shared -ldl -g -I#{arch_include}  -I#{backward_include} -I#{ruby191_include} -o lib_overrides.so #{@file}"
      end

      FileUtils.chdir @dir do
        if !system(compile_line)
          raise CompileError, "There was a problem building the shim, aborted!"
        end
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.1467116357504216, 'fraction_numerical': 0.011081667068176343, 'mean_word_length': 3.0866141732283463, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '121320', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1545, 'n_words': 318}