+ write_c(";")
+ for info in arg_names:
+ if info.arg_conv_cleanup is not None:
+ write_c("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
+ if ret_info.ret_conv is not None:
+ write_c("\n\treturn " + ret_info.ret_conv_name + ";")
+ elif ret_info.c_ty != "void":
+ write_c("\n\treturn ret_val;")
+ write_c("\n}\n\n")
+ if out_java_struct is not None:
+ out_java_struct.write("\t\t")
+ if ret_info.java_ty != "void":
+ out_java_struct.write(ret_info.java_ty + " ret = ")
+ out_java_struct.write("bindings." + re_match.group(2) + "(")
+ for idx, info in enumerate(arg_names):
+ if idx != 0:
+ out_java_struct.write(", ")
+ if idx == 0 and takes_self:
+ out_java_struct.write("this.ptr")
+ elif info.arg_name in default_constructor_args:
+ out_java_struct.write("bindings." + info.java_hu_ty + "_new(")
+ for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]):
+ if explode_idx != 0:
+ out_java_struct.write(", ")
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ if explode_arg.from_hu_conv is not None:
+ out_java_struct.write(explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name))
+ else:
+ out_java_struct.write(expl_arg_name)
+ out_java_struct.write(")")
+ elif info.from_hu_conv is not None:
+ out_java_struct.write(info.from_hu_conv[0])
+ else:
+ out_java_struct.write(info.arg_name)
+ out_java_struct.write(");\n")
+ if ret_info.to_hu_conv is not None:
+ if not takes_self:
+ out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t").replace("this", ret_info.to_hu_conv_name) + "\n")
+ else:
+ out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n")
+
+ for idx, info in enumerate(arg_names):
+ if idx == 0 and takes_self:
+ pass
+ elif info.arg_name in default_constructor_args:
+ for explode_arg in default_constructor_args[info.arg_name]:
+ expl_arg_name = info.arg_name + "_" + explode_arg.arg_name
+ if explode_arg.from_hu_conv is not None and ret_info.to_hu_conv_name:
+ out_java_struct.write("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name, expl_arg_name).replace("this", ret_info.to_hu_conv_name) + ";\n")
+ elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
+ if not takes_self and ret_info.to_hu_conv_name is not None:
+ out_java_struct.write("\t\t" + info.from_hu_conv[1].replace("this", ret_info.to_hu_conv_name) + ";\n")
+ else:
+ out_java_struct.write("\t\t" + info.from_hu_conv[1] + ";\n")
+
+ if ret_info.to_hu_conv_name is not None:
+ out_java_struct.write("\t\treturn " + ret_info.to_hu_conv_name + ";\n")
+ elif ret_info.java_ty != "void" and ret_info.rust_obj != "LDK" + struct_meth:
+ out_java_struct.write("\t\treturn ret;\n")
+ out_java_struct.write("\t}\n\n")
+ out_java_struct.close()
+
+ def map_unitary_enum(struct_name, field_lines):
+ with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum:
+ unitary_enums.add(struct_name)
+ for idx, struct_line in enumerate(field_lines):
+ if idx == 0:
+ assert(struct_line == "typedef enum %s {" % struct_name)
+ elif idx == len(field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(field_lines) - 2:
+ assert(struct_line == "} %s;" % struct_name)
+ elif idx == len(field_lines) - 1:
+ assert(struct_line == "")
+ (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name, [x.strip().strip(",") for x in field_lines[1:-3]])
+ write_c(c_out)
+ out_java_enum.write(native_file_out)
+ out_java.write(native_out)
+
+ def map_complex_enum(struct_name, union_enum_items):
+ java_hu_type = struct_name.replace("LDK", "")
+ complex_enums.add(struct_name)
+
+ enum_variants = []
+ tag_field_lines = union_enum_items["field_lines"]
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx == 0:
+ assert(struct_line == "typedef enum %s_Tag {" % struct_name)
+ elif idx == len(tag_field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(tag_field_lines) - 2:
+ assert(struct_line == "} %s_Tag;" % struct_name)
+ elif idx == len(tag_field_lines) - 1:
+ assert(struct_line == "")
+ else:
+ variant_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ fields = []
+ if "LDK" + variant_name in union_enum_items:
+ enum_var_lines = union_enum_items["LDK" + variant_name]
+ for idx, field in enumerate(enum_var_lines):
+ if idx != 0 and idx < len(enum_var_lines) - 2:
+ fields.append(map_type(field.strip(' ;'), False, None, False, True))
+ else:
+ # TODO: Assert line format
+ pass
+ else:
+ # TODO: Assert line format
+ pass
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, fields))
+
+ 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))
+
+ (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, field_fns)
+ write_c(out_c_addendum)
+ out_java_trait.write(out_java_trait_addendum)
+ out_java.write(out_java_addendum)
+
+ 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")