+ with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum:
+ (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake)
+
+ out_java_enum.write(out_java_enum_addendum)
+ out_java.write(out_java_addendum)
+ write_c(out_c_addendum)
+
+ def map_trait(struct_name, field_var_lines, trait_fn_lines):
+ with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait:
+ field_var_convs = []
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ field_var_convs.append((var_line.group(1), var_line.group(2)))
+ else:
+ field_var_convs.append(map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False))
+
+ field_fns = []
+ for fn_line in trait_fn_lines:
+ ret_ty_info = map_type(fn_line.group(2), True, None, False, False)
+ is_const = fn_line.group(4) is not None
+
+ arg_tys = []
+ for idx, arg in enumerate(fn_line.group(5).split(',')):
+ if arg == "":
+ continue
+ arg_conv_info = map_type(arg, True, None, False, False)
+ arg_tys.append(arg_conv_info)
+ field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys))
+
+ write_c(consts.native_c_map_trait(struct_name, field_var_convs, field_fns)[1])
+
+ out_java_trait.write(consts.hu_struct_file_prefix)
+ out_java_trait.write("public class " + struct_name.replace("LDK","") + " extends CommonBase {\n")
+ out_java_trait.write("\tfinal bindings." + struct_name + " bindings_instance;\n")
+ out_java_trait.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n")
+ out_java_trait.write("\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ out_java_trait.write(", bindings." + var_line.group(1) + " " + var_line.group(2))
+ else:
+ out_java_trait.write(", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2))
+ out_java_trait.write(") {\n")
+ out_java_trait.write("\t\tsuper(bindings." + struct_name + "_new(arg")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ out_java_trait.write(", " + var_line.group(2))
+ elif field_var_convs[idx].from_hu_conv is not None:
+ out_java_trait.write(", " + field_var_convs[idx].from_hu_conv[0])
+ else:
+ out_java_trait.write(", " + var_line.group(2))
+ out_java_trait.write("));\n")
+ out_java_trait.write("\t\tthis.ptrs_to.add(arg);\n")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ out_java_trait.write("\t\tthis.ptrs_to.add(" + var_line.group(2) + ");\n")
+ elif field_var_convs[idx].from_hu_conv is not None and field_var_convs[idx].from_hu_conv[1] != "":
+ out_java_trait.write("\t\t" + field_var_convs[idx].from_hu_conv[1] + ";\n")
+ out_java_trait.write("\t\tthis.bindings_instance = arg;\n")
+ out_java_trait.write("\t}\n")
+ out_java_trait.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
+ out_java_trait.write("\tprotected void finalize() throws Throwable {\n")
+ out_java_trait.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
+ out_java_trait.write("\t}\n\n")
+
+ java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
+ java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ # Ideally we'd be able to take any instance of the interface, but our C code can only represent
+ # Java-implemented version, so we require users pass a Java implementation here :/
+ java_trait_constr = java_trait_constr + ", " + var_line.group(1).replace("LDK", "") + "." + var_line.group(1).replace("LDK", "") + "Interface " + var_line.group(2) + "_impl"
+ else:
+ java_trait_constr = java_trait_constr + ", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2)
+ java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
+ java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n"
+ out_java_trait.write("\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n")
+ out_java.write("\tpublic interface " + struct_name + " {\n")
+ java_meths = []
+ for fn_line in trait_fn_lines:
+ java_meth_descr = "("
+ if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
+ ret_ty_info = map_type(fn_line.group(2), True, None, False, False)
+
+ out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(3) + "(")
+ java_trait_constr = java_trait_constr + "\t\t\t@Override public " + ret_ty_info.java_ty + " " + fn_line.group(3) + "("
+ out_java_trait.write("\t\t" + ret_ty_info.java_hu_ty + " " + fn_line.group(3) + "(")
+ is_const = fn_line.group(4) is not None
+
+ arg_names = []
+ for idx, arg in enumerate(fn_line.group(5).split(',')):
+ if arg == "":
+ continue
+ if idx >= 2:
+ out_java.write(", ")
+ java_trait_constr = java_trait_constr + ", "
+ out_java_trait.write(", ")
+ arg_conv_info = map_type(arg, True, None, False, False)
+ out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
+ out_java_trait.write(arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name)
+ java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
+ arg_names.append(arg_conv_info)
+ java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
+ java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
+ java_meths.append((fn_line, java_meth_descr))
+
+ out_java.write(");\n")
+ out_java_trait.write(");\n")
+ java_trait_constr = java_trait_constr + ") {\n"
+
+ for arg_info in arg_names:
+ if arg_info.to_hu_conv is not None:
+ java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
+
+ if ret_ty_info.java_ty != "void":
+ java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_hu_ty + " ret = arg." + fn_line.group(3) + "("
+ else:
+ java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(3) + "("
+
+ for idx, arg_info in enumerate(arg_names):
+ if idx != 0:
+ java_trait_constr = java_trait_constr + ", "
+ if arg_info.to_hu_conv_name is not None:
+ java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name
+ else:
+ java_trait_constr = java_trait_constr + arg_info.arg_name
+
+ java_trait_constr = java_trait_constr + ");\n"
+ if ret_ty_info.java_ty != "void":
+ if ret_ty_info.from_hu_conv is not None:
+ java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_ty + " result = " + ret_ty_info.from_hu_conv[0] + ";\n"
+ if ret_ty_info.from_hu_conv[1] != "":
+ java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
+ if ret_ty_info.rust_obj in result_types:
+ # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
+ java_trait_constr = java_trait_constr + "\t\t\t\tret.ptr = 0;\n"
+ java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n"
+ else:
+ java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n"
+ java_trait_constr = java_trait_constr + "\t\t\t}\n"
+ java_trait_constr = java_trait_constr + "\t\t}"
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ java_trait_constr = java_trait_constr + ", " + var_line.group(2) + ".new_impl(" + var_line.group(2) + "_impl).bindings_instance"
+ else:
+ java_trait_constr = java_trait_constr + ", " + var_line.group(2)
+ out_java_trait.write("\t}\n")
+ out_java_trait.write(java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n")
+
+ # Write out a clone function whether we need one or not, as we use them in moving to rust
+ write_c("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
+ write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
+ write_c("\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ write_c("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
+ write_c("\treturn (void*) this_arg;\n")
+ write_c("}\n")
+
+ out_java.write("\t}\n")
+
+ out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
+ write_c("static inline " + struct_name + " " + struct_name + "_init (" + consts.c_fn_args_pfx + ", jobject o")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
+ write_c(", jobject " + var_line.group(2))
+ else:
+ out_java.write(", " + field_var_convs[idx].java_ty + " " + var_line.group(2))
+ write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
+ out_java.write(");\n")
+ write_c(") {\n")
+
+ write_c("\tjclass c = (*env)->GetObjectClass(env, o);\n")
+ write_c("\tCHECK(c != NULL);\n")
+ write_c("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
+ write_c("\tatomic_init(&calls->refcnt, 1);\n")
+ write_c("\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n")
+ write_c("\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n")
+ for (fn_line, java_meth_descr) in java_meths:
+ if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
+ write_c("\tcalls->" + fn_line.group(3) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(3) + "\", \"" + java_meth_descr + "\");\n")
+ write_c("\tCHECK(calls->" + fn_line.group(3) + "_meth != NULL);\n")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) not in trait_structs and field_var_convs[idx].arg_conv is not None:
+ write_c("\n\t" + field_var_convs[idx].arg_conv.replace("\n", "\n\t") +"\n")
+ write_c("\n\t" + struct_name + " ret = {\n")
+ write_c("\t\t.this_arg = (void*) calls,\n")
+ for fn_line in trait_fn_lines:
+ if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
+ write_c("\t\t." + fn_line.group(3) + " = " + fn_line.group(3) + "_jcall,\n")
+ elif fn_line.group(3) == "free":
+ write_c("\t\t.free = " + struct_name + "_JCalls_free,\n")
+ else:
+ write_c("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ write_c("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, clz, " + var_line.group(2) + "),\n")
+ elif field_var_convs[idx].arg_conv_name is not None:
+ write_c("\t\t." + var_line.group(2) + " = " + field_var_convs[idx].arg_conv_name + ",\n")
+ write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
+ else:
+ write_c("\t\t." + var_line.group(2) + " = " + var_line.group(2) + ",\n")
+ write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
+ write_c("\t};\n")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ write_c("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
+ write_c("\treturn ret;\n")
+ write_c("}\n")
+
+ write_c(consts.c_fn_ty_pfx + "long " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1new (" + consts.c_fn_args_pfx + ", jobject o")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) in trait_structs:
+ write_c(", jobject " + var_line.group(2))
+ else:
+ write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
+ write_c(") {\n")
+ write_c("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
+ write_c("\t*res_ptr = " + struct_name + "_init(env, clz, o")
+ for var_line in field_var_lines:
+ write_c(", " + var_line.group(2))
+ write_c(");\n")
+ write_c("\treturn (long)res_ptr;\n")
+ write_c("}\n")
+
+ out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
+ write_c(consts.c_fn_ty_pfx + "jobject " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " val) {\n")
+ write_c("\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n")
+ write_c("\tCHECK(ret != NULL);\n")
+ write_c("\treturn ret;\n")
+ write_c("}\n")
+
+ for fn_line in trait_fn_lines:
+ # For now, just disable enabling the _call_log - we don't know how to inverse-map String
+ is_log = fn_line.group(3) == "log" and struct_name == "LDKLogger"
+ if fn_line.group(3) != "free" and fn_line.group(3) != "clone" and fn_line.group(3) != "eq" and not is_log:
+ dummy_line = fn_line.group(2) + struct_name.replace("LDK", "") + "_" + fn_line.group(3) + " " + struct_name + "* this_arg" + fn_line.group(5) + "\n"
+ map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, "(this_arg_conv->" + fn_line.group(3) + ")(this_arg_conv->this_arg")
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) not in trait_structs:
+ write_c(var_line.group(1) + " " + struct_name + "_set_get_" + var_line.group(2) + "(" + struct_name + "* this_arg) {\n")
+ write_c("\tif (this_arg->set_" + var_line.group(2) + " != NULL)\n")
+ write_c("\t\tthis_arg->set_" + var_line.group(2) + "(this_arg);\n")
+ write_c("\treturn this_arg->" + var_line.group(2) + ";\n")
+ write_c("}\n")
+ dummy_line = var_line.group(1) + " " + struct_name.replace("LDK", "") + "_get_" + var_line.group(2) + " " + struct_name + "* this_arg" + fn_line.group(5) + "\n"
+ map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, struct_name + "_set_get_" + var_line.group(2) + "(this_arg_conv")
+
+ def map_result(struct_name, res_ty, err_ty):
+ result_types.add(struct_name)
+ human_ty = struct_name.replace("LDKCResult", "Result")
+ with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct:
+ out_java_struct.write(consts.hu_struct_file_prefix)
+ out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
+ out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
+ out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
+ out_java_struct.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
+ out_java_struct.write("\t}\n\n")
+ out_java_struct.write("\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n")
+ out_java_struct.write("\t\tif (bindings." + struct_name + "_result_ok(ptr)) {\n")
+ out_java_struct.write("\t\t\treturn new " + human_ty + "_OK(null, ptr);\n")
+ out_java_struct.write("\t\t} else {\n")
+ out_java_struct.write("\t\t\treturn new " + human_ty + "_Err(null, ptr);\n")
+ out_java_struct.write("\t\t}\n")
+ out_java_struct.write("\t}\n")
+
+ res_map = map_type(res_ty + " res", True, None, False, True)
+ err_map = map_type(err_ty + " err", True, None, False, True)
+ can_clone = True
+ if not res_map.is_native_primitive and (res_map.rust_obj.replace("LDK", "") + "_clone" not in clone_fns):
+ can_clone = False
+ if not err_map.is_native_primitive and (err_map.rust_obj.replace("LDK", "") + "_clone" not in clone_fns):
+ can_clone = False
+
+ out_java.write("\tpublic static native boolean " + struct_name + "_result_ok(long arg);\n")
+ write_c(consts.c_fn_ty_pfx + "jboolean " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1result_1ok (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " arg) {\n")
+ write_c("\treturn ((" + struct_name + "*)arg)->result_ok;\n")
+ write_c("}\n")
+
+ out_java.write("\tpublic static native " + res_map.java_ty + " " + struct_name + "_get_ok(long arg);\n")
+ write_c(consts.c_fn_ty_pfx + res_map.c_ty + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1ok (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " arg) {\n")
+ write_c("\t" + struct_name + " *val = (" + struct_name + "*)arg;\n")
+ write_c("\tCHECK(val->result_ok);\n\t")
+ out_java_struct.write("\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n")
+ if res_map.ret_conv is not None:
+ write_c(res_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.result)")
+ write_c(res_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + res_map.ret_conv_name)
+ else:
+ write_c("return *val->contents.result")
+ write_c(";\n}\n")
+
+ if res_map.java_hu_ty != "void":
+ out_java_struct.write("\t\tpublic final " + res_map.java_hu_ty + " res;\n")
+ out_java_struct.write("\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n")
+ out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
+ if res_map.java_hu_ty == "void":
+ pass
+ elif res_map.to_hu_conv is not None:
+ out_java_struct.write("\t\t\t" + res_map.java_ty + " res = bindings." + struct_name + "_get_ok(ptr);\n")
+ out_java_struct.write("\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t"))
+ out_java_struct.write("\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n")
+ else:
+ out_java_struct.write("\t\t\tthis.res = bindings." + struct_name + "_get_ok(ptr);\n")
+ out_java_struct.write("\t\t}\n")
+ if struct_name.startswith("LDKCResult_None"):
+ out_java_struct.write("\t\tpublic " + human_ty + "_OK() {\n\t\t\tthis(null, bindings.C" + human_ty + "_ok());\n")
+ else:
+ out_java_struct.write("\t\tpublic " + human_ty + "_OK(" + res_map.java_hu_ty + " res) {\n")
+ if res_map.from_hu_conv is not None:
+ out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n")
+ if res_map.from_hu_conv[1] != "":
+ out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1] + ";\n")
+ else:
+ out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n")
+ out_java_struct.write("\t\t}\n\t}\n\n")
+
+ out_java.write("\tpublic static native " + err_map.java_ty + " " + struct_name + "_get_err(long arg);\n")
+ write_c(consts.c_fn_ty_pfx + err_map.c_ty + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1err (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " arg) {\n")
+ write_c("\t" + struct_name + " *val = (" + struct_name + "*)arg;\n")
+ write_c("\tCHECK(!val->result_ok);\n\t")
+ out_java_struct.write("\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n")
+ if err_map.ret_conv is not None:
+ write_c(err_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.err)")
+ write_c(err_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + err_map.ret_conv_name)
+ else:
+ write_c("return *val->contents.err")
+ write_c(";\n}\n")
+
+ if err_map.java_hu_ty != "void":
+ out_java_struct.write("\t\tpublic final " + err_map.java_hu_ty + " err;\n")
+ out_java_struct.write("\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n")
+ out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
+ if err_map.java_hu_ty == "void":
+ pass
+ elif err_map.to_hu_conv is not None:
+ out_java_struct.write("\t\t\t" + err_map.java_ty + " err = bindings." + struct_name + "_get_err(ptr);\n")
+ out_java_struct.write("\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t"))
+ out_java_struct.write("\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n")
+ else:
+ out_java_struct.write("\t\t\tthis.err = bindings." + struct_name + "_get_err(ptr);\n")
+ out_java_struct.write("\t\t}\n")
+
+ if struct_name.endswith("NoneZ"):
+ out_java_struct.write("\t\tpublic " + human_ty + "_Err() {\n\t\t\tthis(null, bindings.C" + human_ty + "_err());\n")
+ else:
+ out_java_struct.write("\t\tpublic " + human_ty + "_Err(" + err_map.java_hu_ty + " err) {\n")
+ if err_map.from_hu_conv is not None:
+ out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n")
+ if err_map.from_hu_conv[1] != "":
+ out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1] + ";\n")
+ else:
+ out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n")
+ out_java_struct.write("\t\t}\n\t}\n}\n")
+
+ if can_clone:
+ clone_fns.add(struct_name.replace("LDK", "") + "_clone")
+ write_c("static inline " + struct_name + " " + struct_name.replace("LDK", "") + "_clone(const " + struct_name + " *orig) {\n")
+ write_c("\t" + struct_name + " res = { .result_ok = orig->result_ok };\n")
+ write_c("\tif (orig->result_ok) {\n")
+ if res_map.c_ty == "void":
+ write_c("\t\tres.contents.result = NULL;\n")
+ else:
+ if res_map.is_native_primitive:
+ write_c("\t\t" + res_map.c_ty + "* contents = MALLOC(sizeof(" + res_map.c_ty + "), \"" + res_map.c_ty + " result OK clone\");\n")
+ write_c("\t\t*contents = *orig->contents.result;\n")
+ else:
+ write_c("\t\t" + res_map.rust_obj + "* contents = MALLOC(sizeof(" + res_map.rust_obj + "), \"" + res_map.rust_obj + " result OK clone\");\n")
+ write_c("\t\t*contents = " + res_map.rust_obj.replace("LDK", "") + "_clone(orig->contents.result);\n")
+ write_c("\t\tres.contents.result = contents;\n")
+ write_c("\t} else {\n")
+ if err_map.c_ty == "void":
+ write_c("\t\tres.contents.err = NULL;\n")
+ else:
+ if err_map.is_native_primitive:
+ write_c("\t\t" + err_map.c_ty + "* contents = MALLOC(sizeof(" + err_map.c_ty + "), \"" + err_map.c_ty + " result Err clone\");\n")
+ write_c("\t\t*contents = *orig->contents.err;\n")
+ else:
+ write_c("\t\t" + err_map.rust_obj + "* contents = MALLOC(sizeof(" + err_map.rust_obj + "), \"" + err_map.rust_obj + " result Err clone\");\n")
+ write_c("\t\t*contents = " + err_map.rust_obj.replace("LDK", "") + "_clone(orig->contents.err);\n")
+ write_c("\t\tres.contents.err = contents;\n")
+ write_c("\t}\n\treturn res;\n}\n")
+
+ def map_tuple(struct_name, field_lines):
+ out_java.write("\tpublic static native long " + struct_name + "_new(")
+ write_c(consts.c_fn_ty_pfx + consts.ptr_c_ty + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1new(" + consts.c_fn_args_pfx)
+ ty_list = []
+ for idx, line in enumerate(field_lines):
+ if idx != 0 and idx < len(field_lines) - 2:
+ ty_info = java_c_types(line.strip(';'), None)
+ if idx != 1:
+ out_java.write(", ")
+ e = chr(ord('a') + idx - 1)
+ out_java.write(ty_info.java_ty + " " + e)
+ write_c(", " + ty_info.c_ty + " " + e)
+ ty_list.append(ty_info)
+ tuple_types[struct_name] = (ty_list, struct_name)
+ out_java.write(");\n")
+ write_c(") {\n")
+ write_c("\t" + struct_name + "* ret = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
+ can_clone = True
+ clone_str = "static inline " + struct_name + " " + struct_name.replace("LDK", "") + "_clone(const " + struct_name + " *orig) {\n"
+ clone_str = clone_str + "\t" + struct_name + " ret = {\n"
+ for idx, line in enumerate(field_lines):
+ if idx != 0 and idx < len(field_lines) - 2:
+ ty_info = map_type(line.strip(';'), False, None, False, False)
+ e = chr(ord('a') + idx - 1)
+ if ty_info.arg_conv is not None:
+ write_c("\t" + ty_info.arg_conv.replace("\n", "\n\t"))
+ write_c("\n\tret->" + e + " = " + ty_info.arg_conv_name + ";\n")
+ else:
+ write_c("\tret->" + e + " = " + e + ";\n")
+ if ty_info.arg_conv_cleanup is not None:
+ write_c("\t//TODO: Really need to call " + ty_info.arg_conv_cleanup + " here\n")
+ if not ty_info.is_native_primitive and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in clone_fns:
+ can_clone = False
+ elif can_clone and ty_info.is_native_primitive:
+ clone_str = clone_str + "\t\t." + chr(ord('a') + idx - 1) + " = orig->" + chr(ord('a') + idx - 1) + ",\n"
+ elif can_clone:
+ clone_str = clone_str + "\t\t." + chr(ord('a') + idx - 1) + " = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&orig->" + chr(ord('a') + idx - 1) + "),\n"
+ write_c("\treturn (long)ret;\n")
+ write_c("}\n")
+
+ if can_clone:
+ clone_fns.add(struct_name.replace("LDK", "") + "_clone")
+ write_c(clone_str)
+ write_c("\t};\n\treturn ret;\n}\n")
+
+ for idx, ty_info in enumerate(ty_list):
+ e = chr(ord('a') + idx)
+ out_java.write("\tpublic static native " + ty_info.java_ty + " " + struct_name + "_get_" + e + "(long ptr);\n")
+ write_c(consts.c_fn_ty_pfx + ty_info.c_ty + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1" + e + "(" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " ptr) {\n")
+ write_c("\t" + struct_name + " *tuple = (" + struct_name + "*)ptr;\n")
+ conv_info = map_type_with_info(ty_info, False, None, False, True)
+ if conv_info.ret_conv is not None:
+ write_c("\t" + conv_info.ret_conv[0].replace("\n", "\n\t") + "tuple->" + e + conv_info.ret_conv[1].replace("\n", "\n\t") + "\n")
+ write_c("\treturn " + conv_info.ret_conv_name + ";\n")
+ else:
+ write_c("\treturn tuple->" + e + ";\n")
+ write_c("}\n")
+
+ out_java.write("""package org.ldk.impl;
+import org.ldk.enums.*;