1 from bindingstypes import *
9 def __init__(self, DEBUG: bool, target: Target, **kwargs):
11 self.c_array_class_caches = set()
12 self.c_type_map = dict(
19 self.java_type_map = dict(
22 self.java_hu_type_map = dict(
26 self.to_hu_conv_templates = dict(
27 ptr = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }',
28 default = '{human_type} {var_name}_hu_conv = null; if ({var_name} < 0 || {var_name} > 4096) { {var_name}_hu_conv = new {human_type}(null, {var_name}); }'
31 self.bindings_header = """
34 using System.Runtime.InteropServices;
36 namespace org { namespace ldk { namespace impl {
38 internal class bindings {
40 init(java.lang.Enum.class, VecOrSliceDef.class);
42 if (!get_lib_version_string().equals(version.get_ldk_java_bindings_version()))
43 throw new ArgumentException("Compiled LDK library and LDK class failes do not match");
44 // Fetching the LDK versions from C also checks that the header and binaries match
45 Console.Error.WriteLine("Loaded LDK-Java Bindings " + version.get_ldk_java_bindings_version() + " with LDK " + get_ldk_version() + " and LDK-C-Bindings " + get_ldk_c_bindings_version());
47 //static extern void init(java.lang.Class c);
48 //static native void init_class_cache();
50 self.bindings_header += self.native_meth_decl("get_lib_version_string", "string") + "();\n"
51 self.bindings_header += self.native_meth_decl("get_ldk_c_bindings_version", "string") + "();\n"
52 self.bindings_header += self.native_meth_decl("get_ldk_version", "string") + "();\n\n"
54 self.bindings_version_file = """
56 public class version {
57 public static string get_ldk_java_bindings_version() {
58 return "<git_version_ldk_garbagecollected>";
62 self.util_fn_pfx = """using org.ldk.impl;
65 using org.ldk.structs;
68 namespace org { namespace ldk { namespace util {
69 public class UtilMethods {
71 self.util_fn_sfx = "} } } }"
72 self.common_base = """using System.Collections.Generic;
73 using System.Diagnostics;
75 public class CommonBase {
76 protected internal long ptr;
77 protected internal LinkedList<object> ptrs_to = new LinkedList<object>();
78 protected CommonBase(long ptr) { Trace.Assert(ptr < 0 || ptr > 4096); this.ptr = ptr; }
82 self.txout_defn = """public class TxOut : CommonBase {
83 /** The script_pubkey in this output */
84 public readonly byte[] script_pubkey;
85 /** The value, in satoshis, of this output */
86 public readonly long value;
88 internal TxOut(object _dummy, long ptr) : base(ptr) {
89 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
90 this.value = bindings.TxOut_get_value(ptr);
92 public TxOut(long value, byte[] script_pubkey) : base(bindings.TxOut_new(script_pubkey, value)) {
93 this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr);
94 this.value = bindings.TxOut_get_value(ptr);
98 if (ptr != 0) { bindings.TxOut_free(ptr); }
102 self.scalar_defn = """public class BigEndianScalar : CommonBase {
103 /** The bytes of the scalar value, in big endian */
104 public readonly byte[] scalar_bytes;
106 internal BigEndianScalar(object _dummy, long ptr) : base(ptr) {
107 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
109 public BigEndianScalar(byte[] scalar_bytes) : base(bindings.BigEndianScalar_new(scalar_bytes)) {
110 this.scalar_bytes = bindings.BigEndianScalar_get_bytes(ptr);
114 if (ptr != 0) { bindings.BigEndianScalar_free(ptr); }
119 self.c_file_pfx = """
120 // On OSX jlong (ie long long) is not equivalent to int64_t, so we override here
121 #define int64_t jlong
122 #include <lightning.h>
124 #include <stdatomic.h>
127 #define LIKELY(v) __builtin_expect(!!(v), 1)
128 #define UNLIKELY(v) __builtin_expect(!!(v), 0)
132 self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
134 if not DEBUG or sys.platform == "darwin":
135 self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
136 #define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
137 #define CHECK_ACCESS(p)
138 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
141 self.c_file_pfx += """#define DO_ASSERT(a) (void)(a)
145 self.c_file_pfx = self.c_file_pfx + """#include <assert.h>
146 // Always run a, then assert it is true:
147 #define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
148 // Assert a is true or do nothing
149 #define CHECK(a) DO_ASSERT(a)
151 void __attribute__((constructor)) debug_log_version() {
152 if (check_get_ldk_version() == NULL)
153 DEBUG_PRINT("LDK version did not match the header we built against\\n");
154 if (check_get_ldk_bindings_version() == NULL)
155 DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
159 if sys.platform != "darwin":
160 self.c_file_pfx += """
161 // Running a leak check across all the allocations and frees of the JDK is a mess,
162 // so instead we implement our own naive leak checker here, relying on the -wrap
163 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
164 // and free'd in Rust or C across the generated bindings shared library.
168 self.c_file_pfx = self.c_file_pfx + "#include <execinfo.h>\n"
169 self.c_file_pfx = self.c_file_pfx + """
172 static pthread_mutex_t allocation_mtx;
174 void __attribute__((constructor)) init_mtx() {
175 DO_ASSERT(!pthread_mutex_init(&allocation_mtx, NULL));
179 typedef struct allocation {
180 struct allocation* next;
182 const char* struct_name;
185 unsigned long alloc_len;
187 static allocation* allocation_ll = NULL;
189 void* __real_malloc(size_t len);
190 void* __real_calloc(size_t nmemb, size_t len);
191 static void new_allocation(void* res, const char* struct_name, size_t len) {
192 allocation* new_alloc = __real_malloc(sizeof(allocation));
193 new_alloc->ptr = res;
194 new_alloc->struct_name = struct_name;
195 new_alloc->bt_len = backtrace(new_alloc->bt, BT_MAX);
196 new_alloc->alloc_len = len;
197 DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
198 new_alloc->next = allocation_ll;
199 allocation_ll = new_alloc;
200 DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
202 static void* MALLOC(size_t len, const char* struct_name) {
203 void* res = __real_malloc(len);
204 new_allocation(res, struct_name, len);
207 void __real_free(void* ptr);
208 static void alloc_freed(void* ptr) {
209 allocation* p = NULL;
210 DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
211 allocation* it = allocation_ll;
212 while (it->ptr != ptr) {
213 p = it; it = it->next;
215 DEBUG_PRINT("ERROR: Tried to free unknown pointer %p at:\\n", ptr);
217 int bt_len = backtrace(bt, BT_MAX);
218 backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
219 DEBUG_PRINT("\\n\\n");
220 DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
221 return; // addrsan should catch malloc-unknown and print more info than we have
224 if (p) { p->next = it->next; } else { allocation_ll = it->next; }
225 DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
226 DO_ASSERT(it->ptr == ptr);
229 static void FREE(void* ptr) {
230 if ((uint64_t)ptr <= 4096) return; // Rust loves to create pointers to the NULL page for dummys
235 void* __wrap_malloc(size_t len) {
236 void* res = __real_malloc(len);
237 new_allocation(res, "malloc call", len);
240 void* __wrap_calloc(size_t nmemb, size_t len) {
241 void* res = __real_calloc(nmemb, len);
242 new_allocation(res, "calloc call", len);
245 void __wrap_free(void* ptr) {
246 if (ptr == NULL) return;
251 static void CHECK_ACCESS(const void* ptr) {
252 DO_ASSERT(!pthread_mutex_lock(&allocation_mtx));
253 allocation* it = allocation_ll;
254 while (it->ptr != ptr) {
257 DEBUG_PRINT("ERROR: Tried to access unknown pointer %p at:\\n", ptr);
259 int bt_len = backtrace(bt, BT_MAX);
260 backtrace_symbols_fd(bt, bt_len, STDERR_FILENO);
261 DEBUG_PRINT("\\n\\n");
262 DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
263 return; // addrsan should catch and print more info than we have
266 DO_ASSERT(!pthread_mutex_unlock(&allocation_mtx));
268 #define CHECK_INNER_FIELD_ACCESS_OR_NULL(v) \\
269 if (v.is_owned && v.inner != NULL) { \\
270 const void *p = __unmangle_inner_ptr(v.inner); \\
276 void* __real_realloc(void* ptr, size_t newlen);
277 void* __wrap_realloc(void* ptr, size_t len) {
278 if (ptr != NULL) alloc_freed(ptr);
279 void* res = __real_realloc(ptr, len);
280 new_allocation(res, "realloc call", len);
283 void __wrap_reallocarray(void* ptr, size_t new_sz) {
284 // Rust doesn't seem to use reallocarray currently
288 void __attribute__((destructor)) check_leaks() {
289 unsigned long alloc_count = 0;
290 unsigned long alloc_size = 0;
291 DEBUG_PRINT("The following LDK-allocated blocks still remain.\\n");
292 DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
293 DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
294 for (allocation* a = allocation_ll; a != NULL; a = a->next) {
295 DEBUG_PRINT("%s %p (%lu bytes) remains:\\n", a->struct_name, a->ptr, a->alloc_len);
296 backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO);
297 DEBUG_PRINT("\\n\\n");
299 alloc_size += a->alloc_len;
301 DEBUG_PRINT("%lu allocations remained for %lu bytes.\\n", alloc_count, alloc_size);
302 DEBUG_PRINT("Note that this is only accurate if System.gc(); System.runFinalization()\\n");
303 DEBUG_PRINT("was called prior to exit after all LDK objects were out of scope.\\n");
306 self.c_file_pfx = self.c_file_pfx + """
308 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
309 _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
310 _Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
311 _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
313 _Static_assert(sizeof(void*) <= 8, "Pointers must fit into 64 bits");
315 typedef jlongArray int64_tArray;
316 typedef jbyteArray int8_tArray;
318 static inline jstring str_ref_to_java(JNIEnv *env, const char* chars, size_t len) {
319 // Sadly we need to create a temporary because Java can't accept a char* without a 0-terminator
320 char* conv_buf = MALLOC(len + 1, "str conv buf");
321 memcpy(conv_buf, chars, len);
323 jstring ret = (*env)->NewStringUTF(env, conv_buf);
327 static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
328 uint64_t str_len = (*env)->GetStringUTFLength(env, str);
329 char* newchars = MALLOC(str_len + 1, "String chars");
330 const char* jchars = (*env)->GetStringUTFChars(env, str, NULL);
331 memcpy(newchars, jchars, str_len);
332 newchars[str_len] = 0;
333 (*env)->ReleaseStringUTFChars(env, str, jchars);
337 .chars_is_owned = true
342 const char* CS_LDK_get_ldk_c_bindings_version() {
343 return str_ref_to_java(check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
345 const char* CS_LDK_get_ldk_version() {
346 return str_ref_to_java(check_get_ldk_version(), strlen(check_get_ldk_version()));
350 self.c_version_file = """const char* CS_LDK_get_lib_version_string() {
351 return "<git_version_ldk_garbagecollected>";
354 self.hu_struct_file_prefix = """using org.ldk.impl;
359 namespace org { namespace ldk { namespace structs {
362 self.hu_struct_file_suffix = "} } }\n"
363 self.c_fn_args_pfx = ""
364 self.c_fn_ty_pfx = ""
365 self.file_ext = ".cs"
366 self.ptr_c_ty = "int64_t"
367 self.ptr_native_ty = "long"
368 self.u128_native_ty = "UInt128"
369 self.usize_c_ty = "int64_t"
370 self.usize_native_ty = "long"
371 self.native_zero_ptr = "0"
372 self.result_c_ty = "jclass"
373 self.ptr_arr = "jobjectArray"
374 self.is_arr_some_check = ("", " != NULL")
375 self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
377 self.bindings_footer_wip = "\tstatic bindings() {\n"
378 def bindings_footer(self):
379 return self.bindings_footer_wip + "\t}\n}\n} } }\n"
381 def native_meth_decl(self, meth_name, ret_ty_str):
382 return "\t[DllImport (\"ldkcsharp\", EntryPoint=\"CS_LDK_" + meth_name + "\")] public static extern " + ret_ty_str + " " + meth_name
384 def c_fn_name_define_pfx(self, fn_name, have_args):
385 return " CS_LDK_" + fn_name + "("
387 def construct_jenv(self):
388 res = "JNIEnv *env;\n"
389 res += "jint get_jenv_res = (*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_6);\n"
390 res += "if (get_jenv_res == JNI_EDETACHED) {\n"
391 res += "\tDO_ASSERT((*j_calls->vm)->AttachCurrentThread(j_calls->vm, (void**)&env, NULL) == JNI_OK);\n"
393 res += "\tDO_ASSERT(get_jenv_res == JNI_OK);\n"
396 def deconstruct_jenv(self):
397 res = "if (get_jenv_res == JNI_EDETACHED) {\n"
398 res += "\tDO_ASSERT((*j_calls->vm)->DetachCurrentThread(j_calls->vm) == JNI_OK);\n"
402 def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):
403 if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
404 return "(*env)->ReleasePrimitiveArrayCritical(env, " + arr_var + ", " + arr_ptr_var + ", 0)"
406 def create_native_arr_call(self, arr_len, ty_info):
407 if ty_info.c_ty == "int8_tArray":
408 return "(*env)->NewByteArray(env, " + arr_len + ")"
409 elif ty_info.subty.c_ty.endswith("Array"):
410 clz_var = ty_info.java_fn_ty_arg[1:].replace("[", "arr_of_")
411 self.c_array_class_caches.add(clz_var)
412 return "(*env)->NewObjectArray(env, " + arr_len + ", " + clz_var + "_clz, NULL);\n"
414 return "(*env)->New" + ty_info.java_ty.strip("[]").title() + "Array(env, " + arr_len + ")"
415 def set_native_arr_contents(self, arr_name, arr_len, ty_info):
416 if ty_info.c_ty == "int8_tArray":
417 return ("(*env)->SetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", ", ")")
420 def get_native_arr_contents(self, arr_name, dest_name, arr_len, ty_info, copy):
421 if ty_info.c_ty == "int8_tArray":
423 return "(*env)->GetByteArrayRegion(env, " + arr_name + ", 0, " + arr_len + ", " + dest_name + ")"
425 return "(*env)->GetByteArrayElements (env, " + arr_name + ", NULL)"
426 elif not ty_info.java_ty[:len(ty_info.java_ty) - 2].endswith("[]"):
427 return "(*env)->Get" + ty_info.subty.java_ty.title() + "ArrayElements (env, " + arr_name + ", NULL)"
430 def get_native_arr_elem(self, arr_name, idxc, ty_info):
431 if self.get_native_arr_contents(arr_name, "", "", ty_info, False) is None:
432 return "(*env)->GetObjectArrayElement(env, " + arr_name + ", " + idxc + ")"
434 assert False # Only called if above is None
435 def get_native_arr_ptr_call(self, ty_info):
436 if ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array"):
438 return ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
439 def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
440 if ty_info.subty is None or not ty_info.subty.c_ty.endswith("Array"):
442 return "(*env)->SetObjectArrayElement(env, " + arr_name + ", " + idxc + ", " + entry_access + ")"
443 def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):
444 if ty_info.c_ty == "int8_tArray":
445 return "(*env)->ReleaseByteArrayElements(env, " + arr_name + ", (int8_t*)" + dest_name + ", 0);"
447 return "(*env)->Release" + ty_info.java_ty.strip("[]").title() + "ArrayElements(env, " + arr_name + ", " + dest_name + ", 0)"
449 def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
450 if elem_ty.java_hu_ty == "UInt5":
451 return arr_name + " != null ? InternalUtils.convUInt5Array(" + arr_name + ") : null"
452 elif elem_ty.java_hu_ty == "WitnessVersion":
453 return arr_name + " != null ? InternalUtils.convWitnessVersionArray(" + arr_name + ") : null"
455 return arr_name + " != null ? InternalUtils.mapArray(" + arr_name + ", " + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null"
457 def str_ref_to_native_call(self, var_name, str_len):
458 return "str_ref_to_java(env, " + var_name + ", " + str_len + ")"
459 def str_ref_to_c_call(self, var_name):
460 return "java_to_owned_str(env, " + var_name + ")"
461 def str_to_hu_conv(self, var_name):
463 def str_from_hu_conv(self, var_name):
469 def var_decl_statement(self, ty_string, var_name, statement):
470 return ty_string + " " + var_name + " = " + statement
472 def get_java_arr_len(self, arr_name):
473 return arr_name + ".Length"
474 def get_java_arr_elem(self, elem_ty, arr_name, idx):
475 return arr_name + "[" + idx + "]"
476 def constr_hu_array(self, ty_info, arr_len):
477 base_ty = ty_info.subty.java_hu_ty.split("[")[0].split("<")[0]
478 conv = "new " + base_ty + "[" + arr_len + "]"
479 if "[" in ty_info.subty.java_hu_ty.split("<")[0]:
480 # Do a bit of a dance to move any excess [] to the end
481 conv += "[" + ty_info.subty.java_hu_ty.split("<")[0].split("[")[1]
483 def cleanup_converted_native_array(self, ty_info, arr_name):
486 def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
487 mapped_ty = arr_ty.subty
488 if arr_ty.rust_obj == "LDKU128":
489 return ("" + arr_name + ".getLEBytes()", "")
490 if fixed_len is not None:
491 return ("InternalUtils.check_arr_len(" + arr_name + ", " + fixed_len + ")", "")
493 def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
494 if arr_ty.rust_obj == "LDKU128":
495 return "org.ldk.util.UInt128 " + conv_name + " = new org.ldk.util.UInt128(" + arr_name + ");"
498 def java_arr_ty_str(self, elem_ty_str):
499 return elem_ty_str + "[]"
501 def for_n_in_range(self, n, minimum, maximum):
502 return "for (int " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {"
503 def for_n_in_arr(self, n, arr_name, arr_elem_ty):
504 return ("foreach (" + arr_elem_ty.java_hu_ty + " " + n + " in " + arr_name + ") { ", " }")
506 def get_ptr(self, var):
508 def set_null_skip_free(self, var):
509 return var + ".ptr" + " = 0;"
511 def add_ref(self, holder, referent):
512 return "if (" + holder + " != null) { " + holder + ".ptrs_to.AddLast(" + referent + "); }"
514 def fully_qualified_hu_ty_path(self, ty):
515 if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
517 if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt128" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
518 return "org.ldk.util." + ty.java_hu_ty
519 if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
520 return "org.ldk.structs." + ty.java_hu_ty
523 def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
524 out_java_enum = "namespace org { namespace ldk { namespace enums {"
528 out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_cs(int32_t ord) {\n"
529 out_c += "\tswitch (ord) {\n"
531 if enum_doc_comment is not None:
532 out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
533 out_java_enum += "public enum " + struct_name + " {\n"
535 for var, var_docs in variants:
536 if var_docs is not None:
537 out_java_enum += "\t/**\n\t * " + var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
538 out_java_enum += "\t" + var + ",\n"
539 out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
542 out_c += "\t\tdefault: abort();\n"
546 out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_cs(LDK" + struct_name + " val) {\n"
547 out_c = out_c + "\tswitch (val) {\n"
549 for var, _ in variants:
550 out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
552 out_c = out_c + "\t\tdefault: abort();\n"
553 out_c = out_c + "\t}\n"
554 out_c = out_c + "}\n"
556 return (out_c, out_java_enum + "} } }\n", out_java)
558 def c_unitary_enum_to_native_call(self, ty_info):
559 return (ty_info.rust_obj + "_to_cs(", ")")
560 def native_unitary_enum_to_c_call(self, ty_info):
561 return (ty_info.rust_obj + "_from_cs(", ")")
563 def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
566 out_c = out_c + "static jclass " + struct_name + "_" + var + "_class = NULL;\n"
567 out_c = out_c + "static jmethodID " + struct_name + "_" + var + "_meth = NULL;\n"
568 out_c += "void" + self.c_fn_name_define_pfx(struct_name.replace("_", "_1") + "_init", True) + self.c_fn_args_pfx + ") {\n"
569 for var_name in variants:
570 out_c += "\t" + struct_name + "_" + var_name + "_class =\n"
571 out_c += "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"org/ldk/impl/bindings$" + struct_name + "$" + var_name + "\"));\n"
572 out_c += "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
573 out_c += "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
574 out_c += "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
575 out_c = out_c + "}\n"
578 def c_complex_enum_pass_ty(self, struct_name):
581 def c_constr_native_complex_enum(self, struct_name, variant, c_params):
582 ret = "(*env)->NewObject(env, " + struct_name + "_" + variant + "_class, " + struct_name + "_" + variant + "_meth"
583 for param in c_params:
584 ret = ret + ", " + param
587 def native_c_map_trait(self, struct_name, field_vars, flattened_field_vars, field_fns, trait_doc_comment):
591 # First generate most of the Java code, note that we need information about java method argument strings for C
592 out_java_trait += self.hu_struct_file_prefix
593 if trait_doc_comment is not None:
594 out_java_trait += "/**\n * " + trait_doc_comment.replace("\n", "\n * ") + "\n */\n"
595 out_java_trait = out_java_trait + "public class " + struct_name.replace("LDK","") + " : CommonBase {\n"
596 out_java_trait = out_java_trait + "\tinternal readonly bindings." + struct_name + " bindings_instance;\n"
597 out_java_trait = out_java_trait + "\tinternal " + struct_name.replace("LDK", "") + "(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }\n"
598 out_java_trait = out_java_trait + "\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg"
599 for var in flattened_field_vars:
600 if isinstance(var, ConvInfo):
601 out_java_trait += ", " + var.java_hu_ty + " " + var.arg_name
603 out_java_trait += ", bindings." + var[0] + " " + var[1]
604 out_java_trait += ") : base(bindings." + struct_name + "_new(arg"
605 for var in flattened_field_vars:
606 if isinstance(var, ConvInfo):
607 if var.from_hu_conv is not None:
608 out_java_trait = out_java_trait + ", " + var.from_hu_conv[0]
610 out_java_trait = out_java_trait + ", " + var.arg_name
612 out_java_trait = out_java_trait + ", " + var[1]
613 out_java_trait = out_java_trait + ")) {\n"
614 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.AddLast(arg);\n"
615 for var in flattened_field_vars:
616 if isinstance(var, ConvInfo):
617 if var.from_hu_conv is not None and var.from_hu_conv[1] != "":
618 out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
620 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.AddLast(" + var[1] + ");\n"
621 out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n"
622 out_java_trait = out_java_trait + "\t}\n"
623 out_java_trait = out_java_trait + "\t~" + struct_name.replace("LDK","") + "() {\n"
624 out_java_trait = out_java_trait + "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n"
625 out_java_trait = out_java_trait + "\t}\n\n"
627 java_trait_wrapper = "\tprivate class " + struct_name + "Holder { internal " + struct_name.replace("LDK", "") + " held; }\n"
628 java_trait_wrapper += "\tprivate class " + struct_name + "Impl : bindings." + struct_name + " {\n"
629 java_trait_wrapper += "\t\tinternal " + struct_name + "Impl(" + struct_name.replace("LDK", "") + "Interface arg, " + struct_name + "Holder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }\n"
630 java_trait_wrapper += "\t\tprivate " + struct_name.replace("LDK", "") + "Interface arg;\n"
631 java_trait_wrapper += "\t\tprivate " + struct_name + "Holder impl_holder;\n"
632 java_trait_constr = "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
633 for var in flattened_field_vars:
634 if isinstance(var, ConvInfo):
635 java_trait_constr += ", " + var.java_hu_ty + " " + var.arg_name
637 # Ideally we'd be able to take any instance of the interface, but our C code can only represent
638 # Java-implemented version, so we require users pass a Java implementation here :/
639 java_trait_constr += ", " + var[0].replace("LDK", "") + "." + var[0].replace("LDK", "") + "Interface " + var[1] + "_impl"
640 java_trait_constr = java_trait_constr + ") {\n\t\t" + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
641 java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new " + struct_name + "Impl(arg, impl_holder)"
642 out_java_trait += "\tpublic interface " + struct_name.replace("LDK", "") + "Interface {\n"
643 out_java += "\tpublic interface " + struct_name + " {\n"
645 for fn_line in field_fns:
646 java_meth_descr = "("
647 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
648 fn_name = fn_line.fn_name
649 if fn_name == "lock": # reserved symbol
651 out_java += "\t\t" + fn_line.ret_ty_info.java_ty + " " + fn_name + "("
652 java_trait_wrapper += "\t\tpublic " + fn_line.ret_ty_info.java_ty + " " + fn_name + "("
653 out_java_trait += "\t\t/**\n\t\t * " + fn_line.docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n"
654 out_java_trait += "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_name + "("
656 for idx, arg_conv_info in enumerate(fn_line.args_ty):
659 java_trait_wrapper += ", "
660 out_java_trait += ", "
661 out_java += arg_conv_info.java_ty + " _" + arg_conv_info.arg_name
662 out_java_trait += arg_conv_info.java_hu_ty + " _" + arg_conv_info.arg_name
663 java_trait_wrapper += arg_conv_info.java_ty + " _" + arg_conv_info.arg_name
664 java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
665 java_meth_descr = java_meth_descr + ")" + fn_line.ret_ty_info.java_fn_ty_arg
666 java_meths.append((fn_line.fn_name, java_meth_descr))
669 out_java_trait += ");\n"
670 java_trait_wrapper += ") {\n"
672 for arg_info in fn_line.args_ty:
673 if arg_info.to_hu_conv is not None:
674 java_trait_wrapper += "\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t").replace(arg_info.arg_name, "_" + arg_info.arg_name) + "\n"
676 if fn_line.ret_ty_info.java_ty != "void":
677 java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_name + "("
679 java_trait_wrapper += "\t\t\targ." + fn_name + "("
681 for idx, arg_info in enumerate(fn_line.args_ty):
683 java_trait_wrapper += ", "
684 if arg_info.to_hu_conv_name is not None:
685 java_trait_wrapper += arg_info.to_hu_conv_name.replace(arg_info.arg_name, "_" + arg_info.arg_name)
687 java_trait_wrapper += "_" + arg_info.arg_name
689 java_trait_wrapper += ");\n"
690 java_trait_wrapper += "\t\t\t\tGC.KeepAlive(arg);\n"
691 if fn_line.ret_ty_info.java_ty != "void":
692 if fn_line.ret_ty_info.from_hu_conv is not None:
693 java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n"
694 if fn_line.ret_ty_info.from_hu_conv[1] != "":
695 java_trait_wrapper += "\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
696 java_trait_wrapper += "\t\t\treturn result;\n"
698 java_trait_wrapper += "\t\t\treturn ret;\n"
699 java_trait_wrapper += "\t\t}\n"
700 java_trait_wrapper += "\t}"
701 for var in field_vars:
702 if isinstance(var, ConvInfo):
703 java_trait_constr = java_trait_constr + ", " + var.arg_name
705 java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
706 for suparg in var[2]:
707 if isinstance(suparg, ConvInfo):
708 java_trait_constr += ", " + suparg.arg_name
710 java_trait_constr += ", " + suparg[1]
711 java_trait_constr += ").bindings_instance"
712 for suparg in var[2]:
713 if isinstance(suparg, ConvInfo):
714 java_trait_constr += ", " + suparg.arg_name
716 java_trait_constr += ", " + suparg[1]
717 out_java_trait += "\t}\n" + java_trait_wrapper + "\n"
718 out_java_trait += java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
722 out_java += self.native_meth_decl(struct_name + "_new", "long") + "(" + struct_name + " impl"
723 for var in flattened_field_vars:
724 if isinstance(var, ConvInfo):
725 out_java += ", " + var.java_ty + " " + var.arg_name
727 out_java += ", " + var[0] + " " + var[1]
730 # Now that we've written out our java code (and created java_meths), generate C
731 out_c = "typedef struct " + struct_name + "_JCalls {\n"
732 out_c = out_c + "\tatomic_size_t refcnt;\n"
733 out_c = out_c + "\tJavaVM *vm;\n"
734 out_c = out_c + "\tjweak o;\n"
735 for var in flattened_field_vars:
736 if isinstance(var, ConvInfo):
737 # We're a regular ol' field
741 out_c = out_c + "\t" + var[0] + "_JCalls* " + var[1] + ";\n"
743 if fn.fn_name != "free" and fn.fn_name != "cloned":
744 out_c = out_c + "\tjmethodID " + fn.fn_name + "_meth;\n"
745 out_c = out_c + "} " + struct_name + "_JCalls;\n"
747 for fn_line in field_fns:
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 out_c += "\t\t" + self.construct_jenv().replace("\n", "\n\t\t").strip() + "\n"
753 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
754 out_c += "\t\t" + self.deconstruct_jenv().replace("\n", "\n\t\t").strip() + "\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_fns):
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"
772 out_c += "\t" + self.construct_jenv().replace("\n", "\n\t").strip() + "\n"
774 for arg_info in fn_line.args_ty:
775 if arg_info.ret_conv is not None:
776 out_c = out_c + "\t" + arg_info.ret_conv[0].replace('\n', '\n\t')
777 out_c = out_c + arg_info.arg_name
778 out_c = out_c + arg_info.ret_conv[1].replace('\n', '\n\t') + "\n"
780 out_c = out_c + "\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tCHECK(obj != NULL);\n"
781 if fn_line.ret_ty_info.c_ty.endswith("Array"):
782 out_c = out_c + "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
783 elif fn_line.ret_ty_info.c_ty == "void":
784 out_c += "\t(*env)->CallVoidMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
785 elif fn_line.ret_ty_info.java_hu_ty == "string" or "org/ldk/enums" in fn_line.ret_ty_info.java_fn_ty_arg:
786 # Manually write out string methods as they're just an Object
787 out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
788 elif not fn_line.ret_ty_info.passed_as_ptr:
789 out_c += "\t" + fn_line.ret_ty_info.c_ty + " ret = (*env)->Call" + fn_line.ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.fn_name + "_meth"
791 out_c = out_c + "\tuint64_t ret = (*env)->CallLongMethod(env, obj, j_calls->" + fn_line.fn_name + "_meth"
793 for idx, arg_info in enumerate(fn_line.args_ty):
794 if arg_info.ret_conv is not None:
795 out_c = out_c + ", " + arg_info.ret_conv_name
797 out_c = out_c + ", " + arg_info.arg_name
798 out_c = out_c + ");\n"
800 out_c += "\tif (UNLIKELY((*env)->ExceptionCheck(env))) {\n"
801 out_c += "\t\t(*env)->ExceptionDescribe(env);\n"
802 out_c += "\t\t(*env)->FatalError(env, \"A call to " + fn_line.fn_name + " in " + struct_name + " from rust threw an exception.\");\n"
805 if fn_line.ret_ty_info.arg_conv is not None:
806 out_c += "\t" + fn_line.ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n"
807 out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
808 out_c += "\treturn " + fn_line.ret_ty_info.arg_conv_name + ";\n"
810 out_c += "\t" + self.deconstruct_jenv().replace("\n", "\n\t").strip() + "\n"
811 if not fn_line.ret_ty_info.passed_as_ptr and fn_line.ret_ty_info.c_ty != "void":
812 out_c += "\treturn ret;\n"
814 out_c = out_c + "}\n"
816 # If we can, write out a clone function whether we need one or not, as we use them in moving to rust
817 can_clone_with_ptr = True
818 for var in field_vars:
819 if isinstance(var, ConvInfo):
820 can_clone_with_ptr = False
821 if can_clone_with_ptr:
822 out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n"
823 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n"
824 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n"
825 for var in field_vars:
826 if not isinstance(var, ConvInfo):
827 out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n"
828 out_c = out_c + "}\n"
830 out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
831 for var in flattened_field_vars:
832 if isinstance(var, ConvInfo):
833 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
835 out_c = out_c + ", jobject " + var[1]
836 out_c = out_c + ") {\n"
838 out_c = out_c + "\tjclass c = (*env)->GetObjectClass(env, o);\n"
839 out_c = out_c + "\tCHECK(c != NULL);\n"
840 out_c = out_c + "\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n"
841 out_c = out_c + "\tatomic_init(&calls->refcnt, 1);\n"
842 out_c = out_c + "\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n"
843 out_c = out_c + "\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n"
845 for (fn_name, java_meth_descr) in java_meths:
846 if fn_name != "free" and fn_name != "cloned":
847 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
848 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
850 for var in flattened_field_vars:
851 if isinstance(var, ConvInfo) and var.arg_conv is not None:
852 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
853 out_c = out_c + "\n\t" + struct_name + " ret = {\n"
854 out_c = out_c + "\t\t.this_arg = (void*) calls,\n"
855 for fn_line in field_fns:
856 if fn_line.fn_name != "free" and fn_line.fn_name != "cloned":
857 out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n"
858 elif fn_line.fn_name == "free":
859 out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n"
861 out_c = out_c + "\t\t.cloned = " + struct_name + "_JCalls_cloned,\n"
862 for var in field_vars:
863 if isinstance(var, ConvInfo):
864 if var.arg_conv_name is not None:
865 out_c = out_c + "\t\t." + var.arg_name + " = " + var.arg_conv_name + ",\n"
866 out_c = out_c + "\t\t.set_" + var.arg_name + " = NULL,\n"
868 out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
869 out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
871 out_c += "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1]
872 for suparg in var[2]:
873 if isinstance(suparg, ConvInfo):
874 out_c = out_c + ", " + suparg.arg_name
876 out_c = out_c + ", " + suparg[1]
878 out_c = out_c + "\t};\n"
879 for var in flattened_field_vars:
880 if not isinstance(var, ConvInfo):
881 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
882 out_c = out_c + "\treturn ret;\n"
883 out_c = out_c + "}\n"
885 out_c += "int64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
886 for var in flattened_field_vars:
887 if isinstance(var, ConvInfo):
888 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
890 out_c = out_c + ", jobject " + var[1]
891 out_c = out_c + ") {\n"
892 out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
893 out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
894 for var in flattened_field_vars:
895 if isinstance(var, ConvInfo):
896 out_c = out_c + ", " + var.arg_name
898 out_c = out_c + ", " + var[1]
899 out_c = out_c + ");\n"
900 out_c = out_c + "\treturn tag_ptr(res_ptr, true);\n"
901 out_c = out_c + "}\n"
903 for var in flattened_field_vars:
904 if not isinstance(var, ConvInfo):
905 out_java_trait += "\n\t/**\n"
906 out_java_trait += "\t * Gets the underlying " + var[1] + ".\n"
907 out_java_trait += "\t */\n"
908 underscore_name = ''.join('_' + c.lower() if c.isupper() else c for c in var[1]).strip('_')
909 out_java_trait += "\tpublic " + var[1] + " get_" + underscore_name + "() {\n"
910 out_java_trait += "\t\t" + var[1] + " res = new " + var[1] + "(null, bindings." + struct_name + "_get_" + var[1] + "(this.ptr));\n"
911 out_java_trait += "\t\tthis.ptrs_to.AddLast(res);\n"
912 out_java_trait += "\t\treturn res;\n"
913 out_java_trait += "\t}\n"
914 out_java_trait += "\n"
916 out_java += self.native_meth_decl(struct_name + "_get_" + var[1], "long") + "(long arg);\n"
918 out_c += "int64_t " + self.c_fn_name_define_pfx(struct_name + "_get_" + var[1], True) + "int64_t arg) {\n"
919 out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)untag_ptr(arg);\n"
920 out_c += "\treturn tag_ptr(&inp->" + var[1] + ", false);\n"
923 return (out_java, out_java_trait, out_c)
925 def trait_struct_inc_refcnt(self, ty_info):
926 base_conv = "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
927 base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
928 base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_cloned(&" + ty_info.var_name + "_conv);\n}"
931 def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
932 bindings_type = struct_name.replace("LDK", "")
933 java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
939 out_java_enum += (self.hu_struct_file_prefix)
941 java_hu_class = "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
942 java_hu_class += "public class " + java_hu_type + " : CommonBase {\n"
943 java_hu_class += f"\tprotected {java_hu_type}(object _dummy, long ptr) : base(ptr)" + " { }\n"
944 java_hu_class += "\t~" + java_hu_type + "() {\n"
945 java_hu_class += "\t\tif (ptr != 0) { bindings." + bindings_type + "_free(ptr); }\n"
946 java_hu_class += "\t}\n\n"
947 java_hu_class += f"\tinternal static {java_hu_type} constr_from_ptr(long ptr) {{\n"
948 java_hu_class += f"\t\tlong raw_ty = bindings." + struct_name + "_ty_from_ptr(ptr);\n"
949 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"
950 out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
951 out_c += "\tswitch(obj->tag) {\n"
952 java_hu_class += "\t\tswitch (raw_ty) {\n"
953 java_hu_subclasses = ""
956 for var in variant_list:
957 java_hu_subclasses += "\t/** A " + java_hu_type + " of type " + var.var_name + " */\n"
958 java_hu_subclasses += "\tpublic class " + java_hu_type + "_" + var.var_name + " : " + java_hu_type + " {\n"
959 java_hu_class += f"\t\t\tcase {var_idx}: "
960 java_hu_class += "return new " + java_hu_type + "_" + var.var_name + "(ptr);\n"
961 out_c += f"\t\tcase {struct_name}_{var.var_name}: return {var_idx};\n"
963 for idx, (field_ty, field_docs) in enumerate(var.fields):
964 if field_docs is not None:
965 java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n"
966 java_hu_subclasses += f"\t\tpublic {field_ty.java_hu_ty} {field_ty.arg_name};\n"
967 if field_ty.to_hu_conv is not None:
968 hu_conv_body += f"\t\t\t{field_ty.java_ty} {field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
969 hu_conv_body += f"\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
970 hu_conv_body += f"\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
972 hu_conv_body += f"\t\t\tthis.{field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
973 java_hu_subclasses += "\t\tinternal " + java_hu_type + "_" + var.var_name + "(long ptr) : base(null, ptr) {\n"
974 java_hu_subclasses += hu_conv_body
975 java_hu_subclasses += "\t\t}\n\t}\n"
977 java_hu_class += "\t\t\tdefault:\n\t\t\t\tthrow new ArgumentException(\"Impossible enum variant\");\n\t\t}\n\t}\n\n"
978 out_java += self.native_meth_decl(struct_name + "_ty_from_ptr", "long") + "(long ptr);\n"
979 out_c += ("\t\tdefault: abort();\n")
980 out_c += ("\t}\n}\n")
982 for var in variant_list:
983 for idx, (field_map, _) in enumerate(var.fields):
984 fn_name = f"{struct_name}_{var.var_name}_get_{field_map.arg_name}"
985 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"
986 out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
987 out_c += f"\tassert(obj->tag == {struct_name}_{var.var_name});\n"
988 if field_map.ret_conv is not None:
989 out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
990 if var.tuple_variant:
991 out_c += "obj->" + camel_to_snake(var.var_name)
993 out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
994 out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
995 out_c += "\treturn " + field_map.ret_conv_name + ";\n"
997 if var.tuple_variant:
998 out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + ";\n"
1000 out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name + ";\n"
1002 out_java += self.native_meth_decl(fn_name, field_map.java_ty) + "(long ptr);\n"
1003 out_java_enum += java_hu_class
1004 out_java_enum += java_hu_subclasses
1005 return (out_java, out_java_enum, out_c)
1007 def map_opaque_struct(self, struct_name, struct_doc_comment):
1008 out_opaque_struct_human = ""
1009 out_opaque_struct_human += self.hu_struct_file_prefix
1010 out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n"
1011 hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
1012 out_opaque_struct_human += ("public class " + hu_name + " : CommonBase")
1013 if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1014 out_opaque_struct_human += (", IDisposable")
1015 out_opaque_struct_human += (" {\n")
1016 out_opaque_struct_human += ("\tinternal " + hu_name + "(object _dummy, long ptr) : base(ptr) { }\n")
1017 if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
1018 out_opaque_struct_human += ("\tpublic void Dispose() {\n")
1020 out_opaque_struct_human += ("\t~" + hu_name + "() {\n")
1021 out_opaque_struct_human += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n")
1022 out_opaque_struct_human += ("\t}\n\n")
1023 return out_opaque_struct_human
1025 def map_tuple(self, struct_name):
1026 return self.map_opaque_struct(struct_name, "A Tuple")
1028 def map_result(self, struct_name, res_map, err_map):
1029 human_ty = struct_name.replace("LDKCResult", "Result")
1031 java_hu_struct += self.hu_struct_file_prefix
1032 java_hu_struct += "public class " + human_ty + " : CommonBase {\n"
1033 java_hu_struct += "\t" + human_ty + "(object _dummy, long ptr) : base(ptr) { }\n"
1034 java_hu_struct += "\t~" + human_ty + "() {\n"
1035 java_hu_struct += "\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); }\n"
1036 java_hu_struct += "\t}\n\n"
1037 java_hu_struct += "\tinternal static " + human_ty + " constr_from_ptr(long ptr) {\n"
1038 java_hu_struct += "\t\tif (bindings." + struct_name.replace("LDK", "") + "_is_ok(ptr)) {\n"
1039 java_hu_struct += "\t\t\treturn new " + human_ty + "_OK(null, ptr);\n"
1040 java_hu_struct += "\t\t} else {\n"
1041 java_hu_struct += "\t\t\treturn new " + human_ty + "_Err(null, ptr);\n"
1042 java_hu_struct += "\t\t}\n"
1043 java_hu_struct += "\t}\n"
1045 java_hu_struct += "\tpublic class " + human_ty + "_OK : " + human_ty + " {\n"
1047 if res_map.java_hu_ty != "void":
1048 java_hu_struct += "\t\tpublic readonly " + res_map.java_hu_ty + " res;\n"
1049 java_hu_struct += "\t\tinternal " + human_ty + "_OK(object _dummy, long ptr) : base(_dummy, ptr) {\n"
1050 if res_map.java_hu_ty == "void":
1052 elif res_map.to_hu_conv is not None:
1053 java_hu_struct += "\t\t\t" + res_map.java_ty + " res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1054 java_hu_struct += "\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t")
1055 java_hu_struct += "\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n"
1057 java_hu_struct += "\t\t\tthis.res = bindings." + struct_name.replace("LDK", "") + "_get_ok(ptr);\n"
1058 java_hu_struct += "\t\t}\n"
1059 java_hu_struct += "\t}\n\n"
1061 java_hu_struct += "\tpublic class " + human_ty + "_Err : " + human_ty + " {\n"
1062 if err_map.java_hu_ty != "void":
1063 java_hu_struct += "\t\tpublic readonly " + err_map.java_hu_ty + " err;\n"
1064 java_hu_struct += "\t\tinternal " + human_ty + "_Err(object _dummy, long ptr) : base(_dummy, ptr) {\n"
1065 if err_map.java_hu_ty == "void":
1067 elif err_map.to_hu_conv is not None:
1068 java_hu_struct += "\t\t\t" + err_map.java_ty + " err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1069 java_hu_struct += "\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t")
1070 java_hu_struct += "\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n"
1072 java_hu_struct += "\t\t\tthis.err = bindings." + struct_name.replace("LDK", "") + "_get_err(ptr);\n"
1073 java_hu_struct += "\t\t}\n"
1075 java_hu_struct += "\t}\n\n"
1076 return java_hu_struct
1078 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):
1079 arg_name_repl = lambda s, arg_name: s.replace(arg_name, "_" + arg_name) if arg_name == "lock" or arg_name == "event" or arg_name == "params" else s
1082 out_java_struct = None
1084 out_java += self.native_meth_decl(method_name, return_type_info.java_ty) + "("
1085 out_c += (return_type_info.c_ty)
1086 if return_type_info.ret_conv is not None:
1087 ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv
1088 have_args = len(argument_types) > 1 or (len(argument_types) > 0 and argument_types[0].c_ty != "void")
1089 out_c += (" " + self.c_fn_name_define_pfx(method_name, have_args))
1091 for idx, arg_conv_info in enumerate(argument_types):
1095 if arg_conv_info.c_ty != "void":
1096 out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name)
1097 out_java += (arg_conv_info.java_ty + " _" + arg_conv_info.arg_name) # Add a _ to avoid using reserved words
1099 out_java_struct = ""
1100 extra_java_struct_out = ""
1102 out_java_struct += ("\t// Skipped " + method_name + "\n")
1104 if doc_comment is not None:
1105 out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
1106 hu_ret_ty = return_type_info.java_hu_ty
1107 if return_type_info.nullable:
1108 #hu_ret_ty += "?" - apparently mono doesn't support the nullable stuff
1112 out_java_struct += "\tpublic static " + hu_ret_ty + " of("
1113 elif meth_n == "default":
1114 out_java_struct += "\tpublic static " + hu_ret_ty + " with_default("
1116 out_java_struct += "\tpublic static " + hu_ret_ty + " " + meth_n + "("
1117 elif meth_n == "clone_ptr" or (struct_meth.startswith("LDKCResult") and (meth_n == "get_ok" or meth_n == "get_err")):
1118 out_java_struct += "\tinternal " + hu_ret_ty + " " + meth_n + "("
1120 if meth_n == "hash" and return_type_info.java_hu_ty == "long":
1121 extra_java_struct_out = "\tpublic override int GetHashCode() {\n"
1122 extra_java_struct_out += "\t\treturn (int)this.hash();\n"
1123 extra_java_struct_out += "\t}\n"
1124 elif meth_n == "eq" and return_type_info.java_hu_ty == "bool":
1125 extra_java_struct_out = "\tpublic override bool Equals(object o) {\n"
1126 extra_java_struct_out += "\t\tif (!(o is " + struct_meth + ")) return false;\n"
1127 extra_java_struct_out += "\t\treturn this.eq((" + struct_meth + ")o);\n"
1128 extra_java_struct_out += "\t}\n"
1129 if meth_n == "lock":
1130 out_java_struct += "\tpublic " + hu_ret_ty + " do_lock("
1132 out_java_struct += "\tpublic " + hu_ret_ty + " " + meth_n + "("
1133 for idx, arg in enumerate(argument_types):
1135 if not takes_self or idx > 1:
1136 out_java_struct += ", "
1139 if arg.java_ty != "void":
1140 if arg.arg_name in default_constructor_args:
1141 assert not arg.nullable
1142 for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]):
1143 if explode_idx != 0:
1144 out_java_struct += (", ")
1145 out_java_struct += (
1146 explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name)
1148 ty_string = arg.java_hu_ty
1150 #ty_string += "?" - apparently mono doesn't support the nullable stuff
1152 ty_string = self.fully_qualified_hu_ty_path(arg)
1153 out_java_struct += ty_string + " " + arg_name_repl(arg.arg_name, arg.arg_name)
1154 out_java += (");\n")
1156 if out_java_struct is not None:
1157 out_java_struct += (") {\n")
1158 for info in argument_types:
1159 if info.arg_conv is not None:
1160 out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
1161 if return_type_info.ret_conv is not None:
1162 out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t'))
1163 elif return_type_info.c_ty != "void":
1164 out_c += ("\t" + return_type_info.c_ty + " ret_val = ")
1167 if c_call_string is None:
1168 out_c += (method_name + "(")
1170 out_c += (c_call_string)
1171 for idx, info in enumerate(argument_types):
1172 if info.arg_conv_name is not None:
1175 elif c_call_string is not None:
1177 out_c += (info.arg_conv_name)
1179 if return_type_info.ret_conv is not None:
1180 out_c += (ret_conv_sfx.replace('\n', '\n\t'))
1183 for info in argument_types:
1184 if info.arg_conv_cleanup is not None:
1185 out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
1186 if return_type_info.ret_conv is not None:
1187 out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";")
1188 elif return_type_info.c_ty != "void":
1189 out_c += ("\n\treturn ret_val;")
1190 out_c += ("\n}\n\n")
1193 out_java_struct += ("\t\t")
1194 if return_type_info.java_ty != "void":
1195 out_java_struct += (return_type_info.java_ty + " ret = ")
1196 out_java_struct += ("bindings." + method_name + "(")
1197 for idx, info in enumerate(argument_types):
1199 out_java_struct += (", ")
1200 if idx == 0 and takes_self:
1201 out_java_struct += ("this.ptr")
1202 elif info.arg_name in default_constructor_args:
1203 out_java_struct += ("bindings." + info.java_hu_ty + "_new(")
1204 for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1205 if explode_idx != 0:
1206 out_java_struct += (", ")
1207 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1208 if explode_arg.from_hu_conv is not None:
1209 out_java_struct += (
1210 explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
1212 out_java_struct += (expl_arg_name)
1213 out_java_struct += (")")
1214 elif info.from_hu_conv is not None:
1215 out_java_struct += arg_name_repl(info.from_hu_conv[0], info.arg_name)
1217 out_java_struct += arg_name_repl(info.arg_name, info.arg_name)
1218 out_java_struct += (");\n")
1220 # Like Java, the C# GC is quite aggressive and can finalize an object while a method
1221 # on it is operating. Unlike Java, this behavior appears to be better documented,
1223 for idx, info in enumerate(argument_types):
1224 if idx == 0 and takes_self:
1225 out_java_struct += ("\t\tGC.KeepAlive(this);\n")
1226 elif info.arg_name in default_constructor_args:
1227 for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
1228 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1229 out_java_struct += ("\t\tGC.KeepAlive(" + expl_arg_name + ");\n")
1230 elif info.c_ty != "void":
1231 out_java_struct += ("\t\tGC.KeepAlive(" + arg_name_repl(info.arg_name, info.arg_name) + ");\n")
1233 if return_type_info.java_ty == "long" and return_type_info.java_hu_ty != "long":
1234 out_java_struct += "\t\tif (ret >= 0 && ret <= 4096) { return null; }\n"
1236 if return_type_info.to_hu_conv is not None:
1238 out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t")
1239 .replace("this", return_type_info.to_hu_conv_name) + "\n")
1241 out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
1243 for idx, info in enumerate(argument_types):
1244 if idx == 0 and takes_self:
1246 elif info.arg_name in default_constructor_args:
1247 for explode_arg in default_constructor_args[info.arg_name]:
1248 expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
1249 if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name:
1250 out_java_struct += ("\t\t" +
1251 arg_name_repl(explode_arg.from_hu_conv[1], info.arg_name)
1252 .replace(explode_arg.arg_name, expl_arg_name)
1253 .replace("this", return_type_info.to_hu_conv_name) + ";\n")
1254 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
1255 if not takes_self and return_type_info.to_hu_conv_name is not None:
1256 out_java_struct += ("\t\t" + arg_name_repl(info.from_hu_conv[1], info.arg_name)
1257 .replace("this", return_type_info.to_hu_conv_name)
1258 .replace("\n", "\n\t\t") + ";\n")
1260 out_java_struct += ("\t\t" + arg_name_repl(info.from_hu_conv[1], info.arg_name)
1261 .replace("\n", "\n\t\t") + ";\n")
1263 if takes_self and not takes_self_as_ref:
1264 out_java_struct += "\t\t" + argument_types[0].from_hu_conv[1].replace("\n", "\n\t\t").replace("this_arg", "this") + ";\n"
1265 if return_type_info.to_hu_conv_name is not None:
1266 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")
1267 elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth:
1268 out_java_struct += ("\t\treturn ret;\n")
1269 out_java_struct += ("\t}\n\n")
1271 return (out_java, out_c, out_java_struct + extra_java_struct_out)