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