- 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))
- out_java.write(");\n")
- out_c.write(") {\n")
-
- out_c.write("\tjclass c = (*env)->GetObjectClass(env, o);\n")
- out_c.write("\tassert(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("\tcalls->env = env;\n")
- out_c.write("\tcalls->o = (*env)->NewGlobalRef(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("\tassert(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")
+ java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(3) + "("
+
+ for idx, arg_info in enumerate(arg_names):
+ 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
+
+ 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"
+ 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 (" + consts.c_fn_args_pfx + ", 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 var_line.group(1) not in trait_structs 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, clz, " + 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(consts.c_fn_ty_pfx + "long " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1new (" + consts.c_fn_args_pfx + ", 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, clz, 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(consts.c_fn_ty_pfx + "jobject " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " 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")
+