-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathjffi.java
More file actions
85 lines (72 loc) · 3.23 KB
/
jffi.java
File metadata and controls
85 lines (72 loc) · 3.23 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
package org.python.modules.jffi;
import com.kenai.jffi.Library;
import org.python.core.ClassDictInit;
import org.python.core.Py;
import org.python.core.PyInteger;
import org.python.core.PyLong;
import org.python.core.PyObject;
public class jffi implements ClassDictInit {
public static final int FUNCFLAG_STDCALL = 0x0;
public static final int FUNCFLAG_CDECL = 0x1;
public static final int FUNCFLAG_HRESULT = 0x2;
public static final int FUNCFLAG_PYTHONAPI = 0x4;
public static final int FUNCFLAG_USE_ERRNO = 0x8;
public static final int FUNCFLAG_USE_LASTERROR = 0x10;
public static void classDictInit(PyObject dict) {
dict.__setitem__("__name__", Py.newString("jffi"));
dict.__setitem__("DynamicLibrary", DynamicLibrary.TYPE);
dict.__setitem__("Type", CType.TYPE);
dict.__setitem__("Function", Function.TYPE);
dict.__setitem__("CData", CData.TYPE);
dict.__setitem__("ArrayCData", ArrayCData.TYPE);
dict.__setitem__("PointerCData", PointerCData.TYPE);
dict.__setitem__("ScalarCData", ScalarCData.TYPE);
dict.__setitem__("StringCData", StringCData.TYPE);
dict.__setitem__("Structure", Structure.TYPE);
dict.__setitem__("StructLayout", StructLayout.TYPE);
dict.__setitem__("FUNCFLAG_STDCALL", Py.newInteger(FUNCFLAG_STDCALL));
dict.__setitem__("FUNCFLAG_CDECL", Py.newInteger(FUNCFLAG_CDECL));
dict.__setitem__("RTLD_GLOBAL", Py.newInteger(Library.GLOBAL));
dict.__setitem__("RTLD_LOCAL", Py.newInteger(Library.LOCAL));
dict.__setitem__("RTLD_LAZY", Py.newInteger(Library.LAZY));
dict.__setitem__("RTLD_NOW", Py.newInteger(Library.NOW));
dict.__setitem__("__version__", Py.newString("0.0.1"));
}
public static PyObject dlopen(PyObject name, PyObject mode) {
return new DynamicLibrary(name != Py.None ? name.asString() : null, mode.asInt());
}
public static PyObject get_errno() {
return Py.newInteger(0);
}
public static PyObject set_errno(PyObject type) {
return Py.newInteger(0);
}
public static PyObject pointer(PyObject type) {
return Py.newInteger(0);
}
public static PyObject POINTER(PyObject type) {
return type;
}
private static long getMemoryAddress(PyObject obj) {
if (obj instanceof Pointer) {
return ((Pointer) obj).getMemory().getAddress();
} else if (obj instanceof CData) {
return ((CData) obj).getReferenceMemory().getAddress();
} else if (obj instanceof PyInteger) {
return obj.asInt();
} else if (obj instanceof PyLong) {
return ((PyLong) obj).asLong(0);
} else {
throw Py.TypeError("invalid memory address");
}
}
public static PyObject memmove(PyObject dst, PyObject src, PyObject length) {
com.kenai.jffi.MemoryIO.getInstance().copyMemory(getMemoryAddress(src),
getMemoryAddress(dst), length.asInt());
return Py.None;
}
public static PyObject memset(PyObject dst, PyObject value, PyObject length) {
com.kenai.jffi.MemoryIO.getInstance().setMemory(getMemoryAddress(dst), length.asInt(), (byte) value.asInt());
return Py.None;
}
}