+ arg_names = []
+ for idx, arg in enumerate(fn_line.group(4).split(',')):
+ if arg == "":
+ continue
+ 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")
+ out_c.write("\tJNIEnv *env;\n")
+ out_c.write("\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:
+ out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t').replace("_env", "env"));
+ out_c.write(arg_info.arg_name)
+ out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t').replace("_env", "env") + "\n")
+
+ out_c.write("\tjobject obj = (*env)->NewLocalRef(env, j_calls->o);\n\tDO_ASSERT(obj != NULL);\n")
+ if ret_ty_info.c_ty.endswith("Array"):
+ assert(ret_ty_info.c_ty == "jbyteArray")
+ out_c.write("\tjbyteArray jret = (*env)->CallObjectMethod(env, obj, j_calls->" + fn_line.group(2) + "_meth")
+ elif not ret_ty_info.passed_as_ptr:
+ out_c.write("\treturn (*env)->Call" + ret_ty_info.java_ty.title() + "Method(env, obj, j_calls->" + fn_line.group(2) + "_meth")
+ else:
+ out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*env)->CallLongMethod(env, obj, 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.c_ty.endswith("Array"):
+ out_c.write("\t" + ret_ty_info.rust_obj + " ret;\n")
+ out_c.write("\tDO_ASSERT((*env)->GetArrayLength(env, jret) == " + ret_ty_info.arr_len + ");\n")
+ out_c.write("\t(*env)->GetByteArrayRegion(env, jret, 0, " + ret_ty_info.arr_len + ", ret." + ret_ty_info.arr_access + ");\n")
+ out_c.write("\treturn ret;\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\tJNIEnv *env;\n")
+ out_c.write("\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n")
+ out_c.write("\t\t(*env)->DeleteWeakGlobalRef(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))
+ out_java.write(");\n")
+ out_c.write(") {\n")
+
+ out_c.write("\tjclass c = (*env)->GetObjectClass(env, o);\n")
+ out_c.write("\tDO_ASSERT(c != NULL);\n")
+ out_c.write("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
+ out_c.write("\tatomic_init(&calls->refcnt, 1);\n")
+ out_c.write("\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n")
+ out_c.write("\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n")
+ for (fn_line, java_meth_descr) in zip(trait_fn_lines, java_meths):
+ if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
+ out_c.write("\tcalls->" + fn_line.group(2) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(2) + "\", \"" + java_meth_descr + "\");\n")
+ out_c.write("\tDO_ASSERT(calls->" + fn_line.group(2) + "_meth != NULL);\n")
+ out_c.write("\n\t" + struct_name + " ret = {\n")
+ out_c.write("\t\t.this_arg = (void*) calls,\n")
+ for fn_line in trait_fn_lines:
+ if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
+ out_c.write("\t\t." + fn_line.group(2) + " = " + fn_line.group(2) + "_jcall,\n")
+ elif fn_line.group(2) == "free":
+ out_c.write("\t\t.free = " + struct_name + "_JCalls_free,\n")
+ else:
+ out_c.write("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ out_c.write("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, _a, " + var_line.group(2) + "),\n")
+ out_c.write("\t};\n")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ out_c.write("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
+ out_c.write("\treturn ret;\n")
+ out_c.write("}\n")
+
+ out_c.write("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, jobject o")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ out_c.write(", jobject " + var_line.group(2))
+ out_c.write(") {\n")
+ out_c.write("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
+ out_c.write("\t*res_ptr = " + struct_name + "_init(env, _a, o")
+ for var_line in field_var_lines:
+ if var_line.group(1) in trait_structs:
+ out_c.write(", " + var_line.group(2))
+ out_c.write(");\n")
+ out_c.write("\treturn (long)res_ptr;\n")
+ out_c.write("}\n")
+
+ out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
+ out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (JNIEnv * env, jclass _a, jlong val) {\n")
+ out_c.write("\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n")
+ out_c.write("\tDO_ASSERT(ret != NULL);\n")
+ out_c.write("\treturn ret;\n")
+ out_c.write("}\n")