模块 Fiddle::Importer

一个 DSL,提供动态加载库和围绕它们构建模块的方法,包括调用已加载 C 库中的外部函数。

示例

require 'fiddle'
require 'fiddle/import'

module LibSum
  extend Fiddle::Importer
  dlload './libsum.so'
  extern 'double sum(double*, int)'
  extern 'double split(double)'
end

属性

type_alias[R]

公共实例方法

[](name) 单击以切换源

返回映射到 name 的函数,该函数由 Fiddle::Importer.externFiddle::Importer.bind 创建

# File ext/fiddle/lib/fiddle/import.rb, line 237
def [](name)
  @func_map[name]
end
bind(signature, *opts, &blk) 单击以切换源

使用给定的 opts 作为绑定参数,使用给定的块从给定的 C signature 创建一个全局方法。

# File ext/fiddle/lib/fiddle/import.rb, line 193
    def bind(signature, *opts, &blk)
      name, ctype, argtype = parse_signature(signature, type_alias)
      h = parse_bind_options(opts)
      case h[:callback_type]
      when :bind, nil
        f = bind_function(name, ctype, argtype, h[:call_type], &blk)
      else
        raise(RuntimeError, "unknown callback type: #{h[:callback_type]}")
      end
      @func_map[name] = f
      #define_method(name){|*args,&block| f.call(*args,&block)}
      begin
        /^(.+?):(\d+)/ =~ caller.first
        file, line = $1, $2.to_i
      rescue
        file, line = __FILE__, __LINE__+3
      end
      module_eval(<<-EOS, file, line)
        def #{name}(*args,&block)
          @func_map['#{name}'].call(*args,&block)
        end
      EOS
      module_function(name)
      f
    end
bind_function(name, ctype, argtype, call_type = nil, &block) 单击以切换源

返回 name 函数的新闭包包装器。

  • ctype 是函数的返回类型

  • argtype 是一个 Array 的参数,传递给回调函数

  • call_type 是闭包的 abi

  • block 传递给回调

参见 Fiddle::Closure

# File ext/fiddle/lib/fiddle/import.rb, line 313
def bind_function(name, ctype, argtype, call_type = nil, &block)
  abi = CALL_TYPE_TO_ABI[call_type]
  closure = Class.new(Fiddle::Closure) {
    define_method(:call, block)
  }.new(ctype, argtype, abi)

  Function.new(closure, argtype, ctype, abi, name: name)
end
create_value(ty, val=nil) 单击以切换源

创建一个类来包装具有值 ty 的 C 结构

另请参见 Fiddle::Importer.struct

# File ext/fiddle/lib/fiddle/import.rb, line 244
def create_value(ty, val=nil)
  s = struct([ty + " value"])
  ptr = s.malloc()
  if( val )
    ptr.value = val
  end
  return ptr
end
别名:value
dlload(*libs) 单击以切换源

为给定的 libs 创建一个处理程序数组,可以是 Fiddle::HandleFiddle::Importer 的实例,或使用 Fiddle.dlopen 创建 Fiddle::Handle 的新实例

如果无法加载库,则引发 DLError

参见 Fiddle.dlopen

# File ext/fiddle/lib/fiddle/import.rb, line 76
def dlload(*libs)
  handles = libs.collect{|lib|
    case lib
    when nil
      nil
    when Handle
      lib
    when Importer
      lib.handlers
    else
      Fiddle.dlopen(lib)
    end
  }.flatten()
  @handler = CompositeHandler.new(handles)
  @func_map = {}
  @type_alias = {}
end
extern(signature, *opts) 单击以切换源

从给定的 C signature 创建一个全局方法。

# File ext/fiddle/lib/fiddle/import.rb, line 169
    def extern(signature, *opts)
      symname, ctype, argtype = parse_signature(signature, type_alias)
      opt = parse_bind_options(opts)
      f = import_function(symname, ctype, argtype, opt[:call_type])
      name = symname.gsub(/@.+/,'')
      @func_map[name] = f
      # define_method(name){|*args,&block| f.call(*args,&block)}
      begin
        /^(.+?):(\d+)/ =~ caller.first
        file, line = $1, $2.to_i
      rescue
        file, line = __FILE__, __LINE__+3
      end
      module_eval(<<-EOS, file, line)
        def #{name}(*args, &block)
          @func_map['#{name}'].call(*args,&block)
        end
      EOS
      module_function(name)
      f
    end
handler() 单击以切换源

Fiddle::CompositeHandler 实例

如果没有打开任何处理程序,将引发错误。

