1 from bindingstypes import ConvInfo
4 def first_to_lower(string: str) -> str:
6 return first.lower() + string[1:]
14 def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
16 self.struct_file_suffixes = {}
17 self.c_type_map = dict(
18 uint8_t = ['number', 'Uint8Array'],
19 uint16_t = ['number', 'Uint16Array'],
20 uint32_t = ['number', 'Uint32Array'],
21 uint64_t = ['number'],
24 self.wasm_decoding_map = dict(
25 int8_tArray = 'decodeUint8Array'
28 self.wasm_encoding_map = dict(
29 int8_tArray = 'encodeUint8Array',
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});',
37 self.bindings_header = self.wasm_import_header(target) + """
38 export class VecOrSliceDef {
39 public dataptr: number;
40 public datalen: number;
41 public stride: number;
42 public constructor(dataptr: number, datalen: number, stride: number) {
43 this.dataptr = dataptr;
44 this.datalen = datalen;
52 System.loadLibrary(\"lightningjni\");
53 init(java.lang.Enum.class, VecOrSliceDef.class);
57 static native void init(java.lang.Class c, java.lang.Class slicedef);
58 static native void init_class_cache();
60 public static native boolean deref_bool(long ptr);
61 public static native long deref_long(long ptr);
62 public static native void free_heap_ptr(long ptr);
63 public static native byte[] read_bytes(long ptr, long len);
64 public static native byte[] get_u8_slice_bytes(long slice_ptr);
65 public static native long bytes_to_u8_vec(byte[] bytes);
66 public static native long new_txpointer_copy_data(byte[] txdata);
67 public static native void txpointer_free(long ptr);
68 public static native byte[] txpointer_get_buffer(long ptr);
69 public static native long vec_slice_len(long vec);
70 public static native long new_empty_slice_vec();
75 self.bindings_version_file = ""
77 self.bindings_footer = ""
82 self.common_base = """
83 function freer(f: () => void) { f() }
84 const finalizer = new FinalizationRegistry(freer);
85 function get_freeer(ptr: number, free_fn: (number) => void) {
91 export default class CommonBase {
92 protected ptr: number;
93 protected ptrs_to: object[] = [];
94 protected constructor(ptr: number, free_fn: (ptr: number) => void) {
96 if (Number.isFinite(ptr) && ptr != 0){
97 finalizer.register(this, get_freeer(ptr, free_fn));
100 public _test_only_get_ptr(): number { return this.ptr; }
104 self.c_file_pfx = """#include "js-wasm.h"
105 #include <stdatomic.h>
106 #include <lightning.h>
108 // These should be provided...somehow...
109 void *memset(void *s, int c, size_t n);
110 void *memcpy(void *dest, const void *src, size_t n);
111 int memcmp(const void *s1, const void *s2, size_t n);
113 void __attribute__((noreturn)) abort(void);
114 static inline void assert(bool expression) {
115 if (!expression) { abort(); }
120 self.c_file_pfx = self.c_file_pfx + """
121 void *malloc(size_t size);
122 void free(void *ptr);
124 #define MALLOC(a, _) malloc(a)
125 #define FREE(p) if ((unsigned long)(p) > 4096) { free(p); }
126 #define DO_ASSERT(a) (void)(a)
128 #define CHECK_ACCESS(p)
129 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
132 self.c_file_pfx = self.c_file_pfx + """
133 // Always run a, then assert it is true:
134 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
135 // Assert a is true or do nothing
136 #define CHECK(a) DO_ASSERT(a)
138 // Running a leak check across all the allocations and frees of the JDK is a mess,
139 // so instead we implement our own naive leak checker here, relying on the -wrap
140 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
141 // and free'd in Rust or C across the generated bindings shared library.
144 typedef struct allocation {
145 struct allocation* next;
147 const char* struct_name;
149 static allocation* allocation_ll = NULL;
151 void* __real_malloc(size_t len);
152 void* __real_calloc(size_t nmemb, size_t len);
153 static void new_allocation(void* res, const char* struct_name) {
154 allocation* new_alloc = __real_malloc(sizeof(allocation));
155 new_alloc->ptr = res;
156 new_alloc->struct_name = struct_name;
157 new_alloc->next = allocation_ll;
158 allocation_ll = new_alloc;
160 static void* MALLOC(size_t len, const char* struct_name) {
161 void* res = __real_malloc(len);
162 new_allocation(res, struct_name);
165 void __real_free(void* ptr);
166 static void alloc_freed(void* ptr) {
167 allocation* p = NULL;
168 allocation* it = allocation_ll;
169 while (it->ptr != ptr) {
170 p = it; it = it->next;
172 //XXX: fprintf(stderr, "Tried to free unknown pointer %p\\n", ptr);
173 return; // addrsan should catch malloc-unknown and print more info than we have
176 if (p) { p->next = it->next; } else { allocation_ll = it->next; }
177 DO_ASSERT(it->ptr == ptr);
180 static void FREE(void* ptr) {
181 if ((unsigned long)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys
186 static void CHECK_ACCESS(const void* ptr) {
187 allocation* it = allocation_ll;
188 while (it->ptr != ptr) {
191 return; // addrsan should catch malloc-unknown and print more info than we have
195 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
196 if (v.is_owned && v.inner != NULL) { \\
197 const void *p = __unmangle_inner_ptr(v.inner); \\
203 void* __wrap_malloc(size_t len) {
204 void* res = __real_malloc(len);
205 new_allocation(res, "malloc call");
208 void* __wrap_calloc(size_t nmemb, size_t len) {
209 void* res = __real_calloc(nmemb, len);
210 new_allocation(res, "calloc call");
213 void __wrap_free(void* ptr) {
214 if (ptr == NULL) return;
219 void* __real_realloc(void* ptr, size_t newlen);
220 void* __wrap_realloc(void* ptr, size_t len) {
221 if (ptr != NULL) alloc_freed(ptr);
222 void* res = __real_realloc(ptr, len);
223 new_allocation(res, "realloc call");
226 void __wrap_reallocarray(void* ptr, size_t new_sz) {
227 // Rust doesn't seem to use reallocarray currently
231 void __attribute__((destructor)) check_leaks() {
232 for (allocation* a = allocation_ll; a != NULL; a = a->next) {
233 //XXX: fprintf(stderr, "%s %p remains\\n", a->struct_name, a->ptr);
235 DO_ASSERT(allocation_ll == NULL);
238 self.c_file_pfx = self.c_file_pfx + """
239 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
240 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
241 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
242 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
244 _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits");
246 #define DECL_ARR_TYPE(ty, name) \\
247 struct name##array { \\
251 typedef struct name##array * name##Array; \\
252 static inline name##Array init_##name##Array(size_t arr_len) { \\
253 name##Array arr = (name##Array)MALLOC(arr_len * sizeof(ty) + sizeof(uint32_t), "##name array init"); \\
254 arr->arr_len = arr_len; \\
258 DECL_ARR_TYPE(int64_t, int64_t);
259 DECL_ARR_TYPE(int8_t, int8_t);
260 DECL_ARR_TYPE(uint32_t, uint32_t);
261 DECL_ARR_TYPE(void*, ptr);
262 DECL_ARR_TYPE(char, char);
263 typedef charArray jstring;
265 static inline jstring str_ref_to_ts(const char* chars, size_t len) {
266 charArray arr = init_charArray(len);
267 memcpy(arr->elems, chars, len);
270 static inline LDKStr str_ref_to_owned_c(const jstring str) {
271 char* newchars = MALLOC(str->arr_len + 1, "String chars");
272 memcpy(newchars, str->elems, str->arr_len);
273 newchars[str->arr_len] = 0;
277 .chars_is_owned = true
282 typedef bool jboolean;
284 uint32_t __attribute__((visibility("default"))) TS_malloc(uint32_t size) {
285 return (uint32_t)MALLOC(size, "JS-Called malloc");
287 void __attribute__((visibility("default"))) TS_free(uint32_t ptr) {
292 self.c_version_file = ""
294 self.hu_struct_file_prefix = """
295 import CommonBase from './CommonBase.mjs';
296 import * as bindings from '../bindings.mjs'
299 self.c_fn_ty_pfx = ""
300 self.file_ext = ".mts"
301 self.ptr_c_ty = "uint32_t"
302 self.ptr_native_ty = "number"
303 self.result_c_ty = "uint32_t"
304 self.ptr_arr = "ptrArray"
305 self.is_arr_some_check = ("", " != 0")
306 self.get_native_arr_len_call = ("", "->arr_len")
308 def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):
310 def create_native_arr_call(self, arr_len, ty_info):
311 if ty_info.c_ty == "ptrArray":
312 assert ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array")
313 return "init_" + ty_info.c_ty + "(" + arr_len + ")"
314 def set_native_arr_contents(self, arr_name, arr_len, ty_info):
315 if ty_info.c_ty == "int8_tArray":
316 return ("memcpy(" + arr_name + "->elems, ", ", " + arr_len + ")")
319 def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
320 if ty_info.c_ty == "int8_tArray":
322 return "memcpy(" + dest_name + ", " + arr_name + "->elems, " + arr_len + ")"
323 if ty_info.c_ty == "ptrArray":
324 return "(void*) " + arr_name + "->elems"
327 return arr_name + "->elems"
328 def get_native_arr_elem(self, arr_name, idxc, ty_info):
329 assert False # Only called if above is None
330 def get_native_arr_ptr_call(self, ty_info):
331 if ty_info.subty is not None:
332 return "(" + ty_info.subty.c_ty + "*)(", " + 4)"
333 return "(" + ty_info.c_ty + "*)(", " + 4)"
334 def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
336 def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
337 if ty_info.c_ty == "int8_tArray":
342 def str_ref_to_native_call(self, var_name, str_len):
343 return "str_ref_to_ts(" + var_name + ", " + str_len + ")"
344 def str_ref_to_c_call(self, var_name):
345 return "str_ref_to_owned_c(" + var_name + ")"
347 def c_fn_name_define_pfx(self, fn_name, have_args):
348 return " __attribute__((visibility(\"default\"))) TS_" + fn_name + "("
350 def wasm_import_header(self, target):
352 const memory = new WebAssembly.Memory({initial: 256});
354 const imports: any = {};
357 imports.env.memoryBase = 0;
358 imports.env.memory = memory;
359 imports.env.tableBase = 0;
360 imports.env.table = new WebAssembly.Table({initial: 4, element: 'anyfunc'});
362 imports.env["abort"] = function () {
363 console.error("ABORT");
365 imports.env["js_invoke_function"] = function(fn: number, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: number, arg7: number, arg8: number, arg9: number, arg10: number) {
366 console.log('function called from wasm:', fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
368 imports.env["js_free_function_ptr"] = function(fn: number) {
369 console.log("function ptr free'd from wasm:", fn);
372 imports.wasi_snapshot_preview1 = {
374 console.log("ABORT");
376 "random_get" : () => {
377 console.log("RAND GET");
379 "environ_sizes_get" : () => {
380 console.log("wasi_snapshot_preview1:environ_sizes_get");
382 "proc_exit" : () => {
383 console.log("wasi_snapshot_preview1:proc_exit");
385 "environ_get" : () => {
386 console.log("wasi_snapshot_preview1:environ_get");
391 let isWasmInitialized: boolean = false;
394 if target == Target.NODEJS:
395 res += """import * as fs from 'fs';
396 export async function initializeWasm(path) {
397 const source = fs.readFileSync(path);
398 const { instance: wasmInstance } = await WebAssembly.instantiate(source, imports);
399 wasm = wasmInstance.exports;
400 isWasmInitialized = true;
405 export async function initializeWasm(uri) {
406 const stream = fetch(uri);
407 const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
408 wasm = wasmInstance.exports;
409 isWasmInitialized = true;
419 const nextMultipleOfFour = (value: number) => {
420 return Math.ceil(value / 4) * 4;
423 const encodeUint8Array = (inputArray) => {
424 const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
425 const arrayLengthView = new Uint32Array(memory.buffer, cArrayPointer, 1);
426 arrayLengthView[0] = inputArray.length;
427 const arrayMemoryView = new Uint8Array(memory.buffer, cArrayPointer + 4, inputArray.length);
428 arrayMemoryView.set(inputArray);
429 return cArrayPointer;
432 const encodeUint32Array = (inputArray) => {
433 const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
434 const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length);
435 arrayMemoryView.set(inputArray, 1);
436 arrayMemoryView[0] = inputArray.length;
437 return cArrayPointer;
440 const getArrayLength = (arrayPointer) => {
441 const arraySizeViewer = new Uint32Array(
442 memory.buffer, // value
443 arrayPointer, // offset
446 return arraySizeViewer[0];
448 const decodeUint8Array = (arrayPointer, free = true) => {
449 const arraySize = getArrayLength(arrayPointer);
450 const actualArrayViewer = new Uint8Array(
451 memory.buffer, // value
452 arrayPointer + 4, // offset (ignoring length bytes)
453 arraySize // uint8 count
455 // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
456 // will free the underlying memory when it becomes unreachable instead of copying here.
457 const actualArray = actualArrayViewer.slice(0, arraySize);
459 wasm.TS_free(arrayPointer);
463 const decodeUint32Array = (arrayPointer, free = true) => {
464 const arraySize = getArrayLength(arrayPointer);
465 const actualArrayViewer = new Uint32Array(
466 memory.buffer, // value
467 arrayPointer + 4, // offset (ignoring length bytes)
468 arraySize // uint32 count
470 // Clone the contents, TODO: In the future we should wrap the Viewer in a class that
471 // will free the underlying memory when it becomes unreachable instead of copying here.
472 const actualArray = actualArrayViewer.slice(0, arraySize);
474 wasm.TS_free(arrayPointer);
479 const encodeString = (string) => {
480 // make malloc count divisible by 4
481 const memoryNeed = nextMultipleOfFour(string.length + 1);
482 const stringPointer = wasm.TS_malloc(memoryNeed);
483 const stringMemoryView = new Uint8Array(
484 memory.buffer, // value
485 stringPointer, // offset
486 string.length + 1 // length
488 for (let i = 0; i < string.length; i++) {
489 stringMemoryView[i] = string.charCodeAt(i);
491 stringMemoryView[string.length] = 0;
492 return stringPointer;
495 const decodeString = (stringPointer, free = true) => {
496 const memoryView = new Uint8Array(memory.buffer, stringPointer);
500 while (memoryView[cursor] !== 0) {
501 result += String.fromCharCode(memoryView[cursor]);
506 wasm.wasm_free(stringPointer);
516 def var_decl_statement(self, ty_string, var_name, statement):
517 return "const " + var_name + ": " + ty_string + " = " + statement
519 def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
520 out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_js(int32_t ord) {\n"
521 out_c = out_c + "\tswitch (ord) {\n"
524 out_typescript_enum_fields = ""
526 for var, var_docs in variants:
527 out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
529 if var_docs is not None:
530 out_typescript_enum_fields += f"/**\n * {var_docs}\n */\n"
531 out_typescript_enum_fields += f"{var},\n\t\t\t\t"
532 out_c = out_c + "\t}\n"
533 out_c = out_c + "\tabort();\n"
534 out_c = out_c + "}\n"
536 out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_js(LDK" + struct_name + " val) {\n"
537 out_c = out_c + "\tswitch (val) {\n"
539 for var, _ in variants:
540 out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
542 out_c = out_c + "\t\tdefault: abort();\n"
543 out_c = out_c + "\t}\n"
544 out_c = out_c + "}\n"
546 out_typescript = f"""
547 export enum {struct_name} {{
548 {out_typescript_enum_fields}
551 out_typescript_enum = f"export {{ {struct_name} }} from \"../bindings.mjs\";"
552 return (out_c, out_typescript_enum, out_typescript)
554 def c_unitary_enum_to_native_call(self, ty_info):
555 return (ty_info.rust_obj + "_to_js(", ")")
556 def native_unitary_enum_to_c_call(self, ty_info):
557 return (ty_info.rust_obj + "_from_js(", ")")
559 def c_complex_enum_pass_ty(self, struct_name):
562 def c_constr_native_complex_enum(self, struct_name, variant, c_params):
563 ret = "0 /* " + struct_name + " - " + variant + " */"
564 for param in c_params:
565 ret = ret + "; (void) " + param
568 def native_c_map_trait(self, struct_name, field_var_conversions, flattened_field_var_conversions, field_function_lines, trait_doc_comment):
569 out_typescript_bindings = "\n\n\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: START\n\n"
571 constructor_arguments = ""
572 super_instantiator = ""
573 pointer_to_adder = ""
574 impl_constructor_arguments = ""
575 for var in flattened_field_var_conversions:
576 if isinstance(var, ConvInfo):
577 constructor_arguments += f", {first_to_lower(var.arg_name)}?: {var.java_hu_ty}"
578 impl_constructor_arguments += f", {var.arg_name}: {var.java_hu_ty}"
579 if var.from_hu_conv is not None:
580 super_instantiator += ", " + var.from_hu_conv[0]
581 if var.from_hu_conv[1] != "":
582 pointer_to_adder += var.from_hu_conv[1] + ";\n"
584 super_instantiator += ", " + first_to_lower(var.arg_name)
586 constructor_arguments += f", {first_to_lower(var[1])}?: bindings.{var[0]}"
587 super_instantiator += ", " + first_to_lower(var[1])
588 pointer_to_adder += "this.ptrs_to.push(" + first_to_lower(var[1]) + ");\n"
589 impl_constructor_arguments += f", {first_to_lower(var[1])}_impl: {var[0].replace('LDK', '')}.{var[0].replace('LDK', '')}Interface"
591 # BUILD INTERFACE METHODS
592 out_java_interface = ""
593 out_interface_implementation_overrides = ""
595 for fn_line in field_function_lines:
596 java_method_descriptor = ""
597 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
598 out_java_interface += fn_line.fn_name + "("
599 out_interface_implementation_overrides += f"{fn_line.fn_name} ("
601 for idx, arg_conv_info in enumerate(fn_line.args_ty):
603 out_java_interface += ", "
604 out_interface_implementation_overrides += ", "
605 out_java_interface += f"{arg_conv_info.arg_name}: {arg_conv_info.java_hu_ty}"
606 out_interface_implementation_overrides += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
607 java_method_descriptor += arg_conv_info.java_fn_ty_arg
608 out_java_interface += f"): {fn_line.ret_ty_info.java_hu_ty};\n\t\t\t\t"
609 java_method_descriptor += ")" + fn_line.ret_ty_info.java_fn_ty_arg
610 java_methods.append((fn_line.fn_name, java_method_descriptor))
612 out_interface_implementation_overrides += f"): {fn_line.ret_ty_info.java_ty} {{\n"
614 interface_method_override_inset = "\t\t\t\t\t\t"
615 interface_implementation_inset = "\t\t\t\t\t\t\t"
616 for arg_info in fn_line.args_ty:
617 if arg_info.to_hu_conv is not None:
618 out_interface_implementation_overrides += interface_implementation_inset + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
620 if fn_line.ret_ty_info.java_ty != "void":
621 out_interface_implementation_overrides += interface_implementation_inset + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_line.fn_name + "("
623 out_interface_implementation_overrides += f"{interface_implementation_inset}arg." + fn_line.fn_name + "("
625 for idx, arg_info in enumerate(fn_line.args_ty):
627 out_interface_implementation_overrides += ", "
628 if arg_info.to_hu_conv_name is not None:
629 out_interface_implementation_overrides += arg_info.to_hu_conv_name
631 out_interface_implementation_overrides += arg_info.arg_name
633 out_interface_implementation_overrides += ");\n"
634 if fn_line.ret_ty_info.java_ty != "void":
635 if fn_line.ret_ty_info.from_hu_conv is not None:
636 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"
637 if fn_line.ret_ty_info.from_hu_conv[1] != "":
638 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"
639 #if fn_line.ret_ty_info.rust_obj in result_types:
640 # XXX: We need to handle this in conversion logic so that its cross-language!
641 # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
642 # out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\tret.ptr = 0;\n"
643 out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\treturn result;\n"
645 out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\treturn ret;\n"
646 out_interface_implementation_overrides += f"{interface_method_override_inset}}},\n\n{interface_method_override_inset}"
648 trait_constructor_arguments = ""
649 for var in field_var_conversions:
650 if isinstance(var, ConvInfo):
651 trait_constructor_arguments += ", " + var.arg_name
653 trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
654 for suparg in var[2]:
655 if isinstance(suparg, ConvInfo):
656 trait_constructor_arguments += ", " + suparg.arg_name
658 trait_constructor_arguments += ", " + suparg[1]
659 trait_constructor_arguments += ").bindings_instance"
660 for suparg in var[2]:
661 if isinstance(suparg, ConvInfo):
662 trait_constructor_arguments += ", " + suparg.arg_name
664 trait_constructor_arguments += ", " + suparg[1]
666 out_typescript_human = f"""
667 {self.hu_struct_file_prefix}
669 {struct_name.replace("LDK","")} extends CommonBase {{
671 bindings_instance?: bindings.{struct_name};
673 constructor(ptr?: number, arg?: bindings.{struct_name}{constructor_arguments}) {{
674 if (Number.isFinite(ptr)) {{
675 super(ptr, bindings.{struct_name.replace("LDK","")}_free);
676 this.bindings_instance = null;
678 // TODO: private constructor instantiation
679 super(bindings.{struct_name}_new(arg{super_instantiator}));
680 this.ptrs_to.push(arg);
685 static new_impl(arg: {struct_name.replace("LDK", "")}Interface{impl_constructor_arguments}): {struct_name.replace("LDK", "")} {{
686 const impl_holder: {struct_name}Holder = new {struct_name}Holder();
687 let structImplementation = <bindings.{struct_name}>{{
688 // todo: in-line interface filling
689 {out_interface_implementation_overrides}
691 impl_holder.held = new {struct_name.replace("LDK", "")} (null, structImplementation{trait_constructor_arguments});
694 export interface {struct_name.replace("LDK", "")}Interface {{
698 class {struct_name}Holder {{
699 held: {struct_name.replace("LDK", "")};
703 out_typescript_bindings += "\t\texport interface " + struct_name + " {\n"
705 for fn_line in field_function_lines:
706 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
707 out_typescript_bindings += f"\t\t\t{fn_line.fn_name} ("
709 for idx, arg_conv_info in enumerate(fn_line.args_ty):
711 out_typescript_bindings = out_typescript_bindings + ", "
712 out_typescript_bindings += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
714 out_typescript_bindings += f"): {fn_line.ret_ty_info.java_ty};\n"
716 out_typescript_bindings = out_typescript_bindings + "\t\t}\n\n"
718 out_typescript_bindings += f"\t\texport function {struct_name}_new(impl: {struct_name}"
719 for var in flattened_field_var_conversions:
720 if isinstance(var, ConvInfo):
721 out_typescript_bindings += f", {var.arg_name}: {var.java_ty}"
723 out_typescript_bindings += f", {var[1]}: {var[0]}"
725 out_typescript_bindings += f"""): number {{
726 throw new Error('unimplemented'); // TODO: bind to WASM
730 out_typescript_bindings += '\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: END\n\n\n'
732 # Now that we've written out our java code (and created java_meths), generate C
733 out_c = "typedef struct " + struct_name + "_JCalls {\n"
734 out_c = out_c + "\tatomic_size_t refcnt;\n"
735 for var in flattened_field_var_conversions:
736 if isinstance(var, ConvInfo):
737 # We're a regular ol' field
741 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
742 for fn in field_function_lines:
743 if fn.fn_name != "free" and fn.fn_name != "cloned":
744 out_c = out_c + "\tuint32_t " + fn.fn_name + "_meth;\n"
745 out_c = out_c + "} " + struct_name + "_JCalls;\n"
747 for fn_line in field_function_lines:
748 if fn_line.fn_name == "free":
749 out_c = out_c + "static void " + struct_name + "_JCalls_free(void* this_arg) {\n"
750 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
751 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
752 for fn in field_function_lines:
753 if fn.fn_name != "free" and fn.fn_name != "cloned":
754 out_c = out_c + "\t\tjs_free_function_ptr(j_calls->" + fn.fn_name + "_meth);\n"
755 out_c = out_c + "\t\tFREE(j_calls);\n"
756 out_c = out_c + "\t}\n}\n"
758 for idx, fn_line in enumerate(field_function_lines):
759 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
760 assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == ""
761 out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall("
762 if fn_line.self_is_const:
763 out_c = out_c + "const void* this_arg"
765 out_c = out_c + "void* this_arg"
767 for idx, arg in enumerate(fn_line.args_ty):
768 out_c = out_c + ", " + arg.ty_info.get_full_rust_ty()[0] + " " + arg.arg_name + arg.ty_info.get_full_rust_ty()[1]
770 out_c = out_c + ") {\n"
771 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
773 for arg_info in fn_line.args_ty:
774 if arg_info.ret_conv is not None:
775 out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
776 out_c = out_c + arg_info.arg_name
777 out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
779 if fn_line.ret_ty_info.c_ty.endswith("Array"):
780 out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (" + fn_line.ret_ty_info.c_ty + ")"
781 out_c += "js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth"
782 elif fn_line.ret_ty_info.java_ty == "void":
783 out_c = out_c + "\tjs_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth"
784 elif fn_line.ret_ty_info.java_ty == "String":
785 out_c = out_c + "\tjstring ret = (jstring)js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth"
786 elif not fn_line.ret_ty_info.passed_as_ptr:
787 out_c = out_c + "\treturn js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth"
789 out_c = out_c + "\tuint32_t ret = js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->" + fn_line.fn_name + "_meth"
791 for idx, arg_info in enumerate(fn_line.args_ty):
792 if arg_info.ret_conv is not None:
793 out_c = out_c + ", (uint32_t)" + arg_info.ret_conv_name
795 out_c = out_c + ", (uint32_t)" + arg_info.arg_name
796 out_c = out_c + ");\n"
797 if fn_line.ret_ty_info.arg_conv is not None:
798 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"
800 out_c = out_c + "}\n"
802 # Write out a clone function whether we need one or not, as we use them in moving to rust
803 out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n"
804 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n"
805 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
806 for var in field_var_conversions:
807 if not isinstance(var, ConvInfo):
808 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n"
809 out_c = out_c + "}\n"
811 out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (/*TODO: JS Object Reference */void* o"
812 for var in flattened_field_var_conversions:
813 if isinstance(var, ConvInfo):
814 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
816 out_c = out_c + ", /*TODO: JS Object Reference */void* " + var[1]
817 out_c = out_c + ") {\n"
819 out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
820 out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
821 out_c = out_c + "\t//TODO: Assign calls->o from o\n"
823 for (fn_name, java_meth_descr) in java_meths:
824 if fn_name != "free" and fn_name != "cloned":
825 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
826 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
828 for var in flattened_field_var_conversions:
829 if isinstance(var, ConvInfo) and var.arg_conv is not None:
830 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
831 out_c = out_c + "\n\t" + struct_name + " ret = {\n"
832 out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
833 for fn_line in field_function_lines:
834 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
835 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n"
836 elif fn_line.fn_name == "free":
837 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
839 out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n"
840 for var in field_var_conversions:
841 if isinstance(var, ConvInfo):
842 if var.arg_conv_name is not None:
843 out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
844 out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
846 out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
847 out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
849 out_c += "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1]
850 for suparg in var[2]:
851 if isinstance(suparg, ConvInfo):
852 out_c += ", " + suparg.arg_name
854 out_c += ", " + suparg[1]
856 out_c = out_c + "\t};\n"
857 for var in flattened_field_var_conversions:
858 if not isinstance(var, ConvInfo):
859 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
860 out_c = out_c + "\treturn ret;\n"
861 out_c = out_c + "}\n"
863 out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "/*TODO: JS Object Reference */void* o"
864 for var in flattened_field_var_conversions:
865 if isinstance(var, ConvInfo):
866 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
868 out_c = out_c + ", /*TODO: JS Object Reference */ void* " + var[1]
869 out_c = out_c + ") {\n"
870 out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
871 out_c = out_c + "\t*res_ptr = " + struct_name + "_init(o"
872 for var in flattened_field_var_conversions:
873 if isinstance(var, ConvInfo):
874 out_c = out_c + ", " + var.arg_name
876 out_c = out_c + ", " + var[1]
877 out_c = out_c + ");\n"
878 out_c = out_c + "\treturn (long)res_ptr;\n"
879 out_c = out_c + "}\n"
881 return (out_typescript_bindings, out_typescript_human, out_c)
883 def trait_struct_inc_refcnt(self, ty_info):
886 def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
887 bindings_type = struct_name.replace("LDK", "")
888 java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
894 out_java_enum += (self.hu_struct_file_prefix)
897 java_hu_class += "export class " + java_hu_type + " extends CommonBase {\n"
898 java_hu_class += "\tprotected constructor(_dummy: object, ptr: number) { super(ptr, bindings." + bindings_type + "_free); }\n"
899 java_hu_class += "\t/* @internal */\n"
900 java_hu_class += f"\tpublic static constr_from_ptr(ptr: number): {java_hu_type} {{\n"
901 java_hu_class += f"\t\tconst raw_val: bindings.{struct_name} = bindings." + struct_name + "_ref_from_ptr(ptr);\n"
902 java_hu_subclasses = ""
904 out_java += "\texport class " + struct_name + " {\n"
905 out_java += "\t\tprotected constructor() {}\n"
907 for var in variant_list:
908 java_subclasses += "\texport class " + struct_name + "_" + var.var_name + " extends " + struct_name + " {\n"
909 java_hu_subclasses = java_hu_subclasses + "export class " + java_hu_type + "_" + var.var_name + " extends " + java_hu_type + " {\n"
910 java_hu_class += "\t\tif (raw_val instanceof bindings." + struct_name + "_" + var.var_name + ") {\n"
911 java_hu_class += "\t\t\treturn new " + java_hu_type + "_" + var.var_name + "(ptr, raw_val);\n"
912 init_meth_params = ""
914 for idx, (field_ty, field_docs) in enumerate(var.fields):
915 java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
916 if field_ty.to_hu_conv is not None:
917 hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n"
918 hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
919 hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
921 hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
923 init_meth_params += ", "
924 init_meth_params += "public " + field_ty.arg_name + ": " + field_ty.java_ty
925 java_subclasses += "\t\tconstructor(" + init_meth_params + ") { super(); }\n"
926 java_subclasses += "\t}\n"
927 java_hu_class += "\t\t}\n"
928 java_hu_subclasses += "\t/* @internal */\n"
929 java_hu_subclasses += "\tpublic constructor(ptr: number, obj: bindings." + struct_name + "_" + var.var_name + ") {\n\t\tsuper(null, ptr);\n"
930 java_hu_subclasses = java_hu_subclasses + hu_conv_body
931 java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
932 out_java += ("\t}\n")
933 out_java += java_subclasses
934 java_hu_class += "\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t}\n\n"
935 out_java += self.fn_call_body(struct_name + "_ref_from_ptr", "uint32_t", "number", "ptr: number", "ptr")
937 out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n")
938 out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n")
939 out_c += ("\tswitch(obj->tag) {\n")
940 for var in variant_list:
941 out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
943 for idx, (field_map, _) in enumerate(var.fields):
944 if field_map.ret_conv is not None:
945 out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
946 if var.tuple_variant:
947 out_c += "obj->" + camel_to_snake(var.var_name)
949 out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
950 out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
951 c_params.append(field_map.ret_conv_name)
953 if var.tuple_variant:
954 c_params.append("obj->" + camel_to_snake(var.var_name))
956 c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
957 out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
959 out_c += ("\t\tdefault: abort();\n")
960 out_c += ("\t}\n}\n")
961 out_java_enum += java_hu_class
962 self.struct_file_suffixes[java_hu_type] = java_hu_subclasses
963 return (out_java, out_java_enum, out_c)
965 def map_opaque_struct(self, struct_name, struct_doc_comment):
968 if struct_name.startswith("LDKLocked"):
969 return "NOT IMPLEMENTED"
971 hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
972 out_opaque_struct_human = f"""{self.hu_struct_file_prefix}
974 export class {hu_name} extends CommonBase {implementations}{{
976 public constructor(_dummy: object, ptr: number) {{
977 super(ptr, bindings.{struct_name.replace("LDK","")}_free);
981 return out_opaque_struct_human
983 def map_tuple(self, struct_name):
984 return self.map_opaque_struct(struct_name, "A Tuple")
986 def fn_call_body(self, method_name, return_c_ty, return_java_ty, method_argument_string, native_call_argument_string):
987 has_return_value = return_c_ty != 'void'
988 needs_decoding = return_c_ty in self.wasm_decoding_map
989 return_statement = 'return nativeResponseValue;'
990 if not has_return_value:
991 return_statement = '// debug statements here'
993 converter = self.wasm_decoding_map[return_c_ty]
994 return_statement = f"return {converter}(nativeResponseValue);"
996 return f"""\texport function {method_name}({method_argument_string}): {return_java_ty} {{
997 if(!isWasmInitialized) {{
998 throw new Error("initializeWasm() must be awaited first!");
1000 const nativeResponseValue = wasm.TS_{method_name}({native_call_argument_string});
1004 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):
1007 out_java_struct = None
1010 out_c += (self.c_fn_ty_pfx)
1011 out_c += (return_type_info.c_ty)
1012 out_java += (return_type_info.java_ty)
1013 if return_type_info.ret_conv is not None:
1014 ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
1015 out_java += (" " + method_name + "(")
1016 out_c += (" " + self.c_fn_name_define_pfx(method_name, True))
1018 method_argument_string = ""
1019 native_call_argument_string = ""
1020 for idx, arg_conv_info in enumerate(argument_types):
1022 method_argument_string += (", ")
1023 native_call_argument_string += ', '
1025 if arg_conv_info.c_ty != "void":
1026 out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
1027 needs_encoding = arg_conv_info.c_ty in self.wasm_encoding_map
1028 native_argument = arg_conv_info.arg_name
1030 converter = self.wasm_encoding_map[arg_conv_info.c_ty]
1031 native_argument = f"{converter}({arg_conv_info.arg_name})"
1032 method_argument_string += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}"
1033 native_call_argument_string += native_argument
1034 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)
1036 out_java_struct = ""
1038 out_java_struct += ("\t// Skipped " + method_name + "\n")
1041 out_java_struct += (
1042 "\tpublic static constructor_" + meth_n + "(")
1044 out_java_struct += ("\tpublic " + meth_n + "(")
1045 for idx, arg in enumerate(argument_types):
1047 if not takes_self or idx > 1:
1048 out_java_struct += (", ")
1051 if arg.java_ty != "void":
1052 if arg.arg_name in default_constructor_args:
1053 for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
1054 if explode_idx != 0:
1055 out_java_struct += (", ")
1056 out_java_struct += arg.arg_name + "_" + explode_arg.arg_name + ": " + explode_arg.java_hu_ty
1058 out_java_struct += arg.arg_name + ": " + arg.java_hu_ty
1061 if out_java_struct is not None:
1062 out_java_struct += "): " + return_type_info.java_hu_ty + " {\n"
1063 for info in argument_types:
1064 if info.arg_conv is not None:
1065 out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
1066 if return_type_info.ret_conv is not None:
1067 out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
1068 elif return_type_info.c_ty != "void":
1069 out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
1072 if c_call_string is None:
1073 out_c += (method_name + "(")
1075 out_c += (c_call_string)
1076 for idx, info in enumerate(argument_types):
1077 if info.arg_conv_name is not None:
1080 elif c_call_string is not None:
1082 out_c += (info.arg_conv_name)
1084 if return_type_info.ret_conv is not None:
1085 out_c += (ret_conv_sfx.replace('\n', '\n\t'))
1088 for info in argument_types:
1089 if info.arg_conv_cleanup is not None:
1090 out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
1091 if return_type_info.ret_conv is not None:
1092 out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
1093 elif return_type_info.c_ty != "void":
1094 out_c += ("\n\treturn ret_val;")
1095 out_c += ("\n}\n\n")
1098 out_java_struct += ("\t\t")
1099 if return_type_info.java_ty != "void":
1100 out_java_struct += "const ret: " + return_type_info.java_ty + " = "
1101 out_java_struct += ("bindings." + method_name + "(")
1102 for idx, info in enumerate(argument_types):
1104 out_java_struct += (", ")
1105 if idx == 0 and takes_self:
1106 out_java_struct += ("this.ptr")
1107 elif info.arg_name in default_constructor_args:
1108 out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
1109 for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1110 if explode_idx != 0:
1111 out_java_struct += (", ")
1112 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1113 if explode_arg.from_hu_conv is not None:
1114 out_java_struct += (
1115 explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1117 out_java_struct += (expl_arg_name)
1118 out_java_struct += (")")
1119 elif info.from_hu_conv is not None:
1120 out_java_struct += (info.from_hu_conv[0])
1122 out_java_struct += (info.arg_name)
1123 out_java_struct += (");\n")
1124 if return_type_info.to_hu_conv is not None:
1126 out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t").replace("this",
1127 return_type_info.to_hu_conv_name) + "\n")
1129 out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1131 for idx, info in enumerate(argument_types):
1132 if idx == 0 and takes_self:
1134 elif info.arg_name in default_constructor_args:
1135 for explode_arg in default_constructor_args[info.arg_name]:
1136 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1137 if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
1138 out_java_struct += ("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name,
1139 expl_arg_name).replace(
1140 "this", return_type_info.to_hu_conv_name) + ";\n")
1141 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1142 if not takes_self and return_type_info.to_hu_conv_name is not None:
1143 out_java_struct += (
1144 "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n")
1146 out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n")
1148 if return_type_info.to_hu_conv_name is not None:
1149 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
1150 elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
1151 out_java_struct += ("\t\treturn ret;\n")
1152 out_java_struct += ("\t}\n\n")
1154 return (out_java, out_c, out_java_struct)
1157 for struct in self.struct_file_suffixes:
1158 with open(self.outdir + "/structs/" + struct + self.file_ext, "a") as src:
1159 src.write(self.struct_file_suffixes[struct])