summaryrefslogtreecommitdiff
path: root/ext/dl/lib/dl/func.rb
blob: a2e503835fbc3ae786ea41d737fa24a88b4cee3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
164
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require 'dl'
require 'dl/callback'
require 'dl/stack'
require 'dl/value'
require 'thread'

module DL
  parent = DL.fiddle? ? Fiddle::Function : Object

  class Function < parent
    include DL
    include ValueUtil

    if DL.fiddle?
      # :stopdoc:
      CALL_TYPE_TO_ABI = Hash.new { |h, k|
        raise RuntimeError, "unsupported call type: #{k}"
      }.merge({ :stdcall =>
                (Fiddle::Function::STDCALL rescue Fiddle::Function::DEFAULT),
                :cdecl   => Fiddle::Function::DEFAULT,
                nil      => Fiddle::Function::DEFAULT
              }).freeze
      private_constant :CALL_TYPE_TO_ABI
      # :startdoc:

      def self.call_type_to_abi(call_type) # :nodoc:
        CALL_TYPE_TO_ABI[call_type]
      end
      private_class_method :call_type_to_abi

      class FiddleClosureCFunc < Fiddle::Closure # :nodoc: all
        def initialize ctype, arg, abi, name
          @name = name
          super(ctype, arg, abi)
        end
        def name
          @name
        end
        def ptr
          to_i
        end
      end
      private_constant :FiddleClosureCFunc

      def self.class_fiddle_closure_cfunc # :nodoc:
        FiddleClosureCFunc
      end
      private_class_method :class_fiddle_closure_cfunc
    end

    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

    def to_i()
      @cfunc.to_i
    end

    def name
      @cfunc.name
    end

    def call(*args, &block)
      if DL.fiddle?
        if block_given?
          args.find { |a| DL::Function === a }.bind_at_call(&block)
        end
        super
      else
        funcs = []
        _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

    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

    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

    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

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

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

    def unbind_at_call()
    end
  end

  class TempFunction < Function
    def bind_at_call(&block)
      bind(&block)
    end

    def unbind_at_call()
      unbind()
    end
  end

  class CarriedFunction < Function
    def initialize(cfunc, argtypes, n)
      super(cfunc, argtypes)
      @carrier = []
      @index = n
      @mutex = Mutex.new
    end

    def create_carrier(data)
      ary = []
      userdata = [ary, data]
      @mutex.lock()
      @carrier.push(userdata)
      return dlwrap(userdata)
    end

    def bind_at_call(&block)
      userdata = @carrier[-1]
      userdata[0].push(block)
      bind{|*args|
        ptr = args[@index]
        if( !ptr )
          raise(RuntimeError, "The index of userdata should be lower than #{args.size}.")
        end
        userdata = dlunwrap(Integer(ptr))
        args[@index] = userdata[1]
        userdata[0][0].call(*args)
      }
      @mutex.unlock()
    end
  end
end