Merge pull request #146 from TheBlueMatt/main
[ldk-java] / java_strings.py
1 from bindingstypes import *
2 from enum import Enum
3 import sys
4
5 class Target(Enum):
6     JAVA = 1,
7     ANDROID = 2
8     MACOS = 3
9
10 class Consts:
11     def __init__(self, DEBUG: bool, target: Target, **kwargs):
12         self.target = target
13         self.c_array_class_caches = set()
14         self.c_type_map = dict(
15             bool = ['boolean'],
16             uint8_t = ['byte'],
17             uint16_t = ['short'],
18             uint32_t = ['int'],
19             uint64_t = ['long'],
20             int64_t = ['long'],
21             double = ['double'],
22         )
23         self.java_type_map = dict(
24             String = "String"
25         )
26         self.java_hu_type_map = dict(
27             String = "String"
28         )
29
30         self.to_hu_conv_templates = dict(
31             ptr = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }',
32             default = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }'
33         )
34
35         self.bindings_header = """package org.ldk.impl;
36 import org.ldk.enums.*;
37 import org.ldk.impl.version;
38 import java.io.File;
39 import java.io.InputStream;
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.StandardCopyOption;
44
45 public class bindings {
46         static {
47                 try {
48                         // Try to load natively first, this works on Android and in testing.
49                         System.loadLibrary(\"lightningjni\");
50                 } catch (UnsatisfiedLinkError system_load_err) {
51                         // Otherwise try to load from the library jar.
52                         File tmpdir = new File(System.getProperty("java.io.tmpdir"), "ldk-java-nativelib");
53                         tmpdir.mkdir(); // If it fails to create, assume it was there already
54                         tmpdir.deleteOnExit();
55                         String libname = "liblightningjni_" + System.getProperty("os.name").replaceAll(" ", "") +
56                                 "-" + System.getProperty("os.arch").replaceAll(" ", "") + ".nativelib";
57                         try (InputStream is = bindings.class.getResourceAsStream("/" + libname)) {
58                                 Path libpath = new File(tmpdir.toPath().toString(), "liblightningjni.so").toPath();
59                                 Files.copy(is, libpath, StandardCopyOption.REPLACE_EXISTING);
60                                 Runtime.getRuntime().load(libpath.toString());
61                         } catch (Exception e) {
62                                 System.err.println("Failed to load LDK native library.");
63                                 System.err.println("System LDK native library load failed with: " + system_load_err);
64                                 System.err.println("Resource-based LDK native library load failed with: " + e);
65                                 throw new IllegalArgumentException(e);
66                         }
67                 }
68                 init(java.lang.Enum.class);
69                 init_class_cache();
70                 if (!get_lib_version_string().equals(version.get_ldk_java_bindings_version()))
71                         throw new IllegalArgumentException("Compiled LDK library and LDK class failes do not match");
72                 // Fetching the LDK versions from C also checks that the header and binaries match
73                 System.err.println("Loaded LDK-Java Bindings " + version.get_ldk_java_bindings_version() + " with LDK " + get_ldk_version() + " and LDK-C-Bindings " + get_ldk_c_bindings_version());
74         }
75         static native void init(java.lang.Class c);
76         static native void init_class_cache();
77         static native String get_lib_version_string();
78
79         public static native String get_ldk_c_bindings_version();
80         public static native String get_ldk_version();
81
82 """
83         self.bindings_version_file = """package org.ldk.impl;
84
85 public class version {
86         public static String get_ldk_java_bindings_version() {
87                 return "<git_version_ldk_garbagecollected>";
88         }
89 }"""
90
91         self.util_fn_pfx = """package org.ldk.structs;
92 import org.ldk.impl.bindings;
93 import org.ldk.enums.*;
94 import org.ldk.util.*;
95 import java.util.Arrays;
96 import java.lang.ref.Reference;
97 import javax.annotation.Nullable;
98
99 public class UtilMethods {
100 """
101         self.util_fn_sfx = "}"
102         self.common_base = """package org.ldk.structs;
103 import java.util.LinkedList;
104 class CommonBase {
105         long ptr;
106         LinkedList<Object> ptrs_to = new LinkedList();
107         protected CommonBase(long ptr) { assert ptr < 0 || ptr > 4096; this.ptr = ptr; }
108 }
109 """
110
111         self.txout_defn = """public class TxOut extends CommonBase {
112         /** The script_pubkey in this output */
113         public final byte[] script_pubkey;
114         /** The value, in satoshis, of this output */
115         public final long value;
116
117         TxOut(java.lang.Object _dummy, long ptr) {
118                 super(ptr);
119                 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
120                 this.value = bindings.TxOut_get_value(ptr);
121         }
122         public TxOut(long value, byte[] script_pubkey) {
123                 super(bindings.TxOut_new(script_pubkey, value));
124                 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
125                 this.value = bindings.TxOut_get_value(ptr);
126         }
127
128         @Override @SuppressWarnings(\"deprecation\")
129         protected void finalize() throws Throwable {
130                 super.finalize();
131                 if (ptr != 0) { bindings.TxOut_free(ptr); }
132         }
133 }"""
134
135         self.txin_defn = """public class TxIn extends CommonBase {
136         /** The witness in this input, in serialized form */
137         public final byte[] witness;
138         /** The script_sig in this input */
139         public final byte[] script_sig;
140         /** The transaction output's sequence number */
141         public final int sequence;
142         /** The txid this input is spending */
143         public final byte[] previous_txid;
144         /** The output index within the spent transaction of the output this input is spending */
145         public final int previous_vout;
146
147         TxIn(java.lang.Object _dummy, long ptr) {
148                 super(ptr);
149                 this.witness = bindings.TxIn_get_witness(ptr);
150                 this.script_sig = bindings.TxIn_get_script_sig(ptr);
151                 this.sequence = bindings.TxIn_get_sequence(ptr);
152                 this.previous_txid = bindings.TxIn_get_previous_txid(ptr);
153                 this.previous_vout = bindings.TxIn_get_previous_vout(ptr);
154         }
155         /** Constructs a new TxIn, note that previous_txid must be exactly 32 bytes */
156         public TxIn(byte[] witness, byte[] script_sig, int sequence, byte[] previous_txid, int previous_vout) {
157                 this(null, bindings.TxIn_new(witness, script_sig, sequence, previous_txid, previous_vout));
158         }
159
160         @Override @SuppressWarnings(\"deprecation\")
161         protected void finalize() throws Throwable {
162                 super.finalize();
163                 if (ptr != 0) { bindings.TxIn_free(ptr); }
164         }
165 }"""
166
167         self.scalar_defn = """public class BigEndianScalar extends CommonBase {
168         /** The bytes of the scalar value, in big endian */
169         public final byte[] scalar_bytes;
170
171         BigEndianScalar(java.lang.Object _dummy, long ptr) {
172                 super(ptr);
173                 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
174         }
175         public BigEndianScalar(byte[] scalar_bytes) {
176                 super(bindings.BigEndianScalar_new(scalar_bytes));
177                 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
178         }
179
180         @Override @SuppressWarnings(\"deprecation\")
181         protected void finalize() throws Throwable {
182                 super.finalize();
183                 if (ptr != 0) { bindings.BigEndianScalar_free(ptr); }
184         }
185 }"""
186
187         self.witness_program_defn = """public class WitnessProgram extends CommonBase {
188         /** The witness program bytes themselves */
189         public final byte[] program;
190         /** The witness version */
191         public final WitnessVersion version;
192
193         WitnessProgram(java.lang.Object _dummy, long ptr) {
194                 super(ptr);
195                 this.program = bindings.WitnessProgram_get_program(ptr);
196                 this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
197         }
198         static byte check_args(byte[] program, WitnessVersion version) {
199                 if (program.length < 2 || program.length > 40) throw new IllegalArgumentException();
200                 if (version.getVal() == 0 && program.length != 20 && program.length != 32) throw new IllegalArgumentException();
201                 return version.getVal();
202         }
203         public WitnessProgram(byte[] program, WitnessVersion version) {
204                 super(bindings.WitnessProgram_new(check_args(program, version), program));
205                 this.program = bindings.WitnessProgram_get_program(ptr);
206                 this.version = new WitnessVersion(bindings.WitnessProgram_get_version(ptr));
207         }
208
209         @Override @SuppressWarnings(\"deprecation\")
210         protected void finalize() throws Throwable {
211                 super.finalize();
212                 if (ptr != 0) { bindings.WitnessProgram_free(ptr); }
213         }
214 }"""
215
216         self.c_file_pfx = """#include <jni.h>
217 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
218 #define int64_t jlong
219 #include \"org_ldk_impl_bindings.h\"
220 #include <lightning.h>
221 #include <string.h>
222 #include <stdatomic.h>
223 #include <stdlib.h>
224
225 #define LIKELY(v) __builtin_expect(!!(v), 1)
226 #define UNLIKELY(v) __builtin_expect(!!(v), 0)
227
228 """
229
230         if self.target == Target.ANDROID:
231             self.c_file_pfx = self.c_file_pfx + """
232 #include <android/log.h>
233 #define DEBUG_PRINT(...) __android_log_print(ANDROID_LOG_ERROR, "LDK", __VA_ARGS__)
234
235 #include <unistd.h>
236 #include <pthread.h>
237 static void* redirect_stderr(void* ignored) {
238         int pipes[2];
239         pipe(pipes);
240         dup2(pipes[1], STDERR_FILENO);
241         char buf[8192];
242         memset(buf, 0, 8192);
243         ssize_t len;
244         while ((len = read(pipes[0], buf, 8191)) > 0) {
245                 DEBUG_PRINT("%s\\n", buf);
246                 memset(buf, 0, 8192);
247         }
248         DEBUG_PRINT("End of stderr???\\n");
249         return 0;
250 }
251 void __attribute__((constructor)) spawn_stderr_redirection() {
252         pthread_t thread;
253         pthread_create(&thread, NULL, &redirect_stderr, NULL);
254 }
255 """
256         else:
257             self.c_file_pfx = self.c_file_pfx + "#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
258
259         if not DEBUG or self.target == Target.MACOS:
260             self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
261 #define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
262 #define CHECK_ACCESS(p)
263 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
264 """
265         if not DEBUG:
266             self.c_file_pfx += """#define DO_ASSERT(a) (void)(a)
267 #define CHECK(a)
268 """
269         if DEBUG:
270             self.c_file_pfx = self.c_file_pfx + """#include <assert.h>
271 // Always run a, then assert it is true:
272 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
273 // Assert a is true or do nothing
274 #define CHECK(a) DO_ASSERT(a)
275
276 void __attribute__((constructor)) debug_log_version() {
277         if (check_get_ldk_version() == NULL)
278                 DEBUG_PRINT("LDK version did not match the header we built against\\n");
279         if (check_get_ldk_bindings_version() == NULL)
280                 DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
281 }
282 """
283
284             if self.target != Target.MACOS:
285                 self.c_file_pfx += """
286 // Running a leak check across all the allocations and frees of the JDK is a mess,
287 // so instead we implement our own naive leak checker here, relying on the -wrap
288 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
289 // and free'd in Rust or C across the generated bindings shared library.
290 #include <threads.h>
291 """
292
293                 if self.target == Target.ANDROID:
294                     self.c_file_pfx = self.c_file_pfx + """
295 #include <unwind.h>
296 #include <dlfcn.h>
297
298 // Implement the execinfo functions using _Unwind_backtrace. This seems to miss many Rust
299 // symbols, so we only use it on Android, where execinfo.h is unavailable.
300
301 struct BacktraceState {
302         void** current;
303         void** end;
304 };
305 static _Unwind_Reason_Code unwind_callback(struct _Unwind_Context* context, void* arg) {
306         struct BacktraceState* state = (struct BacktraceState*)arg;
307         uintptr_t pc = _Unwind_GetIP(context);
308         if (pc) {
309                 if (state->current == state->end) {
310                         return _URC_END_OF_STACK;
311                 } else {
312                         *state->current++ = (void*)(pc);
313                 }
314         }
315         return _URC_NO_REASON;
316 }
317
318 int backtrace(void** buffer, int max) {
319         struct BacktraceState state = { buffer, buffer + max };
320         _Unwind_Backtrace(unwind_callback, &state);
321         return state.current - buffer;
322 }
323
324 void backtrace_symbols_fd(void ** buffer, int count, int _fd) {
325         for (int idx = 0; idx < count; ++idx) {
326                 Dl_info info;
327                 if (dladdr(buffer[idx], &info) && info.dli_sname) {
328                         DEBUG_PRINT("%p: %s", buffer[idx], info.dli_sname);
329                 } else {
330                         DEBUG_PRINT("%p: ???", buffer[idx]);
331                 }
332         }
333 }
334 """
335                 else:
336                     self.c_file_pfx = self.c_file_pfx + "#include <execinfo.h>\n"
337                 self.c_file_pfx = self.c_file_pfx + """
338 #include <unistd.h>
339 #include <pthread.h>
340 static pthread_mutex_t allocation_mtx;
341
342 void __attribute__((constructor)) init_mtx() {
343         DO_ASSERT(!pthread_mutex_init(&allocation_mtx, NULL));
344 }
345
346 #define BT_MAX 128
347 typedef struct allocation {
348         struct allocation* next;
349         void* ptr;
350         const char* struct_name;
351         void* bt[BT_MAX];
352         int bt_len;
353         unsigned long alloc_len;
354 } allocation;
355 static allocation* allocation_ll = NULL;
356
357 void* __real_malloc(size_t len);
358 void* __real_calloc(size_t nmemb, size_t len);
359 static void new_allocation(void* res, const char* struct_name, size_t len) {
360         allocation* new_alloc = __real_malloc(sizeof(allocation));
361         new_alloc->ptr = res;
362         new_alloc->struct_name = struct_name;
363         new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
364         new_alloc->alloc_len = len;
365         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
366         new_alloc->next = allocation_ll;
367         allocation_ll = new_alloc;
368         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
369 }
370 static void* MALLOC(size_t len, const char* struct_name) {
371         void* res = __real_malloc(len);
372         new_allocation(res, struct_name, len);
373         return res;
374 }
375 void __real_free(void* ptr);
376 static void alloc_freed(void* ptr) {
377         allocation* p = NULL;
378         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
379         allocation* it = allocation_ll;
380         while (it->ptr != ptr) {
381                 p = it; it = it->next;
382                 if (it == NULL) {
383                         DEBUG_PRINT("ERROR: Tried to free unknown pointer %p at:\\n", ptr);
384                         void* bt[BT_MAX];
385                         int bt_len = backtrace(bt, BT_MAX);
386                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
387                         DEBUG_PRINT("\\n\\n");
388                         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
389                         return; // addrsan should catch malloc-unknown and print more info than we have
390                 }
391         }
392         if (p) { p->next = it->next; } else { allocation_ll = it->next; }
393         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
394         DO_ASSERT(it->ptr == ptr);
395         __real_free(it);
396 }
397 static void FREE(void* ptr) {
398         if ((uint64_t)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys
399         alloc_freed(ptr);
400         __real_free(ptr);
401 }
402
403 void* __wrap_malloc(size_t len) {
404         void* res = __real_malloc(len);
405         new_allocation(res, "malloc call", len);
406         return res;
407 }
408 void* __wrap_calloc(size_t nmemb, size_t len) {
409         void* res = __real_calloc(nmemb, len);
410         new_allocation(res, "calloc call", len);
411         return res;
412 }
413 void __wrap_free(void* ptr) {
414         if (ptr == NULL) return;
415         alloc_freed(ptr);
416         __real_free(ptr);
417 }
418
419 static void CHECK_ACCESS(const void* ptr) {
420         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
421         allocation* it = allocation_ll;
422         while (it->ptr != ptr) {
423                 it = it->next;
424                 if (it == NULL) {
425                         DEBUG_PRINT("ERROR: Tried to access unknown pointer %p at:\\n", ptr);
426                         void* bt[BT_MAX];
427                         int bt_len = backtrace(bt, BT_MAX);
428                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
429                         DEBUG_PRINT("\\n\\n");
430                         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
431                         return; // addrsan should catch and print more info than we have
432                 }
433         }
434         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
435 }
436 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
437         if (v.is_owned && v.inner != NULL) { \\
438                 const void *p = __unmangle_inner_ptr(v.inner); \\
439                 if (p != NULL) { \\
440                         CHECK_ACCESS(p); \\
441                 } \\
442         }
443
444 void* __real_realloc(void* ptr, size_t newlen);
445 void* __wrap_realloc(void* ptr, size_t len) {
446         if (ptr != NULL) alloc_freed(ptr);
447         void* res = __real_realloc(ptr, len);
448         new_allocation(res, "realloc call", len);
449         return res;
450 }
451 void __wrap_reallocarray(void* ptr, size_t new_sz) {
452         // Rust doesn't seem to use reallocarray currently
453         DO_ASSERT(false);
454 }
455
456 void __attribute__((destructor)) check_leaks() {
457         unsigned long alloc_count = 0;
458         unsigned long alloc_size = 0;
459         DEBUG_PRINT("The following LDK-allocated blocks still remain.\\n");
460         DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
461         DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
462         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
463                 DEBUG_PRINT("%s %p (%lu bytes) remains:\\n", a->struct_name, a->ptr, a->alloc_len);
464                 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
465                 DEBUG_PRINT("\\n\\n");
466                 alloc_count++;
467                 alloc_size += a->alloc_len;
468         }
469         DEBUG_PRINT("%lu allocations remained for %lu bytes.\\n", alloc_count, alloc_size);
470         DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
471         DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
472 }
473 """
474         self.c_file_pfx = self.c_file_pfx + """
475 static jmethodID ordinal_meth = NULL;
476 JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class) {
477         ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
478         CHECK(ordinal_meth != NULL);
479 }
480
481 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
482 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
483 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
484 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
485
486 _Static_assert(sizeof(jlong) == sizeof(int64_t), "We assume that j-types are the same as C types");
487 _Static_assert(sizeof(jbyte) == sizeof(char), "We assume that j-types are the same as C types");
488 _Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
489
490 typedef jlongArray int64_tArray;
491 typedef jbyteArray int8_tArray;
492 typedef jshortArray int16_tArray;
493
494 static inline jstring str_ref_to_java(JNIEnv *env, const unsigned char* chars, size_t len) {
495         // Java uses "Modified UTF-8" rather than UTF-8. This requires special
496         // handling for codepoints above 0xFFFF, which get converted from four
497         // bytes to six. We don't know upfront how many codepoints in the string
498         // are above 0xFFFF, so we just allocate an extra 33% up front and waste a
499         // bit of space.
500         unsigned char* java_chars = MALLOC(len * 3 / 2 + 1, "str conv buf");
501         unsigned char* next_java_char = java_chars;
502         const unsigned char* next_in_char = chars;
503         const unsigned char* end = chars + len;
504         #define COPY_CHAR_TO_JAVA do { *next_java_char = *next_in_char; next_java_char++; next_in_char++; } while (0)
505
506         while (next_in_char < end) {
507                 if (!*next_in_char) break;
508                 if (!(*next_in_char & 0b10000000)) {
509                         COPY_CHAR_TO_JAVA;
510                 } else if ((*next_in_char & 0b11100000) == 0b11000000) {
511                         if (next_in_char + 2 > end) { CHECK(false); break; } // bad string
512                         COPY_CHAR_TO_JAVA;
513                         COPY_CHAR_TO_JAVA;
514                 } else if ((*next_in_char & 0b11110000) == 0b11100000) {
515                         if (next_in_char + 3 > end) { CHECK(false); break; } // bad string
516                         COPY_CHAR_TO_JAVA;
517                         COPY_CHAR_TO_JAVA;
518                         COPY_CHAR_TO_JAVA;
519                 } else if ((*next_in_char & 0b11111000) == 0b11110000) {
520                         if (next_in_char + 4 > end) { CHECK(false); break; } // bad string
521                         uint32_t codepoint = 0;
522                         codepoint |= (((uint32_t)*(next_in_char    )) & 0b00000111) << 18;
523                         codepoint |= (((uint32_t)*(next_in_char + 1)) & 0b00111111) << 12;
524                         codepoint |= (((uint32_t)*(next_in_char + 2)) & 0b00111111) << 6;
525                         codepoint |= (((uint32_t)*(next_in_char + 3)) & 0b00111111) << 0;
526                         codepoint -= 0x10000;
527                         *next_java_char = 0b11101101;
528                         next_java_char++;
529                         *next_java_char = 0b10100000 | ((codepoint >> 16) & 0b00001111);
530                         next_java_char++;
531                         *next_java_char = 0b10000000 | ((codepoint >> 10) & 0b00111111);
532                         next_java_char++;
533                         *next_java_char = 0b11101101;
534                         next_java_char++;
535                         *next_java_char = 0b10110000 | ((codepoint >>  6) & 0b00001111);
536                         next_java_char++;
537                         *next_java_char = 0b10000000 | ((codepoint >>  0) & 0b00111111);
538                         next_java_char++;
539                         next_in_char += 4;
540                 } else {
541                         // Bad string
542                         CHECK(false);
543                         break;
544                 }
545         }
546         *next_java_char = 0;
547         jstring ret = (*env)->NewStringUTF(env, java_chars);
548         FREE(java_chars);
549         return ret;
550 }
551 static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
552         uint64_t str_len = (*env)->GetStringUTFLength(env, str);
553         // Java uses "Modified UTF-8" rather than UTF-8. This requires special
554         // handling for codepoints above 0xFFFF, which we implement below.
555         unsigned char* newchars = MALLOC(str_len, "String chars");
556         unsigned char* next_newchar = newchars;
557         uint64_t utf8_len = 0;
558
559         const unsigned char* jchars = (*env)->GetStringUTFChars(env, str, NULL);
560         const unsigned char* next_char = jchars;
561         const unsigned char* end = jchars + str_len;
562
563         #define COPY_CHAR_FROM_JAVA do { *next_newchar = *next_char; next_newchar++; next_char++; utf8_len++; } while (0)
564
565         while (next_char < end) {
566                 if (!(*next_char & 0b10000000)) {
567                         CHECK(*next_char != 0); // Bad Modified UTF-8 string, but we'll just cut here
568                         COPY_CHAR_FROM_JAVA;
569                 } else if ((*next_char & 0b11100000) == 0b11000000) {
570                         if (next_char + 2 > end) { CHECK(false); break; } // bad string
571                         uint16_t codepoint = 0;
572                         codepoint |= (((uint16_t)(*next_char & 0x1f)) << 6);
573                         codepoint |= *(next_char + 1) & 0x3f;
574                         if (codepoint == 0) {
575                                 // We should really never get null codepoints, but java allows them.
576                                 // Just skip it.
577                                 next_char += 2;
578                         } else {
579                                 COPY_CHAR_FROM_JAVA;
580                                 COPY_CHAR_FROM_JAVA;
581                         }
582                 } else if ((*next_char & 0b11110000) == 0b11100000) {
583                         if (next_char + 3 > end) { CHECK(false); break; } // bad string
584                         if (*next_char == 0b11101101 && (*(next_char + 1) & 0b11110000) == 0b10100000) {
585                                 // Surrogate code unit shoul indicate we have a codepoint above
586                                 // 0xFFFF, which is where Modified UTF-8 and UTF-8 diverge.
587                                 if (next_char + 6 > end) { CHECK(false); break; } // bad string
588                                 CHECK(*(next_char + 3) == 0b11101101);
589                                 CHECK((*(next_char + 4) & 0b11110000) == 0b10110000);
590                                 // Calculate the codepoint per https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp16542
591                                 uint32_t codepoint = 0x10000;
592                                 codepoint += ((((uint32_t)*(next_char + 1)) & 0x0f) << 16);
593                                 codepoint += ((((uint32_t)*(next_char + 2)) & 0x3f) << 10);
594                                 codepoint += ((((uint32_t)*(next_char + 4)) & 0x0f) <<  6);
595                                 codepoint +=  (((uint32_t)*(next_char + 5)) & 0x3f);
596                                 *next_newchar = 0b11110000 | ((codepoint >> 18) &    0b111);
597                                 next_newchar++;
598                                 *next_newchar = 0b10000000 | ((codepoint >> 12) & 0b111111);
599                                 next_newchar++;
600                                 *next_newchar = 0b10000000 | ((codepoint >>  6) & 0b111111);
601                                 next_newchar++;
602                                 *next_newchar = 0b10000000 | ( codepoint        & 0b111111);
603                                 next_newchar++;
604                                 next_char += 6;
605                                 utf8_len += 4;
606                         } else {
607                                 COPY_CHAR_FROM_JAVA;
608                                 COPY_CHAR_FROM_JAVA;
609                                 COPY_CHAR_FROM_JAVA;
610                         }
611                 } else {
612                         // Bad string
613                         CHECK(false);
614                         break;
615                 }
616         }
617         (*env)->ReleaseStringUTFChars(env, str, jchars);
618         LDKStr res = {
619                 .chars = newchars,
620                 .len = utf8_len,
621                 .chars_is_owned = true
622         };
623         return res;
624 }
625
626 JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
627         return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
628 }
629 JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1version(JNIEnv *env, jclass _c) {
630         return str_ref_to_java(env, check_get_ldk_version(), strlen(check_get_ldk_version()));
631 }
632 #include "version.c"
633 """
634         self.c_version_file = """JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
635         return str_ref_to_java(env, "<git_version_ldk_garbagecollected>", strlen("<git_version_ldk_garbagecollected>"));
636 }"""
637
638         self.hu_struct_file_prefix = """package org.ldk.structs;
639
640 import org.ldk.impl.bindings;
641 import org.ldk.enums.*;
642 import org.ldk.util.*;
643 import java.util.Arrays;
644 import java.lang.ref.Reference;
645 import javax.annotation.Nullable;
646
647 """
648         self.hu_struct_file_suffix = ""
649         self.c_fn_ty_pfx = "JNIEXPORT "
650         self.c_fn_args_pfx = "JNIEnv *env, jclass clz"
651         self.file_ext = ".java"
652         self.ptr_c_ty = "int64_t"
653         self.ptr_native_ty = "long"
654         self.u128_native_ty = "UInt128"
655         self.usize_c_ty = "int64_t"
656         self.usize_native_ty = "long"
657         self.native_zero_ptr = "0"
658         self.unitary_enum_c_ty = "jclass"
659         self.ptr_arr = "jobjectArray"
660         self.is_arr_some_check = ("", " != NULL")
661         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
662
663     def bindings_footer(self):
664         return "}\n"
665
666     def construct_jenv(self):
667         res =  "JNIEnv *env;\n"
668         res += "jint get_jenv_res = (*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_6);\n"
669         res += "if (get_jenv_res == JNI_EDETACHED) {\n"
670         if self.target == Target.ANDROID:
671             res += "\tDO_ASSERT((*j_calls->vm)->AttachCurrentThread(j_calls->vm, &env, NULL) == JNI_OK);\n"
672         else:
673             res += "\tDO_ASSERT((*j_calls->vm)->AttachCurrentThread(j_calls->vm, (void**)&env, NULL) == JNI_OK);\n"
674         res += "} else {\n"
675         res += "\tDO_ASSERT(get_jenv_res == JNI_OK);\n"
676         res += "}\n"
677         return res
678     def deconstruct_jenv(self):
679         res = "if (get_jenv_res == JNI_EDETACHED) {\n"
680         res += "\tDO_ASSERT((*j_calls->vm)->DetachCurrentThread(j_calls->vm) == JNI_OK);\n"
681         res += "}\n"
682         return res
683
684     def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):
685         if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
686             return "(*env)->ReleasePrimitiveArrayCritical(env, " + arr_var + ", " + arr_ptr_var + ", 0)"
687         return None
688     def create_native_arr_call(self, arr_len, ty_info):
689         if ty_info.c_ty == "int8_tArray":
690             return "(*env)->NewByteArray(env, " + arr_len + ")"
691         elif ty_info.subty.c_ty.endswith("Array"):
692             clz_var = ty_info.java_fn_ty_arg[1:].replace("[", "arr_of_")
693             self.c_array_class_caches.add(clz_var)
694             return "(*env)->NewObjectArray(env, " + arr_len + ", " + clz_var + "_clz, NULL);\n"
695         elif ty_info.subty.c_ty == "jstring":
696             return "(*env)->NewObjectArray(env, " + arr_len + ", String_clz, NULL);\n"
697         else:
698             return "(*env)->New" + ty_info.java_ty.strip("[]").title() + "Array(env, " + arr_len + ")"
699     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
700         if ty_info.c_ty == "int8_tArray":
701             return ("(*env)->SetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
702         elif ty_info.c_ty == "int16_tArray":
703             return ("(*env)->SetShortArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
704         else:
705             assert False
706     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
707         if "String" in ty_info.java_ty:
708             return None
709         if ty_info.c_ty == "int8_tArray" or ty_info.c_ty == "int16_tArray":
710             fn_ty = "Byte" if ty_info.c_ty == "int8_tArray" else "Short"
711             if copy:
712                 return "(*env)->Get" + fn_ty + "ArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", " + dest_name + ")"
713             else:
714                 return "(*env)->Get" + fn_ty + "ArrayElements (env, " + arr_name + ", NULL)"
715         elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
716             return "(*env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (env, " + arr_name + ", NULL)"
717         else:
718             return None
719     def get_native_arr_elem(self, arr_name, idxc, ty_info):
720         if self.get_native_arr_contents(arr_name, "", "", ty_info, False) is None:
721             return "(*env)->GetObjectArrayElement(env, " + arr_name + ", " + idxc + ")"
722         else:
723             assert False # Only called if above is None
724     def get_native_arr_ptr_call(self, ty_info):
725         if ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array"):
726             return None
727         return ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
728     def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
729         if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
730             return None
731         return "(*env)->SetObjectArrayElement(env, " + arr_name + ", " + idxc + ", " + entry_access + ")"
732     def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
733         if ty_info.c_ty == "int8_tArray":
734             return "(*env)->ReleaseByteArrayElements(env, " + arr_name + ", (int8_t*)" + dest_name + ", 0);"
735         else:
736             return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)"
737
738     def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty, is_nullable):
739         if elem_ty.java_ty == "long" and elem_ty.java_hu_ty != "long":
740             return arr_name + " != null ? Arrays.stream(" + arr_name + ").mapToLong(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray() : null"
741         elif elem_ty.java_ty == "long":
742             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray() : null"
743         elif elem_ty.java_hu_ty == "UInt5":
744             return arr_name + " != null ? InternalUtils.convUInt5Array(" + arr_name + ") : null"
745         elif elem_ty.java_hu_ty == "WitnessVersion":
746             return arr_name + " != null ? InternalUtils.convWitnessVersionArray(" + arr_name + ") : null"
747         else:
748             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray(" + arr_ty.java_ty + "::new) : null"
749
750     def str_ref_to_native_call(self, var_name, str_len):
751         return "str_ref_to_java(env, " + var_name + ", " + str_len + ")"
752     def str_ref_to_c_call(self, var_name):
753         return "java_to_owned_str(env, " + var_name + ")"
754     def str_to_hu_conv(self, var_name):
755         return None
756     def str_from_hu_conv(self, var_name):
757         return None
758
759     def c_fn_name_define_pfx(self, fn_name, has_args):
760         if has_args:
761             return "JNICALL Java_org_ldk_impl_bindings_" + fn_name.replace("_", "_1") + "(JNIEnv *env, jclass clz, "
762         return "JNICALL Java_org_ldk_impl_bindings_" + fn_name.replace("_", "_1") + "(JNIEnv *env, jclass clz"
763
764     def init_str(self):
765         res = ""
766         for ty in sorted(self.c_array_class_caches):
767             res = res + "static jclass " + ty + "_clz = NULL;\n"
768         res = res + "static jclass String_clz = NULL;\n"
769         res = res + "JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass clz) {\n"
770         for ty in sorted(self.c_array_class_caches):
771             res = res + "\t" + ty + "_clz = (*env)->FindClass(env, \"" + ty.replace("arr_of_", "[") + "\");\n"
772             res = res + "\tCHECK(" + ty + "_clz != NULL);\n"
773             res = res + "\t" + ty + "_clz = (*env)->NewGlobalRef(env, " + ty + "_clz);\n"
774         res = res + "\tString_clz = (*env)->FindClass(env, \"java/lang/String\");\n"
775         res = res + "\tCHECK(String_clz != NULL);\n"
776         res = res + "\tString_clz = (*env)->NewGlobalRef(env, String_clz);\n"
777
778         res = res + "}\n"
779         return res
780
781     def var_decl_statement(self, ty_string, var_name, statement):
782         return ty_string + " " + var_name + " = " + statement
783
784     def get_java_arr_len(self, arr_name):
785         return arr_name + ".length"
786     def get_java_arr_elem(self, elem_ty, arr_name, idx):
787         return arr_name + "[" + idx + "]"
788     def constr_hu_array(self, ty_info, arr_len):
789         base_ty = ty_info.subty.java_hu_ty.split("[")[0].split("<")[0]
790         conv = "new " + base_ty + "[" + arr_len + "]"
791         if "[" in ty_info.subty.java_hu_ty.split("<")[0]:
792             # Do a bit of a dance to move any excess [] to the end
793             conv += "[" + ty_info.subty.java_hu_ty.split("<")[0].split("[")[1]
794         return conv
795     def cleanup_converted_native_array(self, ty_info, arr_name):
796         return None
797
798     def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
799         mapped_ty = arr_ty.subty
800         if arr_ty.rust_obj == "LDKU128":
801             return ("" + arr_name + ".getLEBytes()", "")
802         if fixed_len is not None:
803             if mapped_ty.c_ty == "int8_t" or mapped_ty.c_ty == "uint8_t":
804                 return ("InternalUtils.check_arr_len(" + arr_name + ", " + fixed_len + ")", "")
805             elif mapped_ty.c_ty == "int16_t" or mapped_ty.c_ty == "uint16_t":
806                 return ("InternalUtils.check_arr_16_len(" + arr_name + ", " + fixed_len + ")", "")
807             else:
808                 print(arr_ty.c_ty)
809                 assert False
810         return None
811     def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
812         if arr_ty.rust_obj == "LDKU128":
813             return "org.ldk.util.UInt128 " + conv_name + " = new org.ldk.util.UInt128(" + arr_name + ");"
814         return None
815
816     def java_arr_ty_str(self, elem_ty_str):
817         return elem_ty_str + "[]"
818
819     def for_n_in_range(self, n, minimum, maximum):
820         return "for (int " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
821     def for_n_in_arr(self, n, arr_name, arr_elem_ty):
822         return ("for (" + arr_elem_ty.java_hu_ty + " " + n + ": " + arr_name + ") { ", " }")
823
824     def get_ptr(self, var):
825         return var + ".ptr"
826     def set_null_skip_free(self, var):
827         return var + ".ptr" + " = 0;"
828
829     def add_ref(self, holder, referent):
830         return "if (" + holder + " != null) { " + holder + ".ptrs_to.add(" + referent + "); }"
831
832     def fully_qualified_hu_ty_path(self, ty):
833         if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
834             return ty.java_fn_ty_arg.strip("L;").replace("/", ".")
835         if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt128" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
836             return "org.ldk.util." + ty.java_hu_ty
837         if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
838             return "org.ldk.structs." + ty.java_hu_ty
839         return ty.java_hu_ty
840
841     def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
842         out_java_enum = "package org.ldk.enums;\n\n"
843         out_java = ""
844         out_c = ""
845         out_c += "static inline LDK" + struct_name + " LDK" + struct_name + "_from_java(" + self.c_fn_args_pfx + ") {\n"
846         out_c += "\tjint ord = (*env)->CallIntMethod(env, clz, ordinal_meth);\n"
847         out_c += "\tif (UNLIKELY((*env)->ExceptionCheck(env))) {\n"
848         out_c += "\t\t(*env)->ExceptionDescribe(env);\n"
849         out_c += "\t\t(*env)->FatalError(env, \"A call to " + struct_name + ".ordinal() from rust threw an exception.\");\n"
850         out_c += "\t}\n"
851         out_c += "\tswitch (ord) {\n"
852
853         if enum_doc_comment is not None:
854             out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
855         out_java_enum += "public enum " + struct_name + " {\n"
856         ord_v = 0
857         for var, var_docs in variants:
858             if var_docs is not None:
859                 out_java_enum += "\t/**\n\t * " + var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
860             out_java_enum += "\t" + var + ",\n"
861             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
862             ord_v = ord_v + 1
863         out_java_enum = out_java_enum + "\t; static native void init();\n"
864         out_java_enum = out_java_enum + "\tstatic { init(); }\n"
865         out_java_enum = out_java_enum + "}"
866         out_java = out_java + "\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n"
867         out_c += "\t}\n"
868         out_c += "\t(*env)->FatalError(env, \"A call to " + struct_name + ".ordinal() from rust returned an invalid value.\");\n"
869         out_c += "\tabort(); // Unreachable, but will let the compiler know we don't return here\n"
870         out_c += "}\n"
871
872         out_c = out_c + "static jclass " + struct_name + "_class = NULL;\n"
873         for var, _ in variants:
874             out_c = out_c + "static jfieldID " + struct_name + "_" + var + " = NULL;\n"
875         out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
876         out_c = out_c + "\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n"
877         out_c = out_c + "\tCHECK(" + struct_name + "_class != NULL);\n"
878         for var, _ in variants:
879             out_c = out_c + "\t" + struct_name + "_" + var + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + var + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n"
880             out_c = out_c + "\tCHECK(" + struct_name + "_" + var + " != NULL);\n"
881         out_c = out_c + "}\n"
882         out_c = out_c + "static inline jclass LDK" + struct_name + "_to_java(JNIEnv *env, LDK" + struct_name + " val) {\n"
883         out_c = out_c + "\tswitch (val) {\n"
884         ord_v = 0
885         for var, _ in variants:
886             out_c = out_c + "\t\tcase " + var + ":\n"
887             out_c = out_c + "\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + var + ");\n"
888             ord_v = ord_v + 1
889         out_c = out_c + "\t\tdefault: abort();\n"
890         out_c = out_c + "\t}\n"
891         out_c = out_c + "}\n\n"
892
893         return (out_c, out_java_enum, out_java)
894
895     def c_unitary_enum_to_native_call(self, ty_info):
896         return (ty_info.rust_obj + "_to_java(env, ", ")")
897     def native_unitary_enum_to_c_call(self, ty_info):
898         return (ty_info.rust_obj + "_from_java(env, ", ")")
899
900     def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
901         out_c = ""
902         for var in variants:
903             out_c = out_c + "static jclass " + struct_name + "_" + var + "_class = NULL;\n"
904             out_c = out_c + "static jmethodID " + struct_name + "_" + var + "_meth = NULL;\n"
905         out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
906         for var_name in variants:
907             out_c += "\t" + struct_name + "_" + var_name + "_class =\n"
908             out_c += "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"org/ldk/impl/bindings$" + struct_name + "$" + var_name + "\"));\n"
909             out_c += "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
910             out_c += "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
911             out_c += "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
912         out_c = out_c + "}\n"
913         return out_c
914
915     def c_complex_enum_pass_ty(self, struct_name):
916         return "jobject"
917
918     def c_constr_native_complex_enum(self, struct_name, variant, c_params):
919         ret = "(*env)->NewObject(env, " + struct_name + "_" + variant + "_class, " + struct_name + "_" + variant + "_meth"
920         for param in c_params:
921             ret = ret + ", " + param
922         return ret + ")"
923
924     def native_c_map_trait(self, struct_name, field_vars, flattened_field_vars, field_fns, trait_doc_comment):
925         out_java_trait = ""
926         out_java = ""
927
928         # First generate most of the Java code, note that we need information about java method argument strings for C
929         out_java_trait = out_java_trait + self.hu_struct_file_prefix
930         if trait_doc_comment is not None:
931             out_java_trait += "/**\n * " + trait_doc_comment.replace("\n", "\n * ") + "\n */\n"
932         out_java_trait += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
933         out_java_trait = out_java_trait + "public class " + struct_name.replace("LDK","") + " extends CommonBase {\n"
934         out_java_trait = out_java_trait + "\tfinal bindings." + struct_name + " bindings_instance;\n"
935         out_java_trait = out_java_trait + "\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n"
936         out_java_trait = out_java_trait + "\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg"
937         for var in flattened_field_vars:
938             if isinstance(var, ConvInfo):
939                 out_java_trait = out_java_trait + ", " + var.java_hu_ty + " " + var.arg_name
940             else:
941                 out_java_trait = out_java_trait + ", bindings." + var[0] + " " + var[1]
942         out_java_trait = out_java_trait + ") {\n"
943         out_java_trait = out_java_trait + "\t\tsuper(bindings." + struct_name + "_new(arg"
944         for var in flattened_field_vars:
945             if isinstance(var, ConvInfo):
946                 if var.from_hu_conv is not None:
947                     out_java_trait = out_java_trait + ", " + var.from_hu_conv[0]
948                 else:
949                     out_java_trait = out_java_trait + ", " + var.arg_name
950             else:
951                 out_java_trait = out_java_trait + ", " + var[1]
952         out_java_trait = out_java_trait + "));\n"
953         out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(arg);\n"
954         for var in flattened_field_vars:
955             if isinstance(var, ConvInfo):
956                 if var.from_hu_conv is not None and var.from_hu_conv[1] != "":
957                     out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
958             else:
959                 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(" + var[1] + ");\n"
960         out_java_trait += f"""          this.bindings_instance = arg;
961         }}
962         @Override @SuppressWarnings("deprecation")
963         protected void finalize() throws Throwable {{
964                 if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }} super.finalize();
965         }}
966         /**
967          * Destroys the object, freeing associated resources. After this call, any access
968          * to this object may result in a SEGFAULT or worse.
969          *
970          * You should generally NEVER call this method. You should let the garbage collector
971          * do this for you when it finalizes objects. However, it may be useful for types
972          * which represent locks and should be closed immediately to avoid holding locks
973          * until the GC runs.
974          */
975         public void destroy() {{
976                 if (ptr != 0) {{ bindings.{struct_name.replace("LDK","")}_free(ptr); }}
977                 ptr = 0;
978         }}
979 """
980
981         java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
982         java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
983         for var in flattened_field_vars:
984             if isinstance(var, ConvInfo):
985                 java_trait_constr = java_trait_constr + ", " + var.java_hu_ty + " " + var.arg_name
986             else:
987                 # Ideally we'd be able to take any instance of the interface, but our C code can only represent
988                 # Java-implemented version, so we require users pass a Java implementation here :/
989                 java_trait_constr = java_trait_constr + ", " + var[0].replace("LDK", "") + "." + var[0].replace("LDK", "") + "Interface " + var[1] + "_impl"
990         java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
991         java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n"
992         out_java_trait = out_java_trait + "\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n"
993         out_java = out_java + "\tpublic interface " + struct_name + " {\n"
994         java_meths = []
995         for fn_line in field_fns:
996             java_meth_descr = "("
997             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
998                 out_java = out_java + "\t\t " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "("
999                 java_trait_constr = java_trait_constr + "\t\t\t@Override public " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "("
1000                 out_java_trait += "\t\t/**\n\t\t * " + fn_line.docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n"
1001                 out_java_trait = out_java_trait + "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_line.fn_name + "("
1002
1003                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
1004                     if idx >= 1:
1005                         out_java = out_java + ", "
1006                         java_trait_constr = java_trait_constr + ", "
1007                         out_java_trait = out_java_trait + ", "
1008                     out_java = out_java + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
1009                     out_java_trait = out_java_trait + arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name
1010                     java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
1011                     java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
1012                 java_meth_descr = java_meth_descr + ")" + fn_line.ret_ty_info.java_fn_ty_arg
1013                 java_meths.append((fn_line.fn_name, java_meth_descr))
1014
1015                 out_java = out_java + ");\n"
1016                 out_java_trait = out_java_trait + ");\n"
1017                 java_trait_constr = java_trait_constr + ") {\n"
1018
1019                 for arg_info in fn_line.args_ty:
1020                     if arg_info.to_hu_conv is not None:
1021                         java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
1022
1023                 if fn_line.ret_ty_info.java_ty != "void":
1024                     java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_line.fn_name + "("
1025                 else:
1026                     java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.fn_name + "("
1027
1028                 for idx, arg_info in enumerate(fn_line.args_ty):
1029                     if idx != 0:
1030                         java_trait_constr = java_trait_constr + ", "
1031                     if arg_info.to_hu_conv_name is not None:
1032                         java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name
1033                     else:
1034                         java_trait_constr = java_trait_constr + arg_info.arg_name
1035
1036                 java_trait_constr += ");\n"
1037                 java_trait_constr += "\t\t\t\tReference.reachabilityFence(arg);\n"
1038                 if fn_line.ret_ty_info.java_ty != "void":
1039                     if fn_line.ret_ty_info.from_hu_conv is not None:
1040                         java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n"
1041                         if fn_line.ret_ty_info.from_hu_conv[1] != "":
1042                             java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
1043                         #if fn_line.ret_ty_info.rust_obj in result_types:
1044                         java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n"
1045                     else:
1046                         java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n"
1047                 java_trait_constr = java_trait_constr + "\t\t\t}\n"
1048         java_trait_constr = java_trait_constr + "\t\t}"
1049         for var in field_vars:
1050             if isinstance(var, ConvInfo):
1051                 java_trait_constr = java_trait_constr + ", " + var.arg_name
1052             else:
1053                 java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
1054                 suptrait_constr = ""
1055                 for suparg in var[2]:
1056                     if isinstance(suparg, ConvInfo):
1057                         suptrait_constr += ", " + suparg.arg_name
1058                     else:
1059                         suptrait_constr += ", " + suparg[1] + "_impl"
1060                 java_trait_constr += suptrait_constr + ").bindings_instance"
1061                 for suparg in var[2]:
1062                     if isinstance(suparg, ConvInfo):
1063                         java_trait_constr += ", " + suparg.arg_name
1064                     else:
1065                         java_trait_constr += ", " + suparg[1] + ".new_impl("
1066                         # Blindly assume that we can just strip the first arg to build the args for the supertrait
1067                         java_trait_constr += suptrait_constr.split(", ", 1)[1]
1068                         java_trait_constr += ").bindings_instance"
1069         out_java_trait = out_java_trait + "\t}\n"
1070         out_java_trait = out_java_trait + java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
1071
1072         out_java = out_java + "\t}\n"
1073
1074         out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl"
1075         for var in flattened_field_vars:
1076             if isinstance(var, ConvInfo):
1077                 out_java = out_java + ", " + var.java_ty + " " + var.arg_name
1078             else:
1079                 out_java = out_java + ", " + var[0] + " " + var[1]
1080         out_java = out_java + ");\n"
1081
1082         # Now that we've written out our java code (and created java_meths), generate C
1083         out_c = "typedef struct " + struct_name + "_JCalls {\n"
1084         out_c = out_c + "\tatomic_size_t refcnt;\n"
1085         out_c = out_c + "\tJavaVM *vm;\n"
1086         out_c = out_c + "\tjweak o;\n"
1087         for var in flattened_field_vars:
1088             if isinstance(var, ConvInfo):
1089                 # We're a regular ol' field
1090                 pass
1091             else:
1092                 # We're a supertrait
1093                 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
1094         for fn in field_fns:
1095             if fn.fn_name != "free" and fn.fn_name != "cloned":
1096                 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
1097         out_c = out_c + "} " + struct_name + "_JCalls;\n"
1098
1099         for fn_line in field_fns:
1100             if fn_line.fn_name == "free":
1101                 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
1102                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
1103                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
1104                 out_c += "\t\t" + self.construct_jenv().replace("\n", "\n\t\t").strip() + "\n"
1105                 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
1106                 out_c += "\t\t" + self.deconstruct_jenv().replace("\n", "\n\t\t").strip() + "\n"
1107                 out_c = out_c + "\t\tFREE(j_calls);\n"
1108                 out_c = out_c + "\t}\n}\n"
1109
1110         for idx, fn_line in enumerate(field_fns):
1111             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
1112                 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
1113                 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall("
1114                 if fn_line.self_is_const:
1115                     out_c = out_c + "const void* this_arg"
1116                 else:
1117                     out_c = out_c + "void* this_arg"
1118
1119                 for idx, arg in enumerate(fn_line.args_ty):
1120                     out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
1121
1122                 out_c = out_c + ") {\n"
1123                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
1124                 out_c += "\t" + self.construct_jenv().replace("\n", "\n\t").strip() + "\n"
1125
1126                 for arg_info in fn_line.args_ty:
1127                     if arg_info.ret_conv is not None:
1128                         out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
1129                         out_c = out_c + arg_info.arg_name
1130                         out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
1131
1132                 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
1133                 if fn_line.ret_ty_info.c_ty.endswith("Array"):
1134                     out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
1135                 elif fn_line.ret_ty_info.c_ty == "void":
1136                     out_c += "\t(*env)->CallVoidMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
1137                 elif fn_line.ret_ty_info.java_hu_ty == "String" or "org/ldk/enums" in fn_line.ret_ty_info.java_fn_ty_arg:
1138                     # Manually write out String methods as they're just an Object
1139                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
1140                 elif not fn_line.ret_ty_info.passed_as_ptr:
1141                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
1142                 else:
1143                     out_c = out_c + "\tuint64_t ret = (*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
1144
1145                 for idx, arg_info in enumerate(fn_line.args_ty):
1146                     if arg_info.ret_conv is not None:
1147                         out_c = out_c + ", " + arg_info.ret_conv_name
1148                     else:
1149                         out_c = out_c + ", " + arg_info.arg_name
1150                 out_c = out_c + ");\n"
1151
1152                 out_c += "\tif (UNLIKELY((*env)->ExceptionCheck(env))) {\n"
1153                 out_c += "\t\t(*env)->ExceptionDescribe(env);\n"
1154                 out_c += "\t\t(*env)->FatalError(env, \"A call to " + fn_line.fn_name + " in " + struct_name + " from rust threw an exception.\");\n"
1155                 out_c += "\t}\n"
1156
1157                 if fn_line.ret_ty_info.arg_conv is not None:
1158                     out_c += "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n"
1159                     out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
1160                     out_c += "\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
1161                 else:
1162                     out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
1163                     if not fn_line.ret_ty_info.passed_as_ptr and fn_line.ret_ty_info.c_ty != "void":
1164                         out_c += "\treturn ret;\n"
1165
1166                 out_c = out_c + "}\n"
1167
1168         # If we can, write out a clone function whether we need one or not, as we use them in moving to rust
1169         can_clone_with_ptr = True
1170         for var in field_vars:
1171             if isinstance(var, ConvInfo):
1172                 can_clone_with_ptr = False
1173         if can_clone_with_ptr:
1174             out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n"
1175             out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n"
1176             out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
1177             for var in flattened_field_vars:
1178                 if not isinstance(var, ConvInfo):
1179                     out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[2].replace(".", "->") + "->refcnt, 1, memory_order_release);\n"
1180             out_c = out_c + "}\n"
1181
1182         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
1183         for var in flattened_field_vars:
1184             if isinstance(var, ConvInfo):
1185                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
1186             else:
1187                 out_c = out_c + ", jobject " + var[1]
1188         out_c = out_c + ") {\n"
1189
1190         out_c = out_c + "\tjclass c = (*env)->GetObjectClass(env, o);\n"
1191         out_c = out_c + "\tCHECK(c != NULL);\n"
1192         out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
1193         out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
1194         out_c = out_c + "\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n"
1195         out_c = out_c + "\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n"
1196
1197         for (fn_name, java_meth_descr) in java_meths:
1198             if fn_name != "free" and fn_name != "cloned":
1199                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
1200                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
1201
1202         for var in flattened_field_vars:
1203             if isinstance(var, ConvInfo) and var.arg_conv is not None:
1204                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
1205         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
1206         out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
1207         for fn_line in field_fns:
1208             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
1209                 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n"
1210             elif fn_line.fn_name == "free":
1211                 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
1212             else:
1213                 out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n"
1214         for var in field_vars:
1215             if isinstance(var, ConvInfo):
1216                 if var.arg_conv_name is not None:
1217                     out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
1218                     out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
1219                 else:
1220                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
1221                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
1222             else:
1223                 out_c += "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1]
1224                 for suparg in var[2]:
1225                     if isinstance(suparg, ConvInfo):
1226                         out_c = out_c + ", " + suparg.arg_name
1227                     else:
1228                         out_c = out_c + ", " + suparg[1]
1229                 out_c += "),\n"
1230         out_c = out_c + "\t};\n"
1231         for var in flattened_field_vars:
1232             if not isinstance(var, ConvInfo):
1233                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[2] + ".this_arg;\n"
1234         out_c = out_c + "\treturn ret;\n"
1235         out_c = out_c + "}\n"
1236
1237         out_c = out_c + self.c_fn_ty_pfx + "int64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
1238         for var in flattened_field_vars:
1239             if isinstance(var, ConvInfo):
1240                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
1241             else:
1242                 out_c = out_c + ", jobject " + var[1]
1243         out_c = out_c + ") {\n"
1244         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
1245         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
1246         for var in flattened_field_vars:
1247             if isinstance(var, ConvInfo):
1248                 out_c = out_c + ", " + var.arg_name
1249             else:
1250                 out_c = out_c + ", " + var[1]
1251         out_c = out_c + ");\n"
1252         out_c = out_c + "\treturn tag_ptr(res_ptr, true);\n"
1253         out_c = out_c + "}\n"
1254
1255         for var in flattened_field_vars:
1256             if not isinstance(var, ConvInfo):
1257                 out_java_trait += "\n\t/**\n"
1258                 out_java_trait += "\t * Gets the underlying " + var[1] + ".\n"
1259                 out_java_trait += "\t */\n"
1260                 underscore_name = ''.join('_' + c.lower() if c.isupper() else c for c in var[1]).strip('_')
1261                 out_java_trait += "\tpublic " + var[1] + " get_" + underscore_name + "() {\n"
1262                 out_java_trait += "\t\t" + var[1] + " res = new " + var[1] + "(null, bindings." + struct_name + "_get_" + var[1] + "(this.ptr));\n"
1263                 out_java_trait += "\t\tres.ptrs_to.add(this);\n"
1264                 out_java_trait += "\t\treturn res;\n"
1265                 out_java_trait += "\t}\n"
1266                 out_java_trait += "\n"
1267
1268                 out_java += "\tpublic static native long " + struct_name + "_get_" + var[1] + "(long arg);\n"
1269
1270                 out_c += "JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_" + struct_name + "_1get_1" + var[1] + "(JNIEnv *env, jclass clz, int64_t arg) {\n"
1271                 out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)untag_ptr(arg);\n"
1272                 out_c += "\treturn tag_ptr(&inp->" + var[2] + ", false);\n"
1273                 out_c += "}\n"
1274
1275         return (out_java, out_java_trait, out_c)
1276
1277     def trait_struct_inc_refcnt(self, ty_info):
1278         base_conv = "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
1279         base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
1280         base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_cloned(&" + ty_info.var_name + "_conv);\n}"
1281         return base_conv
1282
1283     def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
1284         java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
1285         out_java_enum = ""
1286         out_java = ""
1287         out_c = ""
1288
1289         out_java_enum += (self.hu_struct_file_prefix)
1290         out_java_enum += "\n/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
1291         out_java_enum += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
1292         out_java_enum += ("public class " + java_hu_type + " extends CommonBase {\n")
1293         out_java_enum += ("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
1294         out_java_enum += ("\t@Override @SuppressWarnings(\"deprecation\")\n")
1295         out_java_enum += ("\tprotected void finalize() throws Throwable {\n")
1296         out_java_enum += ("\t\tsuper.finalize();\n")
1297         out_java_enum += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK", "") + "_free(ptr); }\n")
1298         out_java_enum += ("\t}\n")
1299         out_java_enum += ("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
1300         out_java_enum += ("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
1301         java_hu_subclasses = ""
1302
1303         init_meth_jty_strs = {}
1304
1305         out_java +=  ("\tpublic static class " + struct_name + " {\n")
1306         out_java +=  ("\t\tprivate " + struct_name + "() {}\n")
1307         for var in variant_list:
1308             out_java +=  ("\t\tpublic final static class " + var.var_name + " extends " + struct_name + " {\n")
1309             if var.var_docs is not None:
1310                 java_hu_subclasses += "\t/**\n\t * " + var.var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
1311             java_hu_subclasses += "\tpublic final static class " + var.var_name + " extends " + java_hu_type + " {\n"
1312             out_java_enum += ("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var.var_name + ".class) {\n")
1313             out_java_enum += ("\t\t\treturn new " + var.var_name + "(ptr, (bindings." + struct_name + "." + var.var_name + ")raw_val);\n")
1314             init_meth_jty_str = ""
1315             init_meth_params = ""
1316             init_meth_body = ""
1317             hu_conv_body = ""
1318             for idx, (field_ty, field_docs) in enumerate(var.fields):
1319                 if idx > 0:
1320                     init_meth_params = init_meth_params + ", "
1321
1322                 java_ty = field_ty.java_ty
1323                 if field_ty.java_fn_ty_arg.startswith("L") and field_ty.java_fn_ty_arg.endswith(";"):
1324                     # If this is a simple enum, we have to reference it in the low-level bindings differently:
1325                     java_ty = field_ty.java_fn_ty_arg.strip("L;").replace("/", ".")
1326                 out_java += "\t\t\tpublic " + java_ty + " " + field_ty.arg_name + ";\n"
1327                 if field_docs is not None:
1328                     java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n"
1329                 java_hu_subclasses += "\t\t"
1330                 if field_ty.nullable:
1331                     java_hu_subclasses += "@Nullable "
1332                 java_hu_subclasses += "public final " + self.fully_qualified_hu_ty_path(field_ty) + " " + field_ty.arg_name + ";\n"
1333                 init_meth_params = init_meth_params + java_ty + " " + field_ty.arg_name
1334
1335                 init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
1336                 if field_ty.to_hu_conv is not None:
1337                     hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
1338                     hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
1339                     hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
1340                 else:
1341                     hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
1342                 init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
1343             out_java +=  ("\t\t\t" + var.var_name + "(" + init_meth_params + ") { ")
1344             out_java +=  (init_meth_body)
1345             out_java +=  ("}\n")
1346             out_java += ("\t\t}\n")
1347             out_java_enum += ("\t\t}\n")
1348             java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var.var_name + "(long ptr, bindings." + struct_name + "." + var.var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
1349             java_hu_subclasses = java_hu_subclasses + hu_conv_body
1350             java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n"
1351             init_meth_jty_strs[var.var_name] = init_meth_jty_str
1352         out_java += ("\t\tstatic native void init();\n")
1353         out_java += ("\t}\n")
1354         out_java_enum += ("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
1355         out_java_enum += (java_hu_subclasses)
1356         out_java += ("\tstatic { " + struct_name + ".init(); }\n")
1357         out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
1358
1359         out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs))
1360
1361         out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n")
1362         out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n")
1363         out_c += ("\tswitch(obj->tag) {\n")
1364         for var in variant_list:
1365             out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
1366             c_params = []
1367             for idx, (field_map, field_docs) in enumerate(var.fields):
1368                 if field_map.ret_conv is not None:
1369                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
1370                     if var.tuple_variant:
1371                         out_c += "obj->" + camel_to_snake(var.var_name)
1372                     else:
1373                         out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
1374                     out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
1375                     c_params.append(field_map.ret_conv_name)
1376                 else:
1377                     if var.tuple_variant:
1378                         c_params.append("obj->" + camel_to_snake(var.var_name))
1379                     else:
1380                         c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
1381             out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
1382             out_c += ("\t\t}\n")
1383         out_c += ("\t\tdefault: abort();\n")
1384         out_c += ("\t}\n}\n")
1385         return (out_java, out_java_enum, out_c)
1386
1387     def map_opaque_struct(self, struct_name, struct_doc_comment):
1388         out_opaque_struct_human = ""
1389         out_opaque_struct_human += self.hu_struct_file_prefix
1390         out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n"
1391         out_opaque_struct_human += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
1392         hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
1393         out_opaque_struct_human += ("public class " + hu_name + " extends CommonBase")
1394         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1395             out_opaque_struct_human += (" implements AutoCloseable")
1396         out_opaque_struct_human += (" {\n")
1397         out_opaque_struct_human += ("\t" + hu_name + "(Object _dummy, long ptr) { super(ptr); }\n")
1398         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1399             out_opaque_struct_human += ("\t@Override public void close() {\n")
1400         else:
1401             out_opaque_struct_human += ("\t@Override @SuppressWarnings(\"deprecation\")\n")
1402             out_opaque_struct_human += ("\tprotected void finalize() throws Throwable {\n")
1403             out_opaque_struct_human += ("\t\tsuper.finalize();\n")
1404         out_opaque_struct_human += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n")
1405         out_opaque_struct_human += ("\t}\n\n")
1406         return out_opaque_struct_human
1407
1408     def map_tuple(self, struct_name):
1409         return self.map_opaque_struct(struct_name, "A Tuple")
1410
1411     def map_result(self, struct_name, res_map, err_map):
1412         human_ty = struct_name.replace("LDKCResult", "Result")
1413         java_hu_struct = ""
1414         java_hu_struct += self.hu_struct_file_prefix
1415         java_hu_struct += "public class " + human_ty + " extends CommonBase {\n"
1416         java_hu_struct += "\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n"
1417         java_hu_struct += "\tprotected void finalize() throws Throwable {\n"
1418         java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n"
1419         java_hu_struct += "\t}\n\n"
1420         java_hu_struct += "\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n"
1421         java_hu_struct += "\t\tif (bindings." + struct_name.replace("LDK", "") + "_is_ok(ptr)) {\n"
1422         java_hu_struct += "\t\t\treturn new " + human_ty + "_OK(null, ptr);\n"
1423         java_hu_struct += "\t\t} else {\n"
1424         java_hu_struct += "\t\t\treturn new " + human_ty + "_Err(null, ptr);\n"
1425         java_hu_struct += "\t\t}\n"
1426         java_hu_struct += "\t}\n"
1427
1428         java_hu_struct += "\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n"
1429
1430         if res_map.java_hu_ty != "void":
1431             java_hu_struct += "\t\tpublic final " + res_map.java_hu_ty + " res;\n"
1432         java_hu_struct += "\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n"
1433         java_hu_struct += "\t\t\tsuper(_dummy, ptr);\n"
1434         if res_map.java_hu_ty == "void":
1435             pass
1436         elif res_map.to_hu_conv is not None:
1437             java_hu_struct += "\t\t\t" + res_map.java_ty + " res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1438             java_hu_struct += "\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t")
1439             java_hu_struct += "\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n"
1440         else:
1441             java_hu_struct += "\t\t\tthis.res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1442         java_hu_struct += "\t\t}\n"
1443         java_hu_struct += "\t}\n\n"
1444
1445         java_hu_struct += "\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n"
1446         if err_map.java_hu_ty != "void":
1447             java_hu_struct += "\t\tpublic final " + err_map.java_hu_ty + " err;\n"
1448         java_hu_struct += "\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n"
1449         java_hu_struct += "\t\t\tsuper(_dummy, ptr);\n"
1450         if err_map.java_hu_ty == "void":
1451             pass
1452         elif err_map.to_hu_conv is not None:
1453             java_hu_struct += "\t\t\t" + err_map.java_ty + " err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1454             java_hu_struct += "\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t")
1455             java_hu_struct += "\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n"
1456         else:
1457             java_hu_struct += "\t\t\tthis.err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1458         java_hu_struct += "\t\t}\n"
1459
1460         java_hu_struct += "\t}\n\n"
1461         return java_hu_struct
1462
1463     def map_function(self, argument_types, c_call_string, method_name, meth_n, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_as_ref, args_known, type_mapping_generator, doc_comment):
1464         out_java = ""
1465         out_c = ""
1466         out_java_struct = None
1467
1468         out_java += ("\tpublic static native ")
1469         out_c += (self.c_fn_ty_pfx)
1470         out_c += (return_type_info.c_ty)
1471         out_java += (return_type_info.java_ty)
1472         if return_type_info.ret_conv is not None:
1473             ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
1474         out_java += (" " + method_name + "(")
1475         have_args = len(argument_types) > 1 or (len(argument_types) > 0 and argument_types[0].c_ty != "void")
1476         out_c += (" " + self.c_fn_name_define_pfx(method_name, have_args))
1477
1478         for idx, arg_conv_info in enumerate(argument_types):
1479             if idx != 0:
1480                 out_java += (", ")
1481                 out_c += (", ")
1482             if arg_conv_info.c_ty != "void":
1483                 out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
1484                 out_java += (arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
1485
1486         out_java_struct = ""
1487         extra_java_struct_out = ""
1488         if not args_known:
1489             out_java_struct += ("\t// Skipped " + method_name + "\n")
1490         else:
1491             if doc_comment is not None:
1492                 out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
1493             if return_type_info.nullable:
1494                 out_java_struct += "\t@Nullable\n"
1495             if not takes_self:
1496                 if meth_n == "new":
1497                     out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " of("
1498                 elif meth_n == "default":
1499                     out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " with_default("
1500                 else:
1501                     out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " " + meth_n + "("
1502             elif meth_n == "clone_ptr" or (struct_meth.startswith("LDKCResult") and (meth_n == "get_ok" or meth_n == "get_err")):
1503                 out_java_struct += ("\t" + return_type_info.java_hu_ty + " " + meth_n + "(")
1504             else:
1505                 if meth_n == "hash" and return_type_info.java_hu_ty == "long":
1506                     extra_java_struct_out = "\t@Override public int hashCode() {\n"
1507                     extra_java_struct_out += "\t\treturn (int)this.hash();\n"
1508                     extra_java_struct_out += "\t}\n"
1509                 elif meth_n == "eq" and return_type_info.java_hu_ty == "boolean":
1510                     extra_java_struct_out = "\t@Override public boolean equals(Object o) {\n"
1511                     extra_java_struct_out += "\t\tif (!(o instanceof " + struct_meth + ")) return false;\n"
1512                     extra_java_struct_out += "\t\treturn this.eq((" + struct_meth + ")o);\n"
1513                     extra_java_struct_out += "\t}\n"
1514                 if meth_n == "wait":
1515                     meth_n = "wait_indefinite"
1516                 out_java_struct += ("\tpublic " + return_type_info.java_hu_ty + " " + meth_n + "(")
1517             for idx, arg in enumerate(argument_types):
1518                 if idx != 0:
1519                     if not takes_self or idx > 1:
1520                         out_java_struct += ", "
1521                 elif takes_self:
1522                     continue
1523                 if arg.java_ty != "void":
1524                     if arg.arg_name in default_constructor_args:
1525                         assert not arg.nullable
1526                         for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
1527                             if explode_idx != 0:
1528                                 out_java_struct += (", ")
1529                             out_java_struct += (
1530                                 explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name)
1531                     else:
1532                         if arg.nullable:
1533                             out_java_struct += "@Nullable "
1534                         ty_string = self.fully_qualified_hu_ty_path(arg)
1535                         out_java_struct += ty_string + " " + arg.arg_name
1536         out_java += (");\n")
1537         out_c += (") {\n")
1538         if out_java_struct is not None:
1539             out_java_struct += (") {\n")
1540         for info in argument_types:
1541             if info.arg_conv is not None:
1542                 out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
1543         if return_type_info.ret_conv is not None:
1544             out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
1545         elif return_type_info.c_ty != "void":
1546             out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
1547         else:
1548             out_c += ("\t")
1549         if c_call_string is None:
1550             out_c += (method_name + "(")
1551         else:
1552             out_c += (c_call_string)
1553         for idx, info in enumerate(argument_types):
1554             if info.arg_conv_name is not None:
1555                 if idx != 0:
1556                     out_c += (", ")
1557                 elif c_call_string is not None:
1558                     continue
1559                 out_c += (info.arg_conv_name)
1560         out_c += (")")
1561         if return_type_info.ret_conv is not None:
1562             out_c += (ret_conv_sfx.replace('\n', '\n\t'))
1563         else:
1564             out_c += (";")
1565         for info in argument_types:
1566             if info.arg_conv_cleanup is not None:
1567                 out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
1568         if return_type_info.ret_conv is not None:
1569             out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
1570         elif return_type_info.c_ty != "void":
1571             out_c += ("\n\treturn ret_val;")
1572         out_c += ("\n}\n\n")
1573
1574         if args_known:
1575             out_java_struct += ("\t\t")
1576             if return_type_info.java_ty != "void":
1577                 out_java_struct += (return_type_info.java_ty + " ret = ")
1578             out_java_struct += ("bindings." + method_name + "(")
1579             for idx, info in enumerate(argument_types):
1580                 if idx != 0:
1581                     out_java_struct += (", ")
1582                 if idx == 0 and takes_self:
1583                     out_java_struct += ("this.ptr")
1584                 elif info.arg_name in default_constructor_args:
1585                     out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
1586                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1587                         if explode_idx != 0:
1588                             out_java_struct += (", ")
1589                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1590                         if explode_arg.from_hu_conv is not None:
1591                             out_java_struct += (
1592                                 explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1593                         else:
1594                             out_java_struct += (expl_arg_name)
1595                     out_java_struct += (")")
1596                 elif info.from_hu_conv is not None:
1597                     out_java_struct += (info.from_hu_conv[0])
1598                 else:
1599                     out_java_struct += (info.arg_name)
1600             out_java_struct += (");\n")
1601
1602             # This is completely nuts. The OpenJDK JRE JIT will optimize out a object which is on
1603             # the stack, calling its finalizer immediately even if member methods are *actively
1604             # executing* on the same object, as long as said object is on the stack. There is no
1605             # concrete specification for when the optimizer is allowed to do this, and when it is
1606             # not, so there is absolutely no way to be certain that this fix suffices.
1607             #
1608             # Instead, the "Java Language Specification" says only that an object is reachable
1609             # (i.e. will not yet be finalized) if it "can be accessed in any potential continuing
1610             # computation from any live thread". To any sensible reader this would mean actively
1611             # executing a member function on an object would make it not eligible for finalization.
1612             # But, no, dear reader, this statement does not say that. Well, okay, it says that,
1613             # very explicitly in fact, but those are just, like, words, man.
1614             #
1615             # In the seemingly non-normative text further down, a few examples of things the
1616             # optimizer can do are given, including "if the values in an object's fields are
1617             # stored in registers[, t]he may then access the registers instead of the object, and
1618             # never access the object again[, implying] that the object is garbage". This appears
1619             # to fully contradict both the above statement, the API documentation in java.lang.ref
1620             # regarding when a reference is "strongly reachable", and basic common sense. There is
1621             # no concrete set of limitations stated, however, seemingly implying the JIT could
1622             # decide your code would run faster by simply garbage collecting everything
1623             # immediately, ensuring your code finishes soon, just by SEGFAULT. Thus, we're really
1624             # entirely flying blind here. We add some fences and hope that its sufficient, but
1625             # with no specification to rely on, we cannot be certain of anything.
1626             #
1627             # TL;DR: The Java Language "Specification" provides no real guarantees on when an
1628             # object will be considered available for garbage collection once the JIT kicks in, so
1629             # we put in some fences and hope to god the JIT doesn't get smarter/more broken.
1630             for idx, info in enumerate(argument_types):
1631                 if idx == 0 and takes_self:
1632                     out_java_struct += ("\t\tReference.reachabilityFence(this);\n")
1633                 elif info.arg_name in default_constructor_args:
1634                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1635                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1636                         out_java_struct += ("\t\tReference.reachabilityFence(" + expl_arg_name + ");\n")
1637                 elif info.c_ty != "void":
1638                     out_java_struct += ("\t\tReference.reachabilityFence(" + info.arg_name + ");\n")
1639
1640             if return_type_info.java_ty == "long" and return_type_info.java_hu_ty != "long":
1641                 out_java_struct += "\t\tif (ret >= 0 && ret <= 4096) { return null; }\n"
1642
1643             if return_type_info.to_hu_conv is not None:
1644                 if not takes_self:
1645                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t").replace("this",
1646                                                                                                                return_type_info.to_hu_conv_name) + "\n")
1647                 else:
1648                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1649
1650             for idx, info in enumerate(argument_types):
1651                 if idx == 0 and takes_self:
1652                     pass
1653                 elif info.arg_name in default_constructor_args:
1654                     for explode_arg in default_constructor_args[info.arg_name]:
1655                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1656                         if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
1657                             out_java_struct += ("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name,
1658                                                                                                expl_arg_name).replace(
1659                                 "this", return_type_info.to_hu_conv_name) + ";\n")
1660                 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1661                     if not takes_self and return_type_info.to_hu_conv_name is not None:
1662                         out_java_struct += (
1663                             "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n")
1664                     else:
1665                         out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n")
1666
1667             if takes_self and not takes_self_as_ref:
1668                 out_java_struct += "\t\t" + argument_types[0].from_hu_conv[1].replace("\n", "\n\t\t").replace("this_arg", "this") + ";\n"
1669             if return_type_info.to_hu_conv_name is not None:
1670                 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
1671             elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
1672                 out_java_struct += ("\t\treturn ret;\n")
1673             out_java_struct += ("\t}\n\n")
1674
1675         return (out_java, out_c, out_java_struct + extra_java_struct_out)
1676
1677     def cleanup(self):
1678         pass