+ 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(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum:
+ out_java_enum.write("package org.ldk.enums;\n\n")
+ unitary_enums.add(struct_name)
+ write_c("static inline " + struct_name + " " + struct_name + "_from_java(JNIEnv *env, jclass val) {\n")
+ write_c("\tswitch ((*env)->CallIntMethod(env, val, ordinal_meth)) {\n")
+ ord_v = 0
+ for idx, struct_line in enumerate(field_lines):
+ if idx == 0:
+ out_java_enum.write("public enum " + struct_name + " {\n")
+ elif idx == len(field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(field_lines) - 2:
+ out_java_enum.write("\t; static native void init();\n")
+ out_java_enum.write("\tstatic { init(); }\n")
+ out_java_enum.write("}")
+ out_java.write("\tstatic { " + struct_name + ".values(); /* Force enum statics to run */ }\n")
+ elif idx == len(field_lines) - 1:
+ assert(struct_line == "")
+ else:
+ out_java_enum.write(struct_line + "\n")
+ write_c("\t\tcase %d: return %s;\n" % (ord_v, struct_line.strip().strip(",")))
+ ord_v = ord_v + 1
+ write_c("\t}\n")
+ write_c("\tabort();\n")
+ write_c("}\n")
+
+ ord_v = 0
+ write_c("static jclass " + struct_name + "_class = NULL;\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ write_c("static jfieldID " + struct_name + "_" + variant + " = NULL;\n")
+ write_c("JNIEXPORT void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass clz) {\n")
+ write_c("\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n")
+ write_c("\tCHECK(" + struct_name + "_class != NULL);\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ write_c("\t" + struct_name + "_" + variant + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + variant + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n")
+ write_c("\tCHECK(" + struct_name + "_" + variant + " != NULL);\n")
+ write_c("}\n")
+ write_c("static inline jclass " + struct_name + "_to_java(JNIEnv *env, " + struct_name + " val) {\n")
+ write_c("\tswitch (val) {\n")
+ for idx, struct_line in enumerate(field_lines):
+ if idx > 0 and idx < len(field_lines) - 3:
+ variant = struct_line.strip().strip(",")
+ write_c("\t\tcase " + variant + ":\n")
+ write_c("\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + variant + ");\n")
+ ord_v = ord_v + 1
+ write_c("\t\tdefault: abort();\n")
+ write_c("\t}\n")
+ write_c("}\n\n")
+
+ def map_complex_enum(struct_name, union_enum_items):
+ java_hu_type = struct_name.replace("LDK", "")
+ complex_enums.add(struct_name)
+ with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
+ out_java_enum.write(hu_struct_file_prefix)
+ out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
+ out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
+ out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
+ out_java_enum.write("\tprotected void finalize() throws Throwable {\n")
+ out_java_enum.write("\t\tsuper.finalize();\n")
+ out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n")
+ out_java_enum.write("\t}\n")
+ out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
+ out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
+ java_hu_subclasses = ""
+
+ tag_field_lines = union_enum_items["field_lines"]
+ init_meth_jty_strs = {}
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx == 0:
+ out_java.write("\tpublic static class " + struct_name + " {\n")
+ out_java.write("\t\tprivate " + struct_name + "() {}\n")
+ elif idx == len(tag_field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(tag_field_lines) - 2:
+ out_java.write("\t\tstatic native void init();\n")
+ out_java.write("\t}\n")
+ elif idx == len(tag_field_lines) - 1:
+ assert(struct_line == "")
+ else:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
+ java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
+ out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
+ out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n")
+ write_c("static jclass " + struct_name + "_" + var_name + "_class = NULL;\n")
+ write_c("static jmethodID " + struct_name + "_" + var_name + "_meth = NULL;\n")
+ init_meth_jty_str = ""
+ init_meth_params = ""
+ init_meth_body = ""
+ hu_conv_body = ""
+ if "LDK" + var_name in union_enum_items:
+ enum_var_lines = union_enum_items["LDK" + var_name]
+ for idx, field in enumerate(enum_var_lines):
+ if idx != 0 and idx < len(enum_var_lines) - 2:
+ field_ty = map_type(field.strip(' ;'), False, None, False, True)
+ out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
+ java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
+ if field_ty.to_hu_conv is not None:
+ hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
+ hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
+ hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
+ else:
+ hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
+ init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
+ if idx > 1:
+ init_meth_params = init_meth_params + ", "
+ init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
+ init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
+ out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
+ out_java.write(init_meth_body)
+ out_java.write("}\n")
+ out_java.write("\t\t}\n")
+ out_java_enum.write("\t\t}\n")
+ java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
+ java_hu_subclasses = java_hu_subclasses + hu_conv_body
+ java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n"
+ init_meth_jty_strs[var_name] = init_meth_jty_str
+ out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
+ out_java_enum.write(java_hu_subclasses)
+ out_java.write("\tstatic { " + struct_name + ".init(); }\n")
+ out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
+
+ write_c("JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (JNIEnv * env, jclass _a) {\n")
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx != 0 and idx < len(tag_field_lines) - 3:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ write_c("\t" + struct_name + "_" + var_name + "_class =\n")
+ write_c("\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n")
+ write_c("\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n")
+ write_c("\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n")
+ write_c("\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n")
+ write_c("}\n")
+ write_c("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (JNIEnv * _env, jclass _c, jlong ptr) {\n")
+ write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+ write_c("\tswitch(obj->tag) {\n")
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx != 0 and idx < len(tag_field_lines) - 3:
+ var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+ write_c("\t\tcase " + struct_name + "_" + var_name + ": {\n")
+ c_params_text = ""
+ if "LDK" + var_name in union_enum_items:
+ enum_var_lines = union_enum_items["LDK" + var_name]
+ for idx, field in enumerate(enum_var_lines):
+ if idx != 0 and idx < len(enum_var_lines) - 2:
+ field_map = map_type(field.strip(' ;'), False, None, False, True)
+ if field_map.ret_conv is not None:
+ write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
+ write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
+ write_c(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
+ c_params_text = c_params_text + ", " + field_map.ret_conv_name
+ else:
+ c_params_text = c_params_text + ", obj->" + camel_to_snake(var_name) + "." + field_map.arg_name
+ write_c("\t\t\treturn (*_env)->NewObject(_env, " + struct_name + "_" + var_name + "_class, " + struct_name + "_" + var_name + "_meth" + c_params_text + ");\n")
+ write_c("\t\t}\n")
+ write_c("\t\tdefault: abort();\n")
+ write_c("\t}\n}\n")
+ out_java_enum.write("}\n")
+
+ def map_trait(struct_name, field_var_lines, trait_fn_lines):
+ with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + ".java", "w") as out_java_trait:
+ write_c("typedef struct " + struct_name + "_JCalls {\n")
+ write_c("\tatomic_size_t refcnt;\n")
+ write_c("\tJavaVM *vm;\n")
+ write_c("\tjweak o;\n")
+ field_var_convs = []
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ write_c("\t" + var_line.group(1) + "_JCalls* " + var_line.group(2) + ";\n")
+ field_var_convs.append(None)
+ else:
+ field_var_convs.append(map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False))
+ for fn_line in trait_fn_lines:
+ if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
+ write_c("\tjmethodID " + fn_line.group(3) + "_meth;\n")
+ write_c("} " + struct_name + "_JCalls;\n")
+
+ out_java_trait.write(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
+ write_c(fn_line.group(2) + fn_line.group(3) + "_jcall(")
+ if is_const:
+ write_c("const void* this_arg")
+ else:
+ write_c("void* this_arg")
+
+ 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(", ")
+ write_c(", ")
+ arg_conv_info = map_type(arg, True, None, False, False)
+ write_c(arg.strip())
+ 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"
+ write_c(") {\n")
+ write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
+ write_c("\tJNIEnv *_env;\n")
+ write_c("\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&_env, JNI_VERSION_1_8) == JNI_OK);\n")
+
+ for arg_info in arg_names:
+ if arg_info.ret_conv is not None:
+ write_c("\t" + arg_info.ret_conv[0].replace('\n', '\n\t'));
+ write_c(arg_info.arg_name)
+ write_c(arg_info.ret_conv[1].replace('\n', '\n\t') + "\n")
+ 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"
+
+ write_c("\tjobject obj = (*_env)->NewLocalRef(_env, j_calls->o);\n\tCHECK(obj != NULL);\n")
+ if ret_ty_info.c_ty.endswith("Array"):
+ write_c("\t" + ret_ty_info.c_ty + " arg = (*_env)->CallObjectMethod(_env, obj, j_calls->" + fn_line.group(3) + "_meth")
+ elif not ret_ty_info.passed_as_ptr:
+ write_c("\treturn (*_env)->Call" + ret_ty_info.java_ty.title() + "Method(_env, obj, j_calls->" + fn_line.group(3) + "_meth")
+ else:
+ write_c("\t" + fn_line.group(2).strip() + "* ret = (" + fn_line.group(2).strip() + "*)(*_env)->CallLongMethod(_env, obj, j_calls->" + fn_line.group(3) + "_meth");
+ 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 arg_info.ret_conv is not None:
+ write_c(", " + arg_info.ret_conv_name)
+ else:
+ write_c(", " + arg_info.arg_name)
+ 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
+ write_c(");\n");
+ if ret_ty_info.arg_conv is not None:
+ write_c("\t" + ret_ty_info.arg_conv.replace("\n", "\n\t") + "\n\treturn " + ret_ty_info.arg_conv_name + ";\n")
+
+ write_c("}\n")
+ 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"
+ elif fn_line.group(3) == "free":
+ write_c("static void " + struct_name + "_JCalls_free(void* this_arg) {\n")
+ write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
+ write_c("\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n")
+ write_c("\t\tJNIEnv *env;\n")
+ write_c("\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n")
+ write_c("\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n")
+ write_c("\t\tFREE(j_calls);\n")
+ write_c("\t}\n}\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 (JNIEnv * env, jclass _a, 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 field_var_convs[idx] is not None 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, _a, " + 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("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, 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, _a, 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("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (JNIEnv * env, jclass _a, jlong 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(sys.argv[3] + "/structs/" + human_ty + ".java", "w") as out_java_struct:
+ out_java_struct.write(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("JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1result_1ok (JNIEnv * env, jclass _a, jlong 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("JNIEXPORT " + res_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1ok (JNIEnv * _env, jclass _a, jlong 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("JNIEXPORT " + err_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1err (JNIEnv * _env, jclass _a, jlong 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("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new(JNIEnv *_env, jclass _b")
+ 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("JNIEXPORT " + ty_info.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1" + e + "(JNIEnv *_env, jclass _b, jlong 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")