Module: Fiddley::Library

Includes:
Fiddle::Importer, Utils
Defined in:
lib/fiddley/library.rb

Constant Summary collapse

LIBPREFIX =
libprefix || "lib"
LIBSUFFIX =
libsuffix || "so"
LIBC =
libc_so
LIBM =
libm_so

Constants included from Utils

Utils::SIZET_FORMAT, Utils::SIZET_TYPE

Instance Method Summary collapse

Methods included from Utils

str2value, type2offset_size, type2size, type2str, type2type, value2str

Instance Method Details

#attach_function(rname, cname, params, ret = nil, blocking: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fiddley/library.rb', line 41

def attach_function(rname, cname, params, ret = nil, blocking: false)
  if ret.nil?
    ret = params
    params = cname
    cname = rname
  end
  extern "#{type2str(ret)} #{cname}(#{params.map{|e| type2str(e)}.join(', ')})", @convention
  if ret == :string
    tname = (cname.to_s + '+').to_sym
    instance_eval <<-end
      alias #{tname.inspect} #{cname.inspect}
    end
    define_singleton_method(cname) do |*args|
      __send__(tname, *args).to_s
    end
  end
  if cname != rname
    instance_eval <<-end
      alias #{rname.inspect} #{cname.inspect}
    end
  end
end

#extended(mod) ⇒ Object



33
34
35
# File 'lib/fiddley/library.rb', line 33

def extended(mod)
  @convention = nil
end

#ffi_convention(conv) ⇒ Object



37
38
39
# File 'lib/fiddley/library.rb', line 37

def ffi_convention(conv)
  @convention = conv
end

#ffi_lib(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fiddley/library.rb', line 9

def ffi_lib(*args)
  args.each do |soes|
    soes = [soes] unless soes.is_a?(Array)
    loaded = false
    soes.each do |so|
      begin
        begin
          dlload so
        rescue Fiddle::DLError
          begin
            dlload LIBPREFIX + so
          rescue Fiddle::DLError
            dlload LIBPREFIX + so + "." + LIBSUFFIX
          end
        end
        loaded = true
        break
      rescue Fiddle::DLError
      end
    end
    raise Fiddle::DLError, "can't load #{soes.join(' or ')}" unless loaded
  end
end