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