-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathScalarCData.java
More file actions
142 lines (113 loc) · 3.8 KB
/
ScalarCData.java
File metadata and controls
142 lines (113 loc) · 3.8 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
package org.python.modules.jffi;
import org.python.core.Py;
import org.python.core.PyFloat;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.core.Visitproc;
import org.python.expose.ExposedClassMethod;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;
@ExposedType(name = "jffi.ScalarCData", base = CData.class)
public class ScalarCData extends CData {
public static final PyType TYPE = PyType.fromClass(ScalarCData.class);
// static {
// TYPE.fastGetDict().__setitem__("in_dll", new InDll());
// }
private PyObject value;
@ExposedNew
public static PyObject ScalarCData_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ScalarCData cdata = new ScalarCData(subtype, getScalarType(subtype));
// If an initial value was supplied, use it, else default to zero
cdata.setValue(args.length > 0 ? args[0] : Py.newInteger(0));
return cdata;
}
@ExposedClassMethod(names= { "from_address" })
public static final PyObject from_address(PyType subtype, PyObject address) {
return new ScalarCData(subtype, getScalarType(subtype), Util.getMemoryForAddress(address));
}
static final CType.Builtin getScalarType(PyType subtype) {
PyObject jffi_type = subtype.__getattr__("_jffi_type");
if (!(jffi_type instanceof CType.Builtin)) {
throw Py.TypeError("invalid _jffi_type for " + subtype.getName());
}
return (CType.Builtin) jffi_type;
}
ScalarCData(PyType pytype, CType.Builtin ctype) {
super(pytype, ctype);
}
ScalarCData(PyType pytype, CType.Builtin ctype, DirectMemory m) {
super(pytype, ctype, m);
}
protected final void initReferenceMemory(Memory m) {
getMemoryOp().put(m, 0, value);
}
@ExposedGet(name = "value")
public PyObject getValue() {
// If native memory has been allocated, read the value from there
if (hasReferenceMemory()) {
return getMemoryOp().get(getReferenceMemory(), 0);
}
return value != null ? value : Py.None;
}
@ExposedSet(name = "value")
public void setValue(PyObject value) {
this.value = value;
// If native memory has been allocated, sync the value to memory
if (hasReferenceMemory()) {
getMemoryOp().put(getReferenceMemory(), 0, value);
}
}
@Override
public int asInt() {
return getValue().asInt();
}
@Override
public long asLong(int index) throws ConversionException {
return getValue().asLong(index);
}
public long asLong() {
return getValue().asLong();
}
@ExposedMethod
@Override
public PyObject __int__() {
return getValue().__int__();
}
@ExposedMethod
@Override
public PyObject __long__() {
return getValue().__long__();
}
@ExposedMethod
@Override
public PyFloat __float__() {
return getValue().__float__();
}
@Override
public final String toString() {
return getType().getName() + "(" + getValue().toString() + ")";
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
if (value != null) {
int res = visit.visit(value, arg);
if (res != 0) {
return res;
}
}
return super.traverse(visit, arg);
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
if (ob != null && ob == value) {
return true;
}
return super.refersDirectlyTo(ob);
}
}