Class: DL::Function

Inherits:
Object
  • Object
show all
Includes:
DL, ValueUtil
Defined in:
lib/dl/func.rb

Direct Known Subclasses

CarriedFunction, TempFunction

Constant Summary

Constants included from DL

ALIGN_CHAR, ALIGN_DOUBLE, ALIGN_FLOAT, ALIGN_INT, ALIGN_INTPTR_T, ALIGN_LONG, ALIGN_LONG_LONG, ALIGN_PTRDIFF_T, ALIGN_SHORT, ALIGN_SIZE_T, ALIGN_SSIZE_T, ALIGN_UINTPTR_T, ALIGN_VOIDP, BUILD_RUBY_PLATFORM, BUILD_RUBY_VERSION, CdeclCallbackAddrs, CdeclCallbackProcs, DLSTACK_SIZE, MAX_CALLBACK, NULL, RTLD_GLOBAL, RTLD_LAZY, RTLD_NOW, RUBY_FREE, SEM, SIZEOF_CHAR, SIZEOF_DOUBLE, SIZEOF_FLOAT, SIZEOF_INT, SIZEOF_INTPTR_T, SIZEOF_LONG, SIZEOF_LONG_LONG, SIZEOF_PTRDIFF_T, SIZEOF_SHORT, SIZEOF_SIZE_T, SIZEOF_SSIZE_T, SIZEOF_UINTPTR_T, SIZEOF_VOIDP, StdcallCallbackAddrs, StdcallCallbackProcs, TYPE_CHAR, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INT, TYPE_INTPTR_T, TYPE_LONG, TYPE_LONG_LONG, TYPE_PTRDIFF_T, TYPE_SHORT, TYPE_SIZE_T, TYPE_SSIZE_T, TYPE_UINTPTR_T, TYPE_VOID, TYPE_VOIDP

Instance Method Summary collapse

Methods included from ValueUtil

#signed_value, #unsigned_value, #wrap_arg, #wrap_args

Methods included from DL

dlopen, dlunwrap, dlwrap, fiddle?, free, malloc, realloc, #remove_callback_internal, #remove_cdecl_callback, #remove_stdcall_callback, #set_callback_internal, #set_cdecl_callback, #set_stdcall_callback

Constructor Details

#initialize(cfunc, argtypes, abi = nil, &block) ⇒ Function

Returns a new instance of Function.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dl/func.rb', line 51

def initialize cfunc, argtypes, abi = nil, &block
  if DL.fiddle?
    abi ||= CALL_TYPE_TO_ABI[(cfunc.calltype rescue nil)]
    if block_given?
      @cfunc = Class.new(FiddleClosureCFunc) {
        define_method(:call, block)
      }.new(cfunc.ctype, argtypes, abi, cfunc.name)
    else
      @cfunc  = cfunc
    end

    @args   = argtypes
    super(@cfunc, @args.reject { |x| x == TYPE_VOID }, cfunc.ctype, abi)
  else
    @cfunc = cfunc
    @stack = Stack.new(argtypes.collect{|ty| ty.abs})
    if( @cfunc.ctype < 0 )
      @cfunc.ctype = @cfunc.ctype.abs
      @unsigned = true
    else
      @unsigned = false
    end
    if block_given?
      bind(&block)
    end
  end
end

Instance Method Details

#bind(&block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dl/func.rb', line 117

def bind(&block)
  if DL.fiddle?
    @cfunc = Class.new(FiddleClosureCFunc) {
      def initialize ctype, args, abi, name, block
        super(ctype, args, abi, name)
        @block = block
      end

      def call *args
        @block.call(*args)
      end
    }.new(@cfunc.ctype, @args, abi, name, block)
    @ptr = @cfunc
    return nil
  else
    if( !block )
      raise(RuntimeError, "block must be given.")
    end
    unless block.lambda?
      block = Class.new(self.class){define_method(:call, block); def initialize(obj); obj.instance_variables.each{|s| instance_variable_set(s, obj.instance_variable_get(s))}; end}.new(self).method(:call)
    end
    if( @cfunc.ptr == 0 )
      cb = Proc.new{|*args|
        ary = @stack.unpack(args)
        @stack.types.each_with_index{|ty, idx|
          case ty
          when TYPE_VOIDP
            ary[idx] = CPtr.new(ary[idx])
          end
        }
        r = block.call(*ary)
        wrap_arg(r, @cfunc.ctype, [])
      }
      case @cfunc.calltype
      when :cdecl
        @cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb)
      when :stdcall
        @cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb)
      else
        raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
      end
      if( @cfunc.ptr == 0 )
        raise(RuntimeException, "can't bind C function.")
      end
    end
  end
end

#bind_at_call(&block) ⇒ Object



202
203
204
# File 'lib/dl/func.rb', line 202

def bind_at_call(&block)
  bind(&block)
end

#bound?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/dl/func.rb', line 198

def bound?()
  @cfunc.ptr != 0
end

#call(*args, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dl/func.rb', line 87

def call(*args, &block)
  if DL.fiddle?
    if block_given?
      args.find { |a| DL::Function === a }.bind_at_call(&block)
    end
    super
  else
    funcs = []
    if $SAFE >= 1 && args.any? { |x| x.tainted? }
      raise SecurityError, "tainted parameter not allowed"
    end
    _args = wrap_args(args, @stack.types, funcs, &block)
    r = @cfunc.call(@stack.pack(_args))
    funcs.each{|f| f.unbind_at_call()}
    return wrap_result(r)
  end
end

#nameObject



83
84
85
# File 'lib/dl/func.rb', line 83

def name
  @cfunc.name
end

#to_iObject



79
80
81
# File 'lib/dl/func.rb', line 79

def to_i()
  @cfunc.to_i
end

#unbindObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dl/func.rb', line 165

def unbind()
  if DL.fiddle? then
    if @cfunc.kind_of?(Fiddle::Closure) and @cfunc.ptr != 0 then
      call_type = case abi
                  when CALL_TYPE_TO_ABI[nil]
                    nil
                  when CALL_TYPE_TO_ABI[:stdcall]
                    :stdcall
                  else
                    raise(RuntimeError, "unsupported abi: #{abi}")
                  end
      @cfunc = CFunc.new(0, @cfunc.ctype, name, call_type)
      return 0
    elsif @cfunc.ptr != 0 then
      @cfunc.ptr = 0
      return 0
    else
      return nil
    end
  end
  if( @cfunc.ptr != 0 )
    case @cfunc.calltype
    when :cdecl
      remove_cdecl_callback(@cfunc.ptr, @cfunc.ctype)
    when :stdcall
      remove_stdcall_callback(@cfunc.ptr, @cfunc.ctype)
    else
      raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}")
    end
    @cfunc.ptr = 0
  end
end

#unbind_at_callObject



206
207
# File 'lib/dl/func.rb', line 206

def unbind_at_call()
end

#wrap_result(r) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dl/func.rb', line 105

def wrap_result(r)
  case @cfunc.ctype
  when TYPE_VOIDP
    r = CPtr.new(r)
  else
    if( @unsigned )
      r = unsigned_value(r, @cfunc.ctype)
    end
  end
  r
end