-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathDynamicLibrary.java
More file actions
105 lines (84 loc) · 3.21 KB
/
DynamicLibrary.java
File metadata and controls
105 lines (84 loc) · 3.21 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
package org.python.modules.jffi;
import com.kenai.jffi.Library;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.core.Untraversable;
import org.python.expose.ExposeAsSuperclass;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
@Untraversable
@ExposedType(name = "jffi.DynamicLibrary", base = PyObject.class)
public class DynamicLibrary extends PyObject {
public static final PyType TYPE = PyType.fromClass(DynamicLibrary.class);
@ExposedGet
public final String name;
private final com.kenai.jffi.Library lib;
DynamicLibrary(String libname, int mode) {
this.name = libname;
lib = Library.getCachedInstance(libname, mode);
if (lib == null) {
throw Py.RuntimeError("Could not open "
+ libname != null ? libname : "[current process]"
+ " " + Library.getLastError());
}
}
private long findSymbol(PyObject name) {
long address = lib.getSymbolAddress(name.asString());
if (address == 0) {
throw Py.NameError("Could not locate symbol '" + name.asString() + "' in " + this.name);
}
return address;
}
@ExposedMethod
public final PyObject find_symbol(PyObject name) {
long address = findSymbol(name);
return new Symbol(this, name.asString(), new NativeMemory(address));
}
@ExposedMethod
public final PyObject find_function(PyObject name) {
return new TextSymbol(this, name.asString(), findSymbol(name));
}
@ExposedMethod
public final PyObject find_variable(PyObject name) {
return new DataSymbol(this, name.asString(), findSymbol(name));
}
@Untraversable
@ExposedType(name = "jffi.DynamicLibrary.Symbol", base = PyObject.class)
public static class Symbol extends BasePointer {
public static final PyType TYPE = PyType.fromClass(Symbol.class);
final DynamicLibrary library;
final DirectMemory memory;
@ExposedGet
public final String name;
public Symbol(DynamicLibrary library, String name, DirectMemory memory) {
super(TYPE);
this.library = library;
this.name = name;
this.memory = memory;
}
public final DirectMemory getMemory() {
return memory;
}
}
@Untraversable
public static final class TextSymbol extends Symbol implements ExposeAsSuperclass {
public TextSymbol(DynamicLibrary lib, String name, long address) {
super(lib, name, new SymbolMemory(lib, address));
}
}
@Untraversable
public static final class DataSymbol extends Symbol implements ExposeAsSuperclass {
public DataSymbol(DynamicLibrary lib, String name, long address) {
super(lib, name, new SymbolMemory(lib, address));
}
}
private static final class SymbolMemory extends NativeMemory {
private final DynamicLibrary library; // backlink to keep library alive
public SymbolMemory(DynamicLibrary library, long address) {
super(address);
this.library = library;
}
}
}