update to new upstream
[ldk-java] / genbindings.py
1 #!/usr/bin/env python3
2 import sys, re
3
4 if len(sys.argv) != 6:
5     print("USAGE: /path/to/lightning.h /path/to/bindings/output.java /path/to/bindings/ /path/to/bindings/output.c debug")
6     print("debug should be true or false and indicates whether to track allocations and ensure we don't leak")
7     sys.exit(1)
8
9 hu_struct_file_prefix = """package org.ldk.structs;
10
11 import org.ldk.impl.bindings;
12 import org.ldk.enums.*;
13 import org.ldk.util.*;
14 import java.util.Arrays;
15
16 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
17 """
18
19 c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
20 #include <rust_types.h>
21 #include <lightning.h>
22 #include <string.h>
23 #include <stdatomic.h>
24 """
25
26 if sys.argv[5] == "false":
27     c_file_pfx = c_file_pfx + """#define MALLOC(a, _) malloc(a)
28 #define FREE(p) if ((long)(p) > 1024) { free(p); }
29 #define DO_ASSERT(a) (void)(a)
30 #define CHECK(a)
31 """
32 else:
33     c_file_pfx = c_file_pfx + """#include <assert.h>
34 // Always run a, then assert it is true:
35 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
36 // Assert a is true or do nothing
37 #define CHECK(a) DO_ASSERT(a)
38
39 // Running a leak check across all the allocations and frees of the JDK is a mess,
40 // so instead we implement our own naive leak checker here, relying on the -wrap
41 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
42 // and free'd in Rust or C across the generated bindings shared library.
43 #include <threads.h>
44 #include <execinfo.h>
45 #include <unistd.h>
46 static mtx_t allocation_mtx;
47
48 void __attribute__((constructor)) init_mtx() {
49         DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);
50 }
51
52 #define BT_MAX 128
53 typedef struct allocation {
54         struct allocation* next;
55         void* ptr;
56         const char* struct_name;
57         void* bt[BT_MAX];
58         int bt_len;
59 } allocation;
60 static allocation* allocation_ll = NULL;
61
62 void* __real_malloc(size_t len);
63 void* __real_calloc(size_t nmemb, size_t len);
64 static void new_allocation(void* res, const char* struct_name) {
65         allocation* new_alloc = __real_malloc(sizeof(allocation));
66         new_alloc->ptr = res;
67         new_alloc->struct_name = struct_name;
68         new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
69         DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
70         new_alloc->next = allocation_ll;
71         allocation_ll = new_alloc;
72         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
73 }
74 static void* MALLOC(size_t len, const char* struct_name) {
75         void* res = __real_malloc(len);
76         new_allocation(res, struct_name);
77         return res;
78 }
79 void __real_free(void* ptr);
80 static void alloc_freed(void* ptr) {
81         allocation* p = NULL;
82         DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
83         allocation* it = allocation_ll;
84         while (it->ptr != ptr) {
85                 p = it; it = it->next;
86                 if (it == NULL) {
87                         fprintf(stderr, "Tried to free unknown pointer %p at:\\n", ptr);
88                         void* bt[BT_MAX];
89                         int bt_len = backtrace(bt, BT_MAX);
90                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
91                         fprintf(stderr, "\\n\\n");
92                         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
93                         return; // addrsan should catch malloc-unknown and print more info than we have
94                 }
95         }
96         if (p) { p->next = it->next; } else { allocation_ll = it->next; }
97         DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
98         DO_ASSERT(it->ptr == ptr);
99         __real_free(it);
100 }
101 static void FREE(void* ptr) {
102         if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
103         alloc_freed(ptr);
104         __real_free(ptr);
105 }
106
107 void* __wrap_malloc(size_t len) {
108         void* res = __real_malloc(len);
109         new_allocation(res, "malloc call");
110         return res;
111 }
112 void* __wrap_calloc(size_t nmemb, size_t len) {
113         void* res = __real_calloc(nmemb, len);
114         new_allocation(res, "calloc call");
115         return res;
116 }
117 void __wrap_free(void* ptr) {
118         if (ptr == NULL) return;
119         alloc_freed(ptr);
120         __real_free(ptr);
121 }
122
123 void* __real_realloc(void* ptr, size_t newlen);
124 void* __wrap_realloc(void* ptr, size_t len) {
125         if (ptr != NULL) alloc_freed(ptr);
126         void* res = __real_realloc(ptr, len);
127         new_allocation(res, "realloc call");
128         return res;
129 }
130 void __wrap_reallocarray(void* ptr, size_t new_sz) {
131         // Rust doesn't seem to use reallocarray currently
132         assert(false);
133 }
134
135 void __attribute__((destructor)) check_leaks() {
136         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
137                 fprintf(stderr, "%s %p remains:\\n", a->struct_name, a->ptr);
138                 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
139                 fprintf(stderr, "\\n\\n");
140         }
141         DO_ASSERT(allocation_ll == NULL);
142 }
143 """
144
145 c_file = ""
146 def write_c(s):
147     global c_file
148     c_file += s
149
150 class TypeInfo:
151     def __init__(self, is_native_primitive, rust_obj, java_ty, java_fn_ty_arg, java_hu_ty, c_ty, passed_as_ptr, is_ptr, var_name, arr_len, arr_access, subty=None):
152         self.is_native_primitive = is_native_primitive
153         self.rust_obj = rust_obj
154         self.java_ty = java_ty
155         self.java_hu_ty = java_hu_ty
156         self.java_fn_ty_arg = java_fn_ty_arg
157         self.c_ty = c_ty
158         self.passed_as_ptr = passed_as_ptr
159         self.is_ptr = is_ptr
160         self.var_name = var_name
161         self.arr_len = arr_len
162         self.arr_access = arr_access
163         self.subty = subty
164         self.pass_by_ref = is_ptr
165         self.requires_clone = None
166
167 class ConvInfo:
168     def __init__(self, ty_info, arg_name, arg_conv, arg_conv_name, arg_conv_cleanup, ret_conv, ret_conv_name, to_hu_conv, to_hu_conv_name, from_hu_conv):
169         assert(ty_info.c_ty is not None)
170         assert(ty_info.java_ty is not None)
171         assert(arg_name is not None)
172         self.passed_as_ptr = ty_info.passed_as_ptr
173         self.rust_obj = ty_info.rust_obj
174         self.c_ty = ty_info.c_ty
175         self.java_ty = ty_info.java_ty
176         self.java_hu_ty = ty_info.java_hu_ty
177         self.java_fn_ty_arg = ty_info.java_fn_ty_arg
178         self.arg_name = arg_name
179         self.arg_conv = arg_conv
180         self.arg_conv_name = arg_conv_name
181         self.arg_conv_cleanup = arg_conv_cleanup
182         self.ret_conv = ret_conv
183         self.ret_conv_name = ret_conv_name
184         self.to_hu_conv = to_hu_conv
185         self.to_hu_conv_name = to_hu_conv_name
186         self.from_hu_conv = from_hu_conv
187
188     def print_ty(self):
189         write_c(self.c_ty)
190         out_java.write(self.java_ty)
191
192     def print_name(self):
193         if self.arg_name != "":
194             out_java.write(" " + self.arg_name)
195             write_c(" " + self.arg_name)
196         else:
197             out_java.write(" arg")
198             write_c(" arg")
199
200 def camel_to_snake(s):
201     # Convert camel case to snake case, in a way that appears to match cbindgen
202     con = "_"
203     ret = ""
204     lastchar = ""
205     lastund = False
206     for char in s:
207         if lastchar.isupper():
208             if not char.isupper() and not lastund:
209                 ret = ret + "_"
210                 lastund = True
211             else:
212                 lastund = False
213             ret = ret + lastchar.lower()
214         else:
215             ret = ret + lastchar
216             if char.isupper() and not lastund:
217                 ret = ret + "_"
218                 lastund = True
219             else:
220                 lastund = False
221         lastchar = char
222         if char.isnumeric():
223             lastund = True
224     return (ret + lastchar.lower()).strip("_")
225
226 unitary_enums = set()
227 complex_enums = set()
228 opaque_structs = set()
229 trait_structs = set()
230 result_types = set()
231 tuple_types = {}
232
233 var_is_arr_regex = re.compile("\(\*([A-za-z0-9_]*)\)\[([a-z0-9]*)\]")
234 var_ty_regex = re.compile("([A-za-z_0-9]*)(.*)")
235 java_c_types_none_allowed = True # Unset when we do the real pass that populates the above sets
236 def java_c_types(fn_arg, ret_arr_len):
237     fn_arg = fn_arg.strip()
238     if fn_arg.startswith("MUST_USE_RES "):
239         fn_arg = fn_arg[13:]
240     is_const = False
241     if fn_arg.startswith("const "):
242         fn_arg = fn_arg[6:]
243         is_const = True
244     if fn_arg.startswith("struct "):
245         fn_arg = fn_arg[7:]
246     if fn_arg.startswith("enum "):
247         fn_arg = fn_arg[5:]
248     fn_arg = fn_arg.replace("NONNULL_PTR", "")
249
250     is_ptr = False
251     take_by_ptr = False
252     rust_obj = None
253     arr_access = None
254     java_hu_ty = None
255     if fn_arg.startswith("LDKThirtyTwoBytes"):
256         fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]"
257         assert var_is_arr_regex.match(fn_arg[8:])
258         rust_obj = "LDKThirtyTwoBytes"
259         arr_access = "data"
260     elif fn_arg.startswith("LDKPublicKey"):
261         fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]"
262         assert var_is_arr_regex.match(fn_arg[8:])
263         rust_obj = "LDKPublicKey"
264         arr_access = "compressed_form"
265     elif fn_arg.startswith("LDKSecretKey"):
266         fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]"
267         assert var_is_arr_regex.match(fn_arg[8:])
268         rust_obj = "LDKSecretKey"
269         arr_access = "bytes"
270     elif fn_arg.startswith("LDKSignature"):
271         fn_arg = "uint8_t (*" + fn_arg[13:] + ")[64]"
272         assert var_is_arr_regex.match(fn_arg[8:])
273         rust_obj = "LDKSignature"
274         arr_access = "compact_form"
275     elif fn_arg.startswith("LDKThreeBytes"):
276         fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]"
277         assert var_is_arr_regex.match(fn_arg[8:])
278         rust_obj = "LDKThreeBytes"
279         arr_access = "data"
280     elif fn_arg.startswith("LDKFourBytes"):
281         fn_arg = "uint8_t (*" + fn_arg[13:] + ")[4]"
282         assert var_is_arr_regex.match(fn_arg[8:])
283         rust_obj = "LDKFourBytes"
284         arr_access = "data"
285     elif fn_arg.startswith("LDKSixteenBytes"):
286         fn_arg = "uint8_t (*" + fn_arg[16:] + ")[16]"
287         assert var_is_arr_regex.match(fn_arg[8:])
288         rust_obj = "LDKSixteenBytes"
289         arr_access = "data"
290     elif fn_arg.startswith("LDKTenBytes"):
291         fn_arg = "uint8_t (*" + fn_arg[12:] + ")[10]"
292         assert var_is_arr_regex.match(fn_arg[8:])
293         rust_obj = "LDKTenBytes"
294         arr_access = "data"
295     elif fn_arg.startswith("LDKu8slice"):
296         fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]"
297         assert var_is_arr_regex.match(fn_arg[8:])
298         rust_obj = "LDKu8slice"
299         arr_access = "data"
300     elif fn_arg.startswith("LDKCVec_u8Z"):
301         fn_arg = "uint8_t (*" + fn_arg[12:] + ")[datalen]"
302         rust_obj = "LDKCVec_u8Z"
303         assert var_is_arr_regex.match(fn_arg[8:])
304         arr_access = "data"
305     elif fn_arg.startswith("LDKTransaction"):
306         fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]"
307         rust_obj = "LDKTransaction"
308         assert var_is_arr_regex.match(fn_arg[8:])
309         arr_access = "data"
310     elif fn_arg.startswith("LDKCVec_"):
311         is_ptr = False
312         if "*" in fn_arg:
313             fn_arg = fn_arg.replace("*", "")
314             is_ptr = True
315
316         tyn = fn_arg[8:].split(" ")
317         assert tyn[0].endswith("Z")
318         if tyn[0] == "u64Z":
319             new_arg = "uint64_t"
320         else:
321             new_arg = "LDK" + tyn[0][:-1]
322         for a in tyn[1:]:
323             new_arg = new_arg + " " + a
324         res = java_c_types(new_arg, ret_arr_len)
325         if res is None:
326             assert java_c_types_none_allowed
327             return None
328         if is_ptr:
329             res.pass_by_ref = True
330         if res.is_native_primitive or res.passed_as_ptr:
331             return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=res.java_ty + "[]", java_hu_ty=res.java_hu_ty + "[]",
332                 java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty=res.c_ty + "Array", passed_as_ptr=False, is_ptr=is_ptr,
333                 var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
334         else:
335             return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=res.java_ty + "[]", java_hu_ty=res.java_hu_ty + "[]",
336                 java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty="jobjectArray", passed_as_ptr=False, is_ptr=is_ptr,
337                 var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
338
339     is_primitive = False
340     arr_len = None
341     if fn_arg.startswith("void"):
342         java_ty = "void"
343         c_ty = "void"
344         fn_ty_arg = "V"
345         fn_arg = fn_arg[4:].strip()
346         is_primitive = True
347     elif fn_arg.startswith("bool"):
348         java_ty = "boolean"
349         c_ty = "jboolean"
350         fn_ty_arg = "Z"
351         fn_arg = fn_arg[4:].strip()
352         is_primitive = True
353     elif fn_arg.startswith("uint8_t"):
354         java_ty = "byte"
355         c_ty = "jbyte"
356         fn_ty_arg = "B"
357         fn_arg = fn_arg[7:].strip()
358         is_primitive = True
359     elif fn_arg.startswith("uint16_t"):
360         java_ty = "short"
361         c_ty = "jshort"
362         fn_ty_arg = "S"
363         fn_arg = fn_arg[8:].strip()
364         is_primitive = True
365     elif fn_arg.startswith("uint32_t"):
366         java_ty = "int"
367         c_ty = "jint"
368         fn_ty_arg = "I"
369         fn_arg = fn_arg[8:].strip()
370         is_primitive = True
371     elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"):
372         java_ty = "long"
373         c_ty = "jlong"
374         fn_ty_arg = "J"
375         if fn_arg.startswith("uint64_t"):
376             fn_arg = fn_arg[8:].strip()
377         else:
378             fn_arg = fn_arg[9:].strip()
379         is_primitive = True
380     elif is_const and fn_arg.startswith("char *"):
381         java_ty = "String"
382         c_ty = "const char*"
383         fn_ty_arg = "Ljava/lang/String;"
384         fn_arg = fn_arg[6:].strip()
385     elif fn_arg.startswith("LDKStr"):
386         java_ty = "String"
387         c_ty = "jstring"
388         fn_ty_arg = "Ljava/lang/String;"
389         fn_arg = fn_arg[6:].strip()
390         arr_access = "chars"
391         arr_len = "len"
392     else:
393         ma = var_ty_regex.match(fn_arg)
394         if ma.group(1).strip() in unitary_enums:
395             java_ty = ma.group(1).strip()
396             c_ty = "jclass"
397             fn_ty_arg = "Lorg/ldk/enums/" + ma.group(1).strip() + ";"
398             fn_arg = ma.group(2).strip()
399             rust_obj = ma.group(1).strip()
400             take_by_ptr = True
401         elif ma.group(1).strip().startswith("LDKC2Tuple"):
402             java_ty = "long"
403             java_hu_ty = "TwoTuple<"
404             if not ma.group(1).strip() in tuple_types:
405                 assert java_c_types_none_allowed
406                 return None
407             for idx, ty_info in enumerate(tuple_types[ma.group(1).strip()][0]):
408                 if idx != 0:
409                     java_hu_ty = java_hu_ty + ", "
410                 if ty_info.is_native_primitive:
411                     if ty_info.java_hu_ty == "int":
412                         java_hu_ty = java_hu_ty + "Integer" # Java concrete integer type is Integer, not Int
413                     else:
414                         java_hu_ty = java_hu_ty + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
415                 else:
416                     java_hu_ty = java_hu_ty + ty_info.java_hu_ty
417             java_hu_ty = java_hu_ty + ">"
418             c_ty = "jlong"
419             fn_ty_arg = "J"
420             fn_arg = ma.group(2).strip()
421             rust_obj = ma.group(1).strip()
422             take_by_ptr = True
423         elif ma.group(1).strip().startswith("LDKC3Tuple"):
424             java_ty = "long"
425             java_hu_ty = "ThreeTuple<"
426             if not ma.group(1).strip() in tuple_types:
427                 assert java_c_types_none_allowed
428                 return None
429             for idx, ty_info in enumerate(tuple_types[ma.group(1).strip()][0]):
430                 if idx != 0:
431                     java_hu_ty = java_hu_ty + ", "
432                 if ty_info.is_native_primitive:
433                     if ty_info.java_hu_ty == "int":
434                         java_hu_ty = java_hu_ty + "Integer" # Java concrete integer type is Integer, not Int
435                     else:
436                         java_hu_ty = java_hu_ty + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
437                 else:
438                     java_hu_ty = java_hu_ty + ty_info.java_hu_ty
439             java_hu_ty = java_hu_ty + ">"
440             c_ty = "jlong"
441             fn_ty_arg = "J"
442             fn_arg = ma.group(2).strip()
443             rust_obj = ma.group(1).strip()
444             take_by_ptr = True
445         else:
446             java_ty = "long"
447             java_hu_ty = ma.group(1).strip().replace("LDKCResult", "Result").replace("LDK", "")
448             c_ty = "jlong"
449             fn_ty_arg = "J"
450             fn_arg = ma.group(2).strip()
451             rust_obj = ma.group(1).strip()
452             take_by_ptr = True
453
454     if fn_arg.startswith(" *") or fn_arg.startswith("*"):
455         fn_arg = fn_arg.replace("*", "").strip()
456         is_ptr = True
457         c_ty = "jlong"
458         java_ty = "long"
459         fn_ty_arg = "J"
460         is_primitive = False
461
462     var_is_arr = var_is_arr_regex.match(fn_arg)
463     if var_is_arr is not None or ret_arr_len is not None:
464         assert(not take_by_ptr)
465         assert(not is_ptr)
466         java_ty = java_ty + "[]"
467         c_ty = c_ty + "Array"
468         if var_is_arr is not None:
469             if var_is_arr.group(1) == "":
470                 return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
471                     passed_as_ptr=False, is_ptr=False, var_name="arg", arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
472             return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty,
473                 passed_as_ptr=False, is_ptr=False, var_name=var_is_arr.group(1), arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
474
475     if java_hu_ty is None:
476         java_hu_ty = java_ty
477     return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
478         is_ptr=is_ptr, var_name=fn_arg, arr_len=arr_len, arr_access=arr_access, is_native_primitive=is_primitive)
479
480 fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
481 fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
482 reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
483 clone_fns = set()
484 constructor_fns = {}
485 c_array_class_caches = set()
486 with open(sys.argv[1]) as in_h:
487     for line in in_h:
488         reg_fn = reg_fn_regex.match(line)
489         if reg_fn is not None:
490             if reg_fn.group(2).endswith("_clone"):
491                 clone_fns.add(reg_fn.group(2))
492             else:
493                 rty = java_c_types(reg_fn.group(1), None)
494                 if rty is not None and rty.rust_obj is not None and reg_fn.group(2) == rty.java_hu_ty + "_new":
495                     constructor_fns[rty.rust_obj] = reg_fn.group(3)
496             continue
497         arr_fn = fn_ret_arr_regex.match(line)
498         if arr_fn is not None:
499             if arr_fn.group(2).endswith("_clone"):
500                 clone_fns.add(arr_fn.group(2))
501             # No object constructors return arrays, as then they wouldn't be an object constructor
502             continue
503 java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order
504
505 with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
506     def map_type(fn_arg, print_void, ret_arr_len, is_free, holds_ref):
507         ty_info = java_c_types(fn_arg, ret_arr_len)
508         return map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref)
509
510     def map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref):
511         if ty_info.c_ty == "void":
512             if not print_void:
513                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
514                     arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
515                     ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
516         if ty_info.c_ty.endswith("Array"):
517             arr_len = ty_info.arr_len
518             if arr_len is not None:
519                 arr_name = ty_info.var_name
520             else:
521                 arr_name = "ret"
522                 arr_len = ret_arr_len
523             if ty_info.c_ty == "jbyteArray":
524                 ret_conv = ("jbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", ", "")
525                 arg_conv_cleanup = None
526                 if not arr_len.isdigit():
527                     arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
528                     arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");\n"
529                     if (not ty_info.is_ptr or not holds_ref) and ty_info.rust_obj != "LDKu8slice":
530                         arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = MALLOC(" + arr_name + "_ref." + arr_len + ", \"" + ty_info.rust_obj + " Bytes\");\n"
531                         arg_conv = arg_conv + "(*_env)->GetByteArrayRegion(_env, " + arr_name + ", 0, " + arr_name + "_ref." + arr_len + ", " + arr_name + "_ref." + ty_info.arr_access + ");"
532                     else:
533                         arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = (*_env)->GetByteArrayElements (_env, " + arr_name + ", NULL);"
534                         arg_conv_cleanup = "(*_env)->ReleaseByteArrayElements(_env, " + arr_name + ", (int8_t*)" + arr_name + "_ref." + ty_info.arr_access + ", 0);"
535                     if ty_info.rust_obj == "LDKTransaction":
536                         arg_conv = arg_conv + "\n" + arr_name + "_ref.data_is_owned = " + str(holds_ref).lower() + ";"
537                     ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
538                     ret_conv = (ret_conv[0], ";\njbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_name + "_var." + arr_len + ");\n")
539                     ret_conv = (ret_conv[0], ret_conv[1] + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_name + "_var." + arr_len + ", " + arr_name + "_var." + ty_info.arr_access + ");")
540                     if not holds_ref and ty_info.rust_obj != "LDKu8slice":
541                         ret_conv = (ret_conv[0], ret_conv[1] + "\n" + ty_info.rust_obj.replace("LDK", "") + "_free(" + arr_name + "_var);")
542                 elif ty_info.rust_obj is not None:
543                     arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
544                     arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
545                     arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_ref." + ty_info.arr_access + ");"
546                     ret_conv = (ret_conv[0], "." + ty_info.arr_access + ");")
547                 else:
548                     arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n"
549                     arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n"
550                     arg_conv = arg_conv + "(*_env)->GetByteArrayRegion (_env, " + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" + "unsigned char (*" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;"
551                     ret_conv = (ret_conv[0] + "*", ");")
552                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
553                     arg_conv = arg_conv, arg_conv_name = arr_name + "_ref", arg_conv_cleanup = arg_conv_cleanup,
554                     ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
555             else:
556                 assert not arr_len.isdigit() # fixed length arrays not implemented
557                 assert ty_info.java_ty[len(ty_info.java_ty) - 2:] == "[]"
558                 conv_name = "arr_conv_" + str(len(ty_info.java_hu_ty))
559                 idxc = chr(ord('a') + (len(ty_info.java_hu_ty) % 26))
560                 ty_info.subty.var_name = conv_name
561                 if ty_info.is_ptr or holds_ref:
562                     ty_info.subty.requires_clone = False
563                 subty = map_type_with_info(ty_info.subty, False, None, is_free, holds_ref)
564                 if arr_name == "":
565                     arr_name = "arg"
566                 arg_conv = ty_info.rust_obj + " " + arr_name + "_constr;\n"
567                 arg_conv = arg_conv + arr_name + "_constr." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");\n"
568                 arg_conv = arg_conv + "if (" + arr_name + "_constr." + arr_len + " > 0)\n"
569                 if subty.rust_obj is None:
570                     szof = subty.c_ty
571                 else:
572                     szof = subty.rust_obj
573                 arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = MALLOC(" + arr_name + "_constr." + arr_len + " * sizeof(" + szof + "), \"" + ty_info.rust_obj + " Elements\");\n"
574                 arg_conv = arg_conv + "else\n"
575                 arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = NULL;\n"
576                 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
577                     arg_conv = arg_conv + ty_info.java_ty.strip("[]") + "* " + arr_name + "_vals = (*_env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (_env, " + arr_name + ", NULL);\n"
578                 arg_conv = arg_conv + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_constr." + arr_len + "; " + idxc + "++) {\n"
579                 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
580                     arg_conv = arg_conv + "\t" + ty_info.java_ty.strip("[]") + " " + conv_name + " = " + arr_name + "_vals[" + idxc + "];"
581                     if subty.arg_conv is not None:
582                         arg_conv = arg_conv + "\n\t" + subty.arg_conv.replace("\n", "\n\t")
583                 else:
584                     arg_conv = arg_conv + "\tjobject " + conv_name + " = (*_env)->GetObjectArrayElement(_env, " + arr_name + ", " + idxc + ");\n"
585                     arg_conv = arg_conv + "\t" + subty.arg_conv.replace("\n", "\n\t")
586                 arg_conv = arg_conv + "\n\t" + arr_name + "_constr." + ty_info.arr_access + "[" + idxc + "] = " + subty.arg_conv_name + ";\n}"
587                 if not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
588                     arg_conv = arg_conv + "\n(*_env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements (_env, " + arr_name + ", " + arr_name + "_vals, 0);"
589                 if ty_info.is_ptr:
590                     arg_conv_name = "&" + arr_name + "_constr"
591                 else:
592                     arg_conv_name = arr_name + "_constr"
593                 arg_conv_cleanup = None
594                 if ty_info.is_ptr:
595                     arg_conv_cleanup = "FREE(" + arr_name + "_constr." + ty_info.arr_access + ");"
596
597                 if arr_name == "arg":
598                     arr_name = "ret"
599                 ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
600                 if subty.ret_conv is None:
601                     ret_conv = ("DUMMY", "DUMMY")
602                 elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
603                     ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = (*_env)->New" + ty_info.java_ty.strip("[]").title() + "Array(_env, " + arr_name + "_var." + arr_len + ");\n")
604                     ret_conv = (ret_conv[0], ret_conv[1] + subty.c_ty + " *" + arr_name + "_arr_ptr = (*_env)->GetPrimitiveArrayCritical(_env, " + arr_name + "_arr, NULL);\n")
605                     ret_conv = (ret_conv[0], ret_conv[1] + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_var." + arr_len + "; " + idxc + "++) {\n")
606                     ret_conv = (ret_conv[0], ret_conv[1] + "\t" + subty.ret_conv[0].replace("\n", "\n\t"))
607                     ret_conv = (ret_conv[0], ret_conv[1] + arr_name + "_var." + ty_info.arr_access + "[" + idxc + "]" + subty.ret_conv[1].replace("\n", "\n\t"))
608                     ret_conv = (ret_conv[0], ret_conv[1] + "\n\t" + arr_name + "_arr_ptr[" + idxc + "] = " + subty.ret_conv_name + ";\n")
609                     ret_conv = (ret_conv[0], ret_conv[1] + "}\n(*_env)->ReleasePrimitiveArrayCritical(_env, " + arr_name + "_arr, " + arr_name + "_arr_ptr, 0);")
610                 else:
611                     assert ty_info.java_fn_ty_arg.startswith("[")
612                     clz_var = ty_info.java_fn_ty_arg[1:].replace("[", "arr_of_")
613                     c_array_class_caches.add(clz_var)
614                     ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = (*_env)->NewObjectArray(_env, " + arr_name + "_var." + arr_len + ", " + clz_var + "_clz, NULL);\n")
615                     ret_conv = (ret_conv[0], ret_conv[1] + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_var." + arr_len + "; " + idxc + "++) {\n")
616                     ret_conv = (ret_conv[0], ret_conv[1] + "\t" + subty.ret_conv[0].replace("\n", "\n\t"))
617                     ret_conv = (ret_conv[0], ret_conv[1] + arr_name + "_var." + ty_info.arr_access + "[" + idxc + "]" + subty.ret_conv[1].replace("\n", "\n\t"))
618                     ret_conv = (ret_conv[0], ret_conv[1] + "\n\t(*_env)->SetObjectArrayElement(_env, " + arr_name + "_arr, " + idxc + ", " + subty.ret_conv_name + ");\n")
619                     ret_conv = (ret_conv[0], ret_conv[1] + "}")
620                 if not holds_ref:
621                     # Note that we don't drop the full vec here - we're passing ownership to java (or have cloned) or free'd by now!
622                     ret_conv = (ret_conv[0], ret_conv[1] + "\nFREE(" + arr_name + "_var." + ty_info.arr_access + ");")
623
624                 to_hu_conv = None
625                 to_hu_conv_name = None
626                 if subty.to_hu_conv is not None:
627                     to_hu_conv = ty_info.java_hu_ty + " " + conv_name + "_arr = new " + ty_info.subty.java_hu_ty.split("<")[0] + "[" + arr_name + ".length];\n"
628                     to_hu_conv = to_hu_conv + "for (int " + idxc + " = 0; " + idxc + " < " + arr_name + ".length; " + idxc + "++) {\n"
629                     to_hu_conv = to_hu_conv + "\t" + subty.java_ty + " " + conv_name + " = " + arr_name + "[" + idxc + "];\n"
630                     to_hu_conv = to_hu_conv + "\t" + subty.to_hu_conv.replace("\n", "\n\t") + "\n"
631                     to_hu_conv = to_hu_conv + "\t" + conv_name + "_arr[" + idxc + "] = " + subty.to_hu_conv_name + ";\n}"
632                     to_hu_conv_name = conv_name + "_arr"
633                 from_hu_conv = None
634                 if subty.from_hu_conv is not None:
635                     if subty.java_ty == "long" and subty.java_hu_ty != "long":
636                         from_hu_conv = ("Arrays.stream(" + arr_name + ").mapToLong(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + "  */")
637                     elif subty.java_ty == "long":
638                         from_hu_conv = ("Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + "  */")
639                     else:
640                         from_hu_conv = ("(" + ty_info.java_ty + ")Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + "  */")
641
642                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
643                     arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = arg_conv_cleanup,
644                     ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = to_hu_conv, to_hu_conv_name = to_hu_conv_name, from_hu_conv = from_hu_conv)
645         elif ty_info.java_ty == "String":
646             if ty_info.arr_access is None:
647                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
648                     arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
649                     ret_conv = ("jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv",
650                     to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
651             else:
652                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
653                     arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
654                     ret_conv = ("LDKStr " + ty_info.var_name + "_str = ",
655                         ";\nchar* " + ty_info.var_name + "_buf = MALLOC(" + ty_info.var_name + "_str." + ty_info.arr_len + " + 1, \"str conv buf\");\n" +
656                         "memcpy(" + ty_info.var_name + "_buf, " + ty_info.var_name + "_str." + ty_info.arr_access + ", " + ty_info.var_name + "_str." + ty_info.arr_len + ");\n" +
657                         ty_info.var_name + "_buf[" + ty_info.var_name + "_str." + ty_info.arr_len + "] = 0;\n" +
658                         "jstring " + ty_info.var_name + "_conv = (*_env)->NewStringUTF(_env, " + ty_info.var_name + "_str." + ty_info.arr_access + ");\n" +
659                         "FREE(" + ty_info.var_name + "_buf);"),
660                     ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
661         elif ty_info.var_name == "" and not print_void:
662             # We don't have a parameter name, and want one, just call it arg
663             if ty_info.rust_obj is not None:
664                 assert(not is_free or ty_info.rust_obj not in opaque_structs)
665                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
666                     arg_conv = ty_info.rust_obj + " arg_conv = *(" + ty_info.rust_obj + "*)arg;\nFREE((void*)arg);",
667                     arg_conv_name = "arg_conv", arg_conv_cleanup = None,
668                     ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 7", to_hu_conv_name = None, from_hu_conv = None)
669             else:
670                 assert(not is_free)
671                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
672                     arg_conv = None, arg_conv_name = "arg", arg_conv_cleanup = None,
673                     ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 8", to_hu_conv_name = None, from_hu_conv = None)
674         elif ty_info.rust_obj is None:
675             return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
676                 arg_conv = None, arg_conv_name = ty_info.var_name, arg_conv_cleanup = None,
677                 ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
678         else:
679             if ty_info.var_name == "":
680                 ty_info.var_name = "ret"
681
682             if ty_info.rust_obj in opaque_structs:
683                 opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
684                 opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = (void*)(" + ty_info.var_name + " & (~1));\n"
685                 if ty_info.is_ptr and holds_ref:
686                     opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = false;"
687                 else:
688                     opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.is_owned = (" + ty_info.var_name + " & 1) || (" + ty_info.var_name + " == 0);"
689                 if not is_free and (not ty_info.is_ptr or not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False:
690                     if (ty_info.java_hu_ty + "_clone") in clone_fns:
691                         # TODO: This is a bit too naive, even with the checks above, we really need to know if rust wants a ref or not, not just if its pass as a ptr.
692                         opaque_arg_conv = opaque_arg_conv + "\nif (" + ty_info.var_name + "_conv.inner != NULL)\n"
693                         opaque_arg_conv = opaque_arg_conv + "\t" + ty_info.var_name + "_conv = " + ty_info.java_hu_ty + "_clone(&" + ty_info.var_name + "_conv);"
694                     elif ty_info.passed_as_ptr:
695                         opaque_arg_conv = opaque_arg_conv + "\n// Warning: we may need a move here but can't clone!"
696
697                 opaque_ret_conv_suf = ";\n"
698                 if not holds_ref and ty_info.is_ptr and (ty_info.java_hu_ty + "_clone") in clone_fns: # is_ptr, not holds_ref implies passing a pointed-to value to java, which needs copied
699                     opaque_ret_conv_suf = opaque_ret_conv_suf + "if (" + ty_info.var_name + "->inner != NULL)\n"
700                     opaque_ret_conv_suf = opaque_ret_conv_suf + "\t" + ty_info.var_name + "_var = " + ty_info.java_hu_ty + "_clone(" + ty_info.var_name + ");\n"
701                 elif not holds_ref and ty_info.is_ptr:
702                     opaque_ret_conv_suf = opaque_ret_conv_suf + "// Warning: we may need a move here but can't clone!\n"
703
704                 opaque_ret_conv_suf = opaque_ret_conv_suf + "CHECK((((long)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
705                 opaque_ret_conv_suf = opaque_ret_conv_suf + "CHECK((((long)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
706                 if holds_ref:
707                     opaque_ret_conv_suf = opaque_ret_conv_suf + "long " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner & ~1;"
708                 else:
709                     opaque_ret_conv_suf = opaque_ret_conv_suf + "long " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner;\n"
710                     opaque_ret_conv_suf = opaque_ret_conv_suf + "if (" + ty_info.var_name + "_var.is_owned) {\n"
711                     opaque_ret_conv_suf = opaque_ret_conv_suf + "\t" + ty_info.var_name + "_ref |= 1;\n"
712                     opaque_ret_conv_suf = opaque_ret_conv_suf + "}"
713
714                 if ty_info.is_ptr:
715                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
716                         arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
717                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = *", opaque_ret_conv_suf),
718                         ret_conv_name = ty_info.var_name + "_ref",
719                         to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
720                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
721                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
722                 else:
723                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
724                         arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
725                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", opaque_ret_conv_suf),
726                         ret_conv_name = ty_info.var_name + "_ref",
727                         to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
728                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
729                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
730
731             if not ty_info.is_ptr:
732                 if ty_info.rust_obj in unitary_enums:
733                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
734                         arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_from_java(_env, " + ty_info.var_name + ");",
735                         arg_conv_name = ty_info.var_name + "_conv",
736                         arg_conv_cleanup = None,
737                         ret_conv = ("jclass " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_to_java(_env, ", ");"),
738                         ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
739                 base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";"
740                 if ty_info.rust_obj in trait_structs:
741                     if not is_free:
742                         needs_full_clone = not is_free and (not ty_info.is_ptr and not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False
743                         if needs_full_clone and (ty_info.java_hu_ty + "_clone") in clone_fns:
744                             base_conv = base_conv + "\n" + ty_info.var_name + "_conv = " + ty_info.java_hu_ty + "_clone(" + ty_info.var_name + ");"
745                         else:
746                             base_conv = base_conv + "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
747                             base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
748                             base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}"
749                             if needs_full_clone:
750                                 base_conv = base_conv + "// Warning: we may need a move here but can't do a full clone!\n"
751
752                     else:
753                         base_conv = base_conv + "\n" + "FREE((void*)" + ty_info.var_name + ");"
754                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
755                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
756                         ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"),
757                         ret_conv_name = "(long)ret",
758                         to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
759                         to_hu_conv_name = "ret_hu_conv",
760                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
761                 if ty_info.rust_obj != "LDKu8slice":
762                     # Don't bother free'ing slices passed in - Rust doesn't auto-free the
763                     # underlying unlike Vecs, and it gives Java more freedom.
764                     base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");";
765                 if ty_info.rust_obj in complex_enums:
766                     ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";")
767                     if not holds_ref:
768                         ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_copy = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n", "")
769                         if ty_info.requires_clone == True: # Set in object array mapping
770                             if (ty_info.java_hu_ty + "_clone") in clone_fns:
771                                 ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = " + ty_info.java_hu_ty + "_clone(&", ");\n")
772                             else:
773                                 ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = ", "; // XXX: We likely need to clone here, but no _clone fn is available!\n")
774                         else:
775                             ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = ", ";\n")
776                         ret_conv = (ret_conv[0], ret_conv[1] + "long " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_copy;")
777                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
778                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
779                         ret_conv = ret_conv, ret_conv_name = ty_info.var_name + "_ref",
780                         to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");\n" + ty_info.var_name + "_hu_conv.ptrs_to.add(this);",
781                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + ".ptr", ""))
782                 if ty_info.rust_obj in result_types:
783                     #assert not ty_info.is_ptr and not holds_ref # Otherwise we shouldn't be MALLOC'ing
784                     # XXX ^
785                     ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_conv = ", ";")
786                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
787                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
788                         ret_conv = ret_conv, ret_conv_name = "(long)" + ty_info.var_name + "_conv",
789                         to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");",
790                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + " != null ? " + ty_info.var_name + ".ptr : 0", ""))
791                 if ty_info.rust_obj in tuple_types:
792                     from_hu_conv = "bindings." + tuple_types[ty_info.rust_obj][1].replace("LDK", "") + "_new("
793                     to_hu_conv_pfx = ""
794                     to_hu_conv_sfx = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " + ty_info.java_hu_ty + "("
795                     for idx, conv in enumerate(tuple_types[ty_info.rust_obj][0]):
796                         if idx != 0:
797                             to_hu_conv_sfx = to_hu_conv_sfx + ", "
798                             from_hu_conv = from_hu_conv + ", "
799                         conv.var_name = ty_info.var_name + "_" + chr(idx + ord("a"))
800                         conv_map = map_type_with_info(conv, False, None, is_free, holds_ref)
801                         to_hu_conv_pfx = to_hu_conv_pfx + conv.java_ty + " " + ty_info.var_name + "_" + chr(idx + ord("a")) + " = " + "bindings." + tuple_types[ty_info.rust_obj][1] + "_get_" + chr(idx + ord("a")) + "(" + ty_info.var_name + ");\n"
802                         if conv_map.to_hu_conv is not None:
803                             to_hu_conv_pfx = to_hu_conv_pfx + conv_map.to_hu_conv + ";\n"
804                             to_hu_conv_sfx = to_hu_conv_sfx + conv_map.to_hu_conv_name
805                         else:
806                             to_hu_conv_sfx = to_hu_conv_sfx + ty_info.var_name + "_" + chr(idx + ord("a"))
807                         if conv_map.from_hu_conv is not None:
808                             from_hu_conv = from_hu_conv + conv_map.from_hu_conv[0].replace(ty_info.var_name + "_" + chr(idx + ord("a")), ty_info.var_name + "." + chr(idx + ord("a")))
809                             if conv_map.from_hu_conv[1] != "":
810                                 from_hu_conv = from_hu_conv + "/*XXX: " + conv_map.from_hu_conv[1] + "*/"
811                         else:
812                             from_hu_conv = from_hu_conv + ty_info.var_name + "." + chr(idx + ord("a"))
813
814                     if not ty_info.is_ptr and not holds_ref:
815                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
816                             arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
817
818                             ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_ref = ", ";"),
819                             ret_conv_name = "(long)" + ty_info.var_name + "_ref",
820                             to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", ""))
821                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
822                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
823                         ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref",
824                         to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", ""))
825
826                 # The manually-defined types - TxOut and Transaction
827                 assert ty_info.rust_obj == "LDKTxOut"
828                 if not ty_info.is_ptr and not holds_ref:
829                     ret_conv = ("LDKTxOut* " + ty_info.var_name + "_ref = MALLOC(sizeof(LDKTxOut), \"LDKTxOut\");\n*" + ty_info.var_name + "_ref = ", ";")
830                 else:
831                     ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";")
832                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
833                     arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
834                     ret_conv = ret_conv, ret_conv_name = "(long)" + ty_info.var_name + "_ref",
835                     to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
836                     to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".ptr", ""))
837             elif ty_info.is_ptr:
838                 assert(not is_free)
839                 if ty_info.rust_obj in complex_enums:
840                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
841                         arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
842                         arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
843                         ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
844                         to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");",
845                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
846                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
847                 elif ty_info.rust_obj in trait_structs:
848                     if ty_info.java_hu_ty + "_clone" in clone_fns:
849                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
850                             arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
851                             arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
852                             ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" +
853                                 "*" + ty_info.var_name + "_clone = " + ty_info.java_hu_ty + "_clone(", ");"),
854                             ret_conv_name = "(long)" + ty_info.var_name + "_clone",
855                             to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
856                             to_hu_conv_name = "ret_hu_conv",
857                             from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
858                     else:
859                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
860                             arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
861                             arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
862                             ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
863                             to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
864                             to_hu_conv_name = "ret_hu_conv",
865                             from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))
866                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
867                     arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
868                     arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
869                     ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
870                     to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed
871             assert False # We should have handled every case by now.
872
873     def map_fn(line, re_match, ret_arr_len, c_call_string):
874         out_java.write("\t// " + line)
875         out_java.write("\tpublic static native ")
876         write_c("JNIEXPORT ")
877
878         is_free = re_match.group(2).endswith("_free")
879         struct_meth = re_match.group(2).split("_")[0]
880
881         ret_info = map_type(re_match.group(1), True, ret_arr_len, False, False)
882         ret_info.print_ty()
883
884         if ret_info.ret_conv is not None:
885             ret_conv_pfx, ret_conv_sfx = ret_info.ret_conv
886
887         out_java.write(" " + re_match.group(2) + "(")
888         write_c(" JNICALL Java_org_ldk_impl_bindings_" + re_match.group(2).replace('_', '_1') + "(JNIEnv * _env, jclass _b")
889
890         arg_names = []
891         default_constructor_args = {}
892         takes_self = False
893         args_known = True
894         for idx, arg in enumerate(re_match.group(3).split(',')):
895             if idx != 0:
896                 out_java.write(", ")
897             if arg != "void":
898                 write_c(", ")
899             arg_conv_info = map_type(arg, False, None, is_free, True)
900             if arg_conv_info.c_ty != "void":
901                 arg_conv_info.print_ty()
902                 arg_conv_info.print_name()
903             if idx == 0 and arg_conv_info.java_hu_ty == struct_meth:
904                 takes_self = True
905             if arg_conv_info.arg_conv is not None and "Warning" in arg_conv_info.arg_conv:
906                 if arg_conv_info.rust_obj in constructor_fns:
907                     assert not is_free
908                     for explode_arg in constructor_fns[arg_conv_info.rust_obj].split(','):
909                         explode_arg_conv = map_type(explode_arg, False, None, False, True)
910                         if explode_arg_conv.c_ty == "void":
911                             # We actually want to handle this case, but for now its only used in NetGraphMsgHandler::new()
912                             # which ends up resulting in a redundant constructor - both without arguments for the NetworkGraph.
913                             args_known = False
914                             pass
915                         if not arg_conv_info.arg_name in default_constructor_args:
916                             default_constructor_args[arg_conv_info.arg_name] = []
917                         default_constructor_args[arg_conv_info.arg_name].append(explode_arg_conv)
918             arg_names.append(arg_conv_info)
919
920         out_java_struct = None
921         if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
922             out_java_struct = open(sys.argv[3] + "/structs/" + struct_meth + ".java", "a")
923             if not args_known:
924                 out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n")
925                 out_java_struct.close()
926                 out_java_struct = None
927             else:
928                 meth_n = re_match.group(2)[len(struct_meth) + 1:]
929                 if not takes_self:
930                     out_java_struct.write("\tpublic static " + ret_info.java_hu_ty + " constructor_" + meth_n + "(")
931                 else:
932                     out_java_struct.write("\tpublic " + ret_info.java_hu_ty + " " + meth_n + "(")
933                 for idx, arg in enumerate(arg_names):
934                     if idx != 0:
935                         if not takes_self or idx > 1:
936                             out_java_struct.write(", ")
937                     elif takes_self:
938                         continue
939                     if arg.java_ty != "void":
940                         if arg.arg_name in default_constructor_args:
941                             for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
942                                 if explode_idx != 0:
943                                     out_java_struct.write(", ")
944                                 out_java_struct.write(explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name)
945                         else:
946                             out_java_struct.write(arg.java_hu_ty + " " + arg.arg_name)
947
948
949         out_java.write(");\n")
950         write_c(") {\n")
951         if out_java_struct is not None:
952             out_java_struct.write(") {\n")
953
954         for info in arg_names:
955             if info.arg_conv is not None:
956                 write_c("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
957
958         if ret_info.ret_conv is not None:
959             write_c("\t" + ret_conv_pfx.replace('\n', '\n\t'))
960         elif ret_info.c_ty != "void":
961             write_c("\t" + ret_info.c_ty + " ret_val = ")
962         else:
963             write_c("\t")
964
965         if c_call_string is None:
966             write_c(re_match.group(2) + "(")
967         else:
968             write_c(c_call_string)
969         for idx, info in enumerate(arg_names):
970             if info.arg_conv_name is not None:
971                 if idx != 0:
972                     write_c(", ")
973                 elif c_call_string is not None:
974                     continue
975                 write_c(info.arg_conv_name)
976         write_c(")")
977         if ret_info.ret_conv is not None:
978             write_c(ret_conv_sfx.replace('\n', '\n\t'))
979         else:
980             write_c(";")
981         for info in arg_names:
982             if info.arg_conv_cleanup is not None:
983                 write_c("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
984         if ret_info.ret_conv is not None:
985             write_c("\n\treturn " + ret_info.ret_conv_name + ";")
986         elif ret_info.c_ty != "void":
987             write_c("\n\treturn ret_val;")
988         write_c("\n}\n\n")
989         if out_java_struct is not None:
990             out_java_struct.write("\t\t")
991             if ret_info.java_ty != "void":
992                 out_java_struct.write(ret_info.java_ty + " ret = ")
993             out_java_struct.write("bindings." + re_match.group(2) + "(")
994             for idx, info in enumerate(arg_names):
995                 if idx != 0:
996                     out_java_struct.write(", ")
997                 if idx == 0 and takes_self:
998                     out_java_struct.write("this.ptr")
999                 elif info.arg_name in default_constructor_args:
1000                     out_java_struct.write("bindings." + info.java_hu_ty + "_new(")
1001                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1002                         if explode_idx != 0:
1003                             out_java_struct.write(", ")
1004                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1005                         if explode_arg.from_hu_conv is not None:
1006                             out_java_struct.write(explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1007                         else:
1008                             out_java_struct.write(expl_arg_name)
1009                     out_java_struct.write(")")
1010                 elif info.from_hu_conv is not None:
1011                     out_java_struct.write(info.from_hu_conv[0])
1012                 else:
1013                     out_java_struct.write(info.arg_name)
1014             out_java_struct.write(");\n")
1015             if ret_info.to_hu_conv is not None:
1016                 if not takes_self:
1017                     out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t").replace("this", ret_info.to_hu_conv_name) + "\n")
1018                 else:
1019                     out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1020
1021             for idx, info in enumerate(arg_names):
1022                 if idx == 0 and takes_self:
1023                     pass
1024                 elif info.arg_name in default_constructor_args:
1025                     for explode_arg in default_constructor_args[info.arg_name]:
1026                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1027                         if explode_arg.from_hu_conv is not None and ret_info.to_hu_conv_name:
1028                             out_java_struct.write("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name, expl_arg_name).replace("this", ret_info.to_hu_conv_name) + ";\n")
1029                 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1030                     if not takes_self and ret_info.to_hu_conv_name is not None:
1031                         out_java_struct.write("\t\t" + info.from_hu_conv[1].replace("this", ret_info.to_hu_conv_name) + ";\n")
1032                     else:
1033                         out_java_struct.write("\t\t" + info.from_hu_conv[1] + ";\n")
1034
1035             if ret_info.to_hu_conv_name is not None:
1036                 out_java_struct.write("\t\treturn " + ret_info.to_hu_conv_name + ";\n")
1037             elif ret_info.java_ty != "void" and ret_info.rust_obj != "LDK" + struct_meth:
1038                 out_java_struct.write("\t\treturn ret;\n")
1039             out_java_struct.write("\t}\n\n")
1040             out_java_struct.close()
1041
1042     def map_unitary_enum(struct_name, field_lines):
1043         with open(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum:
1044             out_java_enum.write("package org.ldk.enums;\n\n")
1045             unitary_enums.add(struct_name)
1046             write_c("static inline " + struct_name + " " + struct_name + "_from_java(JNIEnv *env, jclass val) {\n")
1047             write_c("\tswitch ((*env)->CallIntMethod(env, val, ordinal_meth)) {\n")
1048             ord_v = 0
1049             for idx, struct_line in enumerate(field_lines):
1050                 if idx == 0:
1051                     out_java_enum.write("public enum " + struct_name + " {\n")
1052                 elif idx == len(field_lines) - 3:
1053                     assert(struct_line.endswith("_Sentinel,"))
1054                 elif idx == len(field_lines) - 2:
1055                     out_java_enum.write("\t; static native void init();\n")
1056                     out_java_enum.write("\tstatic { init(); }\n")
1057                     out_java_enum.write("}")
1058                     out_java.write("\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n")
1059                 elif idx == len(field_lines) - 1:
1060                     assert(struct_line == "")
1061                 else:
1062                     out_java_enum.write(struct_line + "\n")
1063                     write_c("\t\tcase %d: return %s;\n" % (ord_v, struct_line.strip().strip(",")))
1064                     ord_v = ord_v + 1
1065             write_c("\t}\n")
1066             write_c("\tabort();\n")
1067             write_c("}\n")
1068
1069             ord_v = 0
1070             write_c("static jclass " + struct_name + "_class = NULL;\n")
1071             for idx, struct_line in enumerate(field_lines):
1072                 if idx > 0 and idx < len(field_lines) - 3:
1073                     variant = struct_line.strip().strip(",")
1074                     write_c("static jfieldID " + struct_name + "_" + variant + " = NULL;\n")
1075             write_c("JNIEXPORT void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass clz) {\n")
1076             write_c("\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n")
1077             write_c("\tCHECK(" + struct_name + "_class != NULL);\n")
1078             for idx, struct_line in enumerate(field_lines):
1079                 if idx > 0 and idx < len(field_lines) - 3:
1080                     variant = struct_line.strip().strip(",")
1081                     write_c("\t" + struct_name + "_" + variant + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
1082                     write_c("\tCHECK(" + struct_name + "_" + variant + " != NULL);\n")
1083             write_c("}\n")
1084             write_c("static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n")
1085             write_c("\tswitch (val) {\n")
1086             for idx, struct_line in enumerate(field_lines):
1087                 if idx > 0 and idx < len(field_lines) - 3:
1088                     variant = struct_line.strip().strip(",")
1089                     write_c("\t\tcase " + variant + ":\n")
1090                     write_c("\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + variant + ");\n")
1091                     ord_v = ord_v + 1
1092             write_c("\t\tdefault: abort();\n")
1093             write_c("\t}\n")
1094             write_c("}\n\n")
1095
1096     def map_complex_enum(struct_name, union_enum_items):
1097         java_hu_type = struct_name.replace("LDK", "")
1098         complex_enums.add(struct_name)
1099         with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
1100             out_java_enum.write(hu_struct_file_prefix)
1101             out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
1102             out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
1103             out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
1104             out_java_enum.write("\tprotected void finalize() throws Throwable {\n")
1105             out_java_enum.write("\t\tsuper.finalize();\n")
1106             out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n")
1107             out_java_enum.write("\t}\n")
1108             out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
1109             out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
1110             java_hu_subclasses = ""
1111
1112             tag_field_lines = union_enum_items["field_lines"]
1113             init_meth_jty_strs = {}
1114             for idx, struct_line in enumerate(tag_field_lines):
1115                 if idx == 0:
1116                     out_java.write("\tpublic static class " + struct_name + " {\n")
1117                     out_java.write("\t\tprivate " + struct_name + "() {}\n")
1118                 elif idx == len(tag_field_lines) - 3:
1119                     assert(struct_line.endswith("_Sentinel,"))
1120                 elif idx == len(tag_field_lines) - 2:
1121                     out_java.write("\t\tstatic native void init();\n")
1122                     out_java.write("\t}\n")
1123                 elif idx == len(tag_field_lines) - 1:
1124                     assert(struct_line == "")
1125                 else:
1126                     var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
1127                     out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
1128                     java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
1129                     out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
1130                     out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n")
1131                     write_c("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
1132                     write_c("static jmethodID " + struct_name + "_" + var_name + "_meth = NULL;\n")
1133                     init_meth_jty_str = ""
1134                     init_meth_params = ""
1135                     init_meth_body = ""
1136                     hu_conv_body = ""
1137                     if "LDK" + var_name in union_enum_items:
1138                         enum_var_lines = union_enum_items["LDK" + var_name]
1139                         for idx, field in enumerate(enum_var_lines):
1140                             if idx != 0 and idx < len(enum_var_lines) - 2:
1141                                 field_ty = map_type(field.strip(' ;'), False, None, False, True)
1142                                 out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
1143                                 java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
1144                                 if field_ty.to_hu_conv is not None:
1145                                     hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
1146                                     hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
1147                                     hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
1148                                 else:
1149                                     hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
1150                                 init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
1151                                 if idx > 1:
1152                                     init_meth_params = init_meth_params + ", "
1153                                 init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
1154                                 init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
1155                         out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
1156                         out_java.write(init_meth_body)
1157                         out_java.write("}\n")
1158                     out_java.write("\t\t}\n")
1159                     out_java_enum.write("\t\t}\n")
1160                     java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
1161                     java_hu_subclasses = java_hu_subclasses + hu_conv_body
1162                     java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n"
1163                     init_meth_jty_strs[var_name] = init_meth_jty_str
1164             out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
1165             out_java_enum.write(java_hu_subclasses)
1166             out_java.write("\tstatic { " + struct_name + ".init(); }\n")
1167             out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
1168
1169             write_c("JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass _a) {\n")
1170             for idx, struct_line in enumerate(tag_field_lines):
1171                 if idx != 0 and idx < len(tag_field_lines) - 3:
1172                     var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
1173                     write_c("\t" + struct_name + "_" + var_name + "_class =\n")
1174                     write_c("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
1175                     write_c("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
1176                     write_c("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
1177                     write_c("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
1178             write_c("}\n")
1179             write_c("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
1180             write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
1181             write_c("\tswitch(obj->tag) {\n")
1182             for idx, struct_line in enumerate(tag_field_lines):
1183                 if idx != 0 and idx < len(tag_field_lines) - 3:
1184                     var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
1185                     write_c("\t\tcase " + struct_name + "_" + var_name + ": {\n")
1186                     c_params_text = ""
1187                     if "LDK" + var_name in union_enum_items:
1188                         enum_var_lines = union_enum_items["LDK" + var_name]
1189                         for idx, field in enumerate(enum_var_lines):
1190                             if idx != 0 and idx < len(enum_var_lines) - 2:
1191                                 field_map = map_type(field.strip(' ;'), False, None, False, True)
1192                                 if field_map.ret_conv is not None:
1193                                     write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
1194                                     write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
1195                                     write_c(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
1196                                     c_params_text = c_params_text + ", " + field_map.ret_conv_name
1197                                 else:
1198                                     c_params_text = c_params_text + ", obj->" + camel_to_snake(var_name) + "." + field_map.arg_name
1199                     write_c("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
1200                     write_c("\t\t}\n")
1201             write_c("\t\tdefault: abort();\n")
1202             write_c("\t}\n}\n")
1203             out_java_enum.write("}\n")
1204
1205     def map_trait(struct_name, field_var_lines, trait_fn_lines):
1206         with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_trait:
1207             write_c("typedef struct " + struct_name + "_JCalls {\n")
1208             write_c("\tatomic_size_t refcnt;\n")
1209             write_c("\tJavaVM *vm;\n")
1210             write_c("\tjweak o;\n")
1211             field_var_convs = []
1212             for var_line in field_var_lines:
1213                 if var_line.group(1) in trait_structs:
1214                     write_c("\t" + var_line.group(1) + "_JCalls* " + var_line.group(2) + ";\n")
1215                     field_var_convs.append(None)
1216                 else:
1217                     field_var_convs.append(map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False))
1218             for fn_line in trait_fn_lines:
1219                 if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
1220                     write_c("\tjmethodID " + fn_line.group(3) + "_meth;\n")
1221             write_c("} " + struct_name + "_JCalls;\n")
1222
1223             out_java_trait.write(hu_struct_file_prefix)
1224             out_java_trait.write("public class " + struct_name.replace("LDK","") + " extends CommonBase {\n")
1225             out_java_trait.write("\tfinal bindings." + struct_name + " bindings_instance;\n")
1226             out_java_trait.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n")
1227             out_java_trait.write("\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg")
1228             for idx, var_line in enumerate(field_var_lines):
1229                 if var_line.group(1) in trait_structs:
1230                     out_java_trait.write(", bindings." + var_line.group(1) + " " + var_line.group(2))
1231                 else:
1232                     out_java_trait.write(", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2))
1233             out_java_trait.write(") {\n")
1234             out_java_trait.write("\t\tsuper(bindings." + struct_name + "_new(arg")
1235             for idx, var_line in enumerate(field_var_lines):
1236                 if var_line.group(1) in trait_structs:
1237                     out_java_trait.write(", " + var_line.group(2))
1238                 elif field_var_convs[idx].from_hu_conv is not None:
1239                     out_java_trait.write(", " + field_var_convs[idx].from_hu_conv[0])
1240                 else:
1241                     out_java_trait.write(", " + var_line.group(2))
1242             out_java_trait.write("));\n")
1243             out_java_trait.write("\t\tthis.ptrs_to.add(arg);\n")
1244             for idx, var_line in enumerate(field_var_lines):
1245                 if var_line.group(1) in trait_structs:
1246                     out_java_trait.write("\t\tthis.ptrs_to.add(" + var_line.group(2) + ");\n")
1247                 elif field_var_convs[idx].from_hu_conv is not None and field_var_convs[idx].from_hu_conv[1] != "":
1248                     out_java_trait.write("\t\t" + field_var_convs[idx].from_hu_conv[1] + ";\n")
1249             out_java_trait.write("\t\tthis.bindings_instance = arg;\n")
1250             out_java_trait.write("\t}\n")
1251             out_java_trait.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
1252             out_java_trait.write("\tprotected void finalize() throws Throwable {\n")
1253             out_java_trait.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
1254             out_java_trait.write("\t}\n\n")
1255
1256             java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
1257             java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
1258             for idx, var_line in enumerate(field_var_lines):
1259                 if var_line.group(1) in trait_structs:
1260                     # Ideally we'd be able to take any instance of the interface, but our C code can only represent
1261                     # Java-implemented version, so we require users pass a Java implementation here :/
1262                     java_trait_constr = java_trait_constr + ", " + var_line.group(1).replace("LDK", "") + "." + var_line.group(1).replace("LDK", "") + "Interface " + var_line.group(2) + "_impl"
1263                 else:
1264                     java_trait_constr = java_trait_constr + ", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2)
1265             java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
1266             java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n"
1267             out_java_trait.write("\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n")
1268             out_java.write("\tpublic interface " + struct_name + " {\n")
1269             java_meths = []
1270             for fn_line in trait_fn_lines:
1271                 java_meth_descr = "("
1272                 if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
1273                     ret_ty_info = map_type(fn_line.group(2), True, None, False, False)
1274
1275                     out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(3) + "(")
1276                     java_trait_constr = java_trait_constr + "\t\t\t@Override public " + ret_ty_info.java_ty + " " + fn_line.group(3) + "("
1277                     out_java_trait.write("\t\t" + ret_ty_info.java_hu_ty + " " + fn_line.group(3) + "(")
1278                     is_const = fn_line.group(4) is not None
1279                     write_c(fn_line.group(2) + fn_line.group(3) + "_jcall(")
1280                     if is_const:
1281                         write_c("const void* this_arg")
1282                     else:
1283                         write_c("void* this_arg")
1284
1285                     arg_names = []
1286                     for idx, arg in enumerate(fn_line.group(5).split(',')):
1287                         if arg == "":
1288                             continue
1289                         if idx >= 2:
1290                             out_java.write(", ")
1291                             java_trait_constr = java_trait_constr + ", "
1292                             out_java_trait.write(", ")
1293                         write_c(", ")
1294                         arg_conv_info = map_type(arg, True, None, False, False)
1295                         write_c(arg.strip())
1296                         out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
1297                         out_java_trait.write(arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name)
1298                         java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
1299                         arg_names.append(arg_conv_info)
1300                         java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
1301                     java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
1302                     java_meths.append((fn_line, java_meth_descr))
1303
1304                     out_java.write(");\n")
1305                     out_java_trait.write(");\n")
1306                     java_trait_constr = java_trait_constr + ") {\n"
1307                     write_c(") {\n")
1308                     write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1309                     write_c("\tJNIEnv *_env;\n")
1310                     write_c("\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&_env, JNI_VERSION_1_8) == JNI_OK);\n")
1311
1312                     for arg_info in arg_names:
1313                         if arg_info.ret_conv is not None:
1314                             write_c("\t" + arg_info.ret_conv[0].replace('\n', '\n\t'));
1315                             write_c(arg_info.arg_name)
1316                             write_c(arg_info.ret_conv[1].replace('\n', '\n\t') + "\n")
1317                         if arg_info.to_hu_conv is not None:
1318                             java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
1319
1320                     write_c("\tjobject obj = (*_env)->NewLocalRef(_env, j_calls->o);\n\tCHECK(obj != NULL);\n")
1321                     if ret_ty_info.c_ty.endswith("Array"):
1322                         write_c("\t" + ret_ty_info.c_ty + " arg = (*_env)->CallObjectMethod(_env, obj, j_calls->" + fn_line.group(3) + "_meth")
1323                     elif not ret_ty_info.passed_as_ptr:
1324                         write_c("\treturn (*_env)->Call" + ret_ty_info.java_ty.title() + "Method(_env, obj, j_calls->" + fn_line.group(3) + "_meth")
1325                     else:
1326                         write_c("\t" + fn_line.group(2).strip() + "* ret = (" + fn_line.group(2).strip() + "*)(*_env)->CallLongMethod(_env, obj, j_calls->" + fn_line.group(3) + "_meth");
1327                     if ret_ty_info.java_ty != "void":
1328                         java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_hu_ty + " ret = arg." + fn_line.group(3) + "("
1329                     else:
1330                         java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(3) + "("
1331
1332                     for idx, arg_info in enumerate(arg_names):
1333                         if arg_info.ret_conv is not None:
1334                             write_c(", " + arg_info.ret_conv_name)
1335                         else:
1336                             write_c(", " + arg_info.arg_name)
1337                         if idx != 0:
1338                             java_trait_constr = java_trait_constr + ", "
1339                         if arg_info.to_hu_conv_name is not None:
1340                             java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name
1341                         else:
1342                             java_trait_constr = java_trait_constr + arg_info.arg_name
1343                     write_c(");\n");
1344                     if ret_ty_info.arg_conv is not None:
1345                         write_c("\t" + ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + ret_ty_info.arg_conv_name + ";\n")
1346
1347                     write_c("}\n")
1348                     java_trait_constr = java_trait_constr + ");\n"
1349                     if ret_ty_info.java_ty != "void":
1350                         if ret_ty_info.from_hu_conv is not None:
1351                             java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_ty + " result = " + ret_ty_info.from_hu_conv[0] + ";\n"
1352                             if ret_ty_info.from_hu_conv[1] != "":
1353                                 java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
1354                             if ret_ty_info.rust_obj in result_types:
1355                                 # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
1356                                 java_trait_constr = java_trait_constr + "\t\t\t\tret.ptr = 0;\n"
1357                             java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n"
1358                         else:
1359                             java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n"
1360                     java_trait_constr = java_trait_constr + "\t\t\t}\n"
1361                 elif fn_line.group(3) == "free":
1362                     write_c("static void " + struct_name + "_JCalls_free(void* this_arg) {\n")
1363                     write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1364                     write_c("\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n")
1365                     write_c("\t\tJNIEnv *env;\n")
1366                     write_c("\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n")
1367                     write_c("\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n")
1368                     write_c("\t\tFREE(j_calls);\n")
1369                     write_c("\t}\n}\n")
1370             java_trait_constr = java_trait_constr + "\t\t}"
1371             for var_line in field_var_lines:
1372                 if var_line.group(1) in trait_structs:
1373                     java_trait_constr = java_trait_constr + ", " + var_line.group(2) + ".new_impl(" + var_line.group(2) + "_impl).bindings_instance"
1374                 else:
1375                     java_trait_constr = java_trait_constr + ", " + var_line.group(2)
1376             out_java_trait.write("\t}\n")
1377             out_java_trait.write(java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n")
1378
1379             # Write out a clone function whether we need one or not, as we use them in moving to rust
1380             write_c("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
1381             write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
1382             write_c("\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n")
1383             for var_line in field_var_lines:
1384                 if var_line.group(1) in trait_structs:
1385                     write_c("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
1386             write_c("\treturn (void*) this_arg;\n")
1387             write_c("}\n")
1388
1389             out_java.write("\t}\n")
1390
1391             out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
1392             write_c("static inline " + struct_name + " " + struct_name + "_init (JNIEnv * env, jclass _a, jobject o")
1393             for idx, var_line in enumerate(field_var_lines):
1394                 if var_line.group(1) in trait_structs:
1395                     out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
1396                     write_c(", jobject " + var_line.group(2))
1397                 else:
1398                     out_java.write(", " + field_var_convs[idx].java_ty + " " + var_line.group(2))
1399                     write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
1400             out_java.write(");\n")
1401             write_c(") {\n")
1402
1403             write_c("\tjclass c = (*env)->GetObjectClass(env, o);\n")
1404             write_c("\tCHECK(c != NULL);\n")
1405             write_c("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
1406             write_c("\tatomic_init(&calls->refcnt, 1);\n")
1407             write_c("\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n")
1408             write_c("\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n")
1409             for (fn_line, java_meth_descr) in java_meths:
1410                 if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
1411                     write_c("\tcalls->" + fn_line.group(3) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(3) + "\", \"" + java_meth_descr + "\");\n")
1412                     write_c("\tCHECK(calls->" + fn_line.group(3) + "_meth != NULL);\n")
1413             for idx, var_line in enumerate(field_var_lines):
1414                 if field_var_convs[idx] is not None and field_var_convs[idx].arg_conv is not None:
1415                     write_c("\n\t" + field_var_convs[idx].arg_conv.replace("\n", "\n\t") +"\n")
1416             write_c("\n\t" + struct_name + " ret = {\n")
1417             write_c("\t\t.this_arg = (void*) calls,\n")
1418             for fn_line in trait_fn_lines:
1419                 if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
1420                     write_c("\t\t." + fn_line.group(3) + " = " + fn_line.group(3) + "_jcall,\n")
1421                 elif fn_line.group(3) == "free":
1422                     write_c("\t\t.free = " + struct_name + "_JCalls_free,\n")
1423                 else:
1424                     write_c("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
1425             for idx, var_line in enumerate(field_var_lines):
1426                 if var_line.group(1) in trait_structs:
1427                     write_c("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, _a, " + var_line.group(2) + "),\n")
1428                 elif field_var_convs[idx].arg_conv_name is not None:
1429                     write_c("\t\t." + var_line.group(2) + " = " + field_var_convs[idx].arg_conv_name + ",\n")
1430                     write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
1431                 else:
1432                     write_c("\t\t." + var_line.group(2) + " = " + var_line.group(2) + ",\n")
1433                     write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
1434             write_c("\t};\n")
1435             for var_line in field_var_lines:
1436                 if var_line.group(1) in trait_structs:
1437                     write_c("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
1438             write_c("\treturn ret;\n")
1439             write_c("}\n")
1440
1441             write_c("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, jobject o")
1442             for idx, var_line in enumerate(field_var_lines):
1443                 if var_line.group(1) in trait_structs:
1444                     write_c(", jobject " + var_line.group(2))
1445                 else:
1446                     write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
1447             write_c(") {\n")
1448             write_c("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1449             write_c("\t*res_ptr = " + struct_name + "_init(env, _a, o")
1450             for var_line in field_var_lines:
1451                 write_c(", " + var_line.group(2))
1452             write_c(");\n")
1453             write_c("\treturn (long)res_ptr;\n")
1454             write_c("}\n")
1455
1456             out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
1457             write_c("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (JNIEnv * env, jclass _a, jlong val) {\n")
1458             write_c("\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n")
1459             write_c("\tCHECK(ret != NULL);\n")
1460             write_c("\treturn ret;\n")
1461             write_c("}\n")
1462
1463         for fn_line in trait_fn_lines:
1464             # For now, just disable enabling the _call_log - we don't know how to inverse-map String
1465             is_log = fn_line.group(3) == "log" and struct_name == "LDKLogger"
1466             if fn_line.group(3) != "free" and fn_line.group(3) != "clone" and fn_line.group(3) != "eq" and not is_log:
1467                 dummy_line = fn_line.group(2) + struct_name.replace("LDK", "") + "_" + fn_line.group(3) + " " + struct_name + "* this_arg" + fn_line.group(5) + "\n"
1468                 map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, "(this_arg_conv->" + fn_line.group(3) + ")(this_arg_conv->this_arg")
1469         for idx, var_line in enumerate(field_var_lines):
1470             if var_line.group(1) not in trait_structs:
1471                 write_c(var_line.group(1) + " " + struct_name + "_set_get_" + var_line.group(2) + "(" + struct_name + "* this_arg) {\n")
1472                 write_c("\tif (this_arg->set_" + var_line.group(2) + " != NULL)\n")
1473                 write_c("\t\tthis_arg->set_" + var_line.group(2) + "(this_arg);\n")
1474                 write_c("\treturn this_arg->" + var_line.group(2) + ";\n")
1475                 write_c("}\n")
1476                 dummy_line = var_line.group(1) + " " + struct_name.replace("LDK", "") + "_get_" + var_line.group(2) + " " + struct_name + "* this_arg" + fn_line.group(5) + "\n"
1477                 map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, struct_name + "_set_get_" + var_line.group(2) + "(this_arg_conv")
1478
1479     def map_result(struct_name, res_ty, err_ty):
1480         result_types.add(struct_name)
1481         human_ty = struct_name.replace("LDKCResult", "Result")
1482         with open(sys.argv[3] + "/structs/" + human_ty + ".java", "w") as out_java_struct:
1483             out_java_struct.write(hu_struct_file_prefix)
1484             out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
1485             out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
1486             out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
1487             out_java_struct.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
1488             out_java_struct.write("\t}\n\n")
1489             out_java_struct.write("\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n")
1490             out_java_struct.write("\t\tif (bindings." + struct_name + "_result_ok(ptr)) {\n")
1491             out_java_struct.write("\t\t\treturn new " + human_ty + "_OK(null, ptr);\n")
1492             out_java_struct.write("\t\t} else {\n")
1493             out_java_struct.write("\t\t\treturn new " + human_ty + "_Err(null, ptr);\n")
1494             out_java_struct.write("\t\t}\n")
1495             out_java_struct.write("\t}\n")
1496
1497             res_map = map_type(res_ty + " res", True, None, False, True)
1498             err_map = map_type(err_ty + " err", True, None, False, True)
1499
1500             out_java.write("\tpublic static native boolean " + struct_name + "_result_ok(long arg);\n")
1501             write_c("JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1result_1ok (JNIEnv * env, jclass _a, jlong arg) {\n")
1502             write_c("\treturn ((" + struct_name + "*)arg)->result_ok;\n")
1503             write_c("}\n")
1504
1505             out_java.write("\tpublic static native " + res_map.java_ty + " " + struct_name + "_get_ok(long arg);\n")
1506             write_c("JNIEXPORT " + res_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1ok (JNIEnv * _env, jclass _a, jlong arg) {\n")
1507             write_c("\t" + struct_name + " *val = (" + struct_name + "*)arg;\n")
1508             write_c("\tCHECK(val->result_ok);\n\t")
1509             out_java_struct.write("\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n")
1510             if res_map.ret_conv is not None:
1511                 write_c(res_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.result)")
1512                 write_c(res_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + res_map.ret_conv_name)
1513             else:
1514                 write_c("return *val->contents.result")
1515             write_c(";\n}\n")
1516
1517             if res_map.java_hu_ty != "void":
1518                 out_java_struct.write("\t\tpublic final " + res_map.java_hu_ty + " res;\n")
1519             out_java_struct.write("\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n")
1520             out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
1521             if res_map.java_hu_ty == "void":
1522                 pass
1523             elif res_map.to_hu_conv is not None:
1524                 out_java_struct.write("\t\t\t" + res_map.java_ty + " res = bindings." + struct_name + "_get_ok(ptr);\n")
1525                 out_java_struct.write("\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t"))
1526                 out_java_struct.write("\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n")
1527             else:
1528                 out_java_struct.write("\t\t\tthis.res = bindings." + struct_name + "_get_ok(ptr);\n")
1529             out_java_struct.write("\t\t}\n")
1530             if struct_name.startswith("LDKCResult_None"):
1531                 out_java_struct.write("\t\tpublic " + human_ty + "_OK() {\n\t\t\tthis(null, bindings.C" + human_ty + "_ok());\n")
1532             else:
1533                 out_java_struct.write("\t\tpublic " + human_ty + "_OK(" + res_map.java_hu_ty + " res) {\n")
1534                 if res_map.from_hu_conv is not None:
1535                     out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n")
1536                     if res_map.from_hu_conv[1] != "":
1537                         out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1] + ";\n")
1538                 else:
1539                     out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n")
1540             out_java_struct.write("\t\t}\n\t}\n\n")
1541
1542             out_java.write("\tpublic static native " + err_map.java_ty + " " + struct_name + "_get_err(long arg);\n")
1543             write_c("JNIEXPORT " + err_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1err (JNIEnv * _env, jclass _a, jlong arg) {\n")
1544             write_c("\t" + struct_name + " *val = (" + struct_name + "*)arg;\n")
1545             write_c("\tCHECK(!val->result_ok);\n\t")
1546             out_java_struct.write("\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n")
1547             if err_map.ret_conv is not None:
1548                 write_c(err_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.err)")
1549                 write_c(err_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + err_map.ret_conv_name)
1550             else:
1551                 write_c("return *val->contents.err")
1552             write_c(";\n}\n")
1553
1554             if err_map.java_hu_ty != "void":
1555                 out_java_struct.write("\t\tpublic final " + err_map.java_hu_ty + " err;\n")
1556             out_java_struct.write("\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n")
1557             out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
1558             if err_map.java_hu_ty == "void":
1559                 pass
1560             elif err_map.to_hu_conv is not None:
1561                 out_java_struct.write("\t\t\t" + err_map.java_ty + " err = bindings." + struct_name + "_get_err(ptr);\n")
1562                 out_java_struct.write("\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t"))
1563                 out_java_struct.write("\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n")
1564             else:
1565                 out_java_struct.write("\t\t\tthis.err = bindings." + struct_name + "_get_err(ptr);\n")
1566             out_java_struct.write("\t\t}\n")
1567
1568             if struct_name.endswith("NoneZ"):
1569                 out_java_struct.write("\t\tpublic " + human_ty + "_Err() {\n\t\t\tthis(null, bindings.C" + human_ty + "_err());\n")
1570             else:
1571                 out_java_struct.write("\t\tpublic " + human_ty + "_Err(" + err_map.java_hu_ty + " err) {\n")
1572                 if err_map.from_hu_conv is not None:
1573                     out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n")
1574                     if err_map.from_hu_conv[1] != "":
1575                         out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1] + ";\n")
1576                 else:
1577                     out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n")
1578             out_java_struct.write("\t\t}\n\t}\n}\n")
1579
1580     def map_tuple(struct_name, field_lines):
1581         out_java.write("\tpublic static native long " + struct_name + "_new(")
1582         write_c("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new(JNIEnv *_env, jclass _b")
1583         ty_list = []
1584         for idx, line in enumerate(field_lines):
1585             if idx != 0 and idx < len(field_lines) - 2:
1586                 ty_info = java_c_types(line.strip(';'), None)
1587                 if idx != 1:
1588                     out_java.write(", ")
1589                 e = chr(ord('a') + idx - 1)
1590                 out_java.write(ty_info.java_ty + " " + e)
1591                 write_c(", " + ty_info.c_ty + " " + e)
1592                 ty_list.append(ty_info)
1593         tuple_types[struct_name] = (ty_list, struct_name)
1594         out_java.write(");\n")
1595         write_c(") {\n")
1596         write_c("\t" + struct_name + "* ret = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1597         for idx, line in enumerate(field_lines):
1598             if idx != 0 and idx < len(field_lines) - 2:
1599                 ty_info = map_type(line.strip(';'), False, None, False, False)
1600                 e = chr(ord('a') + idx - 1)
1601                 if ty_info.arg_conv is not None:
1602                     write_c("\t" + ty_info.arg_conv.replace("\n", "\n\t"))
1603                     write_c("\n\tret->" + e + " = " + ty_info.arg_conv_name + ";\n")
1604                 else:
1605                     write_c("\tret->" + e + " = " + e + ";\n")
1606                 if ty_info.arg_conv_cleanup is not None:
1607                     write_c("\t//TODO: Really need to call " + ty_info.arg_conv_cleanup + " here\n")
1608         write_c("\treturn (long)ret;\n")
1609         write_c("}\n")
1610
1611         for idx, ty_info in enumerate(ty_list):
1612             e = chr(ord('a') + idx)
1613             out_java.write("\tpublic static native " + ty_info.java_ty + " " + struct_name + "_get_" + e + "(long ptr);\n")
1614             write_c("JNIEXPORT " + ty_info.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1" + e + "(JNIEnv *_env, jclass _b, jlong ptr) {\n")
1615             write_c("\t" + struct_name + " *tuple = (" + struct_name + "*)ptr;\n")
1616             conv_info = map_type_with_info(ty_info, False, None, False, True)
1617             if conv_info.ret_conv is not None:
1618                 write_c("\t" + conv_info.ret_conv[0].replace("\n", "\n\t") + "tuple->" + e + conv_info.ret_conv[1].replace("\n", "\n\t") + "\n")
1619                 write_c("\treturn " + conv_info.ret_conv_name + ";\n")
1620             else:
1621                 write_c("\treturn tuple->" + e + ";\n")
1622             write_c("}\n")
1623
1624     out_java.write("""package org.ldk.impl;
1625 import org.ldk.enums.*;
1626
1627 public class bindings {
1628         public static class VecOrSliceDef {
1629                 public long dataptr;
1630                 public long datalen;
1631                 public long stride;
1632                 public VecOrSliceDef(long dataptr, long datalen, long stride) {
1633                         this.dataptr = dataptr; this.datalen = datalen; this.stride = stride;
1634                 }
1635         }
1636         static {
1637                 System.loadLibrary(\"lightningjni\");
1638                 init(java.lang.Enum.class, VecOrSliceDef.class);
1639                 init_class_cache();
1640         }
1641         static native void init(java.lang.Class c, java.lang.Class slicedef);
1642         static native void init_class_cache();
1643
1644         public static native boolean deref_bool(long ptr);
1645         public static native long deref_long(long ptr);
1646         public static native void free_heap_ptr(long ptr);
1647         public static native byte[] read_bytes(long ptr, long len);
1648         public static native byte[] get_u8_slice_bytes(long slice_ptr);
1649         public static native long bytes_to_u8_vec(byte[] bytes);
1650         public static native long new_txpointer_copy_data(byte[] txdata);
1651         public static native void txpointer_free(long ptr);
1652         public static native byte[] txpointer_get_buffer(long ptr);
1653         public static native long vec_slice_len(long vec);
1654         public static native long new_empty_slice_vec();
1655
1656 """)
1657     write_c("""
1658 static jmethodID ordinal_meth = NULL;
1659 static jmethodID slicedef_meth = NULL;
1660 static jclass slicedef_cls = NULL;
1661 JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class, jclass slicedef_class) {
1662         ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
1663         CHECK(ordinal_meth != NULL);
1664         slicedef_meth = (*env)->GetMethodID(env, slicedef_class, "<init>", "(JJJ)V");
1665         CHECK(slicedef_meth != NULL);
1666         slicedef_cls = (*env)->NewGlobalRef(env, slicedef_class);
1667         CHECK(slicedef_cls != NULL);
1668 }
1669
1670 JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_deref_1bool (JNIEnv * env, jclass _a, jlong ptr) {
1671         return *((bool*)ptr);
1672 }
1673 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_deref_1long (JNIEnv * env, jclass _a, jlong ptr) {
1674         return *((long*)ptr);
1675 }
1676 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_free_1heap_1ptr (JNIEnv * env, jclass _a, jlong ptr) {
1677         FREE((void*)ptr);
1678 }
1679 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_read_1bytes (JNIEnv * _env, jclass _b, jlong ptr, jlong len) {
1680         jbyteArray ret_arr = (*_env)->NewByteArray(_env, len);
1681         (*_env)->SetByteArrayRegion(_env, ret_arr, 0, len, (unsigned char*)ptr);
1682         return ret_arr;
1683 }
1684 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (JNIEnv * _env, jclass _b, jlong slice_ptr) {
1685         LDKu8slice *slice = (LDKu8slice*)slice_ptr;
1686         jbyteArray ret_arr = (*_env)->NewByteArray(_env, slice->datalen);
1687         (*_env)->SetByteArrayRegion(_env, ret_arr, 0, slice->datalen, slice->data);
1688         return ret_arr;
1689 }
1690 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * _env, jclass _b, jbyteArray bytes) {
1691         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
1692         vec->datalen = (*_env)->GetArrayLength(_env, bytes);
1693         vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
1694         (*_env)->GetByteArrayRegion (_env, bytes, 0, vec->datalen, vec->data);
1695         return (long)vec;
1696 }
1697 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_txpointer_1get_1buffer (JNIEnv * env, jclass _b, jlong ptr) {
1698         LDKTransaction *txdata = (LDKTransaction*)ptr;
1699         LDKu8slice slice;
1700         slice.data = txdata->data;
1701         slice.datalen = txdata->datalen;
1702         return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (long)&slice);
1703 }
1704 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
1705         LDKTransaction *txdata = (LDKTransaction*)MALLOC(sizeof(LDKTransaction), "LDKTransaction");
1706         txdata->datalen = (*env)->GetArrayLength(env, bytes);
1707         txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
1708         txdata->data_is_owned = false;
1709         (*env)->GetByteArrayRegion (env, bytes, 0, txdata->datalen, txdata->data);
1710         return (long)txdata;
1711 }
1712 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_txpointer_1free (JNIEnv * env, jclass _b, jlong ptr) {
1713         LDKTransaction *tx = (LDKTransaction*)ptr;
1714         tx->data_is_owned = true;
1715         Transaction_free(*tx);
1716         FREE((void*)ptr);
1717 }
1718 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) {
1719         // Check offsets of a few Vec types are all consistent as we're meant to be generic across types
1720         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically");
1721         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically");
1722         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
1723         _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
1724         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
1725         return (long)vec->datalen;
1726 }
1727 JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * _env, jclass _b) {
1728         // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
1729         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
1730         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
1731         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically");
1732         _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically");
1733         LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
1734         vec->data = NULL;
1735         vec->datalen = 0;
1736         return (long)vec;
1737 }
1738
1739 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
1740 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
1741 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
1742 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
1743
1744 """)
1745
1746     with open(sys.argv[3] + "/structs/CommonBase.java", "a") as out_java_struct:
1747         out_java_struct.write("""package org.ldk.structs;
1748 import java.util.LinkedList;
1749 class CommonBase {
1750         long ptr;
1751         LinkedList<Object> ptrs_to = new LinkedList();
1752         protected CommonBase(long ptr) { this.ptr = ptr; }
1753         public long _test_only_get_ptr() { return this.ptr; }
1754 }
1755 """)
1756
1757     in_block_comment = False
1758     cur_block_obj = None
1759
1760     const_val_regex = re.compile("^extern const ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
1761
1762     line_indicates_result_regex = re.compile("^   union (LDKCResult_[A-Za-z_0-9]*Ptr) contents;$")
1763     line_indicates_vec_regex = re.compile("^   (struct |enum |union )?([A-Za-z_0-9]*) \*data;$")
1764     line_indicates_opaque_regex = re.compile("^   bool is_owned;$")
1765     line_indicates_trait_regex = re.compile("^   (struct |enum |union )?([A-Za-z_0-9]* \*?)\(\*([A-Za-z_0-9]*)\)\((const )?void \*this_arg(.*)\);$")
1766     assert(line_indicates_trait_regex.match("   uintptr_t (*send_data)(void *this_arg, LDKu8slice data, bool resume_read);"))
1767     assert(line_indicates_trait_regex.match("   struct LDKCVec_MessageSendEventZ (*get_and_clear_pending_msg_events)(const void *this_arg);"))
1768     assert(line_indicates_trait_regex.match("   void *(*clone)(const void *this_arg);"))
1769     assert(line_indicates_trait_regex.match("   struct LDKCVec_u8Z (*write)(const void *this_arg);"))
1770     line_field_var_regex = re.compile("^   struct ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
1771     assert(line_field_var_regex.match("   struct LDKMessageSendEventsProvider MessageSendEventsProvider;"))
1772     assert(line_field_var_regex.match("   struct LDKChannelPublicKeys pubkeys;"))
1773     struct_name_regex = re.compile("^typedef (struct|enum|union) (MUST_USE_STRUCT )?(LDK[A-Za-z_0-9]*) {$")
1774     assert(struct_name_regex.match("typedef struct LDKCVec_u8Z {"))
1775     assert(struct_name_regex.match("typedef enum LDKNetwork {"))
1776
1777     union_enum_items = {}
1778     result_ptr_struct_items = {}
1779     for line in in_h:
1780         if in_block_comment:
1781             if line.endswith("*/\n"):
1782                 in_block_comment = False
1783         elif cur_block_obj is not None:
1784             cur_block_obj  = cur_block_obj + line
1785             if line.startswith("} "):
1786                 field_lines = []
1787                 struct_name = None
1788                 vec_ty = None
1789                 obj_lines = cur_block_obj.split("\n")
1790                 is_opaque = False
1791                 result_contents = None
1792                 is_unitary_enum = False
1793                 is_union_enum = False
1794                 is_union = False
1795                 is_tuple = False
1796                 trait_fn_lines = []
1797                 field_var_lines = []
1798
1799                 for idx, struct_line in enumerate(obj_lines):
1800                     if struct_line.strip().startswith("/*"):
1801                         in_block_comment = True
1802                     if in_block_comment:
1803                         if struct_line.endswith("*/"):
1804                             in_block_comment = False
1805                     else:
1806                         struct_name_match = struct_name_regex.match(struct_line)
1807                         if struct_name_match is not None:
1808                             struct_name = struct_name_match.group(3)
1809                             if struct_name_match.group(1) == "enum":
1810                                 if not struct_name.endswith("_Tag"):
1811                                     is_unitary_enum = True
1812                                 else:
1813                                     is_union_enum = True
1814                             elif struct_name_match.group(1) == "union":
1815                                 is_union = True
1816                         if line_indicates_opaque_regex.match(struct_line):
1817                             is_opaque = True
1818                         result_match = line_indicates_result_regex.match(struct_line)
1819                         if result_match is not None:
1820                             result_contents = result_match.group(1)
1821                         vec_ty_match = line_indicates_vec_regex.match(struct_line)
1822                         if vec_ty_match is not None and struct_name.startswith("LDKCVec_"):
1823                             vec_ty = vec_ty_match.group(2)
1824                         elif struct_name.startswith("LDKC2Tuple_") or struct_name.startswith("LDKC3Tuple_"):
1825                             is_tuple = True
1826                         trait_fn_match = line_indicates_trait_regex.match(struct_line)
1827                         if trait_fn_match is not None:
1828                             trait_fn_lines.append(trait_fn_match)
1829                         field_var_match = line_field_var_regex.match(struct_line)
1830                         if field_var_match is not None:
1831                             field_var_lines.append(field_var_match)
1832                         field_lines.append(struct_line)
1833
1834                 assert(struct_name is not None)
1835                 assert(len(trait_fn_lines) == 0 or not (is_opaque or is_unitary_enum or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1836                 assert(not is_opaque or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1837                 assert(not is_unitary_enum or not (len(trait_fn_lines) != 0 or is_opaque or is_union_enum or is_union or result_contents is not None or vec_ty is not None))
1838                 assert(not is_union_enum or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_opaque or is_union or result_contents is not None or vec_ty is not None))
1839                 assert(not is_union or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or result_contents is not None or vec_ty is not None))
1840                 assert(result_contents is None or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or is_union or vec_ty is not None))
1841                 assert(vec_ty is None or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque or is_union or result_contents is not None))
1842
1843                 if is_opaque:
1844                     opaque_structs.add(struct_name)
1845                     with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_struct:
1846                         out_java_struct.write(hu_struct_file_prefix)
1847                         out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase")
1848                         if struct_name.startswith("LDKLocked"):
1849                             out_java_struct.write(" implements AutoCloseable")
1850                         out_java_struct.write(" {\n")
1851                         out_java_struct.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); }\n")
1852                         if struct_name.startswith("LDKLocked"):
1853                             out_java_struct.write("\t@Override public void close() {\n")
1854                         else:
1855                             out_java_struct.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
1856                             out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
1857                             out_java_struct.write("\t\tsuper.finalize();\n")
1858                         out_java_struct.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n")
1859                         out_java_struct.write("\t}\n\n")
1860                 elif result_contents is not None:
1861                     assert result_contents in result_ptr_struct_items
1862                     res_ty, err_ty = result_ptr_struct_items[result_contents]
1863                     map_result(struct_name, res_ty, err_ty)
1864                 elif struct_name.startswith("LDKCResult_") and struct_name.endswith("ZPtr"):
1865                     for line in field_lines:
1866                         if line.endswith("*result;"):
1867                             res_ty = line[:-8].strip()
1868                         elif line.endswith("*err;"):
1869                             err_ty = line[:-5].strip()
1870                     result_ptr_struct_items[struct_name] = (res_ty, err_ty)
1871                     result_types.add(struct_name[:-3])
1872                 elif is_tuple:
1873                     map_tuple(struct_name, field_lines)
1874                 elif vec_ty is not None:
1875                     if vec_ty in opaque_structs:
1876                         out_java.write("\tpublic static native long[] " + struct_name + "_arr_info(long vec_ptr);\n")
1877                         write_c("JNIEXPORT jlongArray JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1arr_1info(JNIEnv *env, jclass _b, jlong ptr) {\n")
1878                     else:
1879                         out_java.write("\tpublic static native VecOrSliceDef " + struct_name + "_arr_info(long vec_ptr);\n")
1880                         write_c("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1arr_1info(JNIEnv *env, jclass _b, jlong ptr) {\n")
1881                     write_c("\t" + struct_name + " *vec = (" + struct_name + "*)ptr;\n")
1882                     if vec_ty in opaque_structs:
1883                         write_c("\tjlongArray ret = (*env)->NewLongArray(env, vec->datalen);\n")
1884                         write_c("\tjlong *ret_elems = (*env)->GetPrimitiveArrayCritical(env, ret, NULL);\n")
1885                         write_c("\tfor (size_t i = 0; i < vec->datalen; i++) {\n")
1886                         write_c("\t\tCHECK((((long)vec->data[i].inner) & 1) == 0);\n")
1887                         write_c("\t\tret_elems[i] = (long)vec->data[i].inner | (vec->data[i].is_owned ? 1 : 0);\n")
1888                         write_c("\t}\n")
1889                         write_c("\t(*env)->ReleasePrimitiveArrayCritical(env, ret, ret_elems, 0);\n")
1890                         write_c("\treturn ret;\n")
1891                     else:
1892                         write_c("\treturn (*env)->NewObject(env, slicedef_cls, slicedef_meth, (long)vec->data, (long)vec->datalen, sizeof(" + vec_ty + "));\n")
1893                     write_c("}\n")
1894
1895                     ty_info = map_type(vec_ty + " arr_elem", False, None, False, False)
1896                     if len(ty_info.java_fn_ty_arg) == 1: # ie we're a primitive of some form
1897                         out_java.write("\tpublic static native long " + struct_name + "_new(" + ty_info.java_ty + "[] elems);\n")
1898                         write_c("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new(JNIEnv *env, jclass _b, j" + ty_info.java_ty + "Array elems){\n")
1899                         write_c("\t" + struct_name + " *ret = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
1900                         write_c("\tret->datalen = (*env)->GetArrayLength(env, elems);\n")
1901                         write_c("\tif (ret->datalen == 0) {\n")
1902                         write_c("\t\tret->data = NULL;\n")
1903                         write_c("\t} else {\n")
1904                         write_c("\t\tret->data = MALLOC(sizeof(" + vec_ty + ") * ret->datalen, \"" + struct_name + " Data\");\n")
1905                         write_c("\t\t" + ty_info.c_ty + " *java_elems = (*env)->GetPrimitiveArrayCritical(env, elems, NULL);\n")
1906                         write_c("\t\tfor (size_t i = 0; i < ret->datalen; i++) {\n")
1907                         if ty_info.arg_conv is not None:
1908                             write_c("\t\t\t" + ty_info.c_ty + " arr_elem = java_elems[i];\n")
1909                             write_c("\t\t\t" + ty_info.arg_conv.replace("\n", "\n\t\t\t") + "\n")
1910                             write_c("\t\t\tret->data[i] = " + ty_info.arg_conv_name + ";\n")
1911                             assert ty_info.arg_conv_cleanup is None
1912                         else:
1913                             write_c("\t\t\tret->data[i] = java_elems[i];\n")
1914                         write_c("\t\t}\n")
1915                         write_c("\t\t(*env)->ReleasePrimitiveArrayCritical(env, elems, java_elems, 0);\n")
1916                         write_c("\t}\n")
1917                         write_c("\treturn (long)ret;\n")
1918                         write_c("}\n")
1919                 elif is_union_enum:
1920                     assert(struct_name.endswith("_Tag"))
1921                     struct_name = struct_name[:-4]
1922                     union_enum_items[struct_name] = {"field_lines": field_lines}
1923                 elif struct_name.endswith("_Body") and struct_name.split("_")[0] in union_enum_items:
1924                     enum_var_name = struct_name.split("_")
1925                     union_enum_items[enum_var_name[0]][enum_var_name[1]] = field_lines
1926                 elif struct_name in union_enum_items:
1927                     map_complex_enum(struct_name, union_enum_items[struct_name])
1928                 elif is_unitary_enum:
1929                     map_unitary_enum(struct_name, field_lines)
1930                 elif len(trait_fn_lines) > 0:
1931                     trait_structs.add(struct_name)
1932                     map_trait(struct_name, field_var_lines, trait_fn_lines)
1933                 elif struct_name == "LDKTxOut":
1934                     with open(sys.argv[3] + "/structs/TxOut.java", "w") as out_java_struct:
1935                         out_java_struct.write(hu_struct_file_prefix)
1936                         out_java_struct.write("public class TxOut extends CommonBase{\n")
1937                         out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n")
1938                         out_java_struct.write("\tlong to_c_ptr() { return 0; }\n")
1939                         # TODO: TxOut body
1940                         out_java_struct.write("}")
1941                 else:
1942                     pass # Everything remaining is a byte[] or some form
1943                 cur_block_obj = None
1944         else:
1945             fn_ptr = fn_ptr_regex.match(line)
1946             fn_ret_arr = fn_ret_arr_regex.match(line)
1947             reg_fn = reg_fn_regex.match(line)
1948             const_val = const_val_regex.match(line)
1949
1950             if line.startswith("#include <"):
1951                 pass
1952             elif line.startswith("/*"):
1953                 #out_java.write("\t" + line)
1954                 if not line.endswith("*/\n"):
1955                     in_block_comment = True
1956             elif line.startswith("typedef enum "):
1957                 cur_block_obj = line
1958             elif line.startswith("typedef struct "):
1959                 cur_block_obj = line
1960             elif line.startswith("typedef union "):
1961                 cur_block_obj = line
1962             elif fn_ptr is not None:
1963                 map_fn(line, fn_ptr, None, None)
1964             elif fn_ret_arr is not None:
1965                 map_fn(line, fn_ret_arr, fn_ret_arr.group(4), None)
1966             elif reg_fn is not None:
1967                 map_fn(line, reg_fn, None, None)
1968             elif const_val_regex is not None:
1969                 # TODO Map const variables
1970                 pass
1971             else:
1972                 assert(line == "\n")
1973
1974     out_java.write("}\n")
1975     for struct_name in opaque_structs:
1976         with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "a") as out_java_struct:
1977             out_java_struct.write("}\n")
1978     for struct_name in trait_structs:
1979         with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "a") as out_java_struct:
1980             out_java_struct.write("}\n")
1981 with open(sys.argv[4], "w") as out_c:
1982     out_c.write(c_file_pfx)
1983     for ty in c_array_class_caches:
1984         if ty + "_clz" in c_file:
1985             out_c.write("static jclass " + ty + "_clz = NULL;\n")
1986     out_c.write("JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass _b) {\n")
1987     for ty in c_array_class_caches:
1988         if ty + "_clz" in c_file:
1989             out_c.write("\t" + ty + "_clz = (*env)->FindClass(env, \"" + ty.replace("arr_of_", "[") + "\");\n")
1990             out_c.write("\tCHECK(" + ty + "_clz != NULL);\n")
1991             out_c.write("\t" + ty + "_clz = (*env)->NewGlobalRef(env, " + ty + "_clz);\n")
1992     out_c.write("}\n")
1993     out_c.write(c_file)