module FFI::Library

def self.extended(mod)

def self.extended(mod)
  FFI.exporter.mod = mod
end

def attach_function(name, func, args, returns = nil, options = nil)

def attach_function(name, func, args, returns = nil, options = nil)
  mname, a2, a3, a4, a5 = name, func, args, returns, options
  cname, arg_types, ret_type, opts = (a4 && (a2.is_a?(String) || a2.is_a?(Symbol))) ? [ a2, a3, a4, a5 ] : [ mname.to_s, a2, a3, a4 ]
  arg_types = arg_types.map { |e| find_type(e) }
  FFI.exporter.attach(mname, cname, find_type(ret_type), arg_types)
end

def callback(*args)

def callback(*args)
  name, params, ret = if args.length == 3
    args
  else
    [ nil, args[0], args[1] ]
  end
  native_params = params.map { |e| find_type(e) }
  cb = FFI::CallbackInfo.new(find_type(ret), native_params)
  FFI.exporter.callback(name, cb) if name
end

def ffi_lib(*args)

def ffi_lib(*args)
end

def find_type(type)

def find_type(type)
  t = TypeMap[type]
  return t unless t.nil?
  
  if type.is_a?(Class) && type < Struct
    return TypeMap[type] = StructByReference.new(type)
  end
  TypeMap[type] = FFI.find_type(type)
end