- 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")
-
- 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", "j_calls->env"));
- out_c.write(arg_info.arg_name)
- out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t').replace("_env", "j_calls->env") + "\n")
-
- if not ret_ty_info.passed_as_ptr:
- out_c.write("\treturn (*j_calls->env)->Call" + ret_ty_info.java_ty.title() + "Method(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth")
- else:
- out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, 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.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")
- 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("""package org.ldk.impl;
-
-public class bindings {
- static {
- System.loadLibrary(\"lightningjni\");
- }
-
-""")
- out_c.write("#include \"org_ldk_impl_bindings.h\"\n")
- out_c.write("#include <rust_types.h>\n")
- out_c.write("#include <lightning.h>\n")
- out_c.write("#include <assert.h>\n")
- out_c.write("#include <string.h>\n")
- out_c.write("#include <stdatomic.h>\n\n")
- if sys.argv[4] == "false":
- out_c.write("#define MALLOC(a, _) malloc(a)\n")
- out_c.write("#define FREE free\n\n")
- else:
- out_c.write("#include <threads.h>\n")
- out_c.write("static mtx_t allocation_mtx;\n\n")
- out_c.write("void __attribute__((constructor)) init_mtx() {\n")
- out_c.write("\tassert(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);\n")
- out_c.write("}\n\n")
- out_c.write("typedef struct allocation {\n")
- out_c.write("\tstruct allocation* next;\n")
- out_c.write("\tvoid* ptr;\n")
- out_c.write("\tconst char* struct_name;\n")
- out_c.write("} allocation;\n")
- out_c.write("static allocation* allocation_ll = NULL;\n\n")
- out_c.write("void* MALLOC(size_t len, const char* struct_name) {\n")
- out_c.write("\tvoid* res = malloc(len);\n")
- out_c.write("\tallocation* new_alloc = malloc(sizeof(allocation));\n")
- out_c.write("\tnew_alloc->ptr = res;\n")
- out_c.write("\tnew_alloc->struct_name = struct_name;\n")
- out_c.write("\tassert(mtx_lock(&allocation_mtx) == thrd_success);\n")
- out_c.write("\tnew_alloc->next = allocation_ll;\n")
- out_c.write("\tallocation_ll = new_alloc;\n")
- out_c.write("\tassert(mtx_unlock(&allocation_mtx) == thrd_success);\n")
- out_c.write("\treturn res;\n")
- out_c.write("}\n\n")
- out_c.write("void FREE(void* ptr) {\n")
- out_c.write("\tallocation* p = NULL;\n")
- out_c.write("\tassert(mtx_lock(&allocation_mtx) == thrd_success);\n")
- out_c.write("\tallocation* it = allocation_ll;\n")
- out_c.write("\twhile (it->ptr != ptr) { p = it; it = it->next; }\n")
- out_c.write("\tif (p) { p->next = it->next; } else { allocation_ll = it->next; }\n")
- out_c.write("\tassert(mtx_unlock(&allocation_mtx) == thrd_success);\n")
- out_c.write("\tassert(it->ptr == ptr);\n")
- out_c.write("\tfree(it);\n")
- out_c.write("\tfree(ptr);\n")
- out_c.write("}\n\n")
- out_c.write("void __attribute__((destructor)) check_leaks() {\n")
- out_c.write("\tfor (allocation* a = allocation_ll; a != NULL; a = a->next) { fprintf(stderr, \"%s %p remains\\n\", a->struct_name, a->ptr); }\n")
- out_c.write("\tassert(allocation_ll == NULL);\n")
- out_c.write("}\n\n")
-
- # XXX: Temporarily write out a manual SecretKey_new() for testing, we should auto-gen this kind of thing
- out_java.write("\tpublic static native long LDKSecretKey_new();\n\n") # TODO: rm me
- out_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_LDKSecretKey_1new(JNIEnv * _env, jclass _b) {\n") # TODO: rm me
- out_c.write("\tLDKSecretKey* key = (LDKSecretKey*)MALLOC(sizeof(LDKSecretKey), \"LDKSecretKey\");\n") # TODO: rm me
- out_c.write("\treturn (long)key;\n") # TODO: rm me
- out_c.write("}\n") # TODO: rm me
-
- in_block_comment = False
- in_block_enum = False
- cur_block_struct = None
- in_block_union = False
-
- fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
- fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
- reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")