- if idx >= 2:
- out_java.write(", ")
- out_c.write(", ")
- arg_conv_info = map_type(arg, True, None, False)
- out_c.write(arg.strip())
- out_java.write(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(java_meth_descr)
-
- out_java.write(");\n")
- out_c.write(") {\n")
- out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
-
- for arg_info in arg_names:
- if arg_info.ret_conv is not None:
- out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t').replace("_env", "j_calls->env"));
- out_c.write(arg_info.arg_name)
- out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t').replace("_env", "j_calls->env") + "\n")
-
- if not ret_ty_info.passed_as_ptr:
- out_c.write("\treturn (*j_calls->env)->Call" + ret_ty_info.java_ty.title() + "Method(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth")
- else:
- out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth");
-
- for arg_info in arg_names:
- if arg_info.ret_conv is not None:
- out_c.write(", " + arg_info.ret_conv_name)
- else:
- out_c.write(", " + arg_info.arg_name)
- out_c.write(");\n");
-
- if ret_ty_info.passed_as_ptr:
- out_c.write("\t" + fn_line.group(1).strip() + " res = *ret;\n")
- out_c.write("\tFREE(ret);\n")
- out_c.write("\treturn res;\n")
- out_c.write("}\n")
- elif fn_line.group(2) == "free":
- out_c.write("static void " + struct_name + "_JCalls_free(void* this_arg) {\n")
- out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
- out_c.write("\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n")
- out_c.write("\t\t(*j_calls->env)->DeleteGlobalRef(j_calls->env, j_calls->o);\n")
- out_c.write("\t\tFREE(j_calls);\n")
- out_c.write("\t}\n}\n")
-
- # Write out a clone function whether we need one or not, as we use them in moving to rust
- out_c.write("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
- out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
- out_c.write("\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:
- out_c.write("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
- out_c.write("\treturn (void*) this_arg;\n")
- out_c.write("}\n")
-
- out_java.write("\t}\n")
-
- out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
- out_c.write("static inline " + struct_name + " " + struct_name + "_init (JNIEnv * env, jclass _a, jobject o")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
- out_c.write(", jobject " + var_line.group(2))
+ arg_conv_info = type_mapping_generator.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, fn_docs))
+
+ (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, flattened_field_var_convs, field_fns, trait_doc_comment)
+ write_c(out_c_addendum)
+ out_java_trait.write(out_java_trait_addendum)
+ out_java.write(out_java_addendum)
+
+ for fn_docs, 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 + " *NONNULL_PTR 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", fn_docs)
+ 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 + " *NONNULL_PTR 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", fn_docs)
+
+ 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 = type_mapping_generator.map_type(res_ty + " res", True, None, False, True)
+ err_map = type_mapping_generator.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_define_pfx(struct_name + "_result_ok", True) + 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_define_pfx(struct_name + "_get_ok", True) + consts.ptr_c_ty + " arg) {\n")
+ write_c("\t" + struct_name + " *val = (" + struct_name + "*)(arg & ~1);\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")
+ out_java_struct.write("\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_define_pfx(struct_name + "_get_err", True) + consts.ptr_c_ty + " arg) {\n")
+ write_c("\t" + struct_name + " *val = (" + struct_name + "*)(arg & ~1);\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")
+
+ out_java_struct.write("\t}\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_define_pfx(struct_name + "_new", len(field_lines) > 3))
+ 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(", ")
+ write_c(", ")
+ 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)