From 9a536029c699ee501d9278577a300a5ef3d147dd Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 6 Apr 2015 12:22:11 -0700 Subject: add native dlopen extension code This will get dlopen in a consistent place on all the platforms which need it. --- lib/ffi_yajl.rb | 8 ++++---- lib/ffi_yajl/ext.rb | 51 ++++----------------------------------------------- 2 files changed, 8 insertions(+), 51 deletions(-) (limited to 'lib') diff --git a/lib/ffi_yajl.rb b/lib/ffi_yajl.rb index 7f39189..1787766 100644 --- a/lib/ffi_yajl.rb +++ b/lib/ffi_yajl.rb @@ -19,10 +19,10 @@ elsif ENV['FORCE_FFI_YAJL'] == "ffi" require 'ffi_yajl/ffi' elsif RUBY_PLATFORM == "java" require 'ffi_yajl/ffi' -elsif defined?(Yajl) - warn "the ffi-yajl and yajl-ruby gems have incompatible C libyajl libs and should not be loaded in the same Ruby VM" - warn "falling back to ffi which might work (or might not, no promises)" - require 'ffi_yajl/ffi' +#elsif defined?(Yajl) +# warn "the ffi-yajl and yajl-ruby gems have incompatible C libyajl libs and should not be loaded in the same Ruby VM" +# warn "falling back to ffi which might work (or might not, no promises)" +# require 'ffi_yajl/ffi' else begin require 'ffi_yajl/ext' diff --git a/lib/ffi_yajl/ext.rb b/lib/ffi_yajl/ext.rb index bc4d410..12f74d9 100644 --- a/lib/ffi_yajl/ext.rb +++ b/lib/ffi_yajl/ext.rb @@ -4,60 +4,17 @@ require 'ffi_yajl/encoder' require 'ffi_yajl/parser' require 'libyajl2' require 'ffi_yajl/map_library_name' +require 'ffi_yajl/ext/dlopen' module FFI_Yajl extend FFI_Yajl::MapLibraryName + extend FFI_Yajl::Ext::Dlopen + libname = map_library_name libpath = File.expand_path(File.join(Libyajl2.opt_path, libname)) - # - # FFS, what exactly was so wrong with DL.dlopen that ruby had to get rid of it??? - # - - def self.try_fiddle_dlopen(libpath) - require 'fiddle' - if defined?(Fiddle) && Fiddle.respond_to?(:dlopen) - ::Fiddle.dlopen(libpath) - true - else - false - end - rescue LoadError - return false - end - - def self.try_dl_dlopen(libpath) - require 'dl' - if defined?(DL) && DL.respond_to?(:dlopen) - ::DL.dlopen(libpath) - true - else - false - end - rescue LoadError - return false - end - - def self.try_ffi_dlopen(libpath) - require 'ffi' - require 'rbconfig' - extend ::FFI::Library - ffi_lib 'dl' - attach_function 'dlopen', :dlopen, [:string, :int], :void - if RbConfig::CONFIG['host_os'] =~ /linux/i - dlopen libpath, 0x102 # linux: RTLD_GLOBAL | RTLD_NOW - else - dlopen libpath, 0 - end - true - rescue LoadError - return false - end - - unless try_fiddle_dlopen(libpath) || try_dl_dlopen(libpath) || try_ffi_dlopen(libpath) - raise "cannot find dlopen via Fiddle, DL or FFI, what am I supposed to do?" - end + dlopen(libpath) class Parser require 'ffi_yajl/ext/parser' -- cgit v1.2.1