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