[C#] Add initial hooks, the C# code compiles, the C not at all
[ldk-java] / csharp_strings.py
1 from bindingstypes import *
2 from enum import Enum
3 import sys
4
5 class Target(Enum):
6     CSHARP = 1,
7
8 class Consts:
9     def __init__(self, DEBUG: bool, target: Target, **kwargs):
10         self.target = target
11         self.c_array_class_caches = set()
12         self.c_type_map = dict(
13             bool = ['bool'],
14             uint8_t = ['byte'],
15             uint16_t = ['short'],
16             uint32_t = ['int'],
17             uint64_t = ['long'],
18         )
19         self.java_type_map = dict(
20             String = "string"
21         )
22         self.java_hu_type_map = dict(
23             String = "string"
24         )
25
26         self.to_hu_conv_templates = dict(
27             ptr = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }',
28             default = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }'
29         )
30
31         self.bindings_header = """
32 using org.ldk.enums;
33 using org.ldk.impl;
34 using System.Runtime.InteropServices;
35
36 namespace org { namespace ldk { namespace impl {
37
38 internal class bindings {
39         /*static {
40                 init(java.lang.Enum.class, VecOrSliceDef.class);
41                 init_class_cache();
42                 if (!get_lib_version_string().equals(version.get_ldk_java_bindings_version()))
43                         throw new ArgumentException("Compiled LDK library and LDK class failes do not match");
44                 // Fetching the LDK versions from C also checks that the header and binaries match
45                 Console.Error.WriteLine("Loaded LDK-Java Bindings " + version.get_ldk_java_bindings_version() + " with LDK " + get_ldk_version() + " and LDK-C-Bindings " + get_ldk_c_bindings_version());
46         }*/
47         //static extern void init(java.lang.Class c);
48         //static native void init_class_cache();
49 """
50         self.bindings_header += self.native_meth_decl("get_lib_version_string", "string") + "();\n"
51         self.bindings_header += self.native_meth_decl("get_ldk_c_bindings_version", "string") + "();\n"
52         self.bindings_header += self.native_meth_decl("get_ldk_version", "string") + "();\n\n"
53
54         self.bindings_version_file = """
55
56 public class version {
57         public static string get_ldk_java_bindings_version() {
58                 return "<git_version_ldk_garbagecollected>";
59         }
60 }"""
61
62         self.util_fn_pfx = """using org.ldk.impl;
63 using org.ldk.enums;
64 using org.ldk.util;
65 using org.ldk.structs;
66 using System;
67
68 namespace org { namespace ldk { namespace util {
69 public class UtilMethods {
70 """
71         self.util_fn_sfx = "} } } }"
72         self.common_base = """using System.Collections.Generic;
73 using System.Diagnostics;
74
75 public class CommonBase {
76         protected internal long ptr;
77         protected internal LinkedList<object> ptrs_to = new LinkedList<object>();
78         protected CommonBase(long ptr) { Trace.Assert(ptr < 0 || ptr > 4096); this.ptr = ptr; }
79 }
80 """
81
82         self.txout_defn = """public class TxOut : CommonBase {
83         /** The script_pubkey in this output */
84         public readonly byte[] script_pubkey;
85         /** The value, in satoshis, of this output */
86         public readonly long value;
87
88     internal TxOut(object _dummy, long ptr) : base(ptr) {
89                 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
90                 this.value = bindings.TxOut_get_value(ptr);
91         }
92     public TxOut(long value, byte[] script_pubkey) : base(bindings.TxOut_new(script_pubkey, value)) {
93                 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
94                 this.value = bindings.TxOut_get_value(ptr);
95         }
96
97         ~TxOut() {
98                 if (ptr != 0) { bindings.TxOut_free(ptr); }
99         }
100 }"""
101
102         self.scalar_defn = """public class BigEndianScalar : CommonBase {
103         /** The bytes of the scalar value, in big endian */
104         public readonly byte[] scalar_bytes;
105
106     internal BigEndianScalar(object _dummy, long ptr) : base(ptr) {
107                 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
108         }
109     public BigEndianScalar(byte[] scalar_bytes) : base(bindings.BigEndianScalar_new(scalar_bytes)) {
110                 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
111         }
112
113         ~BigEndianScalar() {
114                 if (ptr != 0) { bindings.BigEndianScalar_free(ptr); }
115         }
116 }"""
117
118
119         self.c_file_pfx = """
120 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
121 #define int64_t jlong
122 #include <lightning.h>
123 #include <string.h>
124 #include <stdatomic.h>
125 #include <stdlib.h>
126
127 #define LIKELY(v) __builtin_expect(!!(v), 1)
128 #define UNLIKELY(v) __builtin_expect(!!(v), 0)
129
130 """
131
132         self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
133
134         if not DEBUG or sys.platform == "darwin":
135             self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
136 #define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
137 #define CHECK_ACCESS(p)
138 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
139 """
140         if not DEBUG:
141             self.c_file_pfx += """#define DO_ASSERT(a) (void)(a)
142 #define CHECK(a)
143 """
144         if DEBUG:
145             self.c_file_pfx = self.c_file_pfx + """#include <assert.h>
146 // Always run a, then assert it is true:
147 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
148 // Assert a is true or do nothing
149 #define CHECK(a) DO_ASSERT(a)
150
151 void __attribute__((constructor)) debug_log_version() {
152         if (check_get_ldk_version() == NULL)
153                 DEBUG_PRINT("LDK version did not match the header we built against\\n");
154         if (check_get_ldk_bindings_version() == NULL)
155                 DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
156 }
157 """
158
159             if sys.platform != "darwin":
160                 self.c_file_pfx += """
161 // Running a leak check across all the allocations and frees of the JDK is a mess,
162 // so instead we implement our own naive leak checker here, relying on the -wrap
163 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
164 // and free'd in Rust or C across the generated bindings shared library.
165 #include <threads.h>
166 """
167
168                 self.c_file_pfx = self.c_file_pfx + "#include <execinfo.h>\n"
169                 self.c_file_pfx = self.c_file_pfx + """
170 #include <unistd.h>
171 #include <pthread.h>
172 static pthread_mutex_t allocation_mtx;
173
174 void __attribute__((constructor)) init_mtx() {
175         DO_ASSERT(!pthread_mutex_init(&allocation_mtx, NULL));
176 }
177
178 #define BT_MAX 128
179 typedef struct allocation {
180         struct allocation* next;
181         void* ptr;
182         const char* struct_name;
183         void* bt[BT_MAX];
184         int bt_len;
185         unsigned long alloc_len;
186 } allocation;
187 static allocation* allocation_ll = NULL;
188
189 void* __real_malloc(size_t len);
190 void* __real_calloc(size_t nmemb, size_t len);
191 static void new_allocation(void* res, const char* struct_name, size_t len) {
192         allocation* new_alloc = __real_malloc(sizeof(allocation));
193         new_alloc->ptr = res;
194         new_alloc->struct_name = struct_name;
195         new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
196         new_alloc->alloc_len = len;
197         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
198         new_alloc->next = allocation_ll;
199         allocation_ll = new_alloc;
200         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
201 }
202 static void* MALLOC(size_t len, const char* struct_name) {
203         void* res = __real_malloc(len);
204         new_allocation(res, struct_name, len);
205         return res;
206 }
207 void __real_free(void* ptr);
208 static void alloc_freed(void* ptr) {
209         allocation* p = NULL;
210         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
211         allocation* it = allocation_ll;
212         while (it->ptr != ptr) {
213                 p = it; it = it->next;
214                 if (it == NULL) {
215                         DEBUG_PRINT("ERROR: Tried to free unknown pointer %p at:\\n", ptr);
216                         void* bt[BT_MAX];
217                         int bt_len = backtrace(bt, BT_MAX);
218                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
219                         DEBUG_PRINT("\\n\\n");
220                         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
221                         return; // addrsan should catch malloc-unknown and print more info than we have
222                 }
223         }
224         if (p) { p->next = it->next; } else { allocation_ll = it->next; }
225         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
226         DO_ASSERT(it->ptr == ptr);
227         __real_free(it);
228 }
229 static void FREE(void* ptr) {
230         if ((uint64_t)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys
231         alloc_freed(ptr);
232         __real_free(ptr);
233 }
234
235 void* __wrap_malloc(size_t len) {
236         void* res = __real_malloc(len);
237         new_allocation(res, "malloc call", len);
238         return res;
239 }
240 void* __wrap_calloc(size_t nmemb, size_t len) {
241         void* res = __real_calloc(nmemb, len);
242         new_allocation(res, "calloc call", len);
243         return res;
244 }
245 void __wrap_free(void* ptr) {
246         if (ptr == NULL) return;
247         alloc_freed(ptr);
248         __real_free(ptr);
249 }
250
251 static void CHECK_ACCESS(const void* ptr) {
252         DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
253         allocation* it = allocation_ll;
254         while (it->ptr != ptr) {
255                 it = it->next;
256                 if (it == NULL) {
257                         DEBUG_PRINT("ERROR: Tried to access unknown pointer %p at:\\n", ptr);
258                         void* bt[BT_MAX];
259                         int bt_len = backtrace(bt, BT_MAX);
260                         backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
261                         DEBUG_PRINT("\\n\\n");
262                         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
263                         return; // addrsan should catch and print more info than we have
264                 }
265         }
266         DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
267 }
268 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
269         if (v.is_owned && v.inner != NULL) { \\
270                 const void *p = __unmangle_inner_ptr(v.inner); \\
271                 if (p != NULL) { \\
272                         CHECK_ACCESS(p); \\
273                 } \\
274         }
275
276 void* __real_realloc(void* ptr, size_t newlen);
277 void* __wrap_realloc(void* ptr, size_t len) {
278         if (ptr != NULL) alloc_freed(ptr);
279         void* res = __real_realloc(ptr, len);
280         new_allocation(res, "realloc call", len);
281         return res;
282 }
283 void __wrap_reallocarray(void* ptr, size_t new_sz) {
284         // Rust doesn't seem to use reallocarray currently
285         DO_ASSERT(false);
286 }
287
288 void __attribute__((destructor)) check_leaks() {
289         unsigned long alloc_count = 0;
290         unsigned long alloc_size = 0;
291         DEBUG_PRINT("The following LDK-allocated blocks still remain.\\n");
292         DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
293         DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
294         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
295                 DEBUG_PRINT("%s %p (%lu bytes) remains:\\n", a->struct_name, a->ptr, a->alloc_len);
296                 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
297                 DEBUG_PRINT("\\n\\n");
298                 alloc_count++;
299                 alloc_size += a->alloc_len;
300         }
301         DEBUG_PRINT("%lu allocations remained for %lu bytes.\\n", alloc_count, alloc_size);
302         DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
303         DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
304 }
305 """
306         self.c_file_pfx = self.c_file_pfx + """
307
308 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
309 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
310 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
311 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
312
313 _Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
314
315 typedef jlongArray int64_tArray;
316 typedef jbyteArray int8_tArray;
317
318 static inline jstring str_ref_to_java(JNIEnv *env, const char* chars, size_t len) {
319         // Sadly we need to create a temporary because Java can't accept a char* without a 0-terminator
320         char* conv_buf = MALLOC(len + 1, "str conv buf");
321         memcpy(conv_buf, chars, len);
322         conv_buf[len] = 0;
323         jstring ret = (*env)->NewStringUTF(env, conv_buf);
324         FREE(conv_buf);
325         return ret;
326 }
327 static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
328         uint64_t str_len = (*env)->GetStringUTFLength(env, str);
329         char* newchars = MALLOC(str_len + 1, "String chars");
330         const char* jchars = (*env)->GetStringUTFChars(env, str, NULL);
331         memcpy(newchars, jchars, str_len);
332         newchars[str_len] = 0;
333         (*env)->ReleaseStringUTFChars(env, str, jchars);
334         LDKStr res = {
335                 .chars = newchars,
336                 .len = str_len,
337                 .chars_is_owned = true
338         };
339         return res;
340 }
341
342 const char* CS_LDK_get_ldk_c_bindings_version() {
343         return str_ref_to_java(check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
344 }
345 const char* CS_LDK_get_ldk_version() {
346         return str_ref_to_java(check_get_ldk_version(), strlen(check_get_ldk_version()));
347 }
348 #include "version.c"
349 """
350         self.c_version_file = """const char* CS_LDK_get_lib_version_string() {
351         return "<git_version_ldk_garbagecollected>";
352 }"""
353
354         self.hu_struct_file_prefix = """using org.ldk.impl;
355 using org.ldk.enums;
356 using org.ldk.util;
357 using System;
358
359 namespace org { namespace ldk { namespace structs {
360
361 """
362         self.hu_struct_file_suffix = "} } }\n"
363         self.c_fn_args_pfx = ""
364         self.c_fn_ty_pfx = ""
365         self.file_ext = ".cs"
366         self.ptr_c_ty = "int64_t"
367         self.ptr_native_ty = "long"
368         self.u128_native_ty = "UInt128"
369         self.usize_c_ty = "int64_t"
370         self.usize_native_ty = "long"
371         self.native_zero_ptr = "0"
372         self.result_c_ty = "jclass"
373         self.ptr_arr = "jobjectArray"
374         self.is_arr_some_check = ("", " != NULL")
375         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
376
377         self.bindings_footer_wip = "\tstatic bindings() {\n"
378     def bindings_footer(self):
379         return self.bindings_footer_wip + "\t}\n}\n} } }\n"
380
381     def native_meth_decl(self, meth_name, ret_ty_str):
382         return "\t[DllImport (\"ldkcsharp\", EntryPoint=\"CS_LDK_" + meth_name + "\")] public static extern " + ret_ty_str + " " + meth_name
383
384     def c_fn_name_define_pfx(self, fn_name, have_args):
385         return " CS_LDK_" + fn_name + "("
386
387     def construct_jenv(self):
388         res =  "JNIEnv *env;\n"
389         res += "jint get_jenv_res = (*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_6);\n"
390         res += "if (get_jenv_res == JNI_EDETACHED) {\n"
391         res += "\tDO_ASSERT((*j_calls->vm)->AttachCurrentThread(j_calls->vm, (void**)&env, NULL) == JNI_OK);\n"
392         res += "} else {\n"
393         res += "\tDO_ASSERT(get_jenv_res == JNI_OK);\n"
394         res += "}\n"
395         return res
396     def deconstruct_jenv(self):
397         res = "if (get_jenv_res == JNI_EDETACHED) {\n"
398         res += "\tDO_ASSERT((*j_calls->vm)->DetachCurrentThread(j_calls->vm) == JNI_OK);\n"
399         res += "}\n"
400         return res
401
402     def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):
403         if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
404             return "(*env)->ReleasePrimitiveArrayCritical(env, " + arr_var + ", " + arr_ptr_var + ", 0)"
405         return None
406     def create_native_arr_call(self, arr_len, ty_info):
407         if ty_info.c_ty == "int8_tArray":
408             return "(*env)->NewByteArray(env, " + arr_len + ")"
409         elif ty_info.subty.c_ty.endswith("Array"):
410             clz_var = ty_info.java_fn_ty_arg[1:].replace("[", "arr_of_")
411             self.c_array_class_caches.add(clz_var)
412             return "(*env)->NewObjectArray(env, " + arr_len + ", " + clz_var + "_clz, NULL);\n"
413         else:
414             return "(*env)->New" + ty_info.java_ty.strip("[]").title() + "Array(env, " + arr_len + ")"
415     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
416         if ty_info.c_ty == "int8_tArray":
417             return ("(*env)->SetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
418         else:
419             assert False
420     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
421         if ty_info.c_ty == "int8_tArray":
422             if copy:
423                 return "(*env)->GetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", " + dest_name + ")"
424             else:
425                 return "(*env)->GetByteArrayElements (env, " + arr_name + ", NULL)"
426         elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
427             return "(*env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (env, " + arr_name + ", NULL)"
428         else:
429             return None
430     def get_native_arr_elem(self, arr_name, idxc, ty_info):
431         if self.get_native_arr_contents(arr_name, "", "", ty_info, False) is None:
432             return "(*env)->GetObjectArrayElement(env, " + arr_name + ", " + idxc + ")"
433         else:
434             assert False # Only called if above is None
435     def get_native_arr_ptr_call(self, ty_info):
436         if ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array"):
437             return None
438         return ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
439     def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
440         if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
441             return None
442         return "(*env)->SetObjectArrayElement(env, " + arr_name + ", " + idxc + ", " + entry_access + ")"
443     def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
444         if ty_info.c_ty == "int8_tArray":
445             return "(*env)->ReleaseByteArrayElements(env, " + arr_name + ", (int8_t*)" + dest_name + ", 0);"
446         else:
447             return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)"
448
449     def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
450         if elem_ty.java_hu_ty == "UInt5":
451             return arr_name + " != null ? InternalUtils.convUInt5Array(" + arr_name + ") : null"
452         elif elem_ty.java_hu_ty == "WitnessVersion":
453             return arr_name + " != null ? InternalUtils.convWitnessVersionArray(" + arr_name + ") : null"
454         else:
455             return arr_name + " != null ? InternalUtils.mapArray(" + arr_name + ", " + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null"
456
457     def str_ref_to_native_call(self, var_name, str_len):
458         return "str_ref_to_java(env, " + var_name + ", " + str_len + ")"
459     def str_ref_to_c_call(self, var_name):
460         return "java_to_owned_str(env, " + var_name + ")"
461     def str_to_hu_conv(self, var_name):
462         return None
463     def str_from_hu_conv(self, var_name):
464         return None
465
466     def init_str(self):
467         return ""
468
469     def var_decl_statement(self, ty_string, var_name, statement):
470         return ty_string + " " + var_name + " = " + statement
471
472     def get_java_arr_len(self, arr_name):
473         return arr_name + ".Length"
474     def get_java_arr_elem(self, elem_ty, arr_name, idx):
475         return arr_name + "[" + idx + "]"
476     def constr_hu_array(self, ty_info, arr_len):
477         base_ty = ty_info.subty.java_hu_ty.split("[")[0].split("<")[0]
478         conv = "new " + base_ty + "[" + arr_len + "]"
479         if "[" in ty_info.subty.java_hu_ty.split("<")[0]:
480             # Do a bit of a dance to move any excess [] to the end
481             conv += "[" + ty_info.subty.java_hu_ty.split("<")[0].split("[")[1]
482         return conv
483     def cleanup_converted_native_array(self, ty_info, arr_name):
484         return None
485
486     def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
487         mapped_ty = arr_ty.subty
488         if arr_ty.rust_obj == "LDKU128":
489             return ("" + arr_name + ".getLEBytes()", "")
490         if fixed_len is not None:
491             return ("InternalUtils.check_arr_len(" + arr_name + ", " + fixed_len + ")", "")
492         return None
493     def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
494         if arr_ty.rust_obj == "LDKU128":
495             return "org.ldk.util.UInt128 " + conv_name + " = new org.ldk.util.UInt128(" + arr_name + ");"
496         return None
497
498     def java_arr_ty_str(self, elem_ty_str):
499         return elem_ty_str + "[]"
500
501     def for_n_in_range(self, n, minimum, maximum):
502         return "for (int " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
503     def for_n_in_arr(self, n, arr_name, arr_elem_ty):
504         return ("foreach (" + arr_elem_ty.java_hu_ty + " " + n + " in " + arr_name + ") { ", " }")
505
506     def get_ptr(self, var):
507         return var + ".ptr"
508     def set_null_skip_free(self, var):
509         return var + ".ptr" + " = 0;"
510
511     def add_ref(self, holder, referent):
512         return "if (" + holder + " != null) { " + holder + ".ptrs_to.AddLast(" + referent + "); }"
513
514     def fully_qualified_hu_ty_path(self, ty):
515         if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
516             return ty.java_hu_ty
517         if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt128" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
518             return "org.ldk.util." + ty.java_hu_ty
519         if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
520             return "org.ldk.structs." + ty.java_hu_ty
521         return ty.java_hu_ty
522
523     def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
524         out_java_enum = "namespace org { namespace ldk { namespace enums {"
525         out_java = ""
526         out_c = ""
527
528         out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_cs(int32_t ord) {\n"
529         out_c += "\tswitch (ord) {\n"
530
531         if enum_doc_comment is not None:
532             out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
533         out_java_enum += "public enum " + struct_name + " {\n"
534         ord_v = 0
535         for var, var_docs in variants:
536             if var_docs is not None:
537                 out_java_enum += "\t/**\n\t * " + var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
538             out_java_enum += "\t" + var + ",\n"
539             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
540             ord_v = ord_v + 1
541         out_java_enum += "}"
542         out_c += "\t\tdefault: abort();\n"
543         out_c += "\t}\n"
544         out_c += "}\n"
545
546         out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_cs(LDK" + struct_name + " val) {\n"
547         out_c = out_c + "\tswitch (val) {\n"
548         ord_v = 0
549         for var, _ in variants:
550             out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
551             ord_v = ord_v + 1
552         out_c = out_c + "\t\tdefault: abort();\n"
553         out_c = out_c + "\t}\n"
554         out_c = out_c + "}\n"
555
556         return (out_c, out_java_enum + "} } }\n", out_java)
557
558     def c_unitary_enum_to_native_call(self, ty_info):
559         return (ty_info.rust_obj + "_to_cs(", ")")
560     def native_unitary_enum_to_c_call(self, ty_info):
561         return (ty_info.rust_obj + "_from_cs(", ")")
562
563     def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
564         out_c = ""
565         for var in variants:
566             out_c = out_c + "static jclass " + struct_name + "_" + var + "_class = NULL;\n"
567             out_c = out_c + "static jmethodID " + struct_name + "_" + var + "_meth = NULL;\n"
568         out_c += "void" + self.c_fn_name_define_pfx(struct_name.replace("_", "_1") + "_init", True) + self.c_fn_args_pfx + ") {\n"
569         for var_name in variants:
570             out_c += "\t" + struct_name + "_" + var_name + "_class =\n"
571             out_c += "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"org/ldk/impl/bindings$" + struct_name + "$" + var_name + "\"));\n"
572             out_c += "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
573             out_c += "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
574             out_c += "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
575         out_c = out_c + "}\n"
576         return out_c
577
578     def c_complex_enum_pass_ty(self, struct_name):
579         return "jobject"
580
581     def c_constr_native_complex_enum(self, struct_name, variant, c_params):
582         ret = "(*env)->NewObject(env, " + struct_name + "_" + variant + "_class, " + struct_name + "_" + variant + "_meth"
583         for param in c_params:
584             ret = ret + ", " + param
585         return ret + ")"
586
587     def native_c_map_trait(self, struct_name, field_vars, flattened_field_vars, field_fns, trait_doc_comment):
588         out_java_trait = ""
589         out_java = ""
590
591         # First generate most of the Java code, note that we need information about java method argument strings for C
592         out_java_trait += self.hu_struct_file_prefix
593         if trait_doc_comment is not None:
594             out_java_trait += "/**\n * " + trait_doc_comment.replace("\n", "\n * ") + "\n */\n"
595         out_java_trait = out_java_trait + "public class " + struct_name.replace("LDK","") + " : CommonBase {\n"
596         out_java_trait = out_java_trait + "\tinternal readonly bindings." + struct_name + " bindings_instance;\n"
597         out_java_trait = out_java_trait + "\tinternal " + struct_name.replace("LDK", "") + "(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }\n"
598         out_java_trait = out_java_trait + "\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg"
599         for var in flattened_field_vars:
600             if isinstance(var, ConvInfo):
601                 out_java_trait += ", " + var.java_hu_ty + " " + var.arg_name
602             else:
603                 out_java_trait += ", bindings." + var[0] + " " + var[1]
604         out_java_trait += ") : base(bindings." + struct_name + "_new(arg"
605         for var in flattened_field_vars:
606             if isinstance(var, ConvInfo):
607                 if var.from_hu_conv is not None:
608                     out_java_trait = out_java_trait + ", " + var.from_hu_conv[0]
609                 else:
610                     out_java_trait = out_java_trait + ", " + var.arg_name
611             else:
612                 out_java_trait = out_java_trait + ", " + var[1]
613         out_java_trait = out_java_trait + ")) {\n"
614         out_java_trait = out_java_trait + "\t\tthis.ptrs_to.AddLast(arg);\n"
615         for var in flattened_field_vars:
616             if isinstance(var, ConvInfo):
617                 if var.from_hu_conv is not None and var.from_hu_conv[1] != "":
618                     out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
619             else:
620                 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.AddLast(" + var[1] + ");\n"
621         out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n"
622         out_java_trait = out_java_trait + "\t}\n"
623         out_java_trait = out_java_trait + "\t~" + struct_name.replace("LDK","") + "() {\n"
624         out_java_trait = out_java_trait + "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n"
625         out_java_trait = out_java_trait + "\t}\n\n"
626
627         java_trait_wrapper = "\tprivate class " + struct_name + "Holder { internal " + struct_name.replace("LDK", "") + " held; }\n"
628         java_trait_wrapper += "\tprivate class " + struct_name + "Impl : bindings." + struct_name + " {\n"
629         java_trait_wrapper += "\t\tinternal " + struct_name + "Impl(" + struct_name.replace("LDK", "") + "Interface arg, " + struct_name + "Holder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }\n"
630         java_trait_wrapper += "\t\tprivate " + struct_name.replace("LDK", "") + "Interface arg;\n"
631         java_trait_wrapper += "\t\tprivate " + struct_name + "Holder impl_holder;\n"
632         java_trait_constr = "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
633         for var in flattened_field_vars:
634             if isinstance(var, ConvInfo):
635                 java_trait_constr += ", " + var.java_hu_ty + " " + var.arg_name
636             else:
637                 # Ideally we'd be able to take any instance of the interface, but our C code can only represent
638                 # Java-implemented version, so we require users pass a Java implementation here :/
639                 java_trait_constr += ", " + var[0].replace("LDK", "") + "." + var[0].replace("LDK", "") + "Interface " + var[1] + "_impl"
640         java_trait_constr = java_trait_constr + ") {\n\t\t" + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
641         java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new " + struct_name + "Impl(arg, impl_holder)"
642         out_java_trait += "\tpublic interface " + struct_name.replace("LDK", "") + "Interface {\n"
643         out_java += "\tpublic interface " + struct_name + " {\n"
644         java_meths = []
645         for fn_line in field_fns:
646             java_meth_descr = "("
647             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
648                 fn_name = fn_line.fn_name
649                 if fn_name == "lock": # reserved symbol
650                     fn_name = "do_lock"
651                 out_java += "\t\t" + fn_line.ret_ty_info.java_ty + " " + fn_name + "("
652                 java_trait_wrapper += "\t\tpublic " + fn_line.ret_ty_info.java_ty + " " + fn_name + "("
653                 out_java_trait += "\t\t/**\n\t\t * " + fn_line.docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n"
654                 out_java_trait += "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_name + "("
655
656                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
657                     if idx >= 1:
658                         out_java += ", "
659                         java_trait_wrapper += ", "
660                         out_java_trait += ", "
661                     out_java += arg_conv_info.java_ty + " _" + arg_conv_info.arg_name
662                     out_java_trait += arg_conv_info.java_hu_ty + " _" + arg_conv_info.arg_name
663                     java_trait_wrapper += arg_conv_info.java_ty + " _" + arg_conv_info.arg_name
664                     java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
665                 java_meth_descr = java_meth_descr + ")" + fn_line.ret_ty_info.java_fn_ty_arg
666                 java_meths.append((fn_line.fn_name, java_meth_descr))
667
668                 out_java += ");\n"
669                 out_java_trait += ");\n"
670                 java_trait_wrapper += ") {\n"
671
672                 for arg_info in fn_line.args_ty:
673                     if arg_info.to_hu_conv is not None:
674                         java_trait_wrapper += "\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t").replace(arg_info.arg_name, "_" + arg_info.arg_name) + "\n"
675
676                 if fn_line.ret_ty_info.java_ty != "void":
677                     java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_name + "("
678                 else:
679                     java_trait_wrapper += "\t\t\targ." + fn_name + "("
680
681                 for idx, arg_info in enumerate(fn_line.args_ty):
682                     if idx != 0:
683                         java_trait_wrapper += ", "
684                     if arg_info.to_hu_conv_name is not None:
685                         java_trait_wrapper += arg_info.to_hu_conv_name.replace(arg_info.arg_name, "_" + arg_info.arg_name)
686                     else:
687                         java_trait_wrapper += "_" + arg_info.arg_name
688
689                 java_trait_wrapper += ");\n"
690                 java_trait_wrapper += "\t\t\t\tGC.KeepAlive(arg);\n"
691                 if fn_line.ret_ty_info.java_ty != "void":
692                     if fn_line.ret_ty_info.from_hu_conv is not None:
693                         java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n"
694                         if fn_line.ret_ty_info.from_hu_conv[1] != "":
695                             java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
696                         java_trait_wrapper += "\t\t\treturn result;\n"
697                     else:
698                         java_trait_wrapper += "\t\t\treturn ret;\n"
699                 java_trait_wrapper += "\t\t}\n"
700         java_trait_wrapper += "\t}"
701         for var in field_vars:
702             if isinstance(var, ConvInfo):
703                 java_trait_constr = java_trait_constr + ", " + var.arg_name
704             else:
705                 java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
706                 for suparg in var[2]:
707                     if isinstance(suparg, ConvInfo):
708                         java_trait_constr += ", " + suparg.arg_name
709                     else:
710                         java_trait_constr += ", " + suparg[1]
711                 java_trait_constr += ").bindings_instance"
712                 for suparg in var[2]:
713                     if isinstance(suparg, ConvInfo):
714                         java_trait_constr += ", " + suparg.arg_name
715                     else:
716                         java_trait_constr += ", " + suparg[1]
717         out_java_trait += "\t}\n" + java_trait_wrapper + "\n"
718         out_java_trait += java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
719
720         out_java += "\t}\n"
721
722         out_java += self.native_meth_decl(struct_name + "_new", "long") + "(" + struct_name + " impl"
723         for var in flattened_field_vars:
724             if isinstance(var, ConvInfo):
725                 out_java += ", " + var.java_ty + " " + var.arg_name
726             else:
727                 out_java += ", " + var[0] + " " + var[1]
728         out_java += ");\n"
729
730         # Now that we've written out our java code (and created java_meths), generate C
731         out_c = "typedef struct " + struct_name + "_JCalls {\n"
732         out_c = out_c + "\tatomic_size_t refcnt;\n"
733         out_c = out_c + "\tJavaVM *vm;\n"
734         out_c = out_c + "\tjweak o;\n"
735         for var in flattened_field_vars:
736             if isinstance(var, ConvInfo):
737                 # We're a regular ol' field
738                 pass
739             else:
740                 # We're a supertrait
741                 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
742         for fn in field_fns:
743             if fn.fn_name != "free" and fn.fn_name != "cloned":
744                 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
745         out_c = out_c + "} " + struct_name + "_JCalls;\n"
746
747         for fn_line in field_fns:
748             if fn_line.fn_name == "free":
749                 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
750                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
751                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
752                 out_c += "\t\t" + self.construct_jenv().replace("\n", "\n\t\t").strip() + "\n"
753                 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
754                 out_c += "\t\t" + self.deconstruct_jenv().replace("\n", "\n\t\t").strip() + "\n"
755                 out_c = out_c + "\t\tFREE(j_calls);\n"
756                 out_c = out_c + "\t}\n}\n"
757
758         for idx, fn_line in enumerate(field_fns):
759             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
760                 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
761                 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall("
762                 if fn_line.self_is_const:
763                     out_c = out_c + "const void* this_arg"
764                 else:
765                     out_c = out_c + "void* this_arg"
766
767                 for idx, arg in enumerate(fn_line.args_ty):
768                     out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
769
770                 out_c = out_c + ") {\n"
771                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
772                 out_c += "\t" + self.construct_jenv().replace("\n", "\n\t").strip() + "\n"
773
774                 for arg_info in fn_line.args_ty:
775                     if arg_info.ret_conv is not None:
776                         out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
777                         out_c = out_c + arg_info.arg_name
778                         out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
779
780                 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
781                 if fn_line.ret_ty_info.c_ty.endswith("Array"):
782                     out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
783                 elif fn_line.ret_ty_info.c_ty == "void":
784                     out_c += "\t(*env)->CallVoidMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
785                 elif fn_line.ret_ty_info.java_hu_ty == "string" or "org/ldk/enums" in fn_line.ret_ty_info.java_fn_ty_arg:
786                     # Manually write out string methods as they're just an Object
787                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
788                 elif not fn_line.ret_ty_info.passed_as_ptr:
789                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
790                 else:
791                     out_c = out_c + "\tuint64_t ret = (*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
792
793                 for idx, arg_info in enumerate(fn_line.args_ty):
794                     if arg_info.ret_conv is not None:
795                         out_c = out_c + ", " + arg_info.ret_conv_name
796                     else:
797                         out_c = out_c + ", " + arg_info.arg_name
798                 out_c = out_c + ");\n"
799
800                 out_c += "\tif (UNLIKELY((*env)->ExceptionCheck(env))) {\n"
801                 out_c += "\t\t(*env)->ExceptionDescribe(env);\n"
802                 out_c += "\t\t(*env)->FatalError(env, \"A call to " + fn_line.fn_name + " in " + struct_name + " from rust threw an exception.\");\n"
803                 out_c += "\t}\n"
804
805                 if fn_line.ret_ty_info.arg_conv is not None:
806                     out_c += "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n"
807                     out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
808                     out_c += "\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
809                 else:
810                     out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
811                     if not fn_line.ret_ty_info.passed_as_ptr and fn_line.ret_ty_info.c_ty != "void":
812                         out_c += "\treturn ret;\n"
813
814                 out_c = out_c + "}\n"
815
816         # If we can, write out a clone function whether we need one or not, as we use them in moving to rust
817         can_clone_with_ptr = True
818         for var in field_vars:
819             if isinstance(var, ConvInfo):
820                 can_clone_with_ptr = False
821         if can_clone_with_ptr:
822             out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n"
823             out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n"
824             out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
825             for var in field_vars:
826                 if not isinstance(var, ConvInfo):
827                     out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n"
828             out_c = out_c + "}\n"
829
830         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
831         for var in flattened_field_vars:
832             if isinstance(var, ConvInfo):
833                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
834             else:
835                 out_c = out_c + ", jobject " + var[1]
836         out_c = out_c + ") {\n"
837
838         out_c = out_c + "\tjclass c = (*env)->GetObjectClass(env, o);\n"
839         out_c = out_c + "\tCHECK(c != NULL);\n"
840         out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
841         out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
842         out_c = out_c + "\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n"
843         out_c = out_c + "\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n"
844
845         for (fn_name, java_meth_descr) in java_meths:
846             if fn_name != "free" and fn_name != "cloned":
847                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
848                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
849
850         for var in flattened_field_vars:
851             if isinstance(var, ConvInfo) and var.arg_conv is not None:
852                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
853         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
854         out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
855         for fn_line in field_fns:
856             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
857                 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n"
858             elif fn_line.fn_name == "free":
859                 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
860             else:
861                 out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n"
862         for var in field_vars:
863             if isinstance(var, ConvInfo):
864                 if var.arg_conv_name is not None:
865                     out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
866                     out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
867                 else:
868                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
869                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
870             else:
871                 out_c += "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1]
872                 for suparg in var[2]:
873                     if isinstance(suparg, ConvInfo):
874                         out_c = out_c + ", " + suparg.arg_name
875                     else:
876                         out_c = out_c + ", " + suparg[1]
877                 out_c += "),\n"
878         out_c = out_c + "\t};\n"
879         for var in flattened_field_vars:
880             if not isinstance(var, ConvInfo):
881                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
882         out_c = out_c + "\treturn ret;\n"
883         out_c = out_c + "}\n"
884
885         out_c += "int64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
886         for var in flattened_field_vars:
887             if isinstance(var, ConvInfo):
888                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
889             else:
890                 out_c = out_c + ", jobject " + var[1]
891         out_c = out_c + ") {\n"
892         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
893         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
894         for var in flattened_field_vars:
895             if isinstance(var, ConvInfo):
896                 out_c = out_c + ", " + var.arg_name
897             else:
898                 out_c = out_c + ", " + var[1]
899         out_c = out_c + ");\n"
900         out_c = out_c + "\treturn tag_ptr(res_ptr, true);\n"
901         out_c = out_c + "}\n"
902
903         for var in flattened_field_vars:
904             if not isinstance(var, ConvInfo):
905                 out_java_trait += "\n\t/**\n"
906                 out_java_trait += "\t * Gets the underlying " + var[1] + ".\n"
907                 out_java_trait += "\t */\n"
908                 underscore_name = ''.join('_' + c.lower() if c.isupper() else c for c in var[1]).strip('_')
909                 out_java_trait += "\tpublic " + var[1] + " get_" + underscore_name + "() {\n"
910                 out_java_trait += "\t\t" + var[1] + " res = new " + var[1] + "(null, bindings." + struct_name + "_get_" + var[1] + "(this.ptr));\n"
911                 out_java_trait += "\t\tthis.ptrs_to.AddLast(res);\n"
912                 out_java_trait += "\t\treturn res;\n"
913                 out_java_trait += "\t}\n"
914                 out_java_trait += "\n"
915
916                 out_java += self.native_meth_decl(struct_name + "_get_" + var[1], "long") + "(long arg);\n"
917
918                 out_c += "int64_t " + self.c_fn_name_define_pfx(struct_name + "_get_" + var[1], True) + "int64_t arg) {\n"
919                 out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)untag_ptr(arg);\n"
920                 out_c += "\treturn tag_ptr(&inp->" + var[1] + ", false);\n"
921                 out_c += "}\n"
922
923         return (out_java, out_java_trait, out_c)
924
925     def trait_struct_inc_refcnt(self, ty_info):
926         base_conv = "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
927         base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
928         base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_cloned(&" + ty_info.var_name + "_conv);\n}"
929         return base_conv
930
931     def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
932         bindings_type = struct_name.replace("LDK", "")
933         java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
934
935         out_java_enum = ""
936         out_java = ""
937         out_c = ""
938
939         out_java_enum += (self.hu_struct_file_prefix)
940
941         java_hu_class = "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
942         java_hu_class += "public class " + java_hu_type + " : CommonBase {\n"
943         java_hu_class += f"\tprotected {java_hu_type}(object _dummy, long ptr) : base(ptr)" + " { }\n"
944         java_hu_class += "\t~" + java_hu_type + "() {\n"
945         java_hu_class += "\t\tif (ptr != 0) { bindings." + bindings_type + "_free(ptr); }\n"
946         java_hu_class += "\t}\n\n"
947         java_hu_class += f"\tinternal static {java_hu_type} constr_from_ptr(long ptr) {{\n"
948         java_hu_class += f"\t\tlong raw_ty = bindings." + struct_name + "_ty_from_ptr(ptr);\n"
949         out_c += self.c_fn_ty_pfx + "uint32_t" + self.c_fn_name_define_pfx(struct_name + "_ty_from_ptr", True) + self.ptr_c_ty + " ptr) {\n"
950         out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
951         out_c += "\tswitch(obj->tag) {\n"
952         java_hu_class += "\t\tswitch (raw_ty) {\n"
953         java_hu_subclasses = ""
954
955         var_idx = 0
956         for var in variant_list:
957             java_hu_subclasses += "\t/** A " + java_hu_type + " of type " + var.var_name + " */\n"
958             java_hu_subclasses += "\tpublic class " + java_hu_type + "_" + var.var_name + " : " + java_hu_type + " {\n"
959             java_hu_class += f"\t\t\tcase {var_idx}: "
960             java_hu_class += "return new " + java_hu_type + "_" + var.var_name + "(ptr);\n"
961             out_c += f"\t\tcase {struct_name}_{var.var_name}: return {var_idx};\n"
962             hu_conv_body = ""
963             for idx, (field_ty, field_docs) in enumerate(var.fields):
964                 if field_docs is not None:
965                     java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n"
966                 java_hu_subclasses += f"\t\tpublic {field_ty.java_hu_ty} {field_ty.arg_name};\n"
967                 if field_ty.to_hu_conv is not None:
968                     hu_conv_body += f"\t\t\t{field_ty.java_ty} {field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
969                     hu_conv_body += f"\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
970                     hu_conv_body += f"\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
971                 else:
972                     hu_conv_body += f"\t\t\tthis.{field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
973             java_hu_subclasses += "\t\tinternal " + java_hu_type + "_" + var.var_name + "(long ptr) : base(null, ptr) {\n"
974             java_hu_subclasses += hu_conv_body
975             java_hu_subclasses += "\t\t}\n\t}\n"
976             var_idx += 1
977         java_hu_class += "\t\t\tdefault:\n\t\t\t\tthrow new ArgumentException(\"Impossible enum variant\");\n\t\t}\n\t}\n\n"
978         out_java += self.native_meth_decl(struct_name + "_ty_from_ptr", "long") + "(long ptr);\n"
979         out_c += ("\t\tdefault: abort();\n")
980         out_c += ("\t}\n}\n")
981
982         for var in variant_list:
983             for idx, (field_map, _) in enumerate(var.fields):
984                 fn_name = f"{struct_name}_{var.var_name}_get_{field_map.arg_name}"
985                 out_c += self.c_fn_ty_pfx + field_map.c_ty + self.c_fn_name_define_pfx(fn_name, True) + self.ptr_c_ty + " ptr) {\n"
986                 out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
987                 out_c += f"\tassert(obj->tag == {struct_name}_{var.var_name});\n"
988                 if field_map.ret_conv is not None:
989                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
990                     if var.tuple_variant:
991                         out_c += "obj->" + camel_to_snake(var.var_name)
992                     else:
993                         out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
994                     out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
995                     out_c += "\treturn " + field_map.ret_conv_name + ";\n"
996                 else:
997                     if var.tuple_variant:
998                         out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + ";\n"
999                     else:
1000                         out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name + ";\n"
1001                 out_c += "}\n"
1002                 out_java += self.native_meth_decl(fn_name, field_map.java_ty) + "(long ptr);\n"
1003         out_java_enum += java_hu_class
1004         out_java_enum += java_hu_subclasses
1005         return (out_java, out_java_enum, out_c)
1006
1007     def map_opaque_struct(self, struct_name, struct_doc_comment):
1008         out_opaque_struct_human = ""
1009         out_opaque_struct_human += self.hu_struct_file_prefix
1010         out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n"
1011         hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
1012         out_opaque_struct_human += ("public class " + hu_name + " : CommonBase")
1013         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1014             out_opaque_struct_human += (", IDisposable")
1015         out_opaque_struct_human += (" {\n")
1016         out_opaque_struct_human += ("\tinternal " + hu_name + "(object _dummy, long ptr) : base(ptr) { }\n")
1017         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1018             out_opaque_struct_human += ("\tpublic void Dispose() {\n")
1019         else:
1020             out_opaque_struct_human += ("\t~" + hu_name + "() {\n")
1021         out_opaque_struct_human += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n")
1022         out_opaque_struct_human += ("\t}\n\n")
1023         return out_opaque_struct_human
1024
1025     def map_tuple(self, struct_name):
1026         return self.map_opaque_struct(struct_name, "A Tuple")
1027
1028     def map_result(self, struct_name, res_map, err_map):
1029         human_ty = struct_name.replace("LDKCResult", "Result")
1030         java_hu_struct = ""
1031         java_hu_struct += self.hu_struct_file_prefix
1032         java_hu_struct += "public class " + human_ty + " : CommonBase {\n"
1033         java_hu_struct += "\t" + human_ty + "(object _dummy, long ptr) : base(ptr) { }\n"
1034         java_hu_struct += "\t~" + human_ty + "() {\n"
1035         java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n"
1036         java_hu_struct += "\t}\n\n"
1037         java_hu_struct += "\tinternal static " + human_ty + " constr_from_ptr(long ptr) {\n"
1038         java_hu_struct += "\t\tif (bindings." + struct_name.replace("LDK", "") + "_is_ok(ptr)) {\n"
1039         java_hu_struct += "\t\t\treturn new " + human_ty + "_OK(null, ptr);\n"
1040         java_hu_struct += "\t\t} else {\n"
1041         java_hu_struct += "\t\t\treturn new " + human_ty + "_Err(null, ptr);\n"
1042         java_hu_struct += "\t\t}\n"
1043         java_hu_struct += "\t}\n"
1044
1045         java_hu_struct += "\tpublic class " + human_ty + "_OK : " + human_ty + " {\n"
1046
1047         if res_map.java_hu_ty != "void":
1048             java_hu_struct += "\t\tpublic readonly " + res_map.java_hu_ty + " res;\n"
1049         java_hu_struct += "\t\tinternal " + human_ty + "_OK(object _dummy, long ptr) : base(_dummy, ptr) {\n"
1050         if res_map.java_hu_ty == "void":
1051             pass
1052         elif res_map.to_hu_conv is not None:
1053             java_hu_struct += "\t\t\t" + res_map.java_ty + " res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1054             java_hu_struct += "\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t")
1055             java_hu_struct += "\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n"
1056         else:
1057             java_hu_struct += "\t\t\tthis.res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1058         java_hu_struct += "\t\t}\n"
1059         java_hu_struct += "\t}\n\n"
1060
1061         java_hu_struct += "\tpublic class " + human_ty + "_Err : " + human_ty + " {\n"
1062         if err_map.java_hu_ty != "void":
1063             java_hu_struct += "\t\tpublic readonly " + err_map.java_hu_ty + " err;\n"
1064         java_hu_struct += "\t\tinternal " + human_ty + "_Err(object _dummy, long ptr) : base(_dummy, ptr) {\n"
1065         if err_map.java_hu_ty == "void":
1066             pass
1067         elif err_map.to_hu_conv is not None:
1068             java_hu_struct += "\t\t\t" + err_map.java_ty + " err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1069             java_hu_struct += "\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t")
1070             java_hu_struct += "\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n"
1071         else:
1072             java_hu_struct += "\t\t\tthis.err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1073         java_hu_struct += "\t\t}\n"
1074
1075         java_hu_struct += "\t}\n\n"
1076         return java_hu_struct
1077
1078     def map_function(self, argument_types, c_call_string, method_name, meth_n, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_as_ref, args_known, type_mapping_generator, doc_comment):
1079         arg_name_repl = lambda s, arg_name: s.replace(arg_name, "_" + arg_name) if arg_name == "lock" or arg_name == "event" or arg_name == "params" else s
1080         out_java = ""
1081         out_c = ""
1082         out_java_struct = None
1083
1084         out_java += self.native_meth_decl(method_name, return_type_info.java_ty) + "("
1085         out_c += (return_type_info.c_ty)
1086         if return_type_info.ret_conv is not None:
1087             ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
1088         have_args = len(argument_types) > 1 or (len(argument_types) > 0 and argument_types[0].c_ty != "void")
1089         out_c += (" " + self.c_fn_name_define_pfx(method_name, have_args))
1090
1091         for idx, arg_conv_info in enumerate(argument_types):
1092             if idx != 0:
1093                 out_java += (", ")
1094                 out_c += (", ")
1095             if arg_conv_info.c_ty != "void":
1096                 out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
1097                 out_java += (arg_conv_info.java_ty + " _" + arg_conv_info.arg_name) # Add a _ to avoid using reserved words
1098
1099         out_java_struct = ""
1100         extra_java_struct_out = ""
1101         if not args_known:
1102             out_java_struct += ("\t// Skipped " + method_name + "\n")
1103         else:
1104             if doc_comment is not None:
1105                 out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
1106             hu_ret_ty = return_type_info.java_hu_ty
1107             if return_type_info.nullable:
1108                 #hu_ret_ty += "?" - apparently mono doesn't support the nullable stuff
1109                 pass
1110             if not takes_self:
1111                 if meth_n == "new":
1112                     out_java_struct += "\tpublic static " + hu_ret_ty + " of("
1113                 elif meth_n == "default":
1114                     out_java_struct += "\tpublic static " + hu_ret_ty + " with_default("
1115                 else:
1116                     out_java_struct += "\tpublic static " + hu_ret_ty + " " + meth_n + "("
1117             elif meth_n == "clone_ptr" or (struct_meth.startswith("LDKCResult") and (meth_n == "get_ok" or meth_n == "get_err")):
1118                 out_java_struct += "\tinternal " + hu_ret_ty + " " + meth_n + "("
1119             else:
1120                 if meth_n == "hash" and return_type_info.java_hu_ty == "long":
1121                     extra_java_struct_out = "\tpublic override int GetHashCode() {\n"
1122                     extra_java_struct_out += "\t\treturn (int)this.hash();\n"
1123                     extra_java_struct_out += "\t}\n"
1124                 elif meth_n == "eq" and return_type_info.java_hu_ty == "bool":
1125                     extra_java_struct_out = "\tpublic override bool Equals(object o) {\n"
1126                     extra_java_struct_out += "\t\tif (!(o is " + struct_meth + ")) return false;\n"
1127                     extra_java_struct_out += "\t\treturn this.eq((" + struct_meth + ")o);\n"
1128                     extra_java_struct_out += "\t}\n"
1129                 if meth_n == "lock":
1130                     out_java_struct += "\tpublic " + hu_ret_ty + " do_lock("
1131                 else:
1132                     out_java_struct += "\tpublic " + hu_ret_ty + " " + meth_n + "("
1133             for idx, arg in enumerate(argument_types):
1134                 if idx != 0:
1135                     if not takes_self or idx > 1:
1136                         out_java_struct += ", "
1137                 elif takes_self:
1138                     continue
1139                 if arg.java_ty != "void":
1140                     if arg.arg_name in default_constructor_args:
1141                         assert not arg.nullable
1142                         for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
1143                             if explode_idx != 0:
1144                                 out_java_struct += (", ")
1145                             out_java_struct += (
1146                                 explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name)
1147                     else:
1148                         ty_string = arg.java_hu_ty
1149                         if arg.nullable:
1150                             #ty_string += "?" - apparently mono doesn't support the nullable stuff
1151                             pass
1152                         ty_string = self.fully_qualified_hu_ty_path(arg)
1153                         out_java_struct += ty_string + " " + arg_name_repl(arg.arg_name, arg.arg_name)
1154         out_java += (");\n")
1155         out_c += (") {\n")
1156         if out_java_struct is not None:
1157             out_java_struct += (") {\n")
1158         for info in argument_types:
1159             if info.arg_conv is not None:
1160                 out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
1161         if return_type_info.ret_conv is not None:
1162             out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
1163         elif return_type_info.c_ty != "void":
1164             out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
1165         else:
1166             out_c += ("\t")
1167         if c_call_string is None:
1168             out_c += (method_name + "(")
1169         else:
1170             out_c += (c_call_string)
1171         for idx, info in enumerate(argument_types):
1172             if info.arg_conv_name is not None:
1173                 if idx != 0:
1174                     out_c += (", ")
1175                 elif c_call_string is not None:
1176                     continue
1177                 out_c += (info.arg_conv_name)
1178         out_c += (")")
1179         if return_type_info.ret_conv is not None:
1180             out_c += (ret_conv_sfx.replace('\n', '\n\t'))
1181         else:
1182             out_c += (";")
1183         for info in argument_types:
1184             if info.arg_conv_cleanup is not None:
1185                 out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
1186         if return_type_info.ret_conv is not None:
1187             out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
1188         elif return_type_info.c_ty != "void":
1189             out_c += ("\n\treturn ret_val;")
1190         out_c += ("\n}\n\n")
1191
1192         if args_known:
1193             out_java_struct += ("\t\t")
1194             if return_type_info.java_ty != "void":
1195                 out_java_struct += (return_type_info.java_ty + " ret = ")
1196             out_java_struct += ("bindings." + method_name + "(")
1197             for idx, info in enumerate(argument_types):
1198                 if idx != 0:
1199                     out_java_struct += (", ")
1200                 if idx == 0 and takes_self:
1201                     out_java_struct += ("this.ptr")
1202                 elif info.arg_name in default_constructor_args:
1203                     out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
1204                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1205                         if explode_idx != 0:
1206                             out_java_struct += (", ")
1207                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1208                         if explode_arg.from_hu_conv is not None:
1209                             out_java_struct += (
1210                                 explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1211                         else:
1212                             out_java_struct += (expl_arg_name)
1213                     out_java_struct += (")")
1214                 elif info.from_hu_conv is not None:
1215                     out_java_struct += arg_name_repl(info.from_hu_conv[0], info.arg_name)
1216                 else:
1217                     out_java_struct += arg_name_repl(info.arg_name, info.arg_name)
1218             out_java_struct += (");\n")
1219
1220             # Like Java, the C# GC is quite aggressive and can finalize an object while a method
1221             # on it is operating. Unlike Java, this behavior appears to be better documented,
1222             # which is nice.
1223             for idx, info in enumerate(argument_types):
1224                 if idx == 0 and takes_self:
1225                     out_java_struct += ("\t\tGC.KeepAlive(this);\n")
1226                 elif info.arg_name in default_constructor_args:
1227                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1228                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1229                         out_java_struct += ("\t\tGC.KeepAlive(" + expl_arg_name + ");\n")
1230                 elif info.c_ty != "void":
1231                     out_java_struct += ("\t\tGC.KeepAlive(" + arg_name_repl(info.arg_name, info.arg_name) + ");\n")
1232
1233             if return_type_info.java_ty == "long" and return_type_info.java_hu_ty != "long":
1234                 out_java_struct += "\t\tif (ret >= 0 && ret <= 4096) { return null; }\n"
1235
1236             if return_type_info.to_hu_conv is not None:
1237                 if not takes_self:
1238                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t")
1239                         .replace("this", return_type_info.to_hu_conv_name) + "\n")
1240                 else:
1241                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1242
1243             for idx, info in enumerate(argument_types):
1244                 if idx == 0 and takes_self:
1245                     pass
1246                 elif info.arg_name in default_constructor_args:
1247                     for explode_arg in default_constructor_args[info.arg_name]:
1248                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1249                         if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
1250                             out_java_struct += ("\t\t" +
1251                                 arg_name_repl(explode_arg.from_hu_conv[1], info.arg_name)
1252                                 .replace(explode_arg.arg_name, expl_arg_name)
1253                                 .replace("this", return_type_info.to_hu_conv_name) + ";\n")
1254                 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1255                     if not takes_self and return_type_info.to_hu_conv_name is not None:
1256                         out_java_struct += ("\t\t" + arg_name_repl(info.from_hu_conv[1], info.arg_name)
1257                             .replace("this", return_type_info.to_hu_conv_name)
1258                             .replace("\n", "\n\t\t") + ";\n")
1259                     else:
1260                         out_java_struct += ("\t\t" + arg_name_repl(info.from_hu_conv[1], info.arg_name)
1261                             .replace("\n", "\n\t\t") + ";\n")
1262
1263             if takes_self and not takes_self_as_ref:
1264                 out_java_struct += "\t\t" + argument_types[0].from_hu_conv[1].replace("\n", "\n\t\t").replace("this_arg", "this") + ";\n"
1265             if return_type_info.to_hu_conv_name is not None:
1266                 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
1267             elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
1268                 out_java_struct += ("\t\treturn ret;\n")
1269             out_java_struct += ("\t}\n\n")
1270
1271         return (out_java, out_c, out_java_struct + extra_java_struct_out)
1272
1273     def cleanup(self):
1274         pass