ffi 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

data/Rakefile CHANGED
@@ -75,7 +75,7 @@ PROJ.name = 'ffi'
75
75
  PROJ.authors = 'Wayne Meissner'
76
76
  PROJ.email = 'wmeissner@gmail.com'
77
77
  PROJ.url = 'http://wiki.github.com/ffi/ffi'
78
- PROJ.version = '1.0.1'
78
+ PROJ.version = '1.0.2'
79
79
  PROJ.rubyforge.name = 'ffi'
80
80
  PROJ.readme_file = 'README.rdoc'
81
81
 
@@ -67,6 +67,15 @@ memory_put_##name(VALUE self, VALUE offset, VALUE value) \
67
67
  memory_op_put_##name(memory, NUM2LONG(offset), value); \
68
68
  return self; \
69
69
  } \
70
+ static VALUE memory_write_##name(VALUE self, VALUE value); \
71
+ static VALUE \
72
+ memory_write_##name(VALUE self, VALUE value) \
73
+ { \
74
+ AbstractMemory* memory; \
75
+ Data_Get_Struct(self, AbstractMemory, memory); \
76
+ memory_op_put_##name(memory, 0, value); \
77
+ return self; \
78
+ } \
70
79
  static VALUE memory_op_get_##name(AbstractMemory* memory, long off); \
71
80
  static VALUE \
72
81
  memory_op_get_##name(AbstractMemory* memory, long off) \
@@ -85,6 +94,14 @@ memory_get_##name(VALUE self, VALUE offset) \
85
94
  Data_Get_Struct(self, AbstractMemory, memory); \
86
95
  return memory_op_get_##name(memory, NUM2LONG(offset)); \
87
96
  } \
97
+ static VALUE memory_read_##name(VALUE self); \
98
+ static VALUE \
99
+ memory_read_##name(VALUE self) \
100
+ { \
101
+ AbstractMemory* memory; \
102
+ Data_Get_Struct(self, AbstractMemory, memory); \
103
+ return memory_op_get_##name(memory, 0); \
104
+ } \
88
105
  static MemoryOp memory_op_##name = { memory_op_get_##name, memory_op_put_##name }; \
89
106
  \
90
107
  static VALUE memory_put_array_of_##name(VALUE self, VALUE offset, VALUE ary); \
@@ -103,6 +120,12 @@ memory_put_array_of_##name(VALUE self, VALUE offset, VALUE ary) \
103
120
  } \
104
121
  return self; \
105
122
  } \
123
+ static VALUE memory_write_array_of_##name(VALUE self, VALUE ary); \
124
+ static VALUE \
125
+ memory_write_array_of_##name(VALUE self, VALUE ary) \
126
+ { \
127
+ return memory_put_array_of_##name(self, INT2FIX(0), ary); \
128
+ } \
106
129
  static VALUE memory_get_array_of_##name(VALUE self, VALUE offset, VALUE length); \
107
130
  static VALUE \
108
131
  memory_get_array_of_##name(VALUE self, VALUE offset, VALUE length) \
@@ -120,6 +143,12 @@ memory_get_array_of_##name(VALUE self, VALUE offset, VALUE length) \
120
143
  rb_ary_push(retVal, fromNative(VAL(tmp, swap))); \
121
144
  } \
122
145
  return retVal; \
146
+ } \
147
+ static VALUE memory_read_array_of_##name(VALUE self, VALUE length); \
148
+ static VALUE \
149
+ memory_read_array_of_##name(VALUE self, VALUE length) \
150
+ { \
151
+ return memory_get_array_of_##name(self, INT2FIX(0), length); \
123
152
  }
124
153
 
125
154
  #define NOSWAP(x) (x)
