- 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("\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")
+ write_c("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*_env)->CallLongMethod(_env, obj, j_calls->" + fn_line.group(2) + "_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(2) + "("
+ else:
+ java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(2) + "("
+
+ 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 is_common_base_ext(ret_ty_info.rust_obj):
+ 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(2) == "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 zip(trait_fn_lines, java_meths):
+ if fn_line.group(2) != "free" and fn_line.group(2) != "clone":
+ write_c("\tcalls->" + fn_line.group(2) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(2) + "\", \"" + java_meth_descr + "\");\n")
+ write_c("\tCHECK(calls->" + fn_line.group(2) + "_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(2) != "free" and fn_line.group(2) != "clone":
+ write_c("\t\t." + fn_line.group(2) + " = " + fn_line.group(2) + "_jcall,\n")
+ elif fn_line.group(2) == "free":
+ write_c("\t\t.free = " + struct_name + "_JCalls_free,\n")
+ else:
+ clone_fns.add(struct_name + "_clone")
+ 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")