[TS] Empty environment variables as WASI's environ fetch leaks memory
[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, outdir: str, **kwargs):
15         self.outdir = outdir
16         self.struct_file_suffixes = {}
17         self.function_ptr_counter = 0
18         self.function_ptrs = {}
19         self.c_type_map = dict(
20             uint8_t = ['number', 'number', 'Uint8Array'],
21             uint16_t = ['number', 'number', 'Uint16Array'],
22             uint32_t = ['number', 'number', 'Uint32Array'],
23             uint64_t = ['bigint', 'bigint', 'BigUint64Array'],
24         )
25         self.java_type_map = dict(
26             String = "number"
27         )
28         self.java_hu_type_map = dict(
29             String = "string"
30         )
31
32         self.to_hu_conv_templates = dict(
33             ptr = 'const {var_name}_hu_conv: {human_type} = new {human_type}(null, {var_name});',
34             default = 'const {var_name}_hu_conv: {human_type} = new {human_type}(null, {var_name});',
35         )
36
37         self.bindings_header = """
38 import * as version from './version.mjs';
39
40 const imports: any = {};
41 imports.env = {};
42
43 var js_objs: Array<WeakRef<object>> = [];
44 var js_invoke: Function;
45
46 imports.wasi_snapshot_preview1 = {
47         "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => {
48                 // This should generally only be used to print panic messages
49                 console.log("FD_WRITE to " + fd + " in " + iovec_array_len + " chunks.");
50                 const ptr_len_view = new Uint32Array(wasm.memory.buffer, iovec_array_ptr, iovec_array_len * 2);
51                 for (var i = 0; i < iovec_array_len; i++) {
52                         const bytes_view = new Uint8Array(wasm.memory.buffer, ptr_len_view[i*2], ptr_len_view[i*2+1]);
53                         console.log(String.fromCharCode(...bytes_view));
54                 }
55                 return 0;
56         },
57         "fd_close": (_fd: number) => {
58                 // This is not generally called, but may be referenced in debug builds
59                 console.log("wasi_snapshot_preview1:fd_close");
60                 return 58; // Not Supported
61         },
62         "fd_seek": (_fd: number, _offset: bigint, _whence: number, _new_offset: number) => {
63                 // This is not generally called, but may be referenced in debug builds
64                 console.log("wasi_snapshot_preview1:fd_seek");
65                 return 58; // Not Supported
66         },
67         "random_get": (buf_ptr: number, buf_len: number) => {
68                 const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len);
69                 crypto.getRandomValues(buf);
70                 return 0;
71         },
72         "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => {
73                 // This is called before fd_write to format + print panic messages
74                 console.log("wasi_snapshot_preview1:environ_sizes_get");
75                 const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1);
76                 out_count_view[0] = 0;
77                 const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1);
78                 out_len_view[0] = 0;
79                 return 0;
80         },
81         "environ_get": (environ_ptr: number, environ_buf_ptr: number) => {
82                 // This is called before fd_write to format + print panic messages
83                 console.log("wasi_snapshot_preview1:environ_get");
84                 return 58; // Note supported - we said there were 0 environment entries!
85         },
86         "proc_exit" : () => {
87                 console.log("wasi_snapshot_preview1:proc_exit");
88         },
89 };
90
91 var wasm: any = null;
92 let isWasmInitialized: boolean = false;
93 """
94
95         if target == Target.NODEJS:
96             self.bindings_header += """import * as fs from 'fs';
97 import { webcrypto as crypto } from 'crypto';
98 /* @internal */
99 export async function initializeWasm(path: string) {
100         const source = fs.readFileSync(path);
101         imports.env["js_invoke_function"] = js_invoke;
102         const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);"""
103         else:
104             self.bindings_header += """
105 /* @internal */
106 export async function initializeWasm(uri: string) {
107         const stream = fetch(uri);
108         imports.env["js_invoke_function"] = js_invoke;
109         const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);"""
110
111         self.bindings_header += """
112         wasm = wasmInstance.exports;
113         if (!wasm.test_bigint_pass_deadbeef0badf00d(BigInt("0xdeadbeef0badf00d"))) {
114                 throw new Error(\"Currently need BigInt-as-u64 support, try ----experimental-wasm-bigint");
115         }
116
117         if (decodeString(wasm.TS_get_lib_version_string()) !== version.get_ldk_java_bindings_version())
118                 throw new Error(\"Compiled LDK library and LDK class failes do not match\");
119         // Fetching the LDK versions from C also checks that the header and binaries match
120         if (wasm.TS_get_ldk_c_bindings_version() == 0)
121                 throw new Error(\"LDK version did not match the header we built against\");
122         if (wasm.TS_get_ldk_version() == 0)
123                 throw new Error(\"LDK C bindings version did not match the header we built against\");
124         const c_bindings_version: string = decodeString(wasm.TS_get_ldk_c_bindings_version());
125         const ldk_version: string = decodeString(wasm.TS_get_ldk_version());
126         console.log(\"Loaded LDK-Java Bindings with LDK \" + ldk_version + \" and LDK-C-Bindings \" + c_bindings_version);
127
128         isWasmInitialized = true;
129 };
130
131 // WASM CODEC
132
133 const nextMultipleOfFour = (value: number) => {
134         return Math.ceil(value / 4) * 4;
135 }
136
137 /* @internal */
138 export function encodeUint8Array (inputArray: Uint8Array): number {
139         const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
140         const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
141         arrayLengthView[0] = inputArray.length;
142         const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
143         arrayMemoryView.set(inputArray);
144         return cArrayPointer;
145 }
146 /* @internal */
147 export function encodeUint32Array (inputArray: Uint32Array|Array<number>): number {
148         const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
149         const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length);
150         arrayMemoryView.set(inputArray, 1);
151         arrayMemoryView[0] = inputArray.length;
152         return cArrayPointer;
153 }
154 /* @internal */
155 export function encodeUint64Array (inputArray: BigUint64Array|Array<bigint>): number {
156         const cArrayPointer = wasm.TS_malloc(inputArray.length * 8 + 1);
157         const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
158         arrayLengthView[0] = inputArray.length;
159         const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
160         arrayMemoryView.set(inputArray);
161         return cArrayPointer;
162 }
163
164 /* @internal */
165 export function check_arr_len(arr: Uint8Array, len: number): Uint8Array {
166         if (arr.length != len) { throw new Error("Expected array of length " + len + "got " + arr.length); }
167         return arr;
168 }
169
170 /* @internal */
171 export function getArrayLength(arrayPointer: number): number {
172         const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1);
173         return arraySizeViewer[0];
174 }
175 /* @internal */
176 export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
177         const arraySize = getArrayLength(arrayPointer);
178         const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, arraySize);
179         // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
180         // will free the underlying memory when it becomes unreachable instead of copying here.
181         // Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer).
182         const actualArray = actualArrayViewer.slice(0, arraySize);
183         if (free) {
184                 wasm.TS_free(arrayPointer);
185         }
186         return actualArray;
187 }
188 const decodeUint32Array = (arrayPointer: number, free = true) => {
189         const arraySize = getArrayLength(arrayPointer);
190         const actualArrayViewer = new Uint32Array(
191                 wasm.memory.buffer, // value
192                 arrayPointer + 4, // offset (ignoring length bytes)
193                 arraySize // uint32 count
194         );
195         // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
196         // will free the underlying memory when it becomes unreachable instead of copying here.
197         const actualArray = actualArrayViewer.slice(0, arraySize);
198         if (free) {
199                 wasm.TS_free(arrayPointer);
200         }
201         return actualArray;
202 }
203
204
205 export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
206
207 /* @internal */
208 export function getU32ArrayElem(arrayPointer: number, idx: number): number {
209         const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
210         return actualArrayViewer[idx];
211 }
212
213 /* @internal */
214 export function encodeString(str: string): number {
215         const charArray = new TextEncoder().encode(str);
216         return encodeUint8Array(charArray);
217 }
218
219 /* @internal */
220 export function decodeString(stringPointer: number, free = true): string {
221         const arraySize = getArrayLength(stringPointer);
222         const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize);
223         const result = new TextDecoder("utf-8").decode(memoryView);
224
225         if (free) {
226                 wasm.TS_free(stringPointer);
227         }
228
229         return result;
230 }
231 """
232         if DEBUG:
233             self.bindings_header += """
234 /* @internal */
235 export function getRemainingAllocationCount(): number {
236         return wasm.TS_allocs_remaining();
237 }
238 /* @internal */
239 export function debugPrintRemainingAllocs() {
240         wasm.TS_print_leaks();
241 }
242 """
243         else:
244             self.bindings_header += "\n/* @internal */ export function getRemainingAllocationCount(): number { return 0; }\n"
245             self.bindings_header += "/* @internal */ export function debugPrintRemainingAllocs() { }\n"
246
247         with open(outdir + "/index.mts", 'a') as index:
248             index.write("""import { initializeWasm as bindingsInit } from './bindings.mjs';
249 export function initializeWasm(path: string) {
250         bindingsInit(path);
251 }
252 """)
253
254         self.bindings_version_file = """export function get_ldk_java_bindings_version(): String {
255         return "<git_version_ldk_garbagecollected>";
256 }"""
257
258         self.bindings_footer = ""
259
260         self.common_base = """
261 function freer(f: () => void) { f() }
262 const finalizer = new FinalizationRegistry(freer);
263 function get_freeer(ptr: number, free_fn: (ptr: number) => void) {
264         return () => {
265                 free_fn(ptr);
266         }
267 }
268
269 export default class CommonBase {
270         protected ptr: number;
271         protected ptrs_to: object[] = [];
272         protected constructor(ptr: number, free_fn: (ptr: number) => void) {
273                 this.ptr = ptr;
274                 if (Number.isFinite(ptr) && ptr != 0){
275                         finalizer.register(this, get_freeer(ptr, free_fn));
276                 }
277         }
278         // In Java, protected means "any subclass can access fields on any other subclass'"
279         // In TypeScript, protected means "any subclass can access parent fields on instances of itself"
280         // To work around this, we add accessors for other instances' protected fields here.
281         protected static add_ref_from(holder: CommonBase, referent: object) {
282                 holder.ptrs_to.push(referent);
283         }
284         protected static get_ptr_of(o: CommonBase) {
285                 return o.ptr;
286         }
287         protected static set_null_skip_free(o: CommonBase) {
288                 o.ptr = 0;
289                 finalizer.unregister(o);
290         }
291 }
292 """
293
294         self.txout_defn = """export class TxOut extends CommonBase {
295         /** The script_pubkey in this output */
296         public script_pubkey: Uint8Array;
297         /** The value, in satoshis, of this output */
298         public value: bigint;
299
300         /* @internal */
301         public constructor(_dummy: object, ptr: number) {
302                 super(ptr, bindings.TxOut_free);
303                 this.script_pubkey = bindings.decodeUint8Array(bindings.TxOut_get_script_pubkey(ptr));
304                 this.value = bindings.TxOut_get_value(ptr);
305         }
306         public constructor_new(value: bigint, script_pubkey: Uint8Array): TxOut {
307                 return new TxOut(null, bindings.TxOut_new(bindings.encodeUint8Array(script_pubkey), value));
308         }
309 }"""
310         self.obj_defined(["TxOut"], "structs")
311
312         self.c_file_pfx = """#include "js-wasm.h"
313 #include <stdatomic.h>
314 #include <lightning.h>
315
316 // These should be provided...somehow...
317 void *memset(void *s, int c, size_t n);
318 void *memcpy(void *dest, const void *src, size_t n);
319 int memcmp(const void *s1, const void *s2, size_t n);
320
321 extern void __attribute__((noreturn)) abort(void);
322 static inline void assert(bool expression) {
323         if (!expression) { abort(); }
324 }
325
326 uint32_t __attribute__((export_name("test_bigint_pass_deadbeef0badf00d"))) test_bigint_pass_deadbeef0badf00d(uint64_t val) {
327         return val == 0xdeadbeef0badf00dULL;
328 }
329
330 """
331
332         if not DEBUG:
333             self.c_file_pfx += """
334 void *malloc(size_t size);
335 void free(void *ptr);
336
337 #define MALLOC(a, _) malloc(a)
338 #define do_MALLOC(a, _b, _c) malloc(a)
339 #define FREE(p) if ((unsigned long)(p) > 4096) { free(p); }
340 #define DO_ASSERT(a) (void)(a)
341 #define CHECK(a)
342 #define CHECK_ACCESS(p)
343 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
344 """
345         else:
346             self.c_file_pfx += """
347 extern int snprintf(char *str, size_t size, const char *format, ...);
348 typedef int32_t ssize_t;
349 ssize_t write(int fd, const void *buf, size_t count);
350 #define DEBUG_PRINT(...) do { \\
351         char debug_str[1024]; \\
352         int s_len = snprintf(debug_str, 1023, __VA_ARGS__); \\
353         write(2, debug_str, s_len); \\
354 } while (0);
355
356 // Always run a, then assert it is true:
357 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
358 // Assert a is true or do nothing
359 #define CHECK(a) DO_ASSERT(a)
360
361 // Running a leak check across all the allocations and frees of the JDK is a mess,
362 // so instead we implement our own naive leak checker here, relying on the -wrap
363 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
364 // and free'd in Rust or C across the generated bindings shared library.
365
366 #define BT_MAX 128
367 typedef struct allocation {
368         struct allocation* next;
369         void* ptr;
370         const char* struct_name;
371         int lineno;
372 } allocation;
373 static allocation* allocation_ll = NULL;
374 static allocation* freed_ll = NULL;
375
376 extern void* __real_malloc(size_t len);
377 extern void* __real_calloc(size_t nmemb, size_t len);
378 extern void* __real_aligned_alloc(size_t alignment, size_t size);
379 static void new_allocation(void* res, const char* struct_name, int lineno) {
380         allocation* new_alloc = __real_malloc(sizeof(allocation));
381         new_alloc->ptr = res;
382         new_alloc->struct_name = struct_name;
383         new_alloc->next = allocation_ll;
384         new_alloc->lineno = lineno;
385         allocation_ll = new_alloc;
386 }
387 static void* do_MALLOC(size_t len, const char* struct_name, int lineno) {
388         void* res = __real_malloc(len);
389         new_allocation(res, struct_name, lineno);
390         return res;
391 }
392 #define MALLOC(len, struct_name) do_MALLOC(len, struct_name, __LINE__)
393
394 void __real_free(void* ptr);
395 static void alloc_freed(void* ptr, int lineno) {
396         allocation* p = NULL;
397         allocation* it = allocation_ll;
398         while (it->ptr != ptr) {
399                 p = it; it = it->next;
400                 if (it == NULL) {
401                         p = NULL;
402                         it = freed_ll;
403                         while (it && it->ptr != ptr) { p = it; it = it->next; }
404                         if (it == NULL) {
405                                 DEBUG_PRINT("Tried to free unknown pointer %p at line %d.\\n", ptr, lineno);
406                         } else {
407                                 DEBUG_PRINT("Tried to free unknown pointer %p at line %d.\\n Possibly double-free from %s, allocated on line %d.", ptr, lineno, it->struct_name, it->lineno);
408                         }
409                         abort();
410                 }
411         }
412         if (p) { p->next = it->next; } else { allocation_ll = it->next; }
413         DO_ASSERT(it->ptr == ptr);
414         it->next = freed_ll;
415         freed_ll = it;
416 }
417 static void do_FREE(void* ptr, int lineno) {
418         if ((unsigned long)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys
419         alloc_freed(ptr, lineno);
420         __real_free(ptr);
421 }
422 #define FREE(ptr) do_FREE(ptr, __LINE__)
423
424 static void CHECK_ACCESS(const void* ptr) {
425         allocation* it = allocation_ll;
426         while (it->ptr != ptr) {
427                 it = it->next;
428                 if (it == NULL) {
429                         return; // addrsan should catch malloc-unknown and print more info than we have
430                 }
431         }
432 }
433 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
434         if (v.is_owned && v.inner != NULL) { \\
435                 const void *p = __unmangle_inner_ptr(v.inner); \\
436                 if (p != NULL) { \\
437                         CHECK_ACCESS(p); \\
438                 } \\
439         }
440
441 void* __wrap_malloc(size_t len) {
442         void* res = __real_malloc(len);
443         new_allocation(res, "malloc call", 0);
444         return res;
445 }
446 void* __wrap_calloc(size_t nmemb, size_t len) {
447         void* res = __real_calloc(nmemb, len);
448         new_allocation(res, "calloc call", 0);
449         return res;
450 }
451 void* __wrap_aligned_alloc(size_t alignment, size_t size) {
452         void* res = __real_aligned_alloc(alignment, size);
453         new_allocation(res, "aligned_alloc call", 0);
454         return res;
455 }
456 void __wrap_free(void* ptr) {
457         if (ptr == NULL) return;
458         alloc_freed(ptr, 0);
459         __real_free(ptr);
460 }
461
462 void* __real_realloc(void* ptr, size_t newlen);
463 void* __wrap_realloc(void* ptr, size_t len) {
464         if (ptr != NULL) alloc_freed(ptr, 0);
465         void* res = __real_realloc(ptr, len);
466         new_allocation(res, "realloc call", 0);
467         return res;
468 }
469 void __wrap_reallocarray(void* ptr, size_t new_sz) {
470         // Rust doesn't seem to use reallocarray currently
471         DO_ASSERT(false);
472 }
473
474 uint32_t __attribute__((export_name("TS_allocs_remaining"))) allocs_remaining() {
475         uint32_t count = 0;
476         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
477                 count++;
478         }
479         return count;
480 }
481 void __attribute__((export_name("TS_print_leaks"))) print_leaks() {
482         for (allocation* a = allocation_ll; a != NULL; a = a->next) {
483                 DEBUG_PRINT("%s %p remains. Allocated on line %d\\n", a->struct_name, a->ptr, a->lineno);
484         }
485 }
486 """
487         self.c_file_pfx = self.c_file_pfx + """
488 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
489 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
490 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
491 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
492
493 _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits");
494
495 #define DECL_ARR_TYPE(ty, name) \\
496         struct name##array { \\
497                 uint32_t arr_len; \\
498                 ty elems[]; \\
499         }; \\
500         typedef struct name##array * name##Array; \\
501         static inline name##Array init_##name##Array(size_t arr_len, int lineno) { \\
502                 name##Array arr = (name##Array)do_MALLOC(arr_len * sizeof(ty) + sizeof(uint32_t), #name" array init", lineno); \\
503                 arr->arr_len = arr_len; \\
504                 return arr; \\
505         }
506
507 DECL_ARR_TYPE(int64_t, int64_t);
508 DECL_ARR_TYPE(int8_t, int8_t);
509 DECL_ARR_TYPE(uint32_t, uint32_t);
510 DECL_ARR_TYPE(void*, ptr);
511 DECL_ARR_TYPE(char, char);
512 typedef charArray jstring;
513
514 static inline jstring str_ref_to_ts(const char* chars, size_t len) {
515         charArray arr = init_charArray(len, __LINE__);
516         memcpy(arr->elems, chars, len);
517         return arr;
518 }
519 static inline LDKStr str_ref_to_owned_c(const jstring str) {
520         char* newchars = MALLOC(str->arr_len + 1, "String chars");
521         memcpy(newchars, str->elems, str->arr_len);
522         newchars[str->arr_len] = 0;
523         LDKStr res = {
524                 .chars = newchars,
525                 .len = str->arr_len,
526                 .chars_is_owned = true
527         };
528         return res;
529 }
530
531 typedef bool jboolean;
532
533 uint32_t __attribute__((export_name("TS_malloc"))) TS_malloc(uint32_t size) {
534         return (uint32_t)MALLOC(size, "JS-Called malloc");
535 }
536 void __attribute__((export_name("TS_free"))) TS_free(uint32_t ptr) {
537         FREE((void*)ptr);
538 }
539
540 jstring __attribute__((export_name("TS_get_ldk_c_bindings_version"))) TS_get_ldk_c_bindings_version() {
541         const char *res = check_get_ldk_bindings_version();
542         if (res == NULL) return NULL;
543         return str_ref_to_ts(res, strlen(res));
544 }
545 jstring __attribute__((export_name("TS_get_ldk_version"))) get_ldk_version() {
546         const char *res = check_get_ldk_version();
547         if (res == NULL) return NULL;
548         return str_ref_to_ts(res, strlen(res));
549 }
550 #include "version.c"
551 """
552
553         self.c_version_file = """jstring __attribute__((export_name("TS_get_lib_version_string"))) TS_get_lib_version_string() {
554         return str_ref_to_ts("<git_version_ldk_garbagecollected>", strlen("<git_version_ldk_garbagecollected>"));
555 }"""
556
557         self.hu_struct_file_prefix = """
558 import CommonBase from './CommonBase.mjs';
559 import * as bindings from '../bindings.mjs'
560
561 """
562         self.util_fn_pfx = self.hu_struct_file_prefix + "\nexport class UtilMethods extends CommonBase {\n"
563         self.util_fn_sfx = "}"
564         self.c_fn_ty_pfx = ""
565         self.file_ext = ".mts"
566         self.ptr_c_ty = "uint32_t"
567         self.ptr_native_ty = "number"
568         self.result_c_ty = "uint32_t"
569         self.ptr_arr = "ptrArray"
570         self.is_arr_some_check = ("", " != 0")
571         self.get_native_arr_len_call = ("", "->arr_len")
572
573     def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):
574         return None
575     def create_native_arr_call(self, arr_len, ty_info):
576         if ty_info.c_ty == "ptrArray":
577             assert ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array")
578         return "init_" + ty_info.c_ty + "(" + arr_len + ", __LINE__)"
579     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
580         if ty_info.c_ty == "int8_tArray":
581             return ("memcpy(" + arr_name + "->elems, ", ", " + arr_len + ")")
582         else:
583             assert False
584     def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
585         if ty_info.c_ty == "int8_tArray":
586             if copy:
587                 return "memcpy(" + dest_name + ", " + arr_name + "->elems, " + arr_len + ")"
588         if ty_info.c_ty == "ptrArray":
589             return "(void*) " + arr_name + "->elems"
590         else:
591             assert not copy
592             return arr_name + "->elems"
593     def get_native_arr_elem(self, arr_name, idxc, ty_info):
594         assert False # Only called if above is None
595     def get_native_arr_ptr_call(self, ty_info):
596         if ty_info.subty is not None:
597             return "(" + ty_info.subty.c_ty + "*)(((uint8_t*)", ") + 4)"
598         return "(" + ty_info.c_ty + "*)(((uint8_t*)", ") + 4)"
599     def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
600         return None
601     def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
602         if ty_info.c_ty == "int8_tArray":
603             return None
604         else:
605             return None
606
607     def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
608         assert elem_ty.c_ty == "uint32_t" or elem_ty.c_ty.endswith("Array")
609         return arr_name + " != null ? " + arr_name + ".map(" + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null"
610
611     def str_ref_to_native_call(self, var_name, str_len):
612         return "str_ref_to_ts(" + var_name + ", " + str_len + ")"
613     def str_ref_to_c_call(self, var_name):
614         return "str_ref_to_owned_c(" + var_name + ")"
615     def str_to_hu_conv(self, var_name):
616         return "const " + var_name + "_conv: string = bindings.decodeString(" + var_name + ");"
617     def str_from_hu_conv(self, var_name):
618         return ("bindings.encodeString(" + var_name + ")", "")
619
620     def c_fn_name_define_pfx(self, fn_name, have_args):
621         return " __attribute__((export_name(\"TS_" + fn_name + "\"))) TS_" + fn_name + "("
622
623     def init_str(self):
624         return ""
625
626     def get_java_arr_len(self, arr_name):
627         return "bindings.getArrayLength(" + arr_name + ")"
628     def get_java_arr_elem(self, elem_ty, arr_name, idx):
629         if elem_ty.c_ty == "uint32_t" or elem_ty.c_ty == "uintptr_t" or elem_ty.c_ty.endswith("Array"):
630             return "bindings.getU32ArrayElem(" + arr_name + ", " + idx + ")"
631         else:
632             assert False
633     def constr_hu_array(self, ty_info, arr_len):
634         return "new Array(" + arr_len + ").fill(null)"
635     def cleanup_converted_native_array(self, ty_info, arr_name):
636         return "bindings.freeWasmMemory(" + arr_name + ")"
637
638     def primitive_arr_from_hu(self, mapped_ty, fixed_len, arr_name):
639         inner = arr_name
640         if fixed_len is not None:
641             assert mapped_ty.c_ty == "int8_t"
642             inner = "bindings.check_arr_len(" + arr_name + ", " + fixed_len + ")"
643         if mapped_ty.c_ty.endswith("Array"):
644             return ("bindings.encodeUint32Array(" + inner + ")", "")
645         elif mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
646             return ("bindings.encodeUint8Array(" + inner + ")", "")
647         elif mapped_ty.c_ty == "uint32_t":
648             return ("bindings.encodeUint32Array(" + inner + ")", "")
649         elif mapped_ty.c_ty == "int64_t":
650             return ("bindings.encodeUint64Array(" + inner + ")", "")
651         else:
652             print(mapped_ty.c_ty)
653             assert False
654
655     def primitive_arr_to_hu(self, mapped_ty, fixed_len, arr_name, conv_name):
656         assert mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t"
657         return "const " + conv_name + ": Uint8Array = bindings.decodeUint8Array(" + arr_name + ");"
658
659     def var_decl_statement(self, ty_string, var_name, statement):
660         return "const " + var_name + ": " + ty_string + " = " + statement
661
662     def java_arr_ty_str(self, elem_ty_str):
663         return "number"
664
665     def for_n_in_range(self, n, minimum, maximum):
666         return "for (var " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
667     def for_n_in_arr(self, n, arr_name, arr_elem_ty):
668         return (arr_name + ".forEach((" + n + ": " + arr_elem_ty.java_hu_ty + ") => { ", " })")
669
670     def get_ptr(self, var):
671         return "CommonBase.get_ptr_of(" + var + ")"
672     def set_null_skip_free(self, var):
673         return "CommonBase.set_null_skip_free(" + var + ");"
674
675     def add_ref(self, holder, referent):
676         return "CommonBase.add_ref_from(" + holder + ", " + referent + ")"
677
678     def obj_defined(self, struct_names, folder):
679         with open(self.outdir + "/index.mts", 'a') as index:
680             index.write(f"export * from './{folder}/{struct_names[0]}.mjs';\n")
681         with open(self.outdir + "/imports.mts.part", 'a') as imports:
682             imports.write(f"import {{ {', '.join(struct_names)} }} from '../{folder}/{struct_names[0]}.mjs';\n")
683
684     def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
685         out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_js(int32_t ord) {\n"
686         out_c = out_c + "\tswitch (ord) {\n"
687         ord_v = 0
688
689         out_typescript_enum_fields = ""
690
691         for var, var_docs in variants:
692             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
693             ord_v = ord_v + 1
694             if var_docs is not None:
695                 var_docs_repld = var_docs.replace("\n", "\n\t")
696                 out_typescript_enum_fields += f"/**\n\t * {var_docs_repld}\n\t */\n"
697             out_typescript_enum_fields += f"\t{var},\n\t"
698         out_c = out_c + "\t}\n"
699         out_c = out_c + "\tabort();\n"
700         out_c = out_c + "}\n"
701
702         out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_js(LDK" + struct_name + " val) {\n"
703         out_c = out_c + "\tswitch (val) {\n"
704         ord_v = 0
705         for var, _ in variants:
706             out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
707             ord_v = ord_v + 1
708         out_c = out_c + "\t\tdefault: abort();\n"
709         out_c = out_c + "\t}\n"
710         out_c = out_c + "}\n"
711
712         out_typescript = f"""
713 /* @internal */
714 export enum {struct_name} {{
715         {out_typescript_enum_fields}
716 }}
717 """
718         out_typescript_enum = f"export {{ {struct_name} }} from \"../bindings.mjs\";"
719         self.obj_defined([struct_name], "enums")
720         return (out_c, out_typescript_enum, out_typescript)
721
722     def c_unitary_enum_to_native_call(self, ty_info):
723         return (ty_info.rust_obj + "_to_js(", ")")
724     def native_unitary_enum_to_c_call(self, ty_info):
725         return (ty_info.rust_obj + "_from_js(", ")")
726
727     def native_c_map_trait(self, struct_name, field_var_conversions, flattened_field_var_conversions, field_function_lines, trait_doc_comment):
728         out_typescript_bindings = ""
729         super_instantiator = ""
730         bindings_instantiator = ""
731         pointer_to_adder = ""
732         impl_constructor_arguments = ""
733         for var in flattened_field_var_conversions:
734             if isinstance(var, ConvInfo):
735                 impl_constructor_arguments += f", {var.arg_name}: {var.java_hu_ty}"
736                 super_instantiator += first_to_lower(var.arg_name) + ", "
737                 if var.from_hu_conv is not None:
738                     bindings_instantiator += ", " + var.from_hu_conv[0]
739                     if var.from_hu_conv[1] != "":
740                         pointer_to_adder += "\t\t\t" + var.from_hu_conv[1] + ";\n"
741                 else:
742                     bindings_instantiator += ", " + first_to_lower(var.arg_name)
743             else:
744                 bindings_instantiator += ", " + first_to_lower(var[1]) + ".bindings_instance"
745                 super_instantiator += first_to_lower(var[1]) + "_impl, "
746                 pointer_to_adder += "\t\timpl_holder.held.ptrs_to.push(" + first_to_lower(var[1]) + ");\n"
747                 impl_constructor_arguments += f", {first_to_lower(var[1])}_impl: {var[0].replace('LDK', '')}Interface"
748
749         super_constructor_statements = ""
750         trait_constructor_arguments = ""
751         for var in field_var_conversions:
752             if isinstance(var, ConvInfo):
753                 trait_constructor_arguments += ", " + var.arg_name
754             else:
755                 super_constructor_statements += "\t\tconst " + first_to_lower(var[1]) + " = " + var[1] + ".new_impl(" + super_instantiator + ");\n"
756                 trait_constructor_arguments += ", " + first_to_lower(var[1]) + ".bindings_instance"
757                 for suparg in var[2]:
758                     if isinstance(suparg, ConvInfo):
759                         trait_constructor_arguments += ", " + suparg.arg_name
760                     else:
761                         trait_constructor_arguments += ", " + suparg[1]
762
763         # BUILD INTERFACE METHODS
764         out_java_interface = ""
765         out_interface_implementation_overrides = ""
766         java_methods = []
767         for fn_line in field_function_lines:
768             java_method_descriptor = ""
769             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
770                 out_java_interface += "\t" + fn_line.fn_name + "("
771                 out_interface_implementation_overrides += f"\t\t\t{fn_line.fn_name} ("
772
773                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
774                     if idx >= 1:
775                         out_java_interface += ", "
776                         out_interface_implementation_overrides += ", "
777                     out_java_interface += f"{arg_conv_info.arg_name}: {arg_conv_info.java_hu_ty}"
778                     out_interface_implementation_overrides += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
779                     java_method_descriptor += arg_conv_info.java_fn_ty_arg
780                 out_java_interface += f"): {fn_line.ret_ty_info.java_hu_ty};\n"
781                 java_method_descriptor += ")" + fn_line.ret_ty_info.java_fn_ty_arg
782                 java_methods.append((fn_line.fn_name, java_method_descriptor))
783
784                 out_interface_implementation_overrides += f"): {fn_line.ret_ty_info.java_ty} {{\n"
785
786                 for arg_info in fn_line.args_ty:
787                     if arg_info.to_hu_conv is not None:
788                         out_interface_implementation_overrides += "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
789
790                 if fn_line.ret_ty_info.java_ty != "void":
791                     out_interface_implementation_overrides += "\t\t\t\tconst ret: " + fn_line.ret_ty_info.java_hu_ty + " = arg." + fn_line.fn_name + "("
792                 else:
793                     out_interface_implementation_overrides += f"\t\t\t\targ." + fn_line.fn_name + "("
794
795                 for idx, arg_info in enumerate(fn_line.args_ty):
796                     if idx != 0:
797                         out_interface_implementation_overrides += ", "
798                     if arg_info.to_hu_conv_name is not None:
799                         out_interface_implementation_overrides += arg_info.to_hu_conv_name
800                     else:
801                         out_interface_implementation_overrides += arg_info.arg_name
802
803                 out_interface_implementation_overrides += ");\n"
804                 if fn_line.ret_ty_info.java_ty != "void":
805                     if fn_line.ret_ty_info.from_hu_conv is not None:
806                         out_interface_implementation_overrides += "\t\t\t\t" + f"const result: {fn_line.ret_ty_info.java_ty} = " + fn_line.ret_ty_info.from_hu_conv[0].replace("\n", "\n\t\t\t\t") + ";\n"
807                         if fn_line.ret_ty_info.from_hu_conv[1] != "":
808                             out_interface_implementation_overrides += "\t\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held").replace("\n", "\n\t\t\t\t") + ";\n"
809                         #if fn_line.ret_ty_info.rust_obj in result_types:
810                         # XXX: We need to handle this in conversion logic so that its cross-language!
811                         # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
812                         #    out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\tret.ptr = 0;\n"
813                         out_interface_implementation_overrides += "\t\t\t\treturn result;\n"
814                     else:
815                         out_interface_implementation_overrides += "\t\t\t\treturn ret;\n"
816                 out_interface_implementation_overrides += f"\t\t\t}},\n"
817
818         out_typescript_human = f"""
819 {self.hu_struct_file_prefix}
820
821 export interface {struct_name.replace("LDK", "")}Interface {{
822 {out_java_interface}}}
823
824 class {struct_name}Holder {{
825         held: {struct_name.replace("LDK", "")};
826 }}
827
828 export class {struct_name.replace("LDK","")} extends CommonBase {{
829         /* @internal */
830         public bindings_instance?: bindings.{struct_name};
831
832         /* @internal */
833         constructor(_dummy: object, ptr: number) {{
834                 super(ptr, bindings.{struct_name.replace("LDK","")}_free);
835                 this.bindings_instance = null;
836         }}
837
838         static new_impl(arg: {struct_name.replace("LDK", "")}Interface{impl_constructor_arguments}): {struct_name.replace("LDK", "")} {{
839                 const impl_holder: {struct_name}Holder = new {struct_name}Holder();
840                 let structImplementation = {{
841 {out_interface_implementation_overrides}                }} as bindings.{struct_name};
842 {super_constructor_statements}          const ptr: number = bindings.{struct_name}_new(structImplementation{bindings_instantiator});
843
844                 impl_holder.held = new {struct_name.replace("LDK", "")}(null, ptr);
845                 impl_holder.held.bindings_instance = structImplementation;
846 {pointer_to_adder}              return impl_holder.held;
847         }}
848 """
849         self.obj_defined([struct_name.replace("LDK", ""), struct_name.replace("LDK", "") + "Interface"], "structs")
850
851         out_typescript_bindings += "/* @internal */\nexport interface " + struct_name + " {\n"
852         java_meths = []
853         for fn_line in field_function_lines:
854             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
855                 out_typescript_bindings += f"\t{fn_line.fn_name} ("
856
857                 for idx, arg_conv_info in enumerate(fn_line.args_ty):
858                     if idx >= 1:
859                         out_typescript_bindings = out_typescript_bindings + ", "
860                     out_typescript_bindings += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
861
862                 out_typescript_bindings += f"): {fn_line.ret_ty_info.java_ty};\n"
863
864         out_typescript_bindings += "}\n\n"
865
866         out_typescript_bindings += f"/* @internal */\nexport function {struct_name}_new(impl: {struct_name}"
867         for var in flattened_field_var_conversions:
868             if isinstance(var, ConvInfo):
869                 out_typescript_bindings += f", {var.arg_name}: {var.java_ty}"
870             else:
871                 out_typescript_bindings += f", {var[1]}: {var[0]}"
872
873         out_typescript_bindings += f"""): number {{
874         if(!isWasmInitialized) {{
875                 throw new Error("initializeWasm() must be awaited first!");
876         }}
877         var new_obj_idx = js_objs.length;
878         for (var i = 0; i < js_objs.length; i++) {{
879                 if (js_objs[i] == null || js_objs[i] == undefined) {{ new_obj_idx = i; break; }}
880         }}
881         js_objs[i] = new WeakRef(impl);
882         return wasm.TS_{struct_name}_new(i);
883 }}
884 """
885
886         # Now that we've written out our java code (and created java_meths), generate C
887         out_c = "typedef struct " + struct_name + "_JCalls {\n"
888         out_c += "\tatomic_size_t refcnt;\n"
889         out_c += "\tuint32_t instance_ptr;\n"
890         for var in flattened_field_var_conversions:
891             if isinstance(var, ConvInfo):
892                 # We're a regular ol' field
893                 pass
894             else:
895                 # We're a supertrait
896                 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
897         out_c = out_c + "} " + struct_name + "_JCalls;\n"
898
899         for fn_line in field_function_lines:
900             if fn_line.fn_name == "free":
901                 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
902                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
903                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
904                 out_c = out_c + "\t\tFREE(j_calls);\n"
905                 out_c = out_c + "\t}\n}\n"
906
907         for idx, fn_line in enumerate(field_function_lines):
908             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
909                 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
910                 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall("
911                 if fn_line.self_is_const:
912                     out_c = out_c + "const void* this_arg"
913                 else:
914                     out_c = out_c + "void* this_arg"
915
916                 for idx, arg in enumerate(fn_line.args_ty):
917                     out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
918
919                 out_c = out_c + ") {\n"
920                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
921
922                 for arg_info in fn_line.args_ty:
923                     if arg_info.ret_conv is not None:
924                         out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
925                         out_c = out_c + arg_info.arg_name
926                         out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
927
928                 if fn_line.ret_ty_info.c_ty.endswith("Array"):
929                     out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (" + fn_line.ret_ty_info.c_ty + ")"
930                     out_c += "js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
931                 elif fn_line.ret_ty_info.java_ty == "void":
932                     out_c = out_c + "\tjs_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
933                 elif fn_line.ret_ty_info.java_hu_ty == "string":
934                     out_c = out_c + "\tjstring ret = (jstring)js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
935                 elif not fn_line.ret_ty_info.passed_as_ptr:
936                     out_c = out_c + "\treturn js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
937                 else:
938                     out_c = out_c + "\tuint32_t ret = js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
939
940                 self.function_ptrs[self.function_ptr_counter] = (struct_name, fn_line.fn_name)
941                 self.function_ptr_counter += 1
942
943                 for idx, arg_info in enumerate(fn_line.args_ty):
944                     if arg_info.ret_conv is not None:
945                         out_c = out_c + ", (uint32_t)" + arg_info.ret_conv_name
946                     else:
947                         out_c = out_c + ", (uint32_t)" + arg_info.arg_name
948                 out_c = out_c + ");\n"
949                 if fn_line.ret_ty_info.arg_conv is not None:
950                     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"
951
952                 out_c = out_c + "}\n"
953
954         # Write out a clone function whether we need one or not, as we use them in moving to rust
955         out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n"
956         out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n"
957         out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
958         for var in field_var_conversions:
959             if not isinstance(var, ConvInfo):
960                 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n"
961         out_c = out_c + "}\n"
962
963         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (JSValue o"
964         for var in flattened_field_var_conversions:
965             if isinstance(var, ConvInfo):
966                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
967             else:
968                 out_c = out_c + ", JSValue " + var[1]
969         out_c = out_c + ") {\n"
970
971         out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
972         out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
973         out_c = out_c + "\tcalls->instance_ptr = o;\n"
974
975         for (fn_name, java_meth_descr) in java_meths:
976             if fn_name != "free" and fn_name != "cloned":
977                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
978                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
979
980         for var in flattened_field_var_conversions:
981             if isinstance(var, ConvInfo) and var.arg_conv is not None:
982                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
983         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
984         out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
985         for fn_line in field_function_lines:
986             if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
987                 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n"
988             elif fn_line.fn_name == "free":
989                 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
990             else:
991                 out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n"
992         for var in field_var_conversions:
993             if isinstance(var, ConvInfo):
994                 if var.arg_conv_name is not None:
995                     out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
996                     out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
997                 else:
998                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
999                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
1000             else:
1001                 out_c += "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1]
1002                 for suparg in var[2]:
1003                     if isinstance(suparg, ConvInfo):
1004                         out_c += ", " + suparg.arg_name
1005                     else:
1006                         out_c += ", " + suparg[1]
1007                 out_c += "),\n"
1008         out_c = out_c + "\t};\n"
1009         for var in flattened_field_var_conversions:
1010             if not isinstance(var, ConvInfo):
1011                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
1012         out_c = out_c + "\treturn ret;\n"
1013         out_c = out_c + "}\n"
1014
1015         out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "JSValue o"
1016         for var in flattened_field_var_conversions:
1017             if isinstance(var, ConvInfo):
1018                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
1019             else:
1020                 out_c = out_c + ", JSValue " + var[1]
1021         out_c = out_c + ") {\n"
1022         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
1023         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(o"
1024         for var in flattened_field_var_conversions:
1025             if isinstance(var, ConvInfo):
1026                 out_c = out_c + ", " + var.arg_name
1027             else:
1028                 out_c = out_c + ", " + var[1]
1029         out_c = out_c + ");\n"
1030         out_c = out_c + "\treturn (long)res_ptr;\n"
1031         out_c = out_c + "}\n"
1032
1033         return (out_typescript_bindings, out_typescript_human, out_c)
1034
1035     def trait_struct_inc_refcnt(self, ty_info):
1036         return ""
1037
1038     def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
1039         bindings_type = struct_name.replace("LDK", "")
1040         java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
1041
1042         out_java_enum = ""
1043         out_java = ""
1044         out_c = ""
1045
1046         out_java_enum += (self.hu_struct_file_prefix)
1047
1048         java_hu_class = ""
1049         java_hu_class += "export class " + java_hu_type + " extends CommonBase {\n"
1050         java_hu_class += "\tprotected constructor(_dummy: object, ptr: number) { super(ptr, bindings." + bindings_type + "_free); }\n"
1051         java_hu_class += "\t/* @internal */\n"
1052         java_hu_class += f"\tpublic static constr_from_ptr(ptr: number): {java_hu_type} {{\n"
1053         java_hu_class += f"\t\tconst raw_ty: number = bindings." + struct_name + "_ty_from_ptr(ptr);\n"
1054         out_c += self.c_fn_ty_pfx + "uint32_t" + self.c_fn_name_define_pfx(struct_name + "_ty_from_ptr", True) + self.ptr_c_ty + " ptr) {\n"
1055         out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n"
1056         out_c += "\tswitch(obj->tag) {\n"
1057         java_hu_class += "\t\tswitch (raw_ty) {\n"
1058         java_hu_subclasses = ""
1059
1060         out_java += "/* @internal */\nexport class " + struct_name + " {\n"
1061         out_java += "\tprotected constructor() {}\n"
1062         var_idx = 0
1063         for var in variant_list:
1064             java_hu_subclasses = java_hu_subclasses + "export class " + java_hu_type + "_" + var.var_name + " extends " + java_hu_type + " {\n"
1065             java_hu_class += f"\t\t\tcase {var_idx}: "
1066             java_hu_class += "return new " + java_hu_type + "_" + var.var_name + "(ptr);\n"
1067             out_c += f"\t\tcase {struct_name}_{var.var_name}: return {var_idx};\n"
1068             hu_conv_body = ""
1069             for idx, (field_ty, field_docs) in enumerate(var.fields):
1070                 java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
1071                 if field_ty.to_hu_conv is not None:
1072                     hu_conv_body += f"\t\tconst {field_ty.arg_name}: {field_ty.java_ty} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
1073                     hu_conv_body += f"\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
1074                     hu_conv_body += f"\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
1075                 else:
1076                     hu_conv_body += f"\t\tthis.{field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
1077             java_hu_subclasses += "\t/* @internal */\n"
1078             java_hu_subclasses += "\tpublic constructor(ptr: number) {\n\t\tsuper(null, ptr);\n"
1079             java_hu_subclasses = java_hu_subclasses + hu_conv_body
1080             java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
1081             var_idx += 1
1082         out_java += "}\n"
1083         java_hu_class += "\t\t\tdefault:\n\t\t\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t\t}\n\t}\n\n"
1084         out_java += self.fn_call_body(struct_name + "_ty_from_ptr", "uint32_t", "number", "ptr: number", "ptr")
1085         out_c += ("\t\tdefault: abort();\n")
1086         out_c += ("\t}\n}\n")
1087
1088         for var in variant_list:
1089             for idx, (field_map, _) in enumerate(var.fields):
1090                 fn_name = f"{struct_name}_{var.var_name}_get_{field_map.arg_name}"
1091                 out_c += self.c_fn_ty_pfx + field_map.c_ty + self.c_fn_name_define_pfx(fn_name, True) + self.ptr_c_ty + " ptr) {\n"
1092                 out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n"
1093                 out_c += f"\tassert(obj->tag == {struct_name}_{var.var_name});\n"
1094                 if field_map.ret_conv is not None:
1095                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
1096                     if var.tuple_variant:
1097                         out_c += "obj->" + camel_to_snake(var.var_name)
1098                     else:
1099                         out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
1100                     out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
1101                     out_c += "\treturn " + field_map.ret_conv_name + ";\n"
1102                 else:
1103                     if var.tuple_variant:
1104                         out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + ";\n"
1105                     else:
1106                         out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name + ";\n"
1107                 out_c += "}\n"
1108                 out_java += self.fn_call_body(fn_name, field_map.c_ty, field_map.java_ty, "ptr: number", "ptr")
1109         out_java_enum += java_hu_class
1110         self.struct_file_suffixes[java_hu_type] = java_hu_subclasses
1111         self.obj_defined([java_hu_type], "structs")
1112         return (out_java, out_java_enum, out_c)
1113
1114     def map_opaque_struct(self, struct_name, struct_doc_comment):
1115         implementations = ""
1116         method_header = ""
1117
1118         hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
1119         out_opaque_struct_human = f"{self.hu_struct_file_prefix}"
1120         if struct_name.startswith("LDKLocked"):
1121             out_opaque_struct_human += "/** XXX: DO NOT USE THIS - it remains locked until the GC runs (if that ever happens */"
1122         out_opaque_struct_human += f"""
1123 export class {hu_name} extends CommonBase {implementations}{{
1124         /* @internal */
1125         public constructor(_dummy: object, ptr: number) {{
1126                 super(ptr, bindings.{struct_name.replace("LDK","")}_free);
1127         }}
1128
1129 """
1130         self.obj_defined([hu_name], "structs")
1131         return out_opaque_struct_human
1132
1133     def map_tuple(self, struct_name):
1134         return self.map_opaque_struct(struct_name, "A Tuple")
1135
1136     def map_result(self, struct_name, res_map, err_map):
1137         human_ty = struct_name.replace("LDKCResult", "Result")
1138
1139         suffixes = f"export class {human_ty}_OK extends {human_ty} {{\n"
1140         if res_map.java_hu_ty != "void":
1141             suffixes += "\tpublic res: " + res_map.java_hu_ty + ";\n"
1142         suffixes += f"""
1143         /* @internal */
1144         public constructor(_dummy: object, ptr: number) {{
1145                 super(_dummy, ptr);
1146 """
1147         if res_map.java_hu_ty == "void":
1148             pass
1149         elif res_map.to_hu_conv is not None:
1150             suffixes += "\t\tconst res: " + res_map.java_ty + " = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1151             suffixes += "\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t")
1152             suffixes += "\n\t\tthis.res = " + res_map.to_hu_conv_name + ";\n"
1153         else:
1154             suffixes += "\t\tthis.res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1155         suffixes += "\t}\n}\n"
1156
1157         suffixes += f"export class {human_ty}_Err extends {human_ty} {{\n"
1158         if err_map.java_hu_ty != "void":
1159             suffixes += "\tpublic err: " + err_map.java_hu_ty + ";\n"
1160         suffixes += f"""
1161         /* @internal */
1162         public constructor(_dummy: object, ptr: number) {{
1163                 super(_dummy, ptr);
1164 """
1165         if err_map.java_hu_ty == "void":
1166             pass
1167         elif err_map.to_hu_conv is not None:
1168             suffixes += "\t\tconst err: " + err_map.java_ty + " = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1169             suffixes += "\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t")
1170             suffixes += "\n\t\tthis.err = " + err_map.to_hu_conv_name + ";\n"
1171         else:
1172             suffixes += "\t\tthis.err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1173         suffixes += "\t}\n}"
1174
1175         self.struct_file_suffixes[human_ty] = suffixes
1176         self.obj_defined([human_ty], "structs")
1177
1178         return f"""{self.hu_struct_file_prefix}
1179
1180 export class {human_ty} extends CommonBase {{
1181         protected constructor(_dummy: object, ptr: number) {{
1182                 super(ptr, bindings.{struct_name.replace("LDK","")}_free);
1183         }}
1184         /* @internal */
1185         public static constr_from_ptr(ptr: number): {human_ty} {{
1186                 if (bindings.{struct_name.replace("LDK", "")}_is_ok(ptr)) {{
1187                         return new {human_ty}_OK(null, ptr);
1188                 }} else {{
1189                         return new {human_ty}_Err(null, ptr);
1190                 }}
1191         }}
1192 """
1193
1194     def fn_call_body(self, method_name, return_c_ty, return_java_ty, method_argument_string, native_call_argument_string):
1195         has_return_value = return_c_ty != 'void'
1196         return_statement = 'return nativeResponseValue;'
1197         if not has_return_value:
1198             return_statement = '// debug statements here'
1199
1200         return f"""/* @internal */
1201 export function {method_name}({method_argument_string}): {return_java_ty} {{
1202         if(!isWasmInitialized) {{
1203                 throw new Error("initializeWasm() must be awaited first!");
1204         }}
1205         const nativeResponseValue = wasm.TS_{method_name}({native_call_argument_string});
1206         {return_statement}
1207 }}
1208 """
1209     def map_function(self, argument_types, c_call_string, method_name, meth_n, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_as_ref, args_known, type_mapping_generator, doc_comment):
1210         out_java = ""
1211         out_c = ""
1212         out_java_struct = None
1213
1214         out_java += ("\t")
1215         out_c += (self.c_fn_ty_pfx)
1216         out_c += (return_type_info.c_ty)
1217         out_java += (return_type_info.java_ty)
1218         if return_type_info.ret_conv is not None:
1219             ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
1220         out_java += (" " + method_name + "(")
1221         out_c += (" "  + self.c_fn_name_define_pfx(method_name, True))
1222
1223         method_argument_string = ""
1224         native_call_argument_string = ""
1225         for idx, arg_conv_info in enumerate(argument_types):
1226             if idx != 0:
1227                 method_argument_string += (", ")
1228                 native_call_argument_string += ', '
1229                 out_c += (", ")
1230             if arg_conv_info.c_ty != "void":
1231                 out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
1232                 method_argument_string += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
1233                 native_call_argument_string += arg_conv_info.arg_name
1234         out_java = self.fn_call_body(method_name, return_type_info.c_ty, return_type_info.java_ty, method_argument_string, native_call_argument_string)
1235
1236         out_java_struct = ""
1237         if not args_known:
1238             out_java_struct += ("\t// Skipped " + method_name + "\n")
1239         else:
1240             if not takes_self:
1241                 out_java_struct += (
1242                         "\tpublic static constructor_" + meth_n + "(")
1243             else:
1244                 out_java_struct += ("\tpublic " + meth_n + "(")
1245             for idx, arg in enumerate(argument_types):
1246                 if idx != 0:
1247                     if not takes_self or idx > 1:
1248                         out_java_struct += (", ")
1249                 elif takes_self:
1250                     continue
1251                 if arg.java_ty != "void":
1252                     if arg.arg_name in default_constructor_args:
1253                         for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
1254                             if explode_idx != 0:
1255                                 out_java_struct += (", ")
1256                             out_java_struct += arg.arg_name + "_" + explode_arg.arg_name + ": " + explode_arg.java_hu_ty
1257                     else:
1258                         out_java_struct += arg.arg_name + ": " + arg.java_hu_ty
1259
1260         out_c += (") {\n")
1261         if out_java_struct is not None:
1262             out_java_struct += "): " + return_type_info.java_hu_ty + " {\n"
1263         for info in argument_types:
1264             if info.arg_conv is not None:
1265                 out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
1266         if return_type_info.ret_conv is not None:
1267             out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
1268         elif return_type_info.c_ty != "void":
1269             out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
1270         else:
1271             out_c += ("\t")
1272         if c_call_string is None:
1273             out_c += (method_name + "(")
1274         else:
1275             out_c += (c_call_string)
1276         for idx, info in enumerate(argument_types):
1277             if info.arg_conv_name is not None:
1278                 if idx != 0:
1279                     out_c += (", ")
1280                 elif c_call_string is not None:
1281                     continue
1282                 out_c += (info.arg_conv_name)
1283         out_c += (")")
1284         if return_type_info.ret_conv is not None:
1285             out_c += (ret_conv_sfx.replace('\n', '\n\t'))
1286         else:
1287             out_c += (";")
1288         for info in argument_types:
1289             if info.arg_conv_cleanup is not None:
1290                 out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
1291         if return_type_info.ret_conv is not None:
1292             out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
1293         elif return_type_info.c_ty != "void":
1294             out_c += ("\n\treturn ret_val;")
1295         out_c += ("\n}\n\n")
1296
1297         if args_known:
1298             out_java_struct += ("\t\t")
1299             if return_type_info.java_ty != "void":
1300                 out_java_struct += "const ret: " + return_type_info.java_ty + " = "
1301             out_java_struct += ("bindings." + method_name + "(")
1302             for idx, info in enumerate(argument_types):
1303                 if idx != 0:
1304                     out_java_struct += (", ")
1305                 if idx == 0 and takes_self:
1306                     out_java_struct += ("this.ptr")
1307                 elif info.arg_name in default_constructor_args:
1308                     out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
1309                     for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1310                         if explode_idx != 0:
1311                             out_java_struct += (", ")
1312                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1313                         if explode_arg.from_hu_conv is not None:
1314                             out_java_struct += (
1315                                 explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1316                         else:
1317                             out_java_struct += (expl_arg_name)
1318                     out_java_struct += (")")
1319                 elif info.from_hu_conv is not None:
1320                     out_java_struct += (info.from_hu_conv[0])
1321                 else:
1322                     out_java_struct += (info.arg_name)
1323             out_java_struct += (");\n")
1324             if return_type_info.to_hu_conv is not None:
1325                 if not takes_self:
1326                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t").replace("this",
1327                                                                                                              return_type_info.to_hu_conv_name) + "\n")
1328                 else:
1329                     out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1330
1331             for idx, info in enumerate(argument_types):
1332                 if idx == 0 and takes_self:
1333                     pass
1334                 elif info.arg_name in default_constructor_args:
1335                     for explode_arg in default_constructor_args[info.arg_name]:
1336                         expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1337                         if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
1338                             out_java_struct += ("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name,
1339                                                                                              expl_arg_name).replace(
1340                                 "this", return_type_info.to_hu_conv_name) + ";\n")
1341                 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1342                     if not takes_self and return_type_info.to_hu_conv_name is not None:
1343                         out_java_struct += (
1344                                 "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n")
1345                     else:
1346                         out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n")
1347
1348             if return_type_info.to_hu_conv_name is not None:
1349                 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
1350             elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
1351                 out_java_struct += ("\t\treturn ret;\n")
1352             out_java_struct += ("\t}\n\n")
1353
1354         return (out_java, out_c, out_java_struct)
1355
1356     def cleanup(self):
1357         for struct in self.struct_file_suffixes:
1358             with open(self.outdir + "/structs/" + struct + self.file_ext, "a") as src:
1359                 src.write(self.struct_file_suffixes[struct])
1360
1361         with open(self.outdir + "/bindings.mts", "a") as bindings:
1362             bindings.write("""
1363
1364 js_invoke = function(obj_ptr: number, fn_id: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
1365         const weak: WeakRef<object> = js_objs[obj_ptr];
1366         if (weak == null || weak == undefined) {
1367                 console.error("Got function call on unknown/free'd JS object!");
1368                 throw new Error("Got function call on unknown/free'd JS object!");
1369         }
1370         const obj: object = weak.deref();
1371         if (obj == null || obj == undefined) {
1372                 console.error("Got function call on GC'd JS object!");
1373                 throw new Error("Got function call on GC'd JS object!");
1374         }
1375         var fn;
1376 """)
1377             bindings.write("\tswitch (fn_id) {\n")
1378             for f in self.function_ptrs:
1379                 bindings.write(f"\t\tcase {str(f)}: fn = Object.getOwnPropertyDescriptor(obj, \"{self.function_ptrs[f][1]}\"); break;\n")
1380
1381             bindings.write("""\t\tdefault:
1382                         console.error("Got unknown function call from C!");
1383                         throw new Error("Got unknown function call from C!");
1384         }
1385         if (fn == null || fn == undefined) {
1386                 console.error("Got function call on incorrect JS object!");
1387                 throw new Error("Got function call on incorrect JS object!");
1388         }
1389         return fn.value.bind(obj)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
1390 }""")