ffi 1.9.3 → 1.9.5

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 (86) hide show
  1. checksums.yaml +6 -14
  2. data/Rakefile +12 -15
  3. data/ext/ffi_c/DynamicLibrary.c +9 -5
  4. data/ext/ffi_c/DynamicLibrary.h +49 -0
  5. data/ext/ffi_c/Function.c +4 -3
  6. data/ext/ffi_c/FunctionInfo.c +2 -2
  7. data/ext/ffi_c/Platform.c +2 -2
  8. data/ext/ffi_c/Struct.c +1 -0
  9. data/ext/ffi_c/Variadic.c +2 -2
  10. data/ffi.gemspec +5 -5
  11. data/lib/ffi/library.rb +7 -7
  12. data/lib/ffi/platform.rb +4 -1
  13. data/lib/ffi/platform/x86_64-netbsd/types.conf +2 -0
  14. data/lib/ffi/struct.rb +2 -2
  15. data/lib/ffi/version.rb +1 -1
  16. data/libtest/ClosureTest.c +15 -0
  17. data/libtest/FunctionTest.c +12 -0
  18. data/spec/ffi/LICENSE.SPECS +22 -0
  19. data/spec/ffi/async_callback_spec.rb +1 -0
  20. data/spec/ffi/bool_spec.rb +2 -0
  21. data/spec/ffi/buffer_spec.rb +1 -0
  22. data/spec/ffi/callback_spec.rb +31 -4
  23. data/spec/ffi/custom_param_type.rb +1 -0
  24. data/spec/ffi/custom_type_spec.rb +1 -0
  25. data/spec/ffi/dup_spec.rb +1 -1
  26. data/spec/ffi/enum_spec.rb +1 -0
  27. data/spec/ffi/errno_spec.rb +2 -0
  28. data/spec/ffi/ffi_spec.rb +1 -0
  29. data/spec/ffi/fixtures/Benchmark.c +52 -0
  30. data/spec/ffi/fixtures/Benchmark.o +0 -0
  31. data/spec/ffi/fixtures/BoolTest.c +34 -0
  32. data/spec/ffi/fixtures/BoolTest.o +0 -0
  33. data/spec/ffi/fixtures/BufferTest.c +31 -0
  34. data/spec/ffi/fixtures/BufferTest.o +0 -0
  35. data/spec/ffi/fixtures/ClosureTest.c +190 -0
  36. data/spec/ffi/fixtures/ClosureTest.o +0 -0
  37. data/spec/ffi/fixtures/EnumTest.c +34 -0
  38. data/spec/ffi/fixtures/EnumTest.o +0 -0
  39. data/spec/ffi/fixtures/FunctionTest.c +58 -0
  40. data/spec/ffi/fixtures/FunctionTest.o +0 -0
  41. data/spec/ffi/fixtures/GNUmakefile +149 -0
  42. data/spec/ffi/fixtures/GlobalVariable.c +62 -0
  43. data/spec/ffi/fixtures/GlobalVariable.o +0 -0
  44. data/spec/ffi/fixtures/LastErrorTest.c +21 -0
  45. data/spec/ffi/fixtures/LastErrorTest.o +0 -0
  46. data/spec/ffi/fixtures/NumberTest.c +132 -0
  47. data/spec/ffi/fixtures/NumberTest.o +0 -0
  48. data/spec/ffi/fixtures/PointerTest.c +63 -0
  49. data/spec/ffi/fixtures/PointerTest.o +0 -0
  50. data/spec/ffi/fixtures/ReferenceTest.c +23 -0
  51. data/spec/ffi/fixtures/ReferenceTest.o +0 -0
  52. data/spec/ffi/fixtures/StringTest.c +34 -0
  53. data/spec/ffi/fixtures/StringTest.o +0 -0
  54. data/spec/ffi/fixtures/StructTest.c +243 -0
  55. data/spec/ffi/fixtures/StructTest.o +0 -0
  56. data/spec/ffi/fixtures/UnionTest.c +43 -0
  57. data/spec/ffi/fixtures/UnionTest.o +0 -0
  58. data/spec/ffi/fixtures/VariadicTest.c +99 -0
  59. data/spec/ffi/fixtures/VariadicTest.o +0 -0
  60. data/spec/ffi/fixtures/classes.rb +438 -0
  61. data/spec/ffi/function_spec.rb +4 -2
  62. data/spec/ffi/io_spec.rb +17 -0
  63. data/spec/ffi/library_spec.rb +25 -0
  64. data/spec/ffi/long_double.rb +1 -0
  65. data/spec/ffi/managed_struct_spec.rb +1 -1
  66. data/spec/ffi/memorypointer_spec.rb +72 -0
  67. data/spec/ffi/number_spec.rb +2 -0
  68. data/spec/ffi/platform_spec.rb +97 -0
  69. data/spec/ffi/pointer_spec.rb +8 -2
  70. data/spec/ffi/rbx/attach_function_spec.rb +6 -0
  71. data/spec/ffi/rbx/memory_pointer_spec.rb +7 -2
  72. data/spec/ffi/rbx/spec_helper.rb +7 -1
  73. data/spec/ffi/rbx/struct_spec.rb +7 -1
  74. data/spec/ffi/spec_helper.rb +68 -9
  75. data/spec/ffi/string_spec.rb +3 -1
  76. data/spec/ffi/strptr_spec.rb +1 -0
  77. data/spec/ffi/struct_by_ref_spec.rb +4 -3
  78. data/spec/ffi/struct_callback_spec.rb +2 -1
  79. data/spec/ffi/struct_initialize_spec.rb +1 -0
  80. data/spec/ffi/struct_packed_spec.rb +1 -0
  81. data/spec/ffi/struct_spec.rb +99 -39
  82. data/spec/ffi/typedef_spec.rb +15 -3
  83. data/spec/ffi/union_spec.rb +1 -0
  84. data/spec/ffi/variadic_spec.rb +18 -0
  85. data/spec/spec.opts +1 -2
  86. metadata +129 -94
