alinta-ffi 1.9.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +49 -0
- data/LICENSE +24 -0
- data/README.md +112 -0
- data/Rakefile +243 -0
- data/ext/ffi_c/AbstractMemory.c +1109 -0
- data/ext/ffi_c/AbstractMemory.h +175 -0
- data/ext/ffi_c/ArrayType.c +162 -0
- data/ext/ffi_c/ArrayType.h +59 -0
- data/ext/ffi_c/Buffer.c +365 -0
- data/ext/ffi_c/Call.c +517 -0
- data/ext/ffi_c/Call.h +110 -0
- data/ext/ffi_c/ClosurePool.c +283 -0
- data/ext/ffi_c/ClosurePool.h +57 -0
- data/ext/ffi_c/DataConverter.c +91 -0
- data/ext/ffi_c/DynamicLibrary.c +339 -0
- data/ext/ffi_c/DynamicLibrary.h +98 -0
- data/ext/ffi_c/Function.c +998 -0
- data/ext/ffi_c/Function.h +87 -0
- data/ext/ffi_c/FunctionInfo.c +271 -0
- data/ext/ffi_c/LastError.c +184 -0
- data/ext/ffi_c/LastError.h +47 -0
- data/ext/ffi_c/LongDouble.c +63 -0
- data/ext/ffi_c/LongDouble.h +51 -0
- data/ext/ffi_c/MappedType.c +168 -0
- data/ext/ffi_c/MappedType.h +59 -0
- data/ext/ffi_c/MemoryPointer.c +197 -0
- data/ext/ffi_c/MemoryPointer.h +53 -0
- data/ext/ffi_c/MethodHandle.c +358 -0
- data/ext/ffi_c/MethodHandle.h +55 -0
- data/ext/ffi_c/Platform.c +129 -0
- data/ext/ffi_c/Platform.h +45 -0
- data/ext/ffi_c/Pointer.c +508 -0
- data/ext/ffi_c/Pointer.h +63 -0
- data/ext/ffi_c/Struct.c +829 -0
- data/ext/ffi_c/Struct.h +106 -0
- data/ext/ffi_c/StructByReference.c +190 -0
- data/ext/ffi_c/StructByReference.h +50 -0
- data/ext/ffi_c/StructByValue.c +150 -0
- data/ext/ffi_c/StructByValue.h +55 -0
- data/ext/ffi_c/StructLayout.c +698 -0
- data/ext/ffi_c/Thread.c +352 -0
- data/ext/ffi_c/Thread.h +95 -0
- data/ext/ffi_c/Type.c +397 -0
- data/ext/ffi_c/Type.h +62 -0
- data/ext/ffi_c/Types.c +139 -0
- data/ext/ffi_c/Types.h +89 -0
- data/ext/ffi_c/Variadic.c +304 -0
- data/ext/ffi_c/compat.h +78 -0
- data/ext/ffi_c/extconf.rb +71 -0
- data/ext/ffi_c/ffi.c +98 -0
- data/ext/ffi_c/libffi.bsd.mk +40 -0
- data/ext/ffi_c/libffi.darwin.mk +105 -0
- data/ext/ffi_c/libffi.gnu.mk +32 -0
- data/ext/ffi_c/libffi.mk +18 -0
- data/ext/ffi_c/libffi.vc.mk +26 -0
- data/ext/ffi_c/libffi.vc64.mk +26 -0
- data/ext/ffi_c/rbffi.h +57 -0
- data/ext/ffi_c/rbffi_endian.h +59 -0
- data/ext/ffi_c/win32/stdbool.h +8 -0
- data/ext/ffi_c/win32/stdint.h +201 -0
- data/ffi.gemspec +23 -0
- data/gen/Rakefile +30 -0
- data/lib/ffi.rb +20 -0
- data/lib/ffi/autopointer.rb +203 -0
- data/lib/ffi/buffer.rb +4 -0
- data/lib/ffi/callback.rb +4 -0
- data/lib/ffi/enum.rb +296 -0
- data/lib/ffi/errno.rb +43 -0
- data/lib/ffi/ffi.rb +44 -0
- data/lib/ffi/io.rb +62 -0
- data/lib/ffi/library.rb +590 -0
- data/lib/ffi/managedstruct.rb +84 -0
- data/lib/ffi/memorypointer.rb +1 -0
- data/lib/ffi/platform.rb +164 -0
- data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
- data/lib/ffi/platform/arm-linux/types.conf +104 -0
- data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
- data/lib/ffi/platform/i386-darwin/types.conf +100 -0
- data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
- data/lib/ffi/platform/i386-gnu/types.conf +107 -0
- data/lib/ffi/platform/i386-linux/types.conf +103 -0
- data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
- data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
- data/lib/ffi/platform/i386-solaris/types.conf +122 -0
- data/lib/ffi/platform/i386-windows/types.conf +105 -0
- data/lib/ffi/platform/ia64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips-linux/types.conf +102 -0
- data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
- data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
- data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
- data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
- data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
- data/lib/ffi/platform/s390-linux/types.conf +102 -0
- data/lib/ffi/platform/s390x-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
- data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
- data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
- data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
- data/lib/ffi/platform/x86_64-darwin/types.conf +126 -0
- data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-linux/types.conf +102 -0
- data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
- data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
- data/lib/ffi/platform/x86_64-windows/types.conf +120 -0
- data/lib/ffi/pointer.rb +161 -0
- data/lib/ffi/struct.rb +371 -0
- data/lib/ffi/struct_layout_builder.rb +227 -0
- data/lib/ffi/tools/const_generator.rb +229 -0
- data/lib/ffi/tools/generator.rb +60 -0
- data/lib/ffi/tools/generator_task.rb +36 -0
- data/lib/ffi/tools/struct_generator.rb +194 -0
- data/lib/ffi/tools/types_generator.rb +134 -0
- data/lib/ffi/types.rb +194 -0
- data/lib/ffi/union.rb +43 -0
- data/lib/ffi/variadic.rb +78 -0
- data/lib/ffi/version.rb +4 -0
- data/libtest/Benchmark.c +52 -0
- data/libtest/BoolTest.c +34 -0
- data/libtest/BufferTest.c +31 -0
- data/libtest/ClosureTest.c +205 -0
- data/libtest/EnumTest.c +51 -0
- data/libtest/FunctionTest.c +70 -0
- data/libtest/GNUmakefile +149 -0
- data/libtest/GlobalVariable.c +62 -0
- data/libtest/LastErrorTest.c +21 -0
- data/libtest/NumberTest.c +132 -0
- data/libtest/PointerTest.c +63 -0
- data/libtest/ReferenceTest.c +23 -0
- data/libtest/StringTest.c +34 -0
- data/libtest/StructTest.c +243 -0
- data/libtest/UnionTest.c +43 -0
- data/libtest/VariadicTest.c +99 -0
- data/spec/ffi/LICENSE.SPECS +22 -0
- data/spec/ffi/async_callback_spec.rb +35 -0
- data/spec/ffi/bitmask_spec.rb +575 -0
- data/spec/ffi/bool_spec.rb +32 -0
- data/spec/ffi/buffer_spec.rb +279 -0
- data/spec/ffi/callback_spec.rb +773 -0
- data/spec/ffi/custom_param_type.rb +37 -0
- data/spec/ffi/custom_type_spec.rb +74 -0
- data/spec/ffi/dup_spec.rb +52 -0
- data/spec/ffi/enum_spec.rb +423 -0
- data/spec/ffi/errno_spec.rb +20 -0
- data/spec/ffi/ffi_spec.rb +28 -0
- data/spec/ffi/fixtures/Benchmark.c +52 -0
- data/spec/ffi/fixtures/BitmaskTest.c +51 -0
- data/spec/ffi/fixtures/BoolTest.c +34 -0
- data/spec/ffi/fixtures/BufferTest.c +31 -0
- data/spec/ffi/fixtures/ClosureTest.c +205 -0
- data/spec/ffi/fixtures/EnumTest.c +51 -0
- data/spec/ffi/fixtures/FunctionTest.c +142 -0
- data/spec/ffi/fixtures/GNUmakefile +149 -0
- data/spec/ffi/fixtures/GlobalVariable.c +62 -0
- data/spec/ffi/fixtures/LastErrorTest.c +21 -0
- data/spec/ffi/fixtures/NumberTest.c +132 -0
- data/spec/ffi/fixtures/PipeHelper.h +21 -0
- data/spec/ffi/fixtures/PipeHelperPosix.c +41 -0
- data/spec/ffi/fixtures/PipeHelperWindows.c +72 -0
- data/spec/ffi/fixtures/PointerTest.c +63 -0
- data/spec/ffi/fixtures/ReferenceTest.c +23 -0
- data/spec/ffi/fixtures/StringTest.c +34 -0
- data/spec/ffi/fixtures/StructTest.c +243 -0
- data/spec/ffi/fixtures/UnionTest.c +43 -0
- data/spec/ffi/fixtures/VariadicTest.c +99 -0
- data/spec/ffi/fixtures/classes.rb +438 -0
- data/spec/ffi/function_spec.rb +97 -0
- data/spec/ffi/io_spec.rb +16 -0
- data/spec/ffi/library_spec.rb +286 -0
- data/spec/ffi/long_double.rb +30 -0
- data/spec/ffi/managed_struct_spec.rb +68 -0
- data/spec/ffi/memorypointer_spec.rb +78 -0
- data/spec/ffi/number_spec.rb +247 -0
- data/spec/ffi/platform_spec.rb +114 -0
- data/spec/ffi/pointer_spec.rb +285 -0
- data/spec/ffi/rbx/attach_function_spec.rb +34 -0
- data/spec/ffi/rbx/memory_pointer_spec.rb +198 -0
- data/spec/ffi/rbx/spec_helper.rb +6 -0
- data/spec/ffi/rbx/struct_spec.rb +18 -0
- data/spec/ffi/spec_helper.rb +93 -0
- data/spec/ffi/string_spec.rb +118 -0
- data/spec/ffi/strptr_spec.rb +50 -0
- data/spec/ffi/struct_by_ref_spec.rb +43 -0
- data/spec/ffi/struct_callback_spec.rb +69 -0
- data/spec/ffi/struct_initialize_spec.rb +35 -0
- data/spec/ffi/struct_packed_spec.rb +50 -0
- data/spec/ffi/struct_spec.rb +882 -0
- data/spec/ffi/typedef_spec.rb +91 -0
- data/spec/ffi/union_spec.rb +67 -0
- data/spec/ffi/variadic_spec.rb +132 -0
- data/spec/spec.opts +4 -0
- metadata +309 -0
@@ -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
|
+
|
@@ -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
|
+
|
data/libtest/UnionTest.c
ADDED
@@ -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); }
|
@@ -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
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2008-2014 Ruby-FFI contributors
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#
|
2
|
+
# This file is part of ruby-ffi.
|
3
|
+
# For licensing, see LICENSE.SPECS
|
4
|
+
#
|
5
|
+
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
|
7
|
+
|
8
|
+
describe "async callback" do
|
9
|
+
module LibTest
|
10
|
+
extend FFI::Library
|
11
|
+
ffi_lib TestLibrary::PATH
|
12
|
+
AsyncIntCallback = callback [ :int ], :void
|
13
|
+
|
14
|
+
@blocking = true
|
15
|
+
attach_function :testAsyncCallback, [ AsyncIntCallback, :int ], :void
|
16
|
+
end
|
17
|
+
|
18
|
+
it ":int (0x7fffffff) argument" do
|
19
|
+
v = 0xdeadbeef
|
20
|
+
called = false
|
21
|
+
cb = Proc.new {|i| v = i; called = true }
|
22
|
+
LibTest.testAsyncCallback(cb, 0x7fffffff)
|
23
|
+
expect(called).to be true
|
24
|
+
expect(v).to eq(0x7fffffff)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "called a second time" do
|
28
|
+
v = 0xdeadbeef
|
29
|
+
called = false
|
30
|
+
cb = Proc.new {|i| v = i; called = true }
|
31
|
+
LibTest.testAsyncCallback(cb, 0x7fffffff)
|
32
|
+
expect(called).to be true
|
33
|
+
expect(v).to eq(0x7fffffff)
|
34
|
+
end
|
35
|
+
end
|