forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FFIUtil.java
206 lines (163 loc) · 7.45 KB
/
FFIUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.jruby.ext.ffi.jffi;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.ext.ffi.CallbackInfo;
import org.jruby.ext.ffi.NativeType;
import org.jruby.ext.ffi.StructLayout;
import org.jruby.ext.ffi.Type;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
/**
* Some utility functions for FFI <=> jffi conversions
*/
public final class FFIUtil {
private static final com.kenai.jffi.MemoryIO IO = com.kenai.jffi.MemoryIO.getInstance();
private FFIUtil() {}
private static final Map<NativeType, com.kenai.jffi.Type> typeMap = buildTypeMap();
private static final Map<NativeType, com.kenai.jffi.Type> buildTypeMap() {
Map<NativeType, com.kenai.jffi.Type> m = new EnumMap<NativeType, com.kenai.jffi.Type>(NativeType.class);
m.put(NativeType.VOID, com.kenai.jffi.Type.VOID);
m.put(NativeType.BOOL, com.kenai.jffi.Type.UINT8);
m.put(NativeType.CHAR, com.kenai.jffi.Type.SCHAR);
m.put(NativeType.SHORT, com.kenai.jffi.Type.SSHORT);
m.put(NativeType.INT, com.kenai.jffi.Type.SINT);
m.put(NativeType.LONG, com.kenai.jffi.Type.SLONG);
m.put(NativeType.LONG_LONG, com.kenai.jffi.Type.SLONG_LONG);
m.put(NativeType.UCHAR, com.kenai.jffi.Type.UCHAR);
m.put(NativeType.USHORT, com.kenai.jffi.Type.USHORT);
m.put(NativeType.UINT, com.kenai.jffi.Type.UINT);
m.put(NativeType.ULONG, com.kenai.jffi.Type.ULONG);
m.put(NativeType.ULONG_LONG, com.kenai.jffi.Type.ULONG_LONG);
m.put(NativeType.FLOAT, com.kenai.jffi.Type.FLOAT);
m.put(NativeType.DOUBLE, com.kenai.jffi.Type.DOUBLE);
m.put(NativeType.LONGDOUBLE, com.kenai.jffi.Type.LONGDOUBLE);
m.put(NativeType.POINTER, com.kenai.jffi.Type.POINTER);
m.put(NativeType.BUFFER_IN, com.kenai.jffi.Type.POINTER);
m.put(NativeType.BUFFER_OUT, com.kenai.jffi.Type.POINTER);
m.put(NativeType.BUFFER_INOUT, com.kenai.jffi.Type.POINTER);
m.put(NativeType.STRING, com.kenai.jffi.Type.POINTER);
m.put(NativeType.TRANSIENT_STRING, com.kenai.jffi.Type.POINTER);
return m;
}
static final com.kenai.jffi.Type getFFIType(Type type) {
Object jffiType;
if ((jffiType = type.getFFIHandle()) instanceof com.kenai.jffi.Type) {
return (com.kenai.jffi.Type) jffiType;
}
return cacheFFIType(type);
}
private static com.kenai.jffi.Type cacheFFIType(Type type) {
Object ffiType;
synchronized (type) {
if ((ffiType = type.getFFIHandle()) instanceof com.kenai.jffi.Type) {
return (com.kenai.jffi.Type) ffiType;
}
if (type instanceof Type.Builtin || type instanceof CallbackInfo) {
ffiType = FFIUtil.getFFIType(type.getNativeType());
} else if (type instanceof org.jruby.ext.ffi.StructLayout) {
ffiType = FFIUtil.newStruct((org.jruby.ext.ffi.StructLayout) type);
} else if (type instanceof org.jruby.ext.ffi.StructByValue) {
ffiType = FFIUtil.newStruct(((org.jruby.ext.ffi.StructByValue) type).getStructLayout());
} else if (type instanceof org.jruby.ext.ffi.Type.Array) {
ffiType = FFIUtil.newArray((org.jruby.ext.ffi.Type.Array) type);
} else if (type instanceof org.jruby.ext.ffi.MappedType) {
ffiType = FFIUtil.getFFIType(((org.jruby.ext.ffi.MappedType) type).getRealType());
} else {
return null;
}
type.setFFIHandle(ffiType);
}
return (com.kenai.jffi.Type) ffiType;
}
static final com.kenai.jffi.Type getFFIType(NativeType type) {
return typeMap.get(type);
}
/**
* Creates a new JFFI Struct descriptor for a StructLayout
*
* @param layout The structure layout
* @return A new Struct descriptor.
*/
static final com.kenai.jffi.Aggregate newStruct(org.jruby.ext.ffi.StructLayout layout) {
if (layout.isUnion()) {
//
// The jffi union type is broken, so emulate a union with a Struct type, containing
// an array of elements of the correct alignment.
//
com.kenai.jffi.Type[] alignmentTypes = {
com.kenai.jffi.Type.SINT8,
com.kenai.jffi.Type.SINT16,
com.kenai.jffi.Type.SINT32,
com.kenai.jffi.Type.SINT64,
com.kenai.jffi.Type.FLOAT,
com.kenai.jffi.Type.DOUBLE,
com.kenai.jffi.Type.LONGDOUBLE,
};
com.kenai.jffi.Type alignmentType = null;
for (com.kenai.jffi.Type t : alignmentTypes) {
if (t.alignment() == layout.getNativeAlignment()) {
alignmentType = t;
break;
}
}
if (alignmentType == null) {
throw layout.getRuntime().newRuntimeError("cannot discern base alignment type for union of alignment "
+ layout.getNativeAlignment());
}
com.kenai.jffi.Type[] fields = new com.kenai.jffi.Type[layout.getNativeSize() / alignmentType.size()];
Arrays.fill(fields, alignmentType);
return com.kenai.jffi.Struct.newStruct(fields);
} else {
Collection<StructLayout.Member> structMembers = layout.getMembers();
java.util.List<com.kenai.jffi.Type> fields = new java.util.ArrayList<com.kenai.jffi.Type>();
for (StructLayout.Member m : structMembers) {
com.kenai.jffi.Type fieldType;
fieldType = FFIUtil.getFFIType(m.type());
if (fieldType == null) {
throw layout.getRuntime().newTypeError("unsupported Struct field type " + m);
}
if (fieldType.size() > 0) fields.add(fieldType);
}
return com.kenai.jffi.Struct.newStruct(fields.toArray(new com.kenai.jffi.Type[fields.size()]));
}
}
/**
* Creates a new JFFI type descriptor for an array
*
* @param arrayType The structure layout
* @return A new Struct descriptor.
*/
static com.kenai.jffi.Array newArray(org.jruby.ext.ffi.Type.Array arrayType) {
com.kenai.jffi.Type componentType = FFIUtil.getFFIType(arrayType.getComponentType());
if (componentType == null) {
throw arrayType.getRuntime().newTypeError("unsupported array element type " + arrayType.getComponentType());
}
return com.kenai.jffi.Array.newArray(componentType, arrayType.length());
}
/**
* Reads a nul-terminated string from native memory and boxes it up in a ruby
* string.
*
* @param runtime The ruby runtime for the resulting string.
* @param address The memory address to read the string from.
* @return A ruby string.
*/
static final IRubyObject getString(Ruby runtime, long address) {
if (address == 0) {
return runtime.getNil();
}
byte[] bytes = IO.getZeroTerminatedByteArray(address);
if (bytes.length == 0) {
return RubyString.newEmptyString(runtime);
}
RubyString s = RubyString.newStringNoCopy(runtime, bytes);
s.setTaint(true);
return s;
}
}