initiate language-dependent type mappings from C
[ldk-java] / typescript_strings.py
1 from bindingstypes import ConvInfo
2
3
4 def first_to_lower(string: str) -> str:
5     first = string[0]
6     return first.lower() + string[1:]
7
8
9 class Consts:
10     def __init__(self, DEBUG):
11
12         self.c_type_map = dict(
13             byte = ['number', 'Uint8Array'],
14         )
15
16         self.common_base = """
17             export default class CommonBase {
18                 ptr: number;
19                 ptrs_to: object[] = []; // new LinkedList(); TODO: build linked list implementation
20                 protected constructor(ptr: number) { this.ptr = ptr; }
21                 public _test_only_get_ptr(): number { return this.ptr; }
22                 protected finalize() {
23                     // TODO: finalize myself
24                 }
25             }
26         """
27
28         self.c_file_pfx = """#include <rust_types.h>
29 #include <stdatomic.h>
30 #include <lightning.h>
31
32 // These should be provided...somehow...
33 void *memset(void *s, int c, size_t n);
34 void *memcpy(void *dest, const void *src, size_t n);
35 int memcmp(const void *s1, const void *s2, size_t n);
36
37 void __attribute__((noreturn)) abort(void);
38 void assert(scalar expression);
39 """
40
41         if not DEBUG:
42             self.c_file_pfx = self.c_file_pfx + """
43 void *malloc(size_t size);
44 void free(void *ptr);
45
46 #define MALLOC(a, _) malloc(a)
47 #define FREE(p) if ((long)(p) > 1024) { free(p); }
48 #define DO_ASSERT(a) (void)(a)
49 #define CHECK(a)
50 """
51         else:
52             self.c_file_pfx = self.c_file_pfx + """
53 // Always run a, then assert it is true:
54 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
55 // Assert a is true or do nothing
56 #define CHECK(a) DO_ASSERT(a)
57
58 // Running a leak check across all the allocations and frees of the JDK is a mess,
59 // so instead we implement our own naive leak checker here, relying on the -wrap
60 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
61 // and free'd in Rust or C across the generated bindings shared library.
62
63 #define BT_MAX 128
64 typedef struct allocation {
65         struct allocation* next;
66         void* ptr;
67         const char* struct_name;
68 } allocation;
69 static allocation* allocation_ll = NULL;
70
71 void* __real_malloc(size_t len);
72 void* __real_calloc(size_t nmemb, size_t len);
73 static void new_allocation(void* res, const char* struct_name) {
74         allocation* new_alloc = __real_malloc(sizeof(allocation));
75         new_alloc->ptr = res;
76         new_alloc->struct_name = struct_name;
77         new_alloc->next = allocation_ll;
78         allocation_ll = new_alloc;
79 }
80 static void* MALLOC(size_t len, const char* struct_name) {
81         void* res = __real_malloc(len);
82         new_allocation(res, struct_name);
83         return res;
84 }
85 void __real_free(void* ptr);
86 static void alloc_freed(void* ptr) {
87         allocation* p = NULL;
88         allocation* it = allocation_ll;
89         while (it->ptr != ptr) {
90                 p = it; it = it->next;
91                 if (it == NULL) {
92                         //XXX: fprintf(stderr, "Tried to free unknown pointer %p\\n", ptr);
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(it->ptr == ptr);
98         __real_free(it);
99 }
100 static void FREE(void* ptr) {
101         if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
102         alloc_freed(ptr);
103         __real_free(ptr);
104 }
105
106 void* __wrap_malloc(size_t len) {
107         void* res = __real_malloc(len);
108         new_allocation(res, "malloc call");
109         return res;
110 }
111 void* __wrap_calloc(size_t nmemb, size_t len) {
112         void* res = __real_calloc(nmemb, len);
113         new_allocation(res, "calloc call");
114         return res;
115 }
116 void __wrap_free(void* ptr) {
117         if (ptr == NULL) return;
118         alloc_freed(ptr);
119         __real_free(ptr);
120 }
121
122 void* __real_realloc(void* ptr, size_t newlen);
123 void* __wrap_realloc(void* ptr, size_t len) {
124         if (ptr != NULL) alloc_freed(ptr);
125         void* res = __real_realloc(ptr, len);
126         new_allocation(res, "realloc call");
127         return res;
128 }
129 void __wrap_reallocarray(void* ptr, size_t new_sz) {
130         // Rust doesn't seem to use reallocarray currently
131         DO_ASSERT(false);
132 }
133
134 void __attribute__((destructor)) check_leaks() {
135         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
136                 //XXX: fprintf(stderr, "%s %p remains\\n", a->struct_name, a->ptr);
137         }
138         DO_ASSERT(allocation_ll == NULL);
139 }
140 """
141         self.c_file_pfx = self.c_file_pfx + """
142 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
143 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
144 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
145 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
146
147 _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits");
148
149 typedef struct int64_tArray {uint32_t len;int64_t *ptr;} int64_tArray;
150 typedef struct uint32_tArray {uint32_t len;int32_t *ptr;} uint32_tArray;
151 typedef struct int8_tArray {uint32_t len;int8_t *ptr;} int8_tArray;
152
153 typedef bool jboolean;
154
155 """
156
157         self.hu_struct_file_prefix = f"""
158 import CommonBase from './CommonBase';
159 import * as bindings from '../bindings' // TODO: figure out location
160
161 """
162         self.c_fn_ty_pfx = ""
163         self.c_fn_name_pfx = ""
164         self.c_fn_args_pfx = "void* ctx_TODO"
165         self.file_ext = ".ts"
166         self.ptr_c_ty = "uint32_t"
167         self.ptr_native_ty = "uint32_t"
168         self.result_c_ty = "uint32_t"
169         self.ptr_arr = "uint32_tArray"
170         self.get_native_arr_len_call = ("", ".len")
171         self.get_native_arr_ptr_call = ("", ".ptr")
172
173     def release_native_arr_ptr_call(self, arr_var, arr_ptr_var):
174         return None
175     def create_native_arr_call(self, arr_len, ty_info):
176         if ty_info.c_ty == "int8_tArray":
177             return "{ .len = " + arr_len + ", .ptr = MALLOC(" + arr_len + ", \"Native " + ty_info.c_ty + " Bytes\") }"
178         elif ty_info.c_ty == "int64_tArray":
179             return "{ .len = " + arr_len + ", .ptr = MALLOC(" + arr_len + " * sizeof(int64_t), \"Native " + ty_info.c_ty + " Bytes\") }"
180         elif ty_info.c_ty == "uint32_tArray":
181             return "{ .len = " + arr_len + ", .ptr = MALLOC(" + arr_len + " * sizeof(int32_t), \"Native " + ty_info.c_ty + " Bytes\") }"
182         else:
183             print("Need to create arr!", ty_info.c_ty)
184             return ty_info.c_ty
185     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
186         if ty_info.c_ty == "int8_tArray":
187             return ("memcpy(" + arr_name + ".ptr, ", ", " + arr_len + ")")
188         else:
189             assert False
190     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
191         if ty_info.c_ty == "int8_tArray":
192             if copy:
193                 return "memcpy(" + dest_name + ", " + arr_name + ".ptr, " + arr_len + ")"
194             else:
195                 return arr_name + ".ptr"
196         else:
197             return "(" + ty_info.subty.c_ty + "*) " + arr_name + ".ptr"
198     def get_native_arr_elem(self, arr_name, idxc, ty_info):
199         assert False # Only called if above is None
200     def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
201         if ty_info.c_ty == "int8_tArray":
202             return None
203         else:
204             return None
205
206     def init_str(self, c_array_class_caches):
207         return ""
208
209     def native_c_unitary_enum_map(self, struct_name, variants):
210         out_c = "static inline " + struct_name + " " + struct_name + "_from_js(int32_t ord) {\n"
211         out_c = out_c + "\tswitch (ord) {\n"
212         ord_v = 0
213
214         out_typescript_enum_fields = ""
215
216         for var in variants:
217             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
218             ord_v = ord_v + 1
219             out_typescript_enum_fields += f"{var},\n\t\t\t\t"
220         out_c = out_c + "\t}\n"
221         out_c = out_c + "\tabort();\n"
222         out_c = out_c + "}\n"
223
224         out_c = out_c + "static inline int32_t " + struct_name + "_to_js(" + struct_name + " val) {\n"
225         out_c = out_c + "\tswitch (val) {\n"
226         ord_v = 0
227         for var in variants:
228             out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
229             ord_v = ord_v + 1
230         out_c = out_c + "\t\tdefault: abort();\n"
231         out_c = out_c + "\t}\n"
232         out_c = out_c + "}\n"
233
234         out_typescript_enum = f"""
235             export enum {struct_name} {{
236                 {out_typescript_enum_fields}
237             }}
238         """
239
240         return (out_c, out_typescript_enum, "")
241
242     def c_unitary_enum_to_native_call(self, ty_info):
243         return (ty_info.rust_obj + "_to_js(", ")")
244     def native_unitary_enum_to_c_call(self, ty_info):
245         return (ty_info.rust_obj + "_from_js(", ")")
246
247     def c_complex_enum_pass_ty(self, struct_name):
248         return "uint32_t"
249
250     def c_constr_native_complex_enum(self, struct_name, variant, c_params):
251         ret = "0 /* " + struct_name + " - " + variant + " */"
252         for param in c_params:
253             ret = ret + "; (void) " + param
254         return ret
255
256     def native_c_map_trait(self, struct_name, field_var_conversions, field_function_lines):
257         out_java = "out_java:native_c_map_trait"
258         out_java_trait = "out_java_trait:native_c_map_trait"
259         out_c = "out_c:native_c_map_trait"
260
261
262         out_java_trait = ""
263         out_java = ""
264
265         constructor_arguments = ""
266         super_instantiator = ""
267         pointer_to_adder = ""
268         for var in field_var_conversions:
269             if isinstance(var, ConvInfo):
270                 constructor_arguments += f", {first_to_lower(var.arg_name)}?: {var.java_hu_ty}"
271                 if var.from_hu_conv is not None:
272                     super_instantiator += ", " + var.from_hu_conv[0]
273                     if var.from_hu_conv[1] != "":
274                         pointer_to_adder += var.from_hu_conv[1] + ";\n"
275                 else:
276                     super_instantiator += ", " + first_to_lower(var.arg_name)
277             else:
278                 constructor_arguments += f", {first_to_lower(var[1])}?: bindings.{var[0]}"
279                 super_instantiator += ", " + first_to_lower(var[1])
280                 pointer_to_adder += "this.ptrs_to.push(" + first_to_lower(var[1]) + ");\n"
281
282         # BUILD INTERFACE METHODS
283         out_java_interface = ""
284         for fn_line in field_function_lines:
285             if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
286                 out_java_interface += fn_line.fn_name + "("
287
288                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
289                     if idx >= 1:
290                         out_java_interface += ", "
291                     out_java_interface += f"{arg_conv_info.arg_name}: {arg_conv_info.java_hu_ty}"
292                 out_java_interface += f"): {fn_line.ret_ty_info.java_hu_ty};\n\t\t\t\t"
293
294         out_java_trait = f"""
295             {self.hu_struct_file_prefix}
296             
297             export class {struct_name.replace("LDK","")} extends CommonBase {{
298             
299                 bindings_instance?: bindings.{struct_name};
300                 
301                 constructor(ptr?: number, arg?: bindings.{struct_name}{constructor_arguments}) {{
302                     if (Number.isFinite(ptr)) {{
303                                         super(ptr);
304                                         this.bindings_instance = null;
305                                     }} else {{
306                                         // TODO: private constructor instantiation
307                                         super(bindings.{struct_name}_new(arg{super_instantiator}));
308                                         this.ptrs_to.push(arg);
309                                         {pointer_to_adder}
310                                     }}
311                 }}
312                 
313                 protected finalize() {{
314                     if (this.ptr != 0) {{ 
315                         bindings.{struct_name.replace("LDK","")}_free(this.ptr); 
316                     }} 
317                     super.finalize();
318                 }}
319                 
320             }}
321             
322             export interface {struct_name.replace("LDK", "")}Interface {{
323                 {out_java_interface}
324             }}
325         """
326
327
328
329         java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
330         java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
331         for var in field_var_conversions:
332             if isinstance(var, ConvInfo):
333                 java_trait_constr = java_trait_constr + ", " + var.java_hu_ty + " " + var.arg_name
334             else:
335                 # Ideally we'd be able to take any instance of the interface, but our C code can only represent
336                 # Java-implemented version, so we require users pass a Java implementation here :/
337                 java_trait_constr = java_trait_constr + ", " + var[0].replace("LDK", "") + "." + var[0].replace("LDK", "") + "Interface " + var[1] + "_impl"
338         java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
339         java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n"
340         out_java_trait = out_java_trait + "\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n"
341         out_java = out_java + "\tpublic interface " + struct_name + " {\n"
342         java_meths = []
343         for fn_line in field_function_lines:
344             java_meth_descr = "("
345             if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
346                 out_java = out_java + "\t\t " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "("
347                 java_trait_constr = java_trait_constr + "\t\t\t@Override public " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "("
348                 out_java_trait = out_java_trait + "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_line.fn_name + "("
349
350                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
351                     if idx >= 1:
352                         out_java = out_java + ", "
353                         java_trait_constr = java_trait_constr + ", "
354                         out_java_trait = out_java_trait + ", "
355                     out_java = out_java + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
356                     out_java_trait = out_java_trait + arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name
357                     java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
358                     java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
359                 java_meth_descr = java_meth_descr + ")" + fn_line.ret_ty_info.java_fn_ty_arg
360                 java_meths.append((fn_line.fn_name, java_meth_descr))
361
362                 out_java = out_java + ");\n"
363                 out_java_trait = out_java_trait + ");\n"
364                 java_trait_constr = java_trait_constr + ") {\n"
365
366                 for arg_info in fn_line.args_ty:
367                     if arg_info.to_hu_conv is not None:
368                         java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
369
370                 if fn_line.ret_ty_info.java_ty != "void":
371                     java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_line.fn_name + "("
372                 else:
373                     java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.fn_name + "("
374
375                 for idx, arg_info in enumerate(fn_line.args_ty):
376                     if idx != 0:
377                         java_trait_constr = java_trait_constr + ", "
378                     if arg_info.to_hu_conv_name is not None:
379                         java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name
380                     else:
381                         java_trait_constr = java_trait_constr + arg_info.arg_name
382
383                 java_trait_constr = java_trait_constr + ");\n"
384                 if fn_line.ret_ty_info.java_ty != "void":
385                     if fn_line.ret_ty_info.from_hu_conv is not None:
386                         java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n"
387                         if fn_line.ret_ty_info.from_hu_conv[1] != "":
388                             java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
389                         #if fn_line.ret_ty_info.rust_obj in result_types:
390                         # XXX: We need to handle this in conversion logic so that its cross-language!
391                         # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
392                         #    java_trait_constr = java_trait_constr + "\t\t\t\tret.ptr = 0;\n"
393                         java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n"
394                     else:
395                         java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n"
396                 java_trait_constr = java_trait_constr + "\t\t\t}\n"
397         java_trait_constr = java_trait_constr + "\t\t}"
398         for var in field_var_conversions:
399             if isinstance(var, ConvInfo):
400                 java_trait_constr = java_trait_constr + ", " + var.arg_name
401             else:
402                 java_trait_constr = java_trait_constr + ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance"
403         out_java_trait = out_java_trait + "\t}\n"
404         out_java_trait = out_java_trait + java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
405
406         print(java_trait_constr)
407
408         out_java = out_java + "\t}\n"
409
410         out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl"
411         for var in field_var_conversions:
412             if isinstance(var, ConvInfo):
413                 out_java = out_java + ", " + var.java_ty + " " + var.arg_name
414             else:
415                 out_java = out_java + ", " + var[0] + " " + var[1]
416         out_java = out_java + ");\n"
417         out_java = out_java + "\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n"
418
419         # Now that we've written out our java code (and created java_meths), generate C
420         out_c = "typedef struct " + struct_name + "_JCalls {\n"
421         out_c = out_c + "\tatomic_size_t refcnt;\n"
422         out_c = out_c + "\tJavaVM *vm;\n"
423         out_c = out_c + "\tjweak o;\n"
424         for var in field_var_conversions:
425             if isinstance(var, ConvInfo):
426                 # We're a regular ol' field
427                 pass
428             else:
429                 # We're a supertrait
430                 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
431         for fn in field_function_lines:
432             if fn.fn_name != "free" and fn.fn_name != "clone":
433                 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
434         out_c = out_c + "} " + struct_name + "_JCalls;\n"
435
436         for fn_line in field_function_lines:
437             if fn_line.fn_name == "free":
438                 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
439                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
440                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
441                 out_c = out_c + "\t\tJNIEnv *env;\n"
442                 out_c = out_c + "\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
443                 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
444                 out_c = out_c + "\t\tFREE(j_calls);\n"
445                 out_c = out_c + "\t}\n}\n"
446
447         for idx, fn_line in enumerate(field_function_lines):
448             if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
449                 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
450                 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_jcall("
451                 if fn_line.self_is_const:
452                     out_c = out_c + "const void* this_arg"
453                 else:
454                     out_c = out_c + "void* this_arg"
455
456                 for idx, arg in enumerate(fn_line.args_ty):
457                     out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
458
459                 out_c = out_c + ") {\n"
460                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
461                 out_c = out_c + "\tJNIEnv *env;\n"
462                 out_c = out_c + "\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
463
464                 for arg_info in fn_line.args_ty:
465                     if arg_info.ret_conv is not None:
466                         out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
467                         out_c = out_c + arg_info.arg_name
468                         out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
469
470                 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
471                 if fn_line.ret_ty_info.c_ty.endswith("Array"):
472                     out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " arg = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
473                 elif not fn_line.ret_ty_info.passed_as_ptr:
474                     out_c = out_c + "\treturn (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
475                 else:
476                     out_c = out_c + "\t" + fn_line.ret_ty_info.rust_obj + "* ret = (" + fn_line.ret_ty_info.rust_obj + "*)(*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
477
478                 for idx, arg_info in enumerate(fn_line.args_ty):
479                     if arg_info.ret_conv is not None:
480                         out_c = out_c + ", " + arg_info.ret_conv_name
481                     else:
482                         out_c = out_c + ", " + arg_info.arg_name
483                 out_c = out_c + ");\n"
484                 if fn_line.ret_ty_info.arg_conv is not None:
485                     out_c = out_c + "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
486
487                 out_c = out_c + "}\n"
488
489         # Write out a clone function whether we need one or not, as we use them in moving to rust
490         out_c = out_c + "static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n"
491         out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
492         out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
493         for var in field_var_conversions:
494             if not isinstance(var, ConvInfo):
495                 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n"
496         out_c = out_c + "\treturn (void*) this_arg;\n"
497         out_c = out_c + "}\n"
498
499         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
500         for var in field_var_conversions:
501             if isinstance(var, ConvInfo):
502                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
503             else:
504                 out_c = out_c + ", jobject " + var[1]
505         out_c = out_c + ") {\n"
506
507         out_c = out_c + "\tjclass c = (*env)->GetObjectClass(env, o);\n"
508         out_c = out_c + "\tCHECK(c != NULL);\n"
509         out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
510         out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
511         out_c = out_c + "\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n"
512         out_c = out_c + "\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n"
513
514         for (fn_name, java_meth_descr) in java_meths:
515             if fn_name != "free" and fn_name != "clone":
516                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
517                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
518
519         for var in field_var_conversions:
520             if isinstance(var, ConvInfo) and var.arg_conv is not None:
521                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
522         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
523         out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
524         for fn_line in field_function_lines:
525             if fn_line.fn_name != "free" and fn_line.fn_name != "clone":
526                 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_jcall,\n"
527             elif fn_line.fn_name == "free":
528                 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
529             else:
530                 out_c = out_c + "\t\t.clone = " + struct_name + "_JCalls_clone,\n"
531         for var in field_var_conversions:
532             if isinstance(var, ConvInfo):
533                 if var.arg_conv_name is not None:
534                     out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
535                     out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
536                 else:
537                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
538                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
539             else:
540                 out_c = out_c + "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1] + "),\n"
541         out_c = out_c + "\t};\n"
542         for var in field_var_conversions:
543             if not isinstance(var, ConvInfo):
544                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
545         out_c = out_c + "\treturn ret;\n"
546         out_c = out_c + "}\n"
547
548         out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1new (" + self.c_fn_args_pfx + ", jobject o"
549         for var in field_var_conversions:
550             if isinstance(var, ConvInfo):
551                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
552             else:
553                 out_c = out_c + ", jobject " + var[1]
554         out_c = out_c + ") {\n"
555         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
556         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
557         for var in field_var_conversions:
558             if isinstance(var, ConvInfo):
559                 out_c = out_c + ", " + var.arg_name
560             else:
561                 out_c = out_c + ", " + var[1]
562         out_c = out_c + ");\n"
563         out_c = out_c + "\treturn (long)res_ptr;\n"
564         out_c = out_c + "}\n"
565
566         out_c = out_c + self.c_fn_ty_pfx + "jobject " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " val) {\n"
567         out_c = out_c + "\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n"
568         out_c = out_c + "\tCHECK(ret != NULL);\n"
569         out_c = out_c + "\treturn ret;\n"
570         out_c = out_c + "}\n"
571
572
573         return (out_java, out_java_trait, out_c)
574
575     def map_complex_enum(self, struct_name, variant_list, camel_to_snake):
576         java_hu_type = struct_name.replace("LDK", "")
577
578         out_java_enum = ""
579         out_java = ""
580         out_c = ""
581
582         out_java_enum += (self.hu_struct_file_prefix)
583         out_java_enum += ("export default class " + java_hu_type + " extends CommonBase {\n")
584         out_java_enum += ("\tprotected constructor(_dummy: object, ptr: number) { super(ptr); }\n")
585         out_java_enum += ("\tprotected finalize() {\n")
586         out_java_enum += ("\t\tsuper.finalize();\n")
587         out_java_enum += ("\t\tif (this.ptr != 0) { bindings." + java_hu_type + "_free(this.ptr); }\n")
588         out_java_enum += ("\t}\n")
589         out_java_enum += f"\tstatic constr_from_ptr(ptr: number): {java_hu_type} {{\n"
590         out_java_enum += (f"\t\tconst raw_val: bindings.{struct_name} = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
591         java_hu_subclasses = ""
592
593         out_java +=  ("\tpublic static class " + struct_name + " {\n")
594         out_java +=  ("\t\tprivate " + struct_name + "() {}\n")
595         for var in variant_list:
596             out_java +=  ("\t\texport class " + var.var_name + " extends " + struct_name + " {\n")
597             java_hu_subclasses = java_hu_subclasses + "export class " + var.var_name + " extends " + java_hu_type + " {\n"
598             out_java_enum += ("\t\tif (raw_val instanceof bindings." + struct_name + "." + var.var_name + ") {\n")
599             out_java_enum += ("\t\t\treturn new " + var.var_name + "(this.ptr, raw_val);\n")
600             init_meth_params = ""
601             init_meth_body = ""
602             hu_conv_body = ""
603             for idx, field_ty in enumerate(var.fields):
604                 out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
605                 java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
606                 if field_ty.to_hu_conv is not None:
607                     hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n"
608                     hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
609                     hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
610                 else:
611                     hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
612                 if idx > 0:
613                     init_meth_params = init_meth_params + ", "
614                 init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
615                 init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
616             out_java +=  ("\t\t\t" + var.var_name + "(" + init_meth_params + ") { ")
617             out_java +=  (init_meth_body)
618             out_java +=  ("}\n")
619             out_java += ("\t\t}\n")
620             out_java_enum += ("\t\t}\n")
621             java_hu_subclasses = java_hu_subclasses + "\tprivate constructor(ptr: number, obj: bindings." + struct_name + "." + var.var_name + ") {\n\t\tsuper(null, ptr);\n"
622             java_hu_subclasses = java_hu_subclasses + hu_conv_body
623             java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
624         out_java += ("\t\tstatic native void init();\n")
625         out_java += ("\t}\n")
626         out_java_enum += ("\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
627         out_java += ("\tstatic { " + struct_name + ".init(); }\n")
628         out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
629
630         out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n")
631         out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
632         out_c += ("\tswitch(obj->tag) {\n")
633         for var in variant_list:
634             out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
635             c_params = []
636             for idx, field_map in enumerate(var.fields):
637                 if field_map.ret_conv is not None:
638                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
639                     out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
640                     out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
641                     c_params.append(field_map.ret_conv_name)
642                 else:
643                     c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
644             out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
645             out_c += ("\t\t}\n")
646         out_c += ("\t\tdefault: abort();\n")
647         out_c += ("\t}\n}\n")
648         out_java_enum += ("}\n")
649         out_java_enum += (java_hu_subclasses)
650         return (out_java, out_java_enum, out_c)