File: classes.rb

package info (click to toggle)
ruby-ffi 1.9.6debian-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,376 kB
  • ctags: 2,126
  • sloc: ansic: 7,471; ruby: 5,585; sh: 24; makefile: 14
file content (438 lines) | stat: -rw-r--r-- 12,351 bytes parent folder | download | duplicates (5)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
module FFISpecs
  #
  # Callback fixtures
  #
  module LibTest
    callback :cbVrS8, [ ], :char
    callback :cbVrU8, [ ], :uchar
    callback :cbVrS16, [ ], :short
    callback :cbVrU16, [ ], :ushort
    callback :cbVrS32, [ ], :int
    callback :cbVrU32, [ ], :uint
    callback :cbVrL, [ ], :long
    callback :cbVrUL, [ ], :ulong
    callback :cbVrS64, [ ], :long_long
    callback :cbVrU64, [ ], :ulong_long
    callback :cbVrP, [], :pointer
    callback :cbCrV, [ :char ], :void
    callback :cbSrV, [ :short ], :void
    callback :cbIrV, [ :int ], :void
    callback :cbLrV, [ :long ], :void
    callback :cbULrV, [ :ulong ], :void
    callback :cbLrV, [ :long_long ], :void

    attach_function :testCallbackVrS8, :testClosureVrB, [ :cbVrS8 ], :char
    attach_function :testCallbackVrU8, :testClosureVrB, [ :cbVrU8 ], :uchar
    attach_function :testCallbackVrS16, :testClosureVrS, [ :cbVrS16 ], :short
    attach_function :testCallbackVrU16, :testClosureVrS, [ :cbVrU16 ], :ushort
    attach_function :testCallbackVrS32, :testClosureVrI, [ :cbVrS32 ], :int
    attach_function :testCallbackVrU32, :testClosureVrI, [ :cbVrU32 ], :uint
    attach_function :testCallbackVrL, :testClosureVrL, [ :cbVrL ], :long
    attach_function :testCallbackVrUL, :testClosureVrL, [ :cbVrUL ], :ulong
    attach_function :testCallbackVrS64, :testClosureVrLL, [ :cbVrS64 ], :long_long
    attach_function :testCallbackVrU64, :testClosureVrLL, [ :cbVrU64 ], :ulong_long
    attach_function :testCallbackVrP, :testClosureVrP, [ :cbVrP ], :pointer
    attach_function :testCallbackCrV, :testClosureBrV, [ :cbCrV, :char ], :void
    attach_variable :cbVrS8, :gvar_pointer, :cbVrS8
    attach_variable :pVrS8, :gvar_pointer, :pointer
    attach_function :testGVarCallbackVrS8, :testClosureVrB, [ :pointer ], :char
    attach_function :testOptionalCallbackCrV, :testOptionalClosureBrV, [ :cbCrV, :char ], :void

    attach_function :testCallbackVrS8, :testClosureVrB, [ callback([ ], :char) ], :char

    callback :cb_return_type, [ :int ], :int
    callback :cb_lookup, [ ], :cb_return_type
    attach_function :testReturnsCallback, :testReturnsClosure, [ :cb_lookup, :int ], :int

    callback :funcptr, [ :int ], :int
    attach_function :testReturnsFunctionPointer, [  ], :funcptr

    callback :cbS8rV, [ :char ], :void
    callback :cbU8rV, [ :uchar ], :void
    callback :cbS16rV, [ :short ], :void
    callback :cbU16rV, [ :ushort ], :void

    callback :cbS32rV, [ :int ], :void
    callback :cbU32rV, [ :uint ], :void

    callback :cbLrV, [ :long ], :void
    callback :cbULrV, [ :ulong ], :void

    callback :cbS64rV, [ :long_long ], :void
    attach_function :testCallbackCrV, :testClosureBrV, [ :cbS8rV, :char ], :void
    attach_function :testCallbackU8rV, :testClosureBrV, [ :cbU8rV, :uchar ], :void
    attach_function :testCallbackSrV, :testClosureSrV, [ :cbS16rV, :short ], :void
    attach_function :testCallbackU16rV, :testClosureSrV, [ :cbU16rV, :ushort ], :void
    attach_function :testCallbackIrV, :testClosureIrV, [ :cbS32rV, :int ], :void
    attach_function :testCallbackU32rV, :testClosureIrV, [ :cbU32rV, :uint ], :void

    attach_function :testCallbackLrV, :testClosureLrV, [ :cbLrV, :long ], :void
    attach_function :testCallbackULrV, :testClosureULrV, [ :cbULrV, :ulong ], :void

    attach_function :testCallbackLLrV, :testClosureLLrV, [ :cbS64rV, :long_long ], :void
  end

  #
  # Enum fixtures
  #
  module TestEnum0
    extend FFI::Library
  end

  module TestEnum1
    extend FFI::Library
    ffi_lib LIBRARY

    enum [:c1, :c2, :c3, :c4]
    enum [:c5, 42, :c6, :c7, :c8]
    enum [:c9, 42, :c10, :c11, 4242, :c12]
    enum [:c13, 42, :c14, 4242, :c15, 424242, :c16, 42424242]

    attach_function :test_untagged_enum, [:int], :int
  end

  module TestEnum3
    extend FFI::Library
    ffi_lib LIBRARY

    enum :enum_type1, [:c1, :c2, :c3, :c4]
    enum :enum_type2, [:c5, 42, :c6, :c7, :c8]
    enum :enum_type3, [:c9, 42, :c10, :c11, 4242, :c12]
    enum :enum_type4, [:c13, 42, :c14, 4242, :c15, 424242, :c16, 42424242]

    attach_function :test_tagged_typedef_enum1, [:enum_type1], :enum_type1
    attach_function :test_tagged_typedef_enum2, [:enum_type2], :enum_type2
    attach_function :test_tagged_typedef_enum3, [:enum_type3], :enum_type3
    attach_function :test_tagged_typedef_enum4, [:enum_type4], :enum_type4
  end

  #
  # Errno fixtures
  #
  module LibTest
    attach_function :setLastError, [ :int ], :void
  end

  #
  # ManagedStruct fixtures
  #
  module LibTest
    attach_function :ptr_from_address, [ FFI::Platform::ADDRESS_SIZE == 32 ? :uint : :ulong_long ], :pointer
  end

  class NoRelease < ManagedStruct
    layout :i, :int
  end

  class WhatClassAmI < ManagedStruct
    layout :i, :int
    def self.release; end
  end

  class PleaseReleaseMe < ManagedStruct
    layout :i, :int
    @@count = 0
    def self.release
      @@count += 1
    end
    def self.wait_gc(count)
      loop = 5
      while loop > 0 && @@count < count
        loop -= 1
        if RUBY_PLATFORM =~ /java/
          require 'java'
          java.lang.System.gc
        else
          GC.start
        end
        sleep 0.05 if @@count < count
      end
    end
  end

  #
  # Number fixtures
  #
  module LibTest
    attach_function :ret_s8, [ :char ], :char
    attach_function :ret_u8, [ :uchar ], :uchar
    attach_function :ret_s16, [ :short ], :short
    attach_function :ret_u16, [ :ushort ], :ushort
    attach_function :ret_s32, [ :int ], :int
    attach_function :ret_u32, [ :uint ], :uint
    attach_function :ret_s64, [ :long_long ], :long_long
    attach_function :ret_u64, [ :ulong_long ], :ulong_long
    attach_function :ret_long, [ :long ], :long
    attach_function :ret_ulong, [ :ulong ], :ulong
    attach_function :set_s8, [ :char ], :void
    attach_function :get_s8, [ ], :char
    attach_function :set_float, [ :float ], :void
    attach_function :get_float, [ ], :float
    attach_function :set_double, [ :double ], :void
    attach_function :get_double, [ ], :double
  end

  PACK_VALUES = {
    's8' => [ 0x12  ],
    'u8' => [ 0x34  ],
    's16' => [ 0x5678 ],
    'u16' => [ 0x9abc ],
    's32' => [ 0x7654321f ],
    'u32' => [ 0xfee1babe ],
    'sL' => [ 0x1f2e3d4c ],
    'uL' => [ 0xf7e8d9ca ],
    's64' => [ 0x1eafdeadbeefa1b2 ],
    #'f32' => [ 1.234567 ], # TODO: Why is this disabled?
    'f64' => [ 9.87654321 ]
  }

  TYPE_MAP = {
    's8' => :char, 'u8' => :uchar, 's16' => :short, 'u16' => :ushort,
    's32' => :int, 'u32' => :uint, 's64' => :long_long, 'u64' => :ulong_long,
    'sL' => :long, 'uL' => :ulong, 'f32' => :float, 'f64' => :double
  }
  TYPES = TYPE_MAP.keys

  module LibTest
    [ 's32', 'u32', 's64', 'u64' ].each do |rt|
      TYPES.each do |t1|
        TYPES.each do |t2|
          TYPES.each do |t3|
            begin
              attach_function "pack_#{t1}#{t2}#{t3}_#{rt}",
                [ TYPE_MAP[t1], TYPE_MAP[t2], TYPE_MAP[t3], :buffer_out ], :void
            rescue FFI::NotFoundError
            end
          end
        end
      end
    end
  end

  #
  # Pointer fixtures
  #
  module LibTest
    attach_function :ptr_ret_int32_t, [ :pointer, :int ], :int
    attach_function :ptr_from_address, [ FFI::Platform::ADDRESS_SIZE == 32 ? :uint : :ulong_long ], :pointer
    attach_function :ptr_set_pointer, [ :pointer, :int, :pointer ], :void
  end

  class ToPtrTest
    def initialize(ptr)
      @ptr = ptr
    end
    def to_ptr
      @ptr
    end
  end

  require 'delegate'
  class PointerDelegate < DelegateClass(FFI::Pointer)
    def initialize(ptr)
      super
      @ptr = ptr
    end
    def to_ptr
      @ptr
    end
  end

  class AutoPointerTestHelper
    @@count = 0
    def self.release
      @@count += 1 if @@count > 0
    end
    def self.reset
      @@count = 0
    end
    def self.gc_everything(count)
      loop = 5
      while @@count < count && loop > 0
        loop -= 1
        if RUBY_PLATFORM =~ /java/
          require "java"
          java.lang.System.gc
        else
          GC.start
        end
        sleep 0.05 unless @@count == count
      end
      @@count = 0
    end
    def self.finalizer
      self.method(:release).to_proc
    end
  end

  #
  # String fixtures
  #
  module LibTest
    attach_function :ptr_ret_pointer, [ :pointer, :int], :string
    attach_function :string_equals, [ :string, :string ], :int
    attach_function :string_dummy, [ :string ], :void
  end

  #
  # Struct initialize fixtures
  #
  class StructWithInitialize < FFI::Struct
    layout :string, :string
    attr_accessor :magic
    def initialize
      super
      self.magic = 42
    end
  end

  #
  # Struct fixtures
  #
  StructTypes = {
    's8' => :char,
    's16' => :short,
    's32' => :int,
    's64' => :long_long,
    'long' => :long,
    'f32' => :float,
    'f64' => :double
  }

  module LibTest
    attach_function :ptr_ret_pointer, [ :pointer, :int], :string
    attach_function :ptr_ret_int32_t, [ :pointer, :int ], :int
    attach_function :ptr_from_address, [ :ulong ], :pointer
    attach_function :string_equals, [ :string, :string ], :int
    [ 's8', 's16', 's32', 's64', 'f32', 'f64', 'long' ].each do |t|
      attach_function "struct_align_#{t}", [ :pointer ], StructTypes[t]
    end
  end

  class PointerMember < FFI::Struct
    layout :pointer, :pointer
  end

  class StringMember < FFI::Struct
    layout :string, :string
  end

  module CallbackMember
    extend FFI::Library
    ffi_lib LIBRARY
    callback :add, [ :int, :int ], :int
    callback :sub, [ :int, :int ], :int

    class TestStruct < FFI::Struct
      layout :add, :add,
        :sub, :sub
    end

    attach_function :struct_call_add_cb, [TestStruct, :int, :int], :int
    attach_function :struct_call_sub_cb, [TestStruct, :int, :int], :int
  end

  module LibTest
    class NestedStruct < FFI::Struct
      layout :i, :int
    end

    class ContainerStruct < FFI::Struct
      layout :first, :char, :ns, NestedStruct
    end

    attach_function :struct_align_nested_struct, [ :pointer ], :int
    attach_function :struct_make_container_struct, [ :int ], :pointer

    class StructWithArray < FFI::Struct
      layout :first, :char, :a, [:int, 5]
    end

    attach_function :struct_make_struct_with_array, [:int, :int, :int, :int, :int], :pointer
    attach_function :struct_field_array, [:pointer], :pointer

    class BuggedStruct < FFI::Struct
      layout :visible, :uchar,
              :x, :uint,
              :y, :uint,
              :rx, :short,
              :ry, :short,
              :order, :uchar,
              :size, :uchar
    end

    attach_function :bugged_struct_size, [], :uint
  end

  module StructCustomTypedef
    extend FFI::Library
    ffi_lib LIBRARY
    typedef :uint, :fubar3_t

    class S < FFI::Struct
      layout :a, :fubar3_t
    end
  end

  #
  # Union fixtures
  #
  module LibTest
    Types = {
      's8' => [:char, :c, 1],
      's16' => [:short, :s, 0xff0],
      's32' => [:int, :i, 0xff00],
      's64' => [:long_long, :j, 0xffff00],
      'long' => [:long, :l, 0xffff],
      'f32' => [:float, :f, 1.0001],
      'f64' => [:double, :d, 1.000000001]
    }

    class TestUnion < FFI::Union
      layout( :a, [:char, 10],
              :i, :int,
              :f, :float,
              :d, :double,
              :s, :short,
              :l, :long,
              :j, :long_long,
              :c, :char )
    end

    Types.keys.each do |k|
      attach_function "union_align_#{k}", [ :pointer ], Types[k][0]
      attach_function "union_make_union_with_#{k}", [ Types[k][0] ], :pointer
    end

    attach_function :union_size, [], :uint
  end

  #
  # Variadic fixtures
  #
  module LibTest
    attach_function :pack_varargs, [ :buffer_out, :string, :varargs ], :void
  end

  module Varargs
    PACK_VALUES = {
      'c' => [ 0x12  ],
      'C' => [ 0x34  ],
      's' => [ 0x5678 ],
      'S' => [ 0x9abc ],
      'i' => [ 0x7654321f ],
      'I' => [ 0xfee1babe ],
      'l' => [ 0x1f2e3d4c ],
      'L' => [ 0xf7e8d9ca ],
      'j' => [ 0x1eafdeadbeefa1b2 ],
      'f' => [ 1.23456789 ],
      'd' => [ 9.87654321 ]
    }

    TYPE_MAP = {
      'c' => :char, 'C' => :uchar, 's' => :short, 'S' => :ushort,
      'i' => :int, 'I' => :uint, 'j' => :long_long, 'J' => :ulong_long,
      'l' => :long, 'L' => :ulong, 'f' => :float, 'd' => :double
    }
  end
end