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



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

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
  case ret
  when :string
    wrap_function(cname, &:to_s)
  when :pointer
    wrap_function(cname) do |result|
      Fiddley::MemoryPointer.new(ret, result)
    end
  end
  if cname != rname
    instance_eval <<-end
      alias #{rname.inspect} #{cname.inspect}
    end
  end
end

#enum(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fiddley/library.rb', line 75

def enum(*args)
  if args[1].is_a?(Array)
    tag = args.shift
  else
    tag = nil
  end
  if args[0].is_a?(Array)
    args = args[0]
  end
  @enums ||= []
  @enums << Fiddley::Enum.new(args, tag)
end

#enum_type(key) ⇒ Object



98
99
100
101
# File 'lib/fiddley/library.rb', line 98

def enum_type(key)
  @enums ||= []
  @enums.find{|enum| enum.tag == key}
end

#enum_value(key) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/fiddley/library.rb', line 88

def enum_value(key)
  @enums ||= []
  enum = @enums.find{|enum| enum[key]}
  if enum
    enum[key]
  else
    nil
  end
end

#extended(mod) ⇒ Object



35
36
37
# File 'lib/fiddley/library.rb', line 35

def extended(mod)
  @convention = nil
end

#ffi_convention(conv) ⇒ Object



39
40
41
# File 'lib/fiddley/library.rb', line 39

def ffi_convention(conv)
  @convention = conv
end

#ffi_lib(*args) ⇒ Object



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

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