ffi 0.4.0-x86-mswin32 → 0.5.0-x86-mswin32
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/README.rdoc +15 -15
- data/Rakefile +57 -8
- data/ext/ffi_c/AbstractMemory.c +101 -24
- data/ext/ffi_c/AbstractMemory.h +98 -6
- data/ext/ffi_c/ArrayType.c +129 -0
- data/ext/ffi_c/ArrayType.h +58 -0
- data/ext/ffi_c/AutoPointer.c +1 -0
- data/ext/ffi_c/Buffer.c +59 -43
- data/ext/ffi_c/Call.c +853 -0
- data/ext/ffi_c/Call.h +86 -0
- data/ext/ffi_c/ClosurePool.c +302 -0
- data/ext/ffi_c/ClosurePool.h +29 -0
- data/ext/ffi_c/DynamicLibrary.c +3 -0
- data/ext/ffi_c/Function.c +478 -0
- data/ext/ffi_c/Function.h +80 -0
- data/ext/ffi_c/FunctionInfo.c +221 -0
- data/ext/ffi_c/LastError.c +30 -6
- data/ext/ffi_c/MemoryPointer.c +50 -28
- data/ext/ffi_c/MethodHandle.c +346 -0
- data/ext/ffi_c/MethodHandle.h +53 -0
- data/ext/ffi_c/Pointer.c +73 -13
- data/ext/ffi_c/Pointer.h +31 -7
- data/ext/ffi_c/Struct.c +517 -224
- data/ext/ffi_c/Struct.h +60 -6
- data/ext/ffi_c/StructByValue.c +140 -0
- data/ext/ffi_c/StructByValue.h +53 -0
- data/ext/ffi_c/StructLayout.c +450 -0
- data/ext/ffi_c/Type.c +121 -22
- data/ext/ffi_c/Type.h +37 -8
- data/ext/ffi_c/Types.c +46 -61
- data/ext/ffi_c/Types.h +38 -7
- data/ext/ffi_c/Variadic.c +260 -0
- data/ext/ffi_c/compat.h +53 -3
- data/ext/ffi_c/extconf.rb +6 -7
- data/ext/ffi_c/ffi.c +45 -39
- data/ext/ffi_c/libffi.darwin.mk +2 -2
- data/ext/ffi_c/rbffi.h +3 -0
- data/lib/1.8/ffi_c.so +0 -0
- data/lib/1.9/ffi_c.so +0 -0
- data/lib/ffi/ffi.rb +7 -4
- data/lib/ffi/library.rb +34 -59
- data/lib/ffi/platform.rb +14 -4
- data/lib/ffi/struct.rb +110 -281
- data/lib/ffi/union.rb +4 -9
- data/lib/ffi/variadic.rb +1 -6
- data/spec/ffi/buffer_spec.rb +6 -0
- data/spec/ffi/callback_spec.rb +34 -3
- data/spec/ffi/function_spec.rb +73 -0
- data/spec/ffi/library_spec.rb +56 -52
- data/spec/ffi/pointer_spec.rb +3 -3
- data/spec/ffi/struct_callback_spec.rb +26 -3
- data/spec/ffi/struct_spec.rb +56 -3
- metadata +34 -13
- data/ext/ffi_c/Callback.c +0 -374
- data/ext/ffi_c/Callback.h +0 -47
- data/ext/ffi_c/Invoker.c +0 -962
- data/ext/ffi_c/NullPointer.c +0 -143
data/ext/ffi_c/Type.c
CHANGED
@@ -1,3 +1,30 @@
|
|
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
|
+
|
1
28
|
#include <sys/param.h>
|
2
29
|
#include <sys/types.h>
|
3
30
|
#include <ruby.h>
|
@@ -18,6 +45,8 @@ static void builtin_type_free(BuiltinType *);
|
|
18
45
|
VALUE rbffi_TypeClass = Qnil;
|
19
46
|
|
20
47
|
static VALUE classBuiltinType = Qnil;
|
48
|
+
static VALUE typeMap = Qnil, sizeMap = Qnil;
|
49
|
+
static ID id_find_type = 0, id_type_size = 0, id_size = 0;
|
21
50
|
|
22
51
|
static VALUE
|
23
52
|
type_allocate(VALUE klass)
|
@@ -26,9 +55,8 @@ type_allocate(VALUE klass)
|
|
26
55
|
VALUE obj = Data_Make_Struct(klass, Type, NULL, -1, type);
|
27
56
|
|
28
57
|
type->nativeType = -1;
|
29
|
-
type->
|
30
|
-
|
31
|
-
|
58
|
+
type->ffiType = &ffi_type_void;
|
59
|
+
|
32
60
|
return obj;
|
33
61
|
}
|
34
62
|
|
@@ -45,8 +73,7 @@ type_initialize(VALUE self, VALUE value)
|
|
45
73
|
} else if (rb_obj_is_kind_of(value, rbffi_TypeClass)) {
|
46
74
|
Data_Get_Struct(value, Type, other);
|
47
75
|
type->nativeType = other->nativeType;
|
48
|
-
type->
|
49
|
-
type->alignment = other->alignment;
|
76
|
+
type->ffiType = other->ffiType;
|
50
77
|
} else {
|
51
78
|
rb_raise(rb_eArgError, "wrong type");
|
52
79
|
}
|
@@ -61,7 +88,7 @@ type_size(VALUE self)
|
|
61
88
|
|
62
89
|
Data_Get_Struct(self, Type, type);
|
63
90
|
|
64
|
-
return INT2FIX(type->size);
|
91
|
+
return INT2FIX(type->ffiType->size);
|
65
92
|
}
|
66
93
|
|
67
94
|
static VALUE
|
@@ -71,7 +98,7 @@ type_alignment(VALUE self)
|
|
71
98
|
|
72
99
|
Data_Get_Struct(self, Type, type);
|
73
100
|
|
74
|
-
return INT2FIX(type->alignment);
|
101
|
+
return INT2FIX(type->ffiType->alignment);
|
75
102
|
}
|
76
103
|
|
77
104
|
static VALUE
|
@@ -82,7 +109,7 @@ type_inspect(VALUE self)
|
|
82
109
|
|
83
110
|
Data_Get_Struct(self, Type, type);
|
84
111
|
snprintf(buf, sizeof(buf), "#<FFI::Type:%p size=%d alignment=%d>",
|
85
|
-
type, type->size, type->alignment);
|
112
|
+
type, (int) type->ffiType->size, (int) type->ffiType->alignment);
|
86
113
|
|
87
114
|
return rb_str_new2(buf);
|
88
115
|
}
|
@@ -96,8 +123,6 @@ enum_allocate(VALUE klass)
|
|
96
123
|
obj = Data_Make_Struct(klass, Type, NULL, -1, type);
|
97
124
|
type->nativeType = NATIVE_ENUM;
|
98
125
|
type->ffiType = &ffi_type_sint;
|
99
|
-
type->size = ffi_type_sint.size;
|
100
|
-
type->alignment = ffi_type_sint.alignment;
|
101
126
|
|
102
127
|
return obj;
|
103
128
|
}
|
@@ -105,26 +130,28 @@ enum_allocate(VALUE klass)
|
|
105
130
|
int
|
106
131
|
rbffi_Type_GetIntValue(VALUE type)
|
107
132
|
{
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
} else {
|
113
|
-
rb_raise(rb_eArgError, "Invalid type argument");
|
133
|
+
Type* t;
|
134
|
+
|
135
|
+
if (!rb_obj_is_kind_of(type, rbffi_TypeClass)) {
|
136
|
+
rb_raise(rb_eTypeError, "wrong type. Expected (FFI::Type)");
|
114
137
|
}
|
138
|
+
|
139
|
+
Data_Get_Struct(type, Type, t);
|
140
|
+
|
141
|
+
return t->nativeType;
|
115
142
|
}
|
116
143
|
|
117
144
|
static VALUE
|
118
145
|
builtin_type_new(VALUE klass, int nativeType, ffi_type* ffiType, const char* name)
|
119
146
|
{
|
120
147
|
BuiltinType* type;
|
121
|
-
VALUE obj =
|
148
|
+
VALUE obj = Qnil;
|
149
|
+
|
150
|
+
obj = Data_Make_Struct(klass, BuiltinType, NULL, builtin_type_free, type);
|
122
151
|
|
123
152
|
type->name = strdup(name);
|
124
153
|
type->type.nativeType = nativeType;
|
125
154
|
type->type.ffiType = ffiType;
|
126
|
-
type->type.size = ffiType->size;
|
127
|
-
type->type.alignment = ffiType->alignment;
|
128
155
|
|
129
156
|
return obj;
|
130
157
|
}
|
@@ -143,18 +170,90 @@ builtin_type_inspect(VALUE self)
|
|
143
170
|
BuiltinType *type;
|
144
171
|
|
145
172
|
Data_Get_Struct(self, BuiltinType, type);
|
146
|
-
snprintf(buf, sizeof(buf), "#<FFI::Type::Builtin:%
|
147
|
-
type->name, type->type.size, type->type.alignment);
|
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);
|
148
175
|
|
149
176
|
return rb_str_new2(buf);
|
150
177
|
}
|
151
178
|
|
179
|
+
int
|
180
|
+
rbffi_type_size(VALUE type)
|
181
|
+
{
|
182
|
+
int t = TYPE(type);
|
183
|
+
if (t == T_FIXNUM || t == T_BIGNUM) {
|
184
|
+
return NUM2INT(type);
|
185
|
+
} else if (t == T_SYMBOL) {
|
186
|
+
/*
|
187
|
+
* Try looking up directly in the type and size maps
|
188
|
+
*/
|
189
|
+
VALUE nType;
|
190
|
+
if ((nType = rb_hash_aref(typeMap, type)) != Qnil) {
|
191
|
+
VALUE nSize = rb_hash_aref(sizeMap, nType);
|
192
|
+
if (TYPE(nSize) == T_FIXNUM) {
|
193
|
+
return FIX2INT(nSize);
|
194
|
+
}
|
195
|
+
}
|
196
|
+
// Not found - call up to the ruby version to resolve
|
197
|
+
return NUM2INT(rb_funcall2(rbffi_FFIModule, id_type_size, 1, &type));
|
198
|
+
} else {
|
199
|
+
return NUM2INT(rb_funcall2(type, id_size, 0, NULL));
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
VALUE
|
204
|
+
rbffi_Type_Lookup(VALUE name)
|
205
|
+
{
|
206
|
+
int t = TYPE(name);
|
207
|
+
if (t == T_SYMBOL || t == T_STRING) {
|
208
|
+
/*
|
209
|
+
* Try looking up directly in the type Map
|
210
|
+
*/
|
211
|
+
VALUE nType;
|
212
|
+
if ((nType = rb_hash_aref(typeMap, name)) != Qnil && rb_obj_is_kind_of(nType, rbffi_TypeClass)) {
|
213
|
+
return nType;
|
214
|
+
}
|
215
|
+
} else if (rb_obj_is_kind_of(name, rbffi_TypeClass)) {
|
216
|
+
|
217
|
+
return name;
|
218
|
+
}
|
219
|
+
|
220
|
+
/* Nothing found - let caller handle raising exceptions */
|
221
|
+
return Qnil;
|
222
|
+
}
|
223
|
+
|
224
|
+
/**
|
225
|
+
* rbffi_Type_Find() is like rbffi_Type_Lookup, but an error is raised if the
|
226
|
+
* type is not found.
|
227
|
+
*/
|
228
|
+
VALUE
|
229
|
+
rbffi_Type_Find(VALUE name)
|
230
|
+
{
|
231
|
+
VALUE rbType = rbffi_Type_Lookup(name);
|
232
|
+
|
233
|
+
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));
|
236
|
+
}
|
237
|
+
|
238
|
+
return rbType;
|
239
|
+
}
|
240
|
+
|
152
241
|
void
|
153
242
|
rbffi_Type_Init(VALUE moduleFFI)
|
154
243
|
{
|
155
244
|
VALUE moduleNativeType;
|
156
245
|
VALUE classType = rbffi_TypeClass = rb_define_class_under(moduleFFI, "Type", rb_cObject);
|
157
246
|
VALUE classEnum = rb_define_class_under(moduleFFI, "Enum", classType);
|
247
|
+
|
248
|
+
rb_define_const(moduleFFI, "TypeDefs", typeMap = rb_hash_new());
|
249
|
+
rb_define_const(moduleFFI, "SizeTypes", sizeMap = rb_hash_new());
|
250
|
+
rb_global_variable(&typeMap);
|
251
|
+
rb_global_variable(&sizeMap);
|
252
|
+
id_find_type = rb_intern("find_type");
|
253
|
+
id_type_size = rb_intern("type_size");
|
254
|
+
id_size = rb_intern("size");
|
255
|
+
|
256
|
+
|
158
257
|
classBuiltinType = rb_define_class_under(rbffi_TypeClass, "Builtin", rbffi_TypeClass);
|
159
258
|
moduleNativeType = rb_define_module_under(moduleFFI, "NativeType");
|
160
259
|
|
@@ -166,7 +265,7 @@ rbffi_Type_Init(VALUE moduleFFI)
|
|
166
265
|
rb_define_method(classType, "initialize", type_initialize, 1);
|
167
266
|
rb_define_method(classType, "size", type_size, 0);
|
168
267
|
rb_define_method(classType, "alignment", type_alignment, 0);
|
169
|
-
rb_define_method(
|
268
|
+
rb_define_method(classType, "inspect", type_inspect, 0);
|
170
269
|
|
171
270
|
// Make Type::Builtin non-allocatable
|
172
271
|
rb_undef_method(CLASS_OF(classBuiltinType), "new");
|
data/ext/ffi_c/Type.h
CHANGED
@@ -1,5 +1,32 @@
|
|
1
|
-
|
2
|
-
|
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_TYPE_H
|
29
|
+
#define RBFFI_TYPE_H
|
3
30
|
|
4
31
|
#include <ruby.h>
|
5
32
|
#include <ffi.h>
|
@@ -8,21 +35,23 @@
|
|
8
35
|
extern "C" {
|
9
36
|
#endif
|
10
37
|
|
38
|
+
typedef struct Type_ Type;
|
39
|
+
|
40
|
+
#include "Types.h"
|
11
41
|
|
12
|
-
|
42
|
+
struct Type_ {
|
13
43
|
NativeType nativeType;
|
14
44
|
ffi_type* ffiType;
|
15
|
-
|
16
|
-
int alignment;
|
17
|
-
} Type;
|
45
|
+
};
|
18
46
|
|
19
47
|
extern VALUE rbffi_TypeClass;
|
20
48
|
extern int rbffi_Type_GetIntValue(VALUE type);
|
21
|
-
|
49
|
+
extern VALUE rbffi_Type_Lookup(VALUE type);
|
50
|
+
extern VALUE rbffi_Type_Find(VALUE type);
|
22
51
|
|
23
52
|
#ifdef __cplusplus
|
24
53
|
}
|
25
54
|
#endif
|
26
55
|
|
27
|
-
#endif /*
|
56
|
+
#endif /* RBFFI_TYPE_H */
|
28
57
|
|
data/ext/ffi_c/Types.c
CHANGED
@@ -1,51 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009, Wayne Meissner
|
3
|
+
* Copyright (c) 2009, Luc Heinrich
|
4
|
+
* Copyright (c) 2009, Aman Gupta.
|
5
|
+
* All rights reserved.
|
6
|
+
*
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
9
|
+
*
|
10
|
+
* * Redistributions of source code must retain the above copyright notice, this
|
11
|
+
* list of conditions and the following disclaimer.
|
12
|
+
* * Redistributions in binary form must reproduce the above copyright notice
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
* * The name of the author or authors may not be used to endorse or promote
|
16
|
+
* products derived from this software without specific prior written permission.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
22
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
*/
|
29
|
+
|
1
30
|
#include <ruby.h>
|
2
31
|
#include "Pointer.h"
|
3
32
|
#include "rbffi.h"
|
4
|
-
#include "
|
33
|
+
#include "Function.h"
|
34
|
+
#include "StructByValue.h"
|
5
35
|
#include "Types.h"
|
36
|
+
#include "Struct.h"
|
37
|
+
#include "MemoryPointer.h"
|
6
38
|
|
7
39
|
static ID id_find = 0;
|
8
40
|
|
9
|
-
ffi_type*
|
10
|
-
rbffi_NativeType_ToFFI(NativeType type)
|
11
|
-
{
|
12
|
-
switch (type) {
|
13
|
-
case NATIVE_VOID:
|
14
|
-
return &ffi_type_void;
|
15
|
-
case NATIVE_INT8:
|
16
|
-
return &ffi_type_sint8;
|
17
|
-
case NATIVE_UINT8:
|
18
|
-
return &ffi_type_uint8;
|
19
|
-
case NATIVE_INT16:
|
20
|
-
return &ffi_type_sint16;
|
21
|
-
case NATIVE_UINT16:
|
22
|
-
return &ffi_type_uint16;
|
23
|
-
case NATIVE_BOOL:
|
24
|
-
case NATIVE_INT32:
|
25
|
-
case NATIVE_ENUM:
|
26
|
-
return &ffi_type_sint32;
|
27
|
-
case NATIVE_UINT32:
|
28
|
-
return &ffi_type_uint32;
|
29
|
-
case NATIVE_INT64:
|
30
|
-
return &ffi_type_sint64;
|
31
|
-
case NATIVE_UINT64:
|
32
|
-
return &ffi_type_uint64;
|
33
|
-
case NATIVE_FLOAT32:
|
34
|
-
return &ffi_type_float;
|
35
|
-
case NATIVE_FLOAT64:
|
36
|
-
return &ffi_type_double;
|
37
|
-
case NATIVE_STRING:
|
38
|
-
case NATIVE_RBXSTRING:
|
39
|
-
case NATIVE_POINTER:
|
40
|
-
case NATIVE_BUFFER_IN:
|
41
|
-
case NATIVE_BUFFER_OUT:
|
42
|
-
case NATIVE_BUFFER_INOUT:
|
43
|
-
case NATIVE_CALLBACK:
|
44
|
-
return &ffi_type_pointer;
|
45
|
-
default:
|
46
|
-
return NULL;
|
47
|
-
}
|
48
|
-
}
|
49
41
|
|
50
42
|
VALUE
|
51
43
|
rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums)
|
@@ -74,34 +66,27 @@ rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums)
|
|
74
66
|
case NATIVE_FLOAT64:
|
75
67
|
return rb_float_new(*(double *) ptr);
|
76
68
|
case NATIVE_STRING:
|
77
|
-
return (*(
|
69
|
+
return (*(void **) ptr != NULL) ? rb_tainted_str_new2(*(char **) ptr) : Qnil;
|
78
70
|
case NATIVE_POINTER:
|
79
71
|
return rbffi_Pointer_NewInstance(*(void **) ptr);
|
80
72
|
case NATIVE_BOOL:
|
81
73
|
return ((int) *(ffi_arg *) ptr) ? Qtrue : Qfalse;
|
82
74
|
case NATIVE_ENUM:
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
VALUE s = rb_inspect(rbType);
|
87
|
-
rb_raise(rb_eRuntimeError, "Unknown enumeration: %s", StringValueCStr(s));
|
88
|
-
}
|
89
|
-
return rb_funcall(enum_obj, id_find, 1, INT2NUM((unsigned int) *(ffi_arg *) ptr));
|
90
|
-
}
|
75
|
+
return rb_funcall(rbType, id_find, 1, INT2NUM((unsigned int) *(ffi_arg *) ptr));
|
76
|
+
|
77
|
+
case NATIVE_FUNCTION:
|
91
78
|
case NATIVE_CALLBACK: {
|
92
|
-
|
93
|
-
|
94
|
-
|
79
|
+
return rbffi_Function_NewInstance(rbType, rbffi_Pointer_NewInstance(*(void **) ptr));
|
80
|
+
}
|
81
|
+
case NATIVE_STRUCT: {
|
82
|
+
StructByValue* sbv = (StructByValue *)type;
|
83
|
+
AbstractMemory* mem;
|
84
|
+
VALUE rbMemory = rbffi_MemoryPointer_NewInstance(1, sbv->base.ffiType->size, false);
|
95
85
|
|
96
|
-
Data_Get_Struct(
|
97
|
-
|
98
|
-
argv[1] = cbInfo->rbParameterTypes;
|
99
|
-
argv[2] = ID2SYM(rb_intern("cb")); // just shove a dummy value
|
100
|
-
argv[3] = cbInfo->rbReturnType;
|
101
|
-
argv[4] = rb_str_new2("default");
|
102
|
-
argv[5] = Qnil;
|
86
|
+
Data_Get_Struct(rbMemory, AbstractMemory, mem);
|
87
|
+
memcpy(mem->address, ptr, sbv->base.ffiType->size);
|
103
88
|
|
104
|
-
return rb_class_new_instance(
|
89
|
+
return rb_class_new_instance(1, &rbMemory, sbv->rbStructClass);
|
105
90
|
}
|
106
91
|
|
107
92
|
default:
|
data/ext/ffi_c/Types.h
CHANGED
@@ -1,12 +1,39 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2008, 2009, Wayne Meissner
|
3
|
+
* Copyright (c) 2009, Luc Heinrich <luc@honk-honk.com>
|
4
|
+
*
|
5
|
+
* All rights reserved.
|
6
|
+
*
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
9
|
+
*
|
10
|
+
* * Redistributions of source code must retain the above copyright notice, this
|
11
|
+
* list of conditions and the following disclaimer.
|
12
|
+
* * Redistributions in binary form must reproduce the above copyright notice
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
* * The name of the author or authors may not be used to endorse or promote
|
16
|
+
* products derived from this software without specific prior written permission.
|
17
|
+
*
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
22
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
+
*/
|
29
|
+
|
30
|
+
#ifndef RBFFI_TYPES_H
|
31
|
+
#define RBFFI_TYPES_H
|
3
32
|
|
4
33
|
#ifdef __cplusplus
|
5
34
|
extern "C" {
|
6
35
|
#endif
|
7
|
-
|
8
|
-
# undef VOID
|
9
|
-
#endif
|
36
|
+
|
10
37
|
typedef enum {
|
11
38
|
NATIVE_VOID,
|
12
39
|
NATIVE_INT8,
|
@@ -21,6 +48,7 @@ typedef enum {
|
|
21
48
|
NATIVE_FLOAT64,
|
22
49
|
NATIVE_POINTER,
|
23
50
|
NATIVE_CALLBACK,
|
51
|
+
NATIVE_FUNCTION,
|
24
52
|
NATIVE_BUFFER_IN,
|
25
53
|
NATIVE_BUFFER_OUT,
|
26
54
|
NATIVE_BUFFER_INOUT,
|
@@ -37,12 +65,15 @@ typedef enum {
|
|
37
65
|
NATIVE_VARARGS,
|
38
66
|
/** A typedef-ed enum */
|
39
67
|
NATIVE_ENUM,
|
68
|
+
/** Struct-by-value param or result */
|
69
|
+
NATIVE_STRUCT,
|
70
|
+
/** An array type definition */
|
71
|
+
NATIVE_ARRAY,
|
40
72
|
} NativeType;
|
41
73
|
|
42
74
|
#include <ffi.h>
|
43
75
|
#include "Type.h"
|
44
76
|
|
45
|
-
extern ffi_type* rbffi_NativeType_ToFFI(NativeType type);
|
46
77
|
VALUE rbffi_NativeValue_ToRuby(Type* type, VALUE rbType, const void* ptr, VALUE enums);
|
47
78
|
void rbffi_Types_Init(VALUE moduleFFI);
|
48
79
|
|
@@ -50,5 +81,5 @@ void rbffi_Types_Init(VALUE moduleFFI);
|
|
50
81
|
}
|
51
82
|
#endif
|
52
83
|
|
53
|
-
#endif /*
|
84
|
+
#endif /* RBFFI_TYPES_H */
|
54
85
|
|