-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathCType.java
More file actions
302 lines (247 loc) · 10.3 KB
/
CType.java
File metadata and controls
302 lines (247 loc) · 10.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package org.python.modules.jffi;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.python.core.Py;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.core.Untraversable;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
import org.python.expose.ExposeAsSuperclass;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
@ExposedType(name = "jffi.Type", base = PyObject.class)
public abstract class CType extends PyObject {
public static final PyType TYPE = PyType.fromClass(CType.class);
static {
TYPE.fastGetDict().__setitem__("Array", Array.TYPE);
TYPE.fastGetDict().__setitem__("Pointer", Pointer.TYPE);
}
public static final CType VOID = primitive(NativeType.VOID);
public static final CType BOOL = primitive(NativeType.BOOL);
public static final CType BYTE = primitive(NativeType.BYTE);
public static final CType UBYTE = primitive(NativeType.UBYTE);
public static final CType SHORT = primitive(NativeType.SHORT);
public static final CType USHORT = primitive(NativeType.USHORT);
public static final CType INT = primitive(NativeType.INT);
public static final CType UINT = primitive(NativeType.UINT);
public static final CType LONGLONG = primitive(NativeType.LONGLONG);
public static final CType ULONGLONG = primitive(NativeType.ULONGLONG);
public static final CType LONG = primitive(NativeType.LONG);
public static final CType ULONG = primitive(NativeType.ULONG);
public static final CType FLOAT = primitive(NativeType.FLOAT);
public static final CType DOUBLE = primitive(NativeType.DOUBLE);
public static final CType POINTER = primitive(NativeType.POINTER);
public static final CType STRING = primitive(NativeType.STRING);
final NativeType nativeType;
/** The <tt>MemoryOp</tt> used to read/write items of this type */
private final MemoryOp memoryOp;
CType(NativeType type, MemoryOp memoryOp) {
this.nativeType = type;
this.memoryOp = memoryOp;
}
public NativeType getNativeType() {
return nativeType;
}
abstract com.kenai.jffi.Type jffiType();
MemoryOp getMemoryOp() {
return memoryOp;
}
public final int alignment() {
return jffiType().alignment();
}
public final int size() {
return jffiType().size();
}
@ExposedMethod(names={"size"})
public final PyObject pysize() {
return Py.newInteger(size());
}
@ExposedMethod(names={"alignment"})
public final PyObject pyalignment() {
return Py.newInteger(alignment());
}
static final CType primitive(NativeType type) {
CType t = new Builtin(type);
CType.TYPE.fastGetDict().__setitem__(type.name(), t);
return t;
}
@Untraversable
static final class Builtin extends CType implements ExposeAsSuperclass {
public Builtin(NativeType type) {
super(type, MemoryOp.getMemoryOp(type));
}
@Override
public final String toString() {
return "<jffi.Type." + nativeType.name() + ">";
}
final com.kenai.jffi.Type jffiType() {
return NativeType.jffiType(nativeType);
}
}
@Untraversable
@ExposedType(name = "jffi.Type.Custom", base = CType.class)
static class Custom extends CType {
final com.kenai.jffi.Type jffiType;
public Custom(NativeType type, com.kenai.jffi.Type jffiType, MemoryOp op) {
super(type, op);
this.jffiType = jffiType;
}
final com.kenai.jffi.Type jffiType() {
return jffiType;
}
}
static CType typeOf(PyObject obj) {
if (obj instanceof CType) {
return (CType) obj;
} else if (obj == Py.None) {
return CType.VOID;
}
PyObject jffi_type = obj.__getattr__("_jffi_type");
if (!(jffi_type instanceof CType)) {
throw Py.TypeError("invalid _jffi_type");
}
return (CType) jffi_type;
}
@ExposedType(name = "jffi.Type.Array", base = CType.class)
static final class Array extends CType.Custom implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(Array.class);
final CType componentType;
final PyType pyComponentType;
final MemoryOp componentMemoryOp;
@ExposedGet
public final int length;
public Array(PyType pyComponentType, CType componentType, int length) {
super(NativeType.ARRAY, new com.kenai.jffi.Array(Util.jffiType(componentType), length), null);
this.pyComponentType = pyComponentType;
this.componentType = componentType;
this.componentMemoryOp = getComponentMemoryOp((PyType) pyComponentType, componentType);
this.length = length;
}
@ExposedNew
public static PyObject Array_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
if (args.length != 2) {
throw Py.TypeError(String.format("__init__() takes exactly 2 arguments (%d given)", args.length));
}
if (!(args[0] instanceof PyType)) {
throw Py.TypeError("invalid component type");
}
return new Array((PyType) args[0], typeOf(args[0]), args[1].asInt());
}
@Override
public int __len__() {
return length;
}
@Override
public final String toString() {
return String.format("<ctypes.Array elem_type=%s length=%d>", pyComponentType.toString(), length);
}
static final MemoryOp getComponentMemoryOp(PyType pyComponentType, CType componentType) {
if (pyComponentType.isSubType(ScalarCData.TYPE)) {
return componentType.getMemoryOp();
} else if (pyComponentType.isSubType(Structure.TYPE)) {
return new MemoryOp.StructOp(pyComponentType);
} else {
throw Py.TypeError("only scalar and struct types supported");
}
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
if (componentType != null) {
int res = visit.visit(componentType, arg);
if (res != 0) {
return res;
}
}
return pyComponentType != null ? visit.visit(pyComponentType, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == componentType || ob == pyComponentType);
}
}
@ExposedType(name = "jffi.Type.Pointer", base = CType.class)
final static class Pointer extends Custom implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(Pointer.class);
private static final ConcurrentMap<PyObject, Pointer> typeCache
= new ConcurrentHashMap<PyObject, Pointer>();
final CType componentType;
final PyType pyComponentType;
final MemoryOp componentMemoryOp;
Pointer(PyType subtype, PyType pyComponentType, CType componentType) {
super(NativeType.POINTER, com.kenai.jffi.Type.POINTER, new MemoryOp.PointerOp(subtype, CType.POINTER));
this.pyComponentType = pyComponentType;
this.componentType = componentType;
if (pyComponentType.isSubType(ScalarCData.TYPE)) {
this.componentMemoryOp = new ScalarOp(componentType.getMemoryOp(), pyComponentType);
} else if (pyComponentType.isSubType(Structure.TYPE)) {
this.componentMemoryOp = new MemoryOp.StructOp(pyComponentType);
} else {
throw Py.TypeError("pointer only supported for scalar types");
}
}
@ExposedNew
public static PyObject Pointer_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
if (args.length != 1) {
throw Py.TypeError(String.format("__init__() takes exactly 1 argument (%d given)", args.length));
}
Pointer p = typeCache.get(args[0]);
if (p != null) {
return p;
}
if (!(args[0] instanceof PyType)) {
throw Py.TypeError("expected ctypes class");
}
p = new Pointer(subtype, (PyType) args[0], typeOf(args[0]));
typeCache.put(args[0], p);
return p;
}
@Override
public final String toString() {
return String.format("<jffi.Type.Pointer component_type=%s>", componentType.toString());
}
private static final class ScalarOp extends MemoryOp {
private final MemoryOp op;
private final PyType type;
public ScalarOp(MemoryOp op, PyType type) {
this.op = op;
this.type = type;
}
public final void put(Memory mem, long offset, PyObject value) {
op.put(mem, offset, value);
}
public final PyObject get(Memory mem, long offset) {
PyObject result = type.__call__(op.get(mem, offset));
//
// Point the CData to the backing memory so all value gets/sets
// update the same memory this pointer points to
//
if (result instanceof ScalarCData) {
((ScalarCData) result).setReferenceMemory(mem);
}
return result;
}
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
if (componentType != null) {
int res = visit.visit(componentType, arg);
if (res != 0) {
return res;
}
}
return pyComponentType != null ? visit.visit(pyComponentType, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == componentType || ob == pyComponentType);
}
}
}