|
5 | 5 | #include "java-interop-logger.h" |
6 | 6 | #include "java-interop-util.h" |
7 | 7 |
|
8 | | -static struct DylibJVM *jvm = NULL; |
9 | 8 |
|
10 | | -int java_interop_jvm_load (const char *path) |
| 9 | +typedef int (*java_interop_JNI_CreateJavaVM_fptr) (JavaVM **p_vm, void **p_env, void *vm_args); |
| 10 | +typedef int (*java_interop_JNI_GetCreatedJavaVMs_fptr) (JavaVM **vmBuf, int bufLen, int *nVMs); |
| 11 | + |
| 12 | +struct DylibJVM { |
| 13 | + void *dl_handle; |
| 14 | + java_interop_JNI_CreateJavaVM_fptr JNI_CreateJavaVM; |
| 15 | + java_interop_JNI_GetCreatedJavaVMs_fptr JNI_GetCreatedJavaVMs; |
| 16 | +}; |
| 17 | + |
| 18 | +static struct DylibJVM *jvm; |
| 19 | + |
| 20 | +int |
| 21 | +java_interop_jvm_load_handle (void *handle) |
11 | 22 | { |
12 | | - jvm = calloc (1, sizeof (struct DylibJVM)); |
| 23 | + if (jvm != NULL) { |
| 24 | + return JAVA_INTEROP_JVM_FAILED_LOADED; |
| 25 | + } |
| 26 | + if (handle == NULL) { |
| 27 | + return JAVA_INTEROP_JVM_FAILED_LOAD; |
| 28 | + } |
13 | 29 |
|
14 | | - jvm->dl_handle = dlopen (path, RTLD_LAZY); |
| 30 | + jvm = calloc (1, sizeof (struct DylibJVM)); |
15 | 31 |
|
16 | | - if (!jvm->dl_handle) |
17 | | - return 0; |
| 32 | + jvm->dl_handle = handle; |
18 | 33 |
|
19 | 34 | int symbols_missing = 0; |
20 | 35 |
|
21 | | -#define LOAD_SYMBOL(symbol) \ |
22 | | - jvm->symbol = dlsym (jvm->dl_handle, #symbol); \ |
23 | | - if (!jvm->symbol) { \ |
24 | | - log_error (LOG_DEFAULT, "Failed to load JVM symbol: %s", #symbol); \ |
25 | | - symbols_missing = 1; \ |
26 | | - } |
| 36 | +#define LOAD_SYMBOL(symbol) do { \ |
| 37 | + jvm->symbol = dlsym (jvm->dl_handle, #symbol); \ |
| 38 | + if (!jvm->symbol) { \ |
| 39 | + log_error (LOG_DEFAULT, "Failed to load JVM symbol: %s", #symbol); \ |
| 40 | + symbols_missing = 1; \ |
| 41 | + } \ |
| 42 | + } while (0) |
| 43 | + |
| 44 | + LOAD_SYMBOL(JNI_CreateJavaVM); |
| 45 | + LOAD_SYMBOL(JNI_GetCreatedJavaVMs); |
27 | 46 |
|
28 | | - LOAD_SYMBOL(JNI_CreateJavaVM) |
29 | | - LOAD_SYMBOL(JNI_GetCreatedJavaVMs) |
| 47 | +#undef LOAD_SYMBOL |
30 | 48 |
|
31 | 49 | if (symbols_missing) { |
32 | | - log_fatal (LOG_DEFAULT, "Failed to load some Mono symbols, aborting..."); |
33 | | - exit (FATAL_EXIT_JVM_MISSING_SYMBOLS); |
| 50 | + free (jvm); |
| 51 | + jvm = NULL; |
| 52 | + return JAVA_INTEROP_JVM_FAILED_SYMBOL; |
34 | 53 | } |
35 | 54 |
|
36 | | - return 1; |
| 55 | + return 0; |
37 | 56 | } |
38 | 57 |
|
39 | | -static inline void |
40 | | -_assert_dl_handle () |
| 58 | +int |
| 59 | +java_interop_jvm_load_path (const char *path) |
41 | 60 | { |
42 | | - if (!jvm || !jvm->dl_handle) { |
43 | | - log_fatal (LOG_DEFAULT, "Missing JVM symbols!"); |
44 | | - exit (FATAL_EXIT_JVM_MISSING_SYMBOLS); |
| 61 | + void *h = dlopen (path, RTLD_LAZY); |
| 62 | + if (!h) { |
| 63 | + return JAVA_INTEROP_JVM_FAILED_LOAD; |
45 | 64 | } |
| 65 | + int r = java_interop_jvm_load_handle (h); |
| 66 | + if (r < 0) { |
| 67 | + dlclose (h); |
| 68 | + return JAVA_INTEROP_JVM_FAILED_LOAD; |
| 69 | + } |
| 70 | + return r; |
46 | 71 | } |
47 | 72 |
|
| 73 | + |
| 74 | +#define ji_return_val_if_fail(expr, val) do { if (!(expr)) return (val); } while (0) |
| 75 | + |
48 | 76 | int java_interop_jvm_create (JavaVM **p_vm, void **p_env, void *vm_args) |
49 | 77 | { |
50 | | - _assert_dl_handle (); |
| 78 | + ji_return_val_if_fail (jvm != NULL, JAVA_INTEROP_JVM_FAILED_LOAD); |
51 | 79 |
|
52 | 80 | return (*jvm->JNI_CreateJavaVM) (p_vm, p_env, vm_args); |
53 | 81 | } |
54 | 82 |
|
55 | 83 | int java_interop_jvm_list (JavaVM **vmBuf, int bufLen, int *nVMs) |
56 | 84 | { |
57 | | - _assert_dl_handle (); |
| 85 | + ji_return_val_if_fail (jvm != NULL, JAVA_INTEROP_JVM_FAILED_LOAD); |
58 | 86 |
|
59 | 87 | return (*jvm->JNI_GetCreatedJavaVMs) (vmBuf, bufLen, nVMs); |
60 | 88 | } |
0 commit comments