@@ -492,10 +521,18 @@ rbffi_AbstractMemory_Init(VALUE moduleFFI)
492
521
  rb_define_method(classMemory, "get_" #type, memory_get_##type, 1); \
493
522
  rb_define_method(classMemory, "put_u" #type, memory_put_u##type, 2); \
494
523
  rb_define_method(classMemory, "get_u" #type, memory_get_u##type, 1); \
524
+ rb_define_method(classMemory, "write_" #type, memory_write_##type, 1); \
525
+ rb_define_method(classMemory, "read_" #type, memory_read_##type, 0); \
526
+ rb_define_method(classMemory, "write_u" #type, memory_write_u##type, 1); \
527
+ rb_define_method(classMemory, "read_u" #type, memory_read_u##type, 0); \
495
528
  rb_define_method(classMemory, "put_array_of_" #type, memory_put_array_of_##type, 2); \
496
529
  rb_define_method(classMemory, "get_array_of_" #type, memory_get_array_of_##type, 2); \
497
530
  rb_define_method(classMemory, "put_array_of_u" #type, memory_put_array_of_u##type, 2); \
498
- rb_define_method(classMemory, "get_array_of_u" #type, memory_get_array_of_u##type, 2);
531
+ rb_define_method(classMemory, "get_array_of_u" #type, memory_get_array_of_u##type, 2); \
532
+ rb_define_method(classMemory, "write_array_of_" #type, memory_write_array_of_##type, 1); \
533
+ rb_define_method(classMemory, "read_array_of_" #type, memory_read_array_of_##type, 1); \
534
+ rb_define_method(classMemory, "write_array_of_u" #type, memory_write_array_of_u##type, 1); \
535
+ rb_define_method(classMemory, "read_array_of_u" #type, memory_read_array_of_u##type, 1);
499
536
 
500
537
  INT(int8);
501
538
  INT(int16);
@@ -508,10 +545,18 @@ rbffi_AbstractMemory_Init(VALUE moduleFFI)
508
545
  rb_define_alias(classMemory, "get_" #name, "get_" #old); \
509
546
  rb_define_alias(classMemory, "put_u" #name, "put_u" #old); \
510
547
  rb_define_alias(classMemory, "get_u" #name, "get_u" #old); \
548
+ rb_define_alias(classMemory, "write_" #name, "write_" #old); \
549
+ rb_define_alias(classMemory, "read_" #name, "read_" #old); \
550
+ rb_define_alias(classMemory, "write_u" #name, "write_u" #old); \
551
+ rb_define_alias(classMemory, "read_u" #name, "read_u" #old); \
511
552
  rb_define_alias(classMemory, "put_array_of_" #name, "put_array_of_" #old); \
512
553
  rb_define_alias(classMemory, "get_array_of_" #name, "get_array_of_" #old); \
513
554
  rb_define_alias(classMemory, "put_array_of_u" #name, "put_array_of_u" #old); \
514
- rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old);
555
+ rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old); \
556
+ rb_define_alias(classMemory, "write_array_of_" #name, "write_array_of_" #old); \
557
+ rb_define_alias(classMemory, "read_array_of_" #name, "read_array_of_" #old); \
558
+ rb_define_alias(classMemory, "write_array_of_u" #name, "write_array_of_u" #old); \
559
+ rb_define_alias(classMemory, "read_array_of_u" #name, "read_array_of_u" #old);
515
560
 
516
561
  ALIAS(char, int8);
517
562
  ALIAS(short, int16);
@@ -522,22 +567,35 @@ rbffi_AbstractMemory_Init(VALUE moduleFFI)
522
567
  rb_define_method(classMemory, "get_float32", memory_get_float32, 1);
523
568
  rb_define_alias(classMemory, "put_float", "put_float32");
524
569
  rb_define_alias(classMemory, "get_float", "get_float32");
570
+ rb_define_method(classMemory, "write_float", memory_write_float32, 1);
571
+ rb_define_method(classMemory, "read_float", memory_read_float32, 0);
525
572
  rb_define_method(classMemory, "put_array_of_float32", memory_put_array_of_float32, 2);
526
573
  rb_define_method(classMemory, "get_array_of_float32", memory_get_array_of_float32, 2);
574
+ rb_define_method(classMemory, "write_array_of_float", memory_write_array_of_float32, 1);
575
+ rb_define_method(classMemory, "read_array_of_float", memory_read_array_of_float32, 1);
527
576
  rb_define_alias(classMemory, "put_array_of_float", "put_array_of_float32");
528
577
  rb_define_alias(classMemory, "get_array_of_float", "get_array_of_float32");
529
578
  rb_define_method(classMemory, "put_float64", memory_put_float64, 2);
530
579
  rb_define_method(classMemory, "get_float64", memory_get_float64, 1);
531
580
  rb_define_alias(classMemory, "put_double", "put_float64");
532
581
  rb_define_alias(classMemory, "get_double", "get_float64");
582
+ rb_define_method(classMemory, "write_double", memory_write_float64, 1);
583
+ rb_define_method(classMemory, "read_double", memory_read_float64, 0);
533
584
  rb_define_method(classMemory, "put_array_of_float64", memory_put_array_of_float64, 2);
534
585
  rb_define_method(classMemory, "get_array_of_float64", memory_get_array_of_float64, 2);
586
+ rb_define_method(classMemory, "write_array_of_double", memory_write_array_of_float64, 1);
587
+ rb_define_method(classMemory, "read_array_of_double", memory_read_array_of_float64, 1);
535
588
  rb_define_alias(classMemory, "put_array_of_double", "put_array_of_float64");
536
589
  rb_define_alias(classMemory, "get_array_of_double", "get_array_of_float64");
537
590
  rb_define_method(classMemory, "put_pointer", memory_put_pointer, 2);
538
591
  rb_define_method(classMemory, "get_pointer", memory_get_pointer, 1);
592
+ rb_define_method(classMemory, "write_pointer", memory_write_pointer, 1);
593
+ rb_define_method(classMemory, "read_pointer", memory_read_pointer, 0);
539
594
  rb_define_method(classMemory, "put_array_of_pointer", memory_put_array_of_pointer, 2);
540
595
  rb_define_method(classMemory, "get_array_of_pointer", memory_get_array_of_pointer, 2);
596
+ rb_define_method(classMemory, "write_array_of_pointer", memory_write_array_of_pointer, 1);
597
+ rb_define_method(classMemory, "read_array_of_pointer", memory_read_array_of_pointer, 1);
598
+
541
599
  rb_define_method(classMemory, "get_string", memory_get_string, -1);
542
600
  rb_define_method(classMemory, "put_string", memory_put_string, 2);
543
601
  rb_define_method(classMemory, "get_bytes", memory_get_bytes, 2);
@@ -44,6 +44,7 @@
44
44
  #include "Struct.h"
45
45
  #include "StructByValue.h"
46
46
  #include "ArrayType.h"
47
+ #include "MappedType.h"
47
48
 
48
49
  typedef struct InlineArray_ {
49
50
  VALUE rbMemory;
@@ -285,8 +286,8 @@ struct_set_pointer(VALUE self, VALUE pointer)
285
286
  layout = struct_layout(self);
286
287
 
287
288
  if (layout->base.ffiType->size > memory->size) {
288
- rb_raise(rb_eArgError, "memory of %d bytes too small for struct %s (expected at least %d)",
289
- memory->size, rb_obj_classname(self), layout->base.ffiType->size);
289
+ rb_raise(rb_eArgError, "memory of %ld bytes too small for struct %s (expected at least %ld)",
290
+ memory->size, rb_obj_classname(self), (long) layout->base.ffiType->size);
290
291
  }
291
292
 
292
293
  s->pointer = MEMORY(pointer);
@@ -399,6 +400,10 @@ inline_array_initialize(VALUE self, VALUE rbMemory, VALUE rbField)
399
400
  Data_Get_Struct(array->arrayType->rbComponentType, Type, array->componentType);
400
401
 
401
402
  array->op = get_memory_op(array->componentType);
403
+ if (array->op == NULL && array->componentType->nativeType == NATIVE_MAPPED) {
404
+ array->op = get_memory_op(((MappedType *) array->componentType)->type);
405
+ }
406
+
402
407
  array->length = array->arrayType->length;
403
408
 
404
409
  return self;
@@ -432,7 +437,15 @@ inline_array_aref(VALUE self, VALUE rbIndex)
432
437
  Data_Get_Struct(self, InlineArray, array);
433
438
 
434
439
  if (array->op != NULL) {
435
- return array->op->get(array->memory, inline_array_offset(array, NUM2INT(rbIndex)));
440
+ VALUE rbNativeValue = array->op->get(array->memory,
441
+ inline_array_offset(array, NUM2INT(rbIndex)));
442
+ if (unlikely(array->componentType->nativeType == NATIVE_MAPPED)) {
443
+ return rb_funcall(((MappedType *) array->componentType)->rbConverter,
444
+ rb_intern("from_native"), 2, rbNativeValue, Qnil);
445
+ } else {
446
+ return rbNativeValue;
447
+ }
448
+
436
449
  } else if (array->componentType->nativeType == NATIVE_STRUCT) {
437
450
  VALUE rbOffset = INT2NUM(inline_array_offset(array, NUM2INT(rbIndex)));
438
451
  VALUE rbLength = INT2NUM(array->componentType->ffiType->size);
@@ -454,8 +467,13 @@ inline_array_aset(VALUE self, VALUE rbIndex, VALUE rbValue)
454
467
  Data_Get_Struct(self, InlineArray, array);
455
468
 
456
469
  if (array->op != NULL) {
470
+ if (unlikely(array->componentType->nativeType == NATIVE_MAPPED)) {
471
+ rbValue = rb_funcall(((MappedType *) array->componentType)->rbConverter,
472
+ rb_intern("to_native"), 2, rbValue, Qnil);
473
+ }
457
474
  array->op->put(array->memory, inline_array_offset(array, NUM2INT(rbIndex)),
458
475
  rbValue);
476
+
459
477
  } else if (array->componentType->nativeType == NATIVE_STRUCT) {
460
478
  int offset = inline_array_offset(array, NUM2INT(rbIndex));
461
479
  Struct* s;
@@ -494,25 +512,8 @@ inline_array_each(VALUE self)
494
512
 
495
513
  Data_Get_Struct(self, InlineArray, array);
496
514
 
497
- if (array->op != NULL) {
498
- for (i = 0; i < array->length; ++i) {
499
- int offset = inline_array_offset(array, i);
500
- rb_yield(array->op->get(array->memory, offset));
501
- }
502
- } else if (array->componentType->nativeType == NATIVE_STRUCT) {
503
- for (i = 0; i < array->length; ++i) {
504
- VALUE rbOffset = UINT2NUM(inline_array_offset(array, i));
505
- VALUE rbLength = UINT2NUM(array->componentType->ffiType->size);
506
- VALUE rbPointer = rb_funcall(array->rbMemory, rb_intern("slice"), 2, rbOffset, rbLength);
507
-
508
- rb_yield(rb_class_new_instance(1, &rbPointer, ((StructByValue *) array->componentType)->rbStructClass));
509
- }
510
- } else {
511
- ArrayType* arrayType;
512
- Data_Get_Struct(array->field->rbType, ArrayType, arrayType);
513
-
514
- rb_raise(rb_eArgError, "get not supported for %s", rb_obj_classname(arrayType->rbComponentType));
515
- return Qnil;
515
+ for (i = 0; i < array->length; ++i) {
516
+ rb_yield(inline_array_aref(self, INT2FIX(i)));
516
517
  }
517
518
 
518
519
  return self;
@@ -530,8 +531,7 @@ inline_array_to_a(VALUE self)
530
531
 
531
532
 
532
533
  for (i = 0; i < array->length; ++i) {
533
- int offset = inline_array_offset(array, i);
534
- rb_ary_push(obj, array->op->get(array->memory, offset));
534
+ rb_ary_push(obj, inline_array_aref(self, INT2FIX(i)));
535
535
  }
536
536
 
537
537
  return obj;
@@ -27,48 +27,6 @@ module FFI
27
27
  def self.size
28
28
  SIZE
29
29
  end
30
- # Write +obj+ as a C int at the memory pointed to.
31
- def write_int(obj)
32
- put_int32(0, obj)
33
- end
34
-
35
- # Read a C int from the memory pointed to.
36
- def read_int
37
- get_int32(0)
38
- end
39
-
40
- # Write +obj+ as a C long at the memory pointed to.
41
- def write_long(obj)
42
- put_long(0, obj)
43
- end
44
-
45
- # Read a C long from the memory pointed to.
46
- def read_long
47
- get_long(0)
48
- end
49
- # Write +obj+ as a C long long at the memory pointed to.
50
- def write_long_long(obj)
51
- put_int64(0, obj)
52
- end
53
-
54
- # Read a C long long from the memory pointed to.
55
- def read_long_long
56
- get_int64(0)
57
- end
58
-
59
- def read_pointer
60
- get_pointer(0)
61
- end
62
- def write_pointer(ptr)
63
- put_pointer(0, ptr)
64
- end
65
-
66
- def read_float
67
- get_float32(0)
68
- end
69
- def write_float(obj)
70
- put_float32(0, obj)
71
- end
72
30
 
73
31
  def read_string(len=nil)
74
32
  if len
@@ -77,15 +35,19 @@ module FFI
77
35
  get_string(0)
78
36
  end
79
37
  end
38
+
80
39
  def read_string_length(len)
81
40
  get_bytes(0, len)
82
41
  end
42
+
83
43
  def read_string_to_null
84
44
  get_string(0)
85
45
  end
46
+
86
47
  def write_string_length(str, len)
87
48
  put_bytes(0, str, 0, len)
88
49
  end
50
+
89
51
  def write_string(str, len=nil)
90
52
  len = str.bytesize unless len
91
53
  # Write the string data without NUL termination
@@ -112,29 +74,5 @@ module FFI
112
74
  }
113
75
  self
114
76
  end
115
- def read_array_of_int(length)
116
- get_array_of_int32(0, length)
117
- end
118
-
119
- def write_array_of_int(ary)
120
- put_array_of_int32(0, ary)
121
- end
122
-
123
- def read_array_of_long(length)
124
- get_array_of_long(0, length)
125
- end
126
-
127
- def write_array_of_long(ary)
128
- put_array_of_long(0, ary)
129
- end
130
-
131
- def read_array_of_pointer(length)
132
- read_array_of_type(:pointer, :read_pointer, length)
133
- end
134
-
135
- def write_array_of_pointer(ary)
136
- write_array_of_type(:pointer, :write_pointer, ary)
137
- end
138
-
139
77
  end
140
78
  end
Binary file
@@ -306,11 +306,13 @@ describe "Struct tests" do
306
306
  end
307
307
  module EnumFields
308
308
  extend FFI::Library
309
- enum :test_enum, [:c1, 10, :c2, 20, :c3, 30, :c4, 40]
309
+ TestEnum = enum :test_enum, [:c1, 10, :c2, 20, :c3, 30, :c4, 40]
310
310
  class TestStruct < FFI::Struct
311
- layout :a, :int, :c, :test_enum
311
+ layout :a, :int, :c, :test_enum,
312
+ :d, [ TestEnum, TestEnum.symbols.length ]
312
313
  end
313
314
  end
315
+
314
316
  it ":enum field r/w" do
315
317
  s = EnumFields::TestStruct.new
316
318
  s[:c] = :c3
@@ -318,6 +320,22 @@ describe "Struct tests" do
318
320
  s.pointer.get_uint(FFI::Type::INT32.size).should == 30
319
321
  s[:c].should == :c3
320
322
  end
323
+
324
+ it "array of :enum field" do
325
+ s = EnumFields::TestStruct.new
326
+ EnumFields::TestEnum.symbols.each_with_index do |val, i|
327
+ s[:d][i] = val
328
+ end
329
+
330
+ EnumFields::TestEnum.symbols.each_with_index do |val, i|
331
+ s.pointer.get_uint(FFI::Type::INT32.size * (2 + i)).should == EnumFields::TestEnum[val]
332
+ end
333
+
334
+ s[:d].each_with_index do |val, i|
335
+ val.should == EnumFields::TestEnum.symbols[i]
336
+ end
337
+ end
338
+
321
339
  module CallbackMember
322
340
  extend FFI::Library
323
341
  ffi_lib TestLibrary::PATH
@@ -626,13 +644,13 @@ describe "Struct allocation" do
626
644
  class S < FFI::Struct
627
645
  layout :i, :uint
628
646
  end
629
- S.new(Pointer::NULL).null?.should be_true
647
+ S.new(FFI::Pointer::NULL).null?.should be_true
630
648
  end
631
649
 
632
650
  it "null? should be false when initialized with non-NULL pointer" do
633
651
  class S < FFI::Struct
634
652
  layout :i, :uint
635
653
  end
636
- S.new(MemoryPointer.new(S)).null?.should be_false
654
+ S.new(FFI::MemoryPointer.new(S)).null?.should be_false
637
655
  end
638
656
  end
@@ -14,6 +14,14 @@ spec = Gem::Specification.new do |s|
14
14
  s.require_path = 'lib'
15
15
  s.files = PROJ.gem.files
16
16
  s.add_dependency *PROJ.gem.dependencies.flatten
17
+ PROJ.gem.extras.each do |msg, val|
18
+ case val
19
+ when Proc
20
+ val.call(s.send(msg))
21
+ else
22
+ s.send "#{msg}=", val
23
+ end
24
+ end
17
25
  end
18
26
 
19
27
  Rake::ExtensionTask.new('ffi_c', spec) do |ext|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Wayne Meissner
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-03 00:00:00 +10:00
17
+ date: 2010-12-17 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -62,6 +62,7 @@ extensions:
62
62
  extra_rdoc_files:
63
63
  - History.txt
64
64
  - README.rdoc
65
+ - lib/ffi_c.bundle
65
66
  files:
66
67
  - History.txt
67
68
  - LICENSE
@@ -390,6 +391,7 @@ files:
390
391
  - lib/ffi/types.rb
391
392
  - lib/ffi/union.rb
392
393
  - lib/ffi/variadic.rb
394
+ - lib/ffi_c.bundle
393
395
  - spec/ffi/async_callback_spec.rb
394
396
  - spec/ffi/bool_spec.rb
395
397
  - spec/ffi/buffer_spec.rb