ffi 0.6.3-x86-mingw32 → 1.0.1-x86-mingw32

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.

Files changed (64) hide show
  1. data/History.txt +7 -0
  2. data/LICENSE +10 -21
  3. data/README.rdoc +1 -0
  4. data/Rakefile +4 -2
  5. data/ext/ffi_c/AbstractMemory.c +103 -38
  6. data/ext/ffi_c/AbstractMemory.h +15 -22
  7. data/ext/ffi_c/Buffer.c +61 -22
  8. data/ext/ffi_c/Call.c +52 -540
  9. data/ext/ffi_c/Call.h +1 -1
  10. data/ext/ffi_c/DataConverter.c +62 -0
  11. data/ext/ffi_c/DynamicLibrary.c +21 -1
  12. data/ext/ffi_c/Function.c +315 -30
  13. data/ext/ffi_c/MappedType.c +146 -0
  14. data/ext/ffi_c/MappedType.h +57 -0
  15. data/ext/ffi_c/MemoryPointer.c +12 -33
  16. data/ext/ffi_c/Platform.c +2 -0
  17. data/ext/ffi_c/Pointer.c +66 -28
  18. data/ext/ffi_c/Struct.c +19 -306
  19. data/ext/ffi_c/Struct.h +6 -0
  20. data/ext/ffi_c/StructByReference.c +150 -0
  21. data/ext/ffi_c/StructByReference.h +50 -0
  22. data/ext/ffi_c/StructLayout.c +25 -14
  23. data/ext/ffi_c/Type.c +39 -68
  24. data/ext/ffi_c/Type.h +12 -22
  25. data/ext/ffi_c/Types.c +20 -5
  26. data/ext/ffi_c/Types.h +7 -7
  27. data/ext/ffi_c/Variadic.c +21 -17
  28. data/ext/ffi_c/extconf.rb +4 -0
  29. data/ext/ffi_c/ffi.c +8 -2
  30. data/ext/ffi_c/rbffi.h +1 -0
  31. data/lib/ffi/autopointer.rb +23 -22
  32. data/lib/ffi/enum.rb +36 -21
  33. data/lib/ffi/errno.rb +20 -0
  34. data/lib/ffi/ffi.rb +13 -80
  35. data/lib/ffi/io.rb +12 -20
  36. data/lib/ffi/library.rb +109 -92
  37. data/lib/ffi/managedstruct.rb +1 -1
  38. data/lib/ffi/memorypointer.rb +15 -21
  39. data/lib/ffi/platform.rb +24 -28
  40. data/lib/ffi/pointer.rb +14 -21
  41. data/lib/ffi/struct.rb +98 -49
  42. data/lib/ffi/struct_layout_builder.rb +158 -0
  43. data/lib/ffi/types.rb +99 -128
  44. data/lib/ffi/union.rb +20 -0
  45. data/lib/ffi/variadic.rb +33 -22
  46. data/lib/ffi_c.so +0 -0
  47. data/spec/ffi/async_callback_spec.rb +23 -0
  48. data/spec/ffi/callback_spec.rb +62 -0
  49. data/spec/ffi/custom_param_type.rb +31 -0
  50. data/spec/ffi/custom_type_spec.rb +73 -0
  51. data/spec/ffi/enum_spec.rb +19 -0
  52. data/spec/ffi/ffi_spec.rb +24 -0
  53. data/spec/ffi/pointer_spec.rb +15 -0
  54. data/spec/ffi/rbx/memory_pointer_spec.rb +7 -1
  55. data/spec/ffi/strptr_spec.rb +36 -0
  56. data/spec/ffi/struct_packed_spec.rb +46 -0
  57. data/spec/ffi/struct_spec.rb +19 -5
  58. data/spec/ffi/typedef_spec.rb +14 -0
  59. data/tasks/setup.rb +2 -1
  60. metadata +15 -6
  61. data/ext/ffi_c/AutoPointer.c +0 -60
  62. data/ext/ffi_c/AutoPointer.h +0 -18
  63. data/lib/1.8/ffi_c.so +0 -0
  64. data/lib/1.9/ffi_c.so +0 -0
