可以使用ruby ffi私有附加功能吗?

时间:2014-07-03 15:19:14

标签: c ruby shared-libraries ffi ruby-ffi

我有一个共享对象库,我使用ruby ffi附加函数。我想用别名附加每个函数并使别名为private,因为调用它们可能很危险。我将每个函数包装在他们自己的ruby模块函数中,这是一个简单的例子:

module LibC
    extend FFI::Library

    ffi_lib FFI::Library::LIBC

    attach_function :free, [:pointer], :void
end

module MyModule
    class << self
        extend FFI::Library

        ffi_lib '../my_shared_lib.so'

        def function(str)
            is_string(str)
            ptr = ffi_function(str)
            result = String.new(ptr.read_string)
            LibC.free(ptr)

            result
        end

        private
        # attach function
        attach_function :ffi_function, :function, [:string], :pointer

        def is_string(object)
             unless object.kind_of? String
                 raise TypeError,
                     "Wrong argument type #{object.class} (expected String)"
             end
        end
    end
end

函数ffi_function似乎仍然可以在模块外调用。我怎样才能完全私密化?

1 个答案:

答案 0 :(得分:0)

将attach_function放在类&lt;&lt;之外自我并在self中添加一个私有:ffi_function。

这是一个改编自其他代码的例子。 EnumProcesses是隐藏的ffi_function,process_ids是公共包装器。

require 'ffi'

module Win32

  extend FFI::Library
  ffi_lib 'Psapi'
  ffi_convention :stdcall

=begin
  BOOL WINAPI EnumProcesses(
    _Out_  DWORD *pProcessIds,
    _In_   DWORD cb,
    _Out_  DWORD *pBytesReturned
  );
=end
  attach_function :EnumProcesses, [:pointer, :uint32, :pointer], :int

  class << self

    def process_ids
      # Allocate room for the windows process ids.
      windows_process_ids = FFI::MemoryPointer.new(:uint32, 1024)

      # Allocate room for windows to tell us how many process ids there were.
      bytes_returned = FFI::MemoryPointer.new(:uint32)

      # Ask for the process ids
      if EnumProcesses(windows_process_ids, windows_process_ids.size, bytes_returned) != 0

        # Determine the number of ids we were given
        process_ids_returned = bytes_returned.read_int / bytes_returned.size

        # Pull all the ids out of the raw memory and into a local ruby array.
        windows_process_ids.read_array_of_type(:uint32, :read_uint32, process_ids_returned)
      end

    end

    # Hide EnumProcesses from the outside
    private :EnumProcesses
  end

end

Win32.process_ids # This will work
Win32.EnumProcesses # This throws an exception: private method `EnumProcesses' called for Win32:Module (NoMethodError)