Binary file
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * For licensing, see LICENSE.SPECS
5
+ */
6
+
7
+ #include <sys/types.h>
8
+ #include <sys/param.h>
9
+ #include <stdint.h>
10
+ #include <stdio.h>
11
+ #include <stdlib.h>
12
+ #include <string.h>
13
+ typedef void* ptr;
14
+ typedef void* pointer;
15
+ #ifdef _WIN32
16
+ typedef char* caddr_t;
17
+ #endif
18
+
19
+ #define RET(T) T ptr_ret_##T(void* arg1, int offset) { \
20
+ T tmp; memcpy(&tmp, (caddr_t) arg1 + offset, sizeof(tmp)); return tmp; \
21
+ }
22
+ #define SET(T) void ptr_set_##T(void* arg1, int offset, T value) { \
23
+ memcpy((caddr_t) arg1 + offset, &value, sizeof(value)); \
24
+ }
25
+ #define TEST(T) SET(T) RET(T)
26
+
27
+ TEST(int8_t);
28
+ TEST(int16_t);
29
+ TEST(int32_t);
30
+ TEST(int64_t);
31
+ TEST(float);
32
+ TEST(double);
33
+ TEST(pointer);
34
+
35
+ void*
36
+ ptr_return_array_element(void **ptrArray, int arrayIndex)
37
+ {
38
+ return ptrArray[arrayIndex];
39
+ }
40
+
41
+ void
42
+ ptr_set_array_element(void **ptrArray, int arrayIndex, void *value)
43
+ {
44
+ ptrArray[arrayIndex] = value;
45
+ }
46
+
47
+ void*
48
+ ptr_malloc(int size)
49
+ {
50
+ return calloc(1, size);
51
+ }
52
+ void
53
+ ptr_free(void* ptr)
54
+ {
55
+ free(ptr);
56
+ }
57
+
58
+ void*
59
+ ptr_from_address(uintptr_t addr)
60
+ {
61
+ return (void *) addr;
62
+ }
63
+
Binary file
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * For licensing, see LICENSE.SPECS
5
+ */
6
+
7
+ #include <stdint.h>
8
+
9
+ #define REF(T) void ref_##T(T arg, T* result) { *result = arg; }
10
+ #define ADD(T) void ref_add_##T(T arg1, T arg2, T* result) { *result = arg1 + arg2; }
11
+ #define SUB(T) void ref_sub_##T(T arg1, T arg2, T* result) { *result = arg1 - arg2; }
12
+ #define MUL(T) void ref_mul_##T(T arg1, T arg2, T* result) { *result = arg1 * arg2; }
13
+ #define DIV(T) void ref_div_##T(T arg1, T arg2, T* result) { *result = arg1 / arg2; }
14
+ #define TEST(T) ADD(T) SUB(T) MUL(T) DIV(T) REF(T)
15
+
16
+ TEST(int8_t);
17
+ TEST(int16_t);
18
+ TEST(int32_t);
19
+ TEST(int64_t);
20
+ TEST(float);
21
+ TEST(double);
22
+
23
+
Binary file
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * For licensing, see LICENSE.SPECS
5
+ */
6
+
7
+ #include <string.h>
8
+
9
+ int
10
+ string_equals(const char* s1, const char* s2)
11
+ {
12
+ return strcmp(s1, s2) == 0;
13
+ }
14
+
15
+ void
16
+ string_set(char* s1, const char* s2)
17
+ {
18
+ strcpy(s1, s2);
19
+ }
20
+ void
21
+ string_concat(char* dst, const char* src)
22
+ {
23
+ strcat(dst, src);
24
+ }
25
+ void
26
+ string_dummy(char* dummy)
27
+ {
28
+ }
29
+ const char*
30
+ string_null(void)
31
+ {
32
+ return NULL;
33
+ }
34
+
Binary file
@@ -0,0 +1,243 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner.
3
+ * Copyright (c) 2009 Andrea Fazzi <andrea.fazzi@alcacoop.it>.
4
+ *
5
+ * All rights reserved.
6
+ *
7
+ * For licensing, see LICENSE.SPECS
8
+ */
9
+
10
+ #include <stdio.h>
11
+ #include <stdlib.h>
12
+ #include <stdbool.h>
13
+ #include <stdint.h>
14
+ #include <string.h>
15
+ #include <stdarg.h>
16
+
17
+ typedef char s8;
18
+ typedef short s16;
19
+ typedef int s32;
20
+ typedef long long s64;
21
+ typedef float f32;
22
+ typedef double f64;
23
+
24
+ typedef struct bugged_struct {
25
+ unsigned char visible;
26
+ unsigned int x;
27
+ unsigned int y;
28
+ short rx;
29
+ short ry;
30
+ unsigned char order;
31
+ unsigned char size;
32
+ } bugged_struct_t;
33
+
34
+ unsigned int
35
+ bugged_struct_size() {
36
+ return sizeof(bugged_struct_t);
37
+ }
38
+
39
+ struct test1 {
40
+ char b;
41
+ short s;
42
+ int i;
43
+ long long j;
44
+ long l;
45
+ float f;
46
+ double d;
47
+ char string[32];
48
+ };
49
+
50
+ struct struct_with_array {
51
+ char c;
52
+ int a[5];
53
+ };
54
+
55
+ struct nested {
56
+ int i;
57
+ };
58
+
59
+ struct container {
60
+ char first;
61
+ struct nested s;
62
+ };
63
+
64
+ int
65
+ struct_align_nested_struct(struct container* a) { return a->s.i; }
66
+
67
+ void*
68
+ struct_field_array(struct struct_with_array* s) { return &s->a; }
69
+
70
+ struct container*
71
+ struct_make_container_struct(int i)
72
+ {
73
+ static struct container cs;
74
+ memset(&cs, 0, sizeof(cs));
75
+ cs.first = 1;
76
+ cs.s.i = i;
77
+ return &cs;
78
+ }
79
+
80
+ #define T(x, type) \
81
+ type struct_field_##type(struct test1* t) { return t->x; } \
82
+ struct type##_align { char first; type value; }; \
83
+ type struct_align_##type(struct type##_align* a) { return a->value; }
84
+
85
+ T(b, s8);
86
+ T(s, s16);
87
+ T(i, s32);
88
+ T(j, s64);
89
+ T(f, f32);
90
+ T(d, f64);
91
+ T(l, long);
92
+
93
+ void
94
+ struct_set_string(struct test1* t, char* s)
95
+ {
96
+ strcpy(t->string, s);
97
+ }
98
+
99
+ struct test1*
100
+ struct_make_struct(char b, short s, int i, long long ll, float f, double d)
101
+ {
102
+ static struct test1 t;
103
+ memset(&t, 0, sizeof(t));
104
+ t.b = b;
105
+ t.s = s;
106
+ t.i = i;
107
+ t.j = ll;
108
+ t.f = f;
109
+ t.d = d;
110
+ return &t;
111
+ }
112
+
113
+ typedef int (*add_cb)(int a1, int a2);
114
+ typedef int (*sub_cb)(int a1, int a2);
115
+ struct test2 {
116
+ add_cb add_callback;
117
+ sub_cb sub_callback;
118
+ };
119
+
120
+ int
121
+ struct_call_add_cb(struct test2* t, int a1, int a2)
122
+ {
123
+ return t->add_callback(a1, a2);
124
+ }
125
+
126
+ int
127
+ struct_call_sub_cb(struct test2* t, int a1, int a2)
128
+ {
129
+ return t->sub_callback(a1, a2);
130
+ }
131
+
132
+
133
+ struct struct_with_array*
134
+ struct_make_struct_with_array(int a_0, int a_1, int a_2, int a_3, int a_4)
135
+ {
136
+ static struct struct_with_array s;
137
+
138
+ memset(&s, 0, sizeof(s));
139
+
140
+ s.a[0] = a_0;
141
+ s.a[1] = a_1;
142
+ s.a[2] = a_2;
143
+ s.a[3] = a_3;
144
+ s.a[4] = a_4;
145
+
146
+ return &s;
147
+
148
+ }
149
+
150
+ struct s8s32 {
151
+ char s8;
152
+ int s32;
153
+ };
154
+
155
+ struct s8s32
156
+ struct_return_s8s32()
157
+ {
158
+ struct s8s32 s;
159
+ s.s8 = 0x7f;
160
+ s.s32 = 0x12345678;
161
+
162
+ return s;
163
+ }
164
+
165
+ struct s8s32
166
+ struct_s8s32_set(char s8, int s32)
167
+ {
168
+ struct s8s32 s;
169
+
170
+ s.s8 = s8;
171
+ s.s32 = s32;
172
+
173
+ return s;
174
+ }
175
+
176
+ int
177
+ struct_s8s32_get_s8(struct s8s32 s)
178
+ {
179
+ return s.s8;
180
+ }
181
+
182
+ int
183
+ struct_s8s32_get_s32(struct s8s32 s)
184
+ {
185
+ return s.s32;
186
+ }
187
+
188
+ struct s8s32
189
+ struct_s8s32_ret_s8s32(struct s8s32 s)
190
+ {
191
+ return s;
192
+ }
193
+
194
+ // Pass a struct and an int arg, ensure the int arg is passed correctly
195
+ int
196
+ struct_s8s32_s32_ret_s32(struct s8s32 s, int s32)
197
+ {
198
+ return s32;
199
+ }
200
+
201
+ // Pass a struct and a long long arg, ensure the long long arg is passed correctly
202
+ long long
203
+ struct_s8s32_s64_ret_s64(struct s8s32 s, long long s64)
204
+ {
205
+ return s64;
206
+ }
207
+
208
+ // Pass a struct and a long long arg, ensure the long long arg is passed correctly
209
+ int
210
+ struct_s32_ptr_s32_s8s32_ret_s32(int s32a, void *ptr, int s32b, struct s8s32 s)
211
+ {
212
+ if (ptr != NULL) *(struct s8s32 *) ptr = s;
213
+ return s.s32;
214
+ }
215
+
216
+ // Pass a char *, copy into buffer length struct
217
+ struct struct_string {
218
+ char *bytes;
219
+ int len;
220
+ };
221
+
222
+ struct struct_string
223
+ struct_varargs_ret_struct_string(int len, ...)
224
+ {
225
+ struct struct_string ss;
226
+ va_list vl;
227
+ char* cp = NULL;
228
+
229
+ va_start(vl, len);
230
+
231
+ ss.len = len;
232
+ ss.bytes = va_arg(vl, char *);
233
+ if (ss.bytes != NULL) {
234
+ cp = malloc(strlen(ss.bytes) + 1);
235
+ strcpy(cp, ss.bytes);
236
+ ss.bytes = cp;
237
+ }
238
+
239
+ va_end(vl);
240
+
241
+ return ss;
242
+ }
243
+
Binary file
@@ -0,0 +1,43 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * For licensing, see LICENSE.SPECS
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include <stdbool.h>
9
+ #include <stdint.h>
10
+ #include <string.h>
11
+ #include <stdlib.h>
12
+
13
+ typedef char s8;
14
+ typedef short s16;
15
+ typedef int s32;
16
+ typedef long long s64;
17
+ typedef float f32;
18
+ typedef double f64;
19
+
20
+ typedef union union_test {
21
+ char b;
22
+ short s;
23
+ int i;
24
+ long long j;
25
+ long l;
26
+ float f;
27
+ double d;
28
+ s8 a[10];
29
+ } union_test_t;
30
+
31
+ #define T(x, type) \
32
+ type union_align_##type(union_test_t* u) { return u->x; } \
33
+ union_test_t* union_make_union_with_##type(type value) { static union_test_t u; u.x = value; return &u; }
34
+
35
+ T(b, s8);
36
+ T(s, s16);
37
+ T(i, s32);
38
+ T(j, s64);
39
+ T(f, f32);
40
+ T(d, f64);
41
+ T(l, long);
42
+
43
+ unsigned int union_size() { return sizeof(union_test_t); }
Binary file
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * For licensing, see LICENSE.SPECS
5
+ */
6
+
7
+ #include <sys/types.h>
8
+ #include <stdio.h>
9
+ #include <stdint.h>
10
+ #include <string.h>
11
+ #include <stdarg.h>
12
+
13
+ typedef int8_t s8;
14
+ typedef uint8_t u8;
15
+ typedef int16_t s16;
16
+ typedef uint16_t u16;
17
+ typedef int32_t s32;
18
+ typedef uint32_t u32;
19
+ typedef int64_t s64;
20
+ typedef uint64_t u64;
21
+ typedef signed long sL;
22
+ typedef unsigned long uL;
23
+ typedef float F;
24
+ typedef double D;
25
+
26
+ void pack_varargs(s64* buf, const char* fmt, ...)
27
+ {
28
+ va_list ap;
29
+ int c;
30
+ double d;
31
+ va_start(ap, fmt);
32
+ while ((c = *fmt++)) {
33
+ switch (c) {
34
+ case 'c':
35
+ case 's':
36
+ case 'i':
37
+ *buf++ = va_arg(ap, s32);
38
+ break;
39
+ case 'l':
40
+ *buf++ = va_arg(ap, long);
41
+ break;
42
+ case 'j':
43
+ *buf++ = va_arg(ap, s64);
44
+ break;
45
+ case 'f':
46
+ case 'd':
47
+ d = va_arg(ap, double);
48
+ memcpy(buf++, &d, sizeof(d));
49
+ break;
50
+ case 'C':
51
+ case 'S':
52
+ case 'I':
53
+ *buf++ = va_arg(ap, u32);
54
+ break;
55
+ case 'L':
56
+ *buf++ = va_arg(ap, unsigned long);
57
+ break;
58
+ }
59
+ }
60
+ va_end(ap);
61
+ }
62
+
63
+ int pack_varargs2(s64* buf, int retval, const char* fmt, ...)
64
+ {
65
+ va_list ap;
66
+ int c;
67
+ double d;
68
+ va_start(ap, fmt);
69
+ while ((c = *fmt++)) {
70
+ switch (c) {
71
+ case 'c':
72
+ case 's':
73
+ case 'i':
74
+ *buf++ = va_arg(ap, s32);
75
+ break;
76
+ case 'l':
77
+ *buf++ = va_arg(ap, long);
78
+ break;
79
+ case 'j':
80
+ *buf++ = va_arg(ap, s64);
81
+ break;
82
+ case 'f':
83
+ case 'd':
84
+ d = va_arg(ap, double);
85
+ memcpy(buf++, &d, sizeof(d));
86
+ break;
87
+ case 'C':
88
+ case 'S':
89
+ case 'I':
90
+ *buf++ = va_arg(ap, u32);
91
+ break;
92
+ case 'L':
93
+ *buf++ = va_arg(ap, unsigned long);
94
+ break;
95
+ }
96
+ }
97
+ va_end(ap);
98
+ return retval + 1;
99
+ }