@@ -32,6 +32,11 @@
32
32
 
33
33
  #include "AbstractMemory.h"
34
34
  #include "Type.h"
35
+ #ifdef RUBY_1_9
36
+ #include <ruby/st.h>
37
+ #else
38
+ #include <st.h>
39
+ #endif
35
40
 
36
41
  #ifdef __cplusplus
37
42
  extern "C" {
@@ -63,6 +68,7 @@ extern "C" {
63
68
  int size;
64
69
  int align;
65
70
  ffi_type** ffiTypes;
71
+ struct st_table* fieldSymbolTable;
66
72
  VALUE rbFieldNames;
67
73
  VALUE rbFieldMap;
68
74
  VALUE rbFields;
@@ -0,0 +1,150 @@
1
+ /*
2
+ * Copyright (c) 2010, Wayne Meissner
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright notice
11
+ * this list of conditions and the following disclaimer in the documentation
12
+ * and/or other materials provided with the distribution.
13
+ * * The name of the author or authors may not be used to endorse or promote
14
+ * products derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <sys/param.h>
29
+ #include <sys/types.h>
30
+ #include <stdio.h>
31
+ #include <stdint.h>
32
+ #include <stdbool.h>
33
+ #include <errno.h>
34
+ #include <ruby.h>
35
+
36
+ #include <ffi.h>
37
+ #include "rbffi.h"
38
+ #include "compat.h"
39
+
40
+ #include "Pointer.h"
41
+ #include "Struct.h"
42
+ #include "StructByReference.h"
43
+
44
+
45
+ #define FFI_ALIGN(v, a) (((((size_t) (v))-1) | ((a)-1))+1)
46
+
47
+ static VALUE sbr_allocate(VALUE);
48
+ static VALUE sbr_initialize(VALUE, VALUE);
49
+ static void sbr_mark(StructByReference *);
50
+
51
+ VALUE rbffi_StructByReferenceClass = Qnil;
52
+
53
+ static VALUE
54
+ sbr_allocate(VALUE klass)
55
+ {
56
+ StructByReference* sbr;
57
+
58
+ VALUE obj = Data_Make_Struct(klass, StructByReference, sbr_mark, -1, sbr);
59
+
60
+ sbr->rbStructClass = Qnil;
61
+
62
+ return obj;
63
+ }
64
+
65
+ static VALUE
66
+ sbr_initialize(VALUE self, VALUE rbStructClass)
67
+ {
68
+ StructByReference* sbr = NULL;
69
+
70
+ if (!rb_class_inherited_p(rbStructClass, rbffi_StructClass)) {
71
+ rb_raise(rb_eTypeError, "wrong type (expected subclass of FFI::Struct)");
72
+ }
73
+
74
+ Data_Get_Struct(self, StructByReference, sbr);
75
+ sbr->rbStructClass = rbStructClass;
76
+
77
+ return self;
78
+ }
79
+
80
+ static void
81
+ sbr_mark(StructByReference *sbr)
82
+ {
83
+ rb_gc_mark(sbr->rbStructClass);
84
+ }
85
+
86
+
87
+ static VALUE
88
+ sbr_struct_class(VALUE self)
89
+ {
90
+ StructByReference* sbr;
91
+
92
+ Data_Get_Struct(self, StructByReference, sbr);
93
+
94
+ return sbr->rbStructClass;
95
+ }
96
+
97
+ static VALUE
98
+ sbr_native_type(VALUE self)
99
+ {
100
+ return rb_const_get(rbffi_TypeClass, rb_intern("POINTER"));
101
+ }
102
+
103
+ static VALUE
104
+ sbr_to_native(VALUE self, VALUE value, VALUE ctx)
105
+ {
106
+ StructByReference* sbr;
107
+ Struct* s;
108
+
109
+ if (unlikely(value == Qnil)) {
110
+ return rbffi_NullPointerSingleton;
111
+ }
112
+
113
+ Data_Get_Struct(self, StructByReference, sbr);
114
+ if (!rb_obj_is_kind_of(value, sbr->rbStructClass)) {
115
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
116
+ rb_obj_classname(value),
117
+ RSTRING_PTR(rb_class_name(sbr->rbStructClass)));
118
+ }
119
+
120
+ Data_Get_Struct(value, Struct, s);
121
+
122
+ return s->rbPointer;
123
+ }
124
+
125
+ static VALUE
126
+ sbr_from_native(VALUE self, VALUE value, VALUE ctx)
127
+ {
128
+ StructByReference* sbr;
129
+
130
+ Data_Get_Struct(self, StructByReference, sbr);
131
+
132
+ return rb_class_new_instance(1, &value, sbr->rbStructClass);
133
+ }
134
+
135
+
136
+ void
137
+ rbffi_StructByReference_Init(VALUE moduleFFI)
138
+ {
139
+ rbffi_StructByReferenceClass = rb_define_class_under(moduleFFI, "StructByReference", rb_cObject);
140
+ rb_global_variable(&rbffi_StructByReferenceClass);
141
+ rb_include_module(rbffi_StructByReferenceClass, rb_const_get(moduleFFI, rb_intern("DataConverter")));
142
+
143
+ rb_define_alloc_func(rbffi_StructByReferenceClass, sbr_allocate);
144
+ rb_define_method(rbffi_StructByReferenceClass, "initialize", sbr_initialize, 1);
145
+ rb_define_method(rbffi_StructByReferenceClass, "struct_class", sbr_struct_class, 0);
146
+ rb_define_method(rbffi_StructByReferenceClass, "native_type", sbr_native_type, 0);
147
+ rb_define_method(rbffi_StructByReferenceClass, "to_native", sbr_to_native, 2);
148
+ rb_define_method(rbffi_StructByReferenceClass, "from_native", sbr_from_native, 2);
149
+ }
150
+
@@ -0,0 +1,50 @@
1
+ /*
2
+ * Copyright (c) 2009, Wayne Meissner
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright notice
11
+ * this list of conditions and the following disclaimer in the documentation
12
+ * and/or other materials provided with the distribution.
13
+ * * The name of the author or authors may not be used to endorse or promote
14
+ * products derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #ifndef RBFFI_STRUCTBYREFERENCE_H
29
+ #define RBFFI_STRUCTBYREFERENCE_H
30
+
31
+ #include <ruby.h>
32
+
33
+ #ifdef __cplusplus
34
+ extern "C" {
35
+ #endif
36
+
37
+ typedef struct StructByReference_ {
38
+ VALUE rbStructClass;
39
+ } StructByReference;
40
+
41
+ void rbffi_StructByReference_Init(VALUE moduleFFI);
42
+
43
+ extern VALUE rbffi_StructByReferenceClass;
44
+
45
+ #ifdef __cplusplus
46
+ }
47
+ #endif
48
+
49
+ #endif /* RBFFI_STRUCTBYREFERENCE_H */
50
+
@@ -52,6 +52,8 @@ static void struct_layout_free(StructLayout *);
52
52
  static void struct_field_mark(StructField* );
53
53
 
54
54
  VALUE rbffi_StructLayoutFieldClass = Qnil;
55
+ VALUE rbffi_StructLayoutNumberFieldClass = Qnil, rbffi_StructLayoutPointerFieldClass = Qnil;
56
+ VALUE rbffi_StructLayoutStringFieldClass = Qnil;
55
57
  VALUE rbffi_StructLayoutFunctionFieldClass = Qnil, rbffi_StructLayoutArrayFieldClass = Qnil;
56
58
 
57
59
  VALUE rbffi_StructLayoutClass = Qnil;
@@ -320,6 +322,7 @@ struct_layout_allocate(VALUE klass)
320
322
  layout->rbFieldMap = Qnil;
321
323
  layout->rbFieldNames = Qnil;
322
324
  layout->rbFields = Qnil;
325
+ layout->fieldSymbolTable = st_init_numtable();
323
326
  layout->base.ffiType = xcalloc(1, sizeof(*layout->base.ffiType));
324
327
  layout->base.ffiType->size = 0;
325
328
  layout->base.ffiType->alignment = 0;
@@ -329,29 +332,29 @@ struct_layout_allocate(VALUE klass)
329
332
  }
330
333
 
331
334
  static VALUE
332
- struct_layout_initialize(VALUE self, VALUE field_names, VALUE fields, VALUE size, VALUE align)
335
+ struct_layout_initialize(VALUE self, VALUE fields, VALUE size, VALUE align)
333
336
  {
334
337
  StructLayout* layout;
335
338
  ffi_type* ltype;
336
339
  int i;
337
340
 
338
341
  Data_Get_Struct(self, StructLayout, layout);
342
+ layout->fieldCount = RARRAY_LEN(fields);
339
343
  layout->rbFieldMap = rb_hash_new();
340
- layout->rbFieldNames = rb_ary_dup(field_names);
344
+ layout->rbFieldNames = rb_ary_new2(layout->fieldCount);
341
345
  layout->size = NUM2INT(size);
342
346
  layout->align = NUM2INT(align);
343
- layout->fieldCount = RARRAY_LEN(field_names);
344
347
  layout->fields = xcalloc(layout->fieldCount, sizeof(StructField *));
345
348
  layout->ffiTypes = xcalloc(layout->fieldCount + 1, sizeof(ffi_type *));
346
349
  layout->rbFields = rb_ary_new2(layout->fieldCount);
347
350
  layout->base.ffiType->elements = layout->ffiTypes;
348
- layout->base.ffiType->size = 0;
349
- layout->base.ffiType->alignment = 1;
351
+ layout->base.ffiType->size = layout->size;
352
+ layout->base.ffiType->alignment = layout->align;
350
353
 
351
354
  ltype = layout->base.ffiType;
352
355
  for (i = 0; i < (int) layout->fieldCount; ++i) {
353
- VALUE rbName = rb_ary_entry(field_names, i);
354
- VALUE rbField = rb_hash_aref(fields, rbName);
356
+ VALUE rbField = rb_ary_entry(fields, i);
357
+ VALUE rbName;
355
358
  StructField* field;
356
359
  ffi_type* ftype;
357
360
 
@@ -359,6 +362,7 @@ struct_layout_initialize(VALUE self, VALUE field_names, VALUE fields, VALUE size
359
362
  if (!rb_obj_is_kind_of(rbField, rbffi_StructLayoutFieldClass)) {
360
363
  rb_raise(rb_eTypeError, "wrong type for field %d.", i);
361
364
  }
365
+ rbName = rb_funcall2(rbField, rb_intern("name"), 0, NULL);
362
366
 
363
367
  Data_Get_Struct(rbField, StructField, field = layout->fields[i]);
364
368
 
@@ -371,20 +375,17 @@ struct_layout_initialize(VALUE self, VALUE field_names, VALUE fields, VALUE size
371
375
  rb_raise(rb_eTypeError, "type of field %d has zero size", i);
372
376
  }
373
377
 
374
- rb_hash_aset(layout->rbFieldMap, rbName, rbField);
375
378
  layout->ffiTypes[i] = ftype;
379
+ st_insert(layout->fieldSymbolTable, rbName, rbField);
380
+ rb_hash_aset(layout->rbFieldMap, rbName, rbField);
376
381
  rb_ary_push(layout->rbFields, rbField);
377
- ltype->size = MAX(ltype->size, field->offset + ftype->size);
378
- ltype->alignment = MAX(ltype->alignment, ftype->alignment);
382
+ rb_ary_push(layout->rbFieldNames, rbName);
379
383
  }
380
384
 
381
385
  if (ltype->size == 0) {
382
386
  rb_raise(rb_eRuntimeError, "Struct size is zero");
383
387
  }
384
388
 
385
- // Include tail padding
386
- ltype->size = FFI_ALIGN(ltype->size, ltype->alignment);
387
-
388
389
  return self;
389
390
  }
390
391
 
@@ -442,6 +443,7 @@ struct_layout_free(StructLayout *layout)
442
443
  xfree(layout->ffiTypes);
443
444
  xfree(layout->base.ffiType);
444
445
  xfree(layout->fields);
446
+ st_free_table(layout->fieldSymbolTable);
445
447
  xfree(layout);
446
448
  }
447
449
 
@@ -455,6 +457,15 @@ rbffi_StructLayout_Init(VALUE moduleFFI)
455
457
  rbffi_StructLayoutFieldClass = rb_define_class_under(rbffi_StructLayoutClass, "Field", rb_cObject);
456
458
  rb_global_variable(&rbffi_StructLayoutFieldClass);
457
459
 
460
+ rbffi_StructLayoutNumberFieldClass = rb_define_class_under(rbffi_StructLayoutClass, "Number", rbffi_StructLayoutFieldClass);
461
+ rb_global_variable(&rbffi_StructLayoutNumberFieldClass);
462
+
463
+ rbffi_StructLayoutStringFieldClass = rb_define_class_under(rbffi_StructLayoutClass, "String", rbffi_StructLayoutFieldClass);
464
+ rb_global_variable(&rbffi_StructLayoutStringFieldClass);
465
+
466
+ rbffi_StructLayoutPointerFieldClass = rb_define_class_under(rbffi_StructLayoutClass, "Pointer", rbffi_StructLayoutFieldClass);
467
+ rb_global_variable(&rbffi_StructLayoutPointerFieldClass);
468
+
458
469
  rbffi_StructLayoutFunctionFieldClass = rb_define_class_under(rbffi_StructLayoutClass, "Function", rbffi_StructLayoutFieldClass);
459
470
  rb_global_variable(&rbffi_StructLayoutFunctionFieldClass);
460
471
 
@@ -478,7 +489,7 @@ rbffi_StructLayout_Init(VALUE moduleFFI)
478
489
  rb_define_method(rbffi_StructLayoutArrayFieldClass, "put", array_field_put, 2);
479
490
 
480
491
  rb_define_alloc_func(rbffi_StructLayoutClass, struct_layout_allocate);
481
- rb_define_method(rbffi_StructLayoutClass, "initialize", struct_layout_initialize, 4);
492
+ rb_define_method(rbffi_StructLayoutClass, "initialize", struct_layout_initialize, 3);
482
493
  rb_define_method(rbffi_StructLayoutClass, "[]", struct_layout_aref, 1);
483
494
  rb_define_method(rbffi_StructLayoutClass, "fields", struct_layout_fields, 0);
484
495
  rb_define_method(rbffi_StructLayoutClass, "members", struct_layout_members, 0);
@@ -1,28 +1,19 @@
1
1
  /*
2
2
  * Copyright (c) 2009, Wayne Meissner
3
- * All rights reserved.
4
3
  *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
4
+ * This file is part of ruby-ffi.
7
5
  *
8
- * * Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- * * Redistributions in binary form must reproduce the above copyright notice
11
- * this list of conditions and the following disclaimer in the documentation
12
- * and/or other materials provided with the distribution.
13
- * * The name of the author or authors may not be used to endorse or promote
14
- * products derived from this software without specific prior written permission.
6
+ * This code is free software: you can redistribute it and/or modify it under
7
+ * the terms of the GNU Lesser General Public License version 3 only, as
8
+ * published by the Free Software Foundation.
15
9
  *
16
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10
+ * This code is distributed in the hope that it will be useful, but WITHOUT
11
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13
+ * version 3 for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
26
17
  */
27
18
 
28
19
  #include <sys/param.h>
@@ -42,7 +33,7 @@ typedef struct BuiltinType_ {
42
33
 
43
34
  static void builtin_type_free(BuiltinType *);
44
35
 
45
- VALUE rbffi_TypeClass = Qnil, rbffi_EnumTypeClass = Qnil;
36
+ VALUE rbffi_TypeClass = Qnil;
46
37
 
47
38
  static VALUE classBuiltinType = Qnil;
48
39
  static VALUE typeMap = Qnil, sizeMap = Qnil;
@@ -104,41 +95,12 @@ type_alignment(VALUE self)
104
95
  static VALUE
105
96
  type_inspect(VALUE self)
106
97
  {
107
- char buf[256];
108
98
  Type *type;
109
99
 
110
100
  Data_Get_Struct(self, Type, type);
111
- snprintf(buf, sizeof(buf), "#<FFI::Type:%p size=%d alignment=%d>",
112
- type, (int) type->ffiType->size, (int) type->ffiType->alignment);
113
-
114
- return rb_str_new2(buf);
115
- }
116
-
117
- static VALUE
118
- enum_allocate(VALUE klass)
119
- {
120
- Type* type;
121
- VALUE obj;
122
-
123
- obj = Data_Make_Struct(klass, Type, NULL, -1, type);
124
- type->nativeType = NATIVE_ENUM;
125
- type->ffiType = &ffi_type_sint;
126
-
127
- return obj;
128
- }
129
-
130
- int
131
- rbffi_Type_GetIntValue(VALUE type)
132
- {
133
- Type* t;
134
-
135
- if (!rb_obj_is_kind_of(type, rbffi_TypeClass)) {
136
- rb_raise(rb_eTypeError, "wrong type. Expected (FFI::Type)");
137
- }
138
-
139
- Data_Get_Struct(type, Type, t);
140
101
 
141
- return t->nativeType;
102
+ return rb_sprintf("#<%s:%p size=%d alignment=%d>",
103
+ rb_obj_classname(self), type, (int) type->ffiType->size, (int) type->ffiType->alignment);
142
104
  }
143
105
 
144
106
  static VALUE
@@ -166,14 +128,11 @@ builtin_type_free(BuiltinType *type)
166
128
  static VALUE
167
129
  builtin_type_inspect(VALUE self)
168
130
  {
169
- char buf[256];
170
131
  BuiltinType *type;
171
132
 
172
133
  Data_Get_Struct(self, BuiltinType, type);
173
- snprintf(buf, sizeof(buf), "#<FFI::Type::Builtin:%s size=%d alignment=%d>",
174
- type->name, (int) type->type.ffiType->size, type->type.ffiType->alignment);
175
-
176
- return rb_str_new2(buf);
134
+ return rb_sprintf("#<%s:%s size=%d alignment=%d>",
135
+ rb_obj_classname(self), type->name, (int) type->type.ffiType->size, type->type.ffiType->alignment);
177
136
  }
178
137
 
179
138
  int
@@ -231,8 +190,7 @@ rbffi_Type_Find(VALUE name)
231
190
  VALUE rbType = rbffi_Type_Lookup(name);
232
191
 
233
192
  if (!RTEST(rbType)) {
234
- VALUE typeName = rb_funcall2(name, rb_intern("inspect"), 0, NULL);
235
- rb_raise(rb_eTypeError, "Invalid type (%s)", RSTRING_PTR(typeName));
193
+ rb_raise(rb_eTypeError, "invalid type, %s", RSTRING_PTR(rb_inspect(name)));
236
194
  }
237
195
 
238
196
  return rbType;
@@ -243,7 +201,6 @@ rbffi_Type_Init(VALUE moduleFFI)
243
201
  {
244
202
  VALUE moduleNativeType;
245
203
  VALUE classType = rbffi_TypeClass = rb_define_class_under(moduleFFI, "Type", rb_cObject);
246
- VALUE classEnum = rbffi_EnumTypeClass = rb_define_class_under(moduleFFI, "Enum", classType);
247
204
 
248
205
  rb_define_const(moduleFFI, "TypeDefs", typeMap = rb_hash_new());
249
206
  rb_define_const(moduleFFI, "SizeTypes", sizeMap = rb_hash_new());
@@ -270,9 +227,7 @@ rbffi_Type_Init(VALUE moduleFFI)
270
227
  // Make Type::Builtin non-allocatable
271
228
  rb_undef_method(CLASS_OF(classBuiltinType), "new");
272
229
  rb_define_method(classBuiltinType, "inspect", builtin_type_inspect, 0);
273
-
274
- rb_define_alloc_func(classEnum, enum_allocate);
275
-
230
+
276
231
  rb_global_variable(&rbffi_TypeClass);
277
232
  rb_global_variable(&classBuiltinType);
278
233
 
@@ -284,29 +239,45 @@ rbffi_Type_Init(VALUE moduleFFI)
284
239
  rb_define_const(moduleFFI, "TYPE_" #x, t); \
285
240
  } while(0)
286
241
 
242
+ #define A(old_type, new_type) do { \
243
+ VALUE t = rb_const_get(classType, rb_intern(#old_type)); \
244
+ rb_const_set(classType, rb_intern(#new_type), t); \
245
+ } while(0)
246
+
287
247
  T(VOID, &ffi_type_void);
288
248
  T(INT8, &ffi_type_sint8);
249
+ A(INT8, SCHAR);
250
+ A(INT8, CHAR);
289
251
  T(UINT8, &ffi_type_uint8);
252
+ A(UINT8, UCHAR);
253
+
290
254
  T(INT16, &ffi_type_sint16);
255
+ A(INT16, SHORT);
256
+ A(INT16, SSHORT);
291
257
  T(UINT16, &ffi_type_uint16);
258
+ A(UINT16, USHORT);
292
259
  T(INT32, &ffi_type_sint32);
260
+ A(INT32, INT);
261
+ A(INT32, SINT);
293
262
  T(UINT32, &ffi_type_uint32);
263
+ A(UINT32, UINT);
294
264
  T(INT64, &ffi_type_sint64);
265
+ A(INT64, LONG_LONG);
266
+ A(INT64, SLONG_LONG);
295
267
  T(UINT64, &ffi_type_uint64);
268
+ A(UINT64, ULONG_LONG);
296
269
  T(LONG, &ffi_type_slong);
270
+ A(LONG, SLONG);
297
271
  T(ULONG, &ffi_type_ulong);
298
272
  T(FLOAT32, &ffi_type_float);
273
+ A(FLOAT32, FLOAT);
299
274
  T(FLOAT64, &ffi_type_double);
275
+ A(FLOAT64, DOUBLE);
300
276
  T(POINTER, &ffi_type_pointer);
301
277
  T(STRING, &ffi_type_pointer);
302
- T(RBXSTRING, &ffi_type_pointer);
303
278
  T(BUFFER_IN, &ffi_type_pointer);
304
279
  T(BUFFER_OUT, &ffi_type_pointer);
305
280
  T(BUFFER_INOUT, &ffi_type_pointer);
306
- T(ENUM, &ffi_type_sint);
307
281
  T(BOOL, &ffi_type_uchar);
308
-
309
-
310
- T(CHAR_ARRAY, &ffi_type_void);
311
282
  T(VARARGS, &ffi_type_void);
312
283
  }