- 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) + "("