Module: Mach::Functions

Extended by:
FFI::Library, Types
Included in:
Clock, Error, Host, Port, Port::ReceiveRightMsg, Port::SendRightMsg, Semaphore, Task
Defined in:
lib/mach/functions.rb

Overview

FFI wrapper around a subset of the Mach API (likely Mac OS X specific).

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types

enum, find_type, typedefs

Class Method Details

.attach_mach_function(sym, argtypes, rettype, options = nil) ⇒ Object

Attach a function as with attach_function, but check the return value and raise an exception on errors.



75
76
77
78
# File 'lib/mach/functions.rb', line 75

def self.attach_mach_function(sym, argtypes, rettype, options = nil)
  attach_function(sym, argtypes, rettype, options)
  error_check(sym)
end

.error_check(*syms, &is_err) ⇒ Object

Replace methods in syms with error checking wrappers that invoke the original method and raise a SystemCallError.

The original method is invoked, and its return value is passed to the block (or a default check). The block should return true if the return value indicates an error state.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mach/functions.rb', line 21

def self.error_check(*syms, &is_err)
  unless block_given?
    is_err = lambda { |v| (v != KERN_SUCCESS) }
  end

  syms.each do |sym|
    method = self.method(sym)

    new_method_body = proc do |*args|
      ret = method.call(*args)
      if is_err.call(ret)
        raise Error.new("error in #{sym}", ret)
      else
        ret
      end
    end

    define_singleton_method(sym, &new_method_body)
    define_method(sym, &new_method_body)
   end
end

.error_check_bootstrap(*syms, &is_err) ⇒ Object

Replace methods in syms with error checking wrappers that invoke the original method and raise a SystemCallError.

The original method is invoked, and it’s return value is passed to the block (or a default check). The block should return true if the return value indicates an error state.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mach/functions.rb', line 49

def self.error_check_bootstrap(*syms, &is_err)
  unless block_given?
    is_err = lambda { |v| (v != KERN_SUCCESS) }
  end

  syms.each do |sym|
    method = self.method(sym)

    new_method_body = proc do |*args|
      ret = method.call(*args)
      if is_err.call(ret)
        ptr = bootstrap_strerror(ret)
        msg = ptr.null? ? nil : ptr.read_string()
        raise "error in #{sym}: #{msg}"
      else
        ret
      end
    end
    
    define_singleton_method(sym, &new_method_body)
    define_method(sym, &new_method_body)
  end
end

.new_memory_pointer(type) ⇒ Object



80
81
82
# File 'lib/mach/functions.rb', line 80

def self.new_memory_pointer(type)
  FFI::MemoryPointer.new(find_type(type))
end

Instance Method Details

#new_memory_pointer(type) ⇒ Object



84
85
86
# File 'lib/mach/functions.rb', line 84

def new_memory_pointer(type)
  Mach::Functions.new_memory_pointer(type)
end