- 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("\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("\treturn ((" + struct_name + "_JCalls*)val)->o;\n")
- out_c.write("}\n")
-
-
- out_java.write("""package org.ldk.impl;
-
-public class bindings {
- static native void init(java.lang.Class c);
- static {
- System.loadLibrary(\"lightningjni\");
- init(java.lang.Enum.class);
- }
-
-""")
- 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")
-
- out_c.write("jmethodID ordinal_meth = NULL;\n")
- out_c.write("JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class) {\n")
- out_c.write("\tordinal_meth = (*env)->GetMethodID(env, enum_class, \"ordinal\", \"()I\");\n")
- out_c.write("\tassert(ordinal_meth != NULL);\n")
- out_c.write("}\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
+ ret_ty_info = type_mapping_generator.map_type(fn_line.group(2).strip() + " ret", True, None, False, False)
+ is_const = fn_line.group(4) is not None
+
+ arg_tys = []
+ for idx, arg in enumerate(fn_line.group(5).split(',')):
+ if arg == "":
+ continue
+ arg_ty_info = type_mapping_generator.java_c_types(arg, None)
+ if arg_ty_info.var_name in nullable_params:
+ # Types that are actually null instead of all-0s aren't yet handled on the Java side:
+ arg_conv_info = type_mapping_generator.map_type_with_info(arg_ty_info, True, None, False, False, True)
+ else:
+ arg_conv_info = type_mapping_generator.map_type_with_info(arg_ty_info, True, None, False, False, False)
+ arg_tys.append(arg_conv_info)
+ field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys, fn_docs))
+
+ (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, flattened_field_var_convs, field_fns, trait_doc_comment)
+ write_c(out_c_addendum)
+ out_java_trait.write(out_java_trait_addendum)
+ out_java.write(out_java_addendum)
+
+ for fn_docs, fn_line in trait_fn_lines:
+ if fn_line == "cloned":
+ continue
+ # For now, just disable enabling the _call_log - we don't know how to inverse-map String
+ is_log = fn_line.group(3) == "log" and struct_name == "LDKLogger"
+ if fn_line.group(3) != "free" and fn_line.group(3) != "eq" and not is_log:
+ dummy_line = fn_line.group(2) + struct_name.replace("LDK", "") + "_" + fn_line.group(3) + " " + struct_name + " *NONNULL_PTR this_arg" + fn_line.group(5) + "\n"
+ map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, "(this_arg_conv->" + fn_line.group(3) + ")(this_arg_conv->this_arg", fn_docs)
+ for idx, var_line in enumerate(field_var_lines):
+ if var_line.group(1) not in trait_structs:
+ write_c(var_line.group(1) + " " + struct_name + "_set_get_" + var_line.group(2) + "(" + struct_name + "* this_arg) {\n")
+ write_c("\tif (this_arg->set_" + var_line.group(2) + " != NULL)\n")
+ write_c("\t\tthis_arg->set_" + var_line.group(2) + "(this_arg);\n")
+ write_c("\treturn this_arg->" + var_line.group(2) + ";\n")
+ write_c("}\n")
+ dummy_line = var_line.group(1) + " " + struct_name.replace("LDK", "") + "_get_" + var_line.group(2) + " " + struct_name + " *NONNULL_PTR this_arg" + fn_line.group(5) + "\n"
+ map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, struct_name + "_set_get_" + var_line.group(2) + "(this_arg_conv", fn_docs)
+
+ def map_result(struct_name, res_ty, err_ty):
+ result_types.add(struct_name)
+ human_ty = struct_name.replace("LDKCResult", "Result")
+ res_map = type_mapping_generator.map_type(res_ty + " res", True, None, False, True)
+ err_map = type_mapping_generator.map_type(err_ty + " err", True, None, False, True)
+ java_hu_struct = consts.map_result(struct_name, res_map, err_map)
+ create_getter(struct_name, res_ty, "ok", ("*", "->contents.result"), ("", "->result_ok"))
+ create_getter(struct_name, err_ty, "err", ("*", "->contents.err"), ("!", "->result_ok"))
+ with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct:
+ out_java_struct.write(java_hu_struct)
+
+ def create_getter(struct_name, field_decl, field_name, accessor, check_sfx):
+ field_ty = java_c_types(field_decl + " " + field_name, None)
+ ptr_fn_defn = field_decl + " *" + struct_name.replace("LDK", "") + "_get_" + field_name + "(" + struct_name + " *NONNULL_PTR owner)"
+ owned_fn_defn = field_decl + " " + struct_name.replace("LDK", "") + "_get_" + field_name + "(" + struct_name + " *NONNULL_PTR owner)"
+
+ holds_ref = False
+ if field_ty.rust_obj is not None and field_ty.rust_obj.replace("LDK", "") + "_clone" in clone_fns:
+ fn_defn = owned_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn " + field_ty.rust_obj.replace("LDK", "") + "_clone(&" + accessor[0] + "owner" + accessor[1] + ");\n")
+ elif field_ty.arr_len is not None or field_ty.is_native_primitive or field_ty.rust_obj in unitary_enums:
+ fn_defn = owned_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn " + accessor[0] + "owner" + accessor[1] + ";\n")
+ holds_ref = True
+ else:
+ fn_defn = ptr_fn_defn
+ write_c("static inline " + fn_defn + "{\n")
+ if check_sfx is not None:
+ write_c("CHECK(" + check_sfx[0] + "owner" + check_sfx[1] + ");\n")
+ write_c("\treturn &" + accessor[0] + "owner" + accessor[1] + ";\n")
+ holds_ref = True
+ write_c("}\n")
+ dummy_line = fn_defn + ";\n"
+ map_fn_with_ref_option(dummy_line, reg_fn_regex.match(dummy_line), None, None, "", holds_ref)
+
+ def map_tuple(struct_name, field_lines):
+ human_ty = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple")
+ with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct:
+ out_java_struct.write(consts.map_tuple(struct_name))
+ ty_list = []
+ for idx, (line, _) in enumerate(field_lines):
+ if idx != 0 and idx < len(field_lines) - 2:
+ ty_list.append(java_c_types(line.strip(';'), None))
+ tuple_types[struct_name] = (ty_list, struct_name)
+
+ # Map virtual getter functions
+ for idx, (line, _) in enumerate(field_lines):
+ if idx != 0 and idx < len(field_lines) - 2:
+ field_name = chr(ord('a') + idx - 1)
+ assert line.endswith(" " + field_name + ";")
+ create_getter(struct_name, line[:-3].strip(), field_name, ("", "->" + field_name), None)
+
+ out_java.write(consts.bindings_header)
+ with open(f"{sys.argv[2]}/version{consts.file_ext}", "w") as out_java_version:
+ out_java_version.write(consts.bindings_version_file.replace('<git_version_ldk_garbagecollected>', local_git_version))
+
+ with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct:
+ out_java_struct.write(consts.common_base)
+
+ block_comment = None
+ last_block_comment = None