# File ext/fiddle/lib/fiddle/import.rb, line 266
def handler
  (@handler ||= nil) or raise "call dlload before importing symbols and functions"
end
import_function(name, ctype, argtype, call_type = nil) 单击以切换源

在给定 name 函数的内存地址处返回一个新的 Fiddle::Function 实例。

如果 name 不存在,则引发 DLError

  • argtype 是传递给 name 函数的参数的 Array

  • ctype 是函数的返回类型

  • call_type 是函数的 ABI

另请参见 Fiddle:Function.new

参见 Fiddle::CompositeHandler.sym 和 Fiddle::Handler.sym

# File ext/fiddle/lib/fiddle/import.rb, line 296
def import_function(name, ctype, argtype, call_type = nil)
  addr = handler.sym(name)
  if( !addr )
    raise(DLError, "cannot find the function: #{name}()")
  end
  Function.new(addr, argtype, ctype, CALL_TYPE_TO_ABI[call_type],
               name: name)
end
import_symbol(name) 单击以切换源

在给定 name 符号的内存地址处返回一个新的 Fiddle::Pointer 实例。

如果 name 不存在,则引发 DLError

参见 Fiddle::CompositeHandler.symFiddle::Handle.sym

# File ext/fiddle/lib/fiddle/import.rb, line 276
def import_symbol(name)
  addr = handler.sym(name)
  if( !addr )
    raise(DLError, "cannot find the symbol: #{name}")
  end
  Pointer.new(addr)
end
import_value(ty, addr) 单击以切换源

addr 地址处返回 C 结构的新实例,其值为 ty

# File ext/fiddle/lib/fiddle/import.rb, line 256
def import_value(ty, addr)
  s = struct([ty + " value"])
  ptr = s.new(addr)
  return ptr
end
sizeof(ty) 单击以切换源

返回 ty 的 sizeof,使用 Fiddle::Importer.parse_ctype 确定 C 类型和适当的 Fiddle 常量。

# File ext/fiddle/lib/fiddle/import.rb, line 101
def sizeof(ty)
  case ty
  when String
    ty = parse_ctype(ty, type_alias).abs()
    case ty
    when TYPE_CHAR
      return SIZEOF_CHAR
    when TYPE_SHORT
      return SIZEOF_SHORT
    when TYPE_INT
      return SIZEOF_INT
    when TYPE_LONG
      return SIZEOF_LONG
    when TYPE_FLOAT
      return SIZEOF_FLOAT
    when TYPE_DOUBLE
      return SIZEOF_DOUBLE
    when TYPE_VOIDP
      return SIZEOF_VOIDP
    when TYPE_CONST_STRING
      return SIZEOF_CONST_STRING
    when TYPE_BOOL
      return SIZEOF_BOOL
    else
      if defined?(TYPE_LONG_LONG) and
        ty == TYPE_LONG_LONG
        return SIZEOF_LONG_LONG
      else
        raise(DLError, "unknown type: #{ty}")
      end
    end
  when Class
    if( ty.instance_methods().include?(:to_ptr) )
      return ty.size()
    end
  end
  return Pointer[ty].size()
end
struct(signature) 单击以切换源

创建一个类来包装 signature 描述的 C 结构。

MyStruct = struct ['int i', 'char c']
# File ext/fiddle/lib/fiddle/import.rb, line 222
def struct(signature)
  tys, mems = parse_struct_signature(signature, type_alias)
  Fiddle::CStructBuilder.create(CStruct, tys, mems)
end
typealias(alias_type, orig_type) 单击以切换源

alias_type 的类型别名设置为 orig_type

# File ext/fiddle/lib/fiddle/import.rb, line 95
def typealias(alias_type, orig_type)
  @type_alias[alias_type] = orig_type
end
union(签名) 单击以切换源

创建一个类来包装 signature 描述的 C 联合。

MyUnion = union ['int i', 'char c']
# File ext/fiddle/lib/fiddle/import.rb, line 230
def union(signature)
  tys, mems = parse_struct_signature(signature, type_alias)
  Fiddle::CStructBuilder.create(CUnion, tys, mems)
end
value(ty, val=nil)
别名:create_value

私有实例方法

parse_bind_options(选项) 单击以切换源
# File ext/fiddle/lib/fiddle/import.rb, line 140
def parse_bind_options(opts)
  h = {}
  while( opt = opts.shift() )
    case opt
    when :stdcall, :cdecl
      h[:call_type] = opt
    when :carried, :temp, :temporal, :bind
      h[:callback_type] = opt
      h[:carrier] = opts.shift()
    else
      h[opt] = true
    end
  end
  h
end