X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.py;h=3f7a12ee23d402b00044cd967b4c27c308af2006;hb=ac4869b989e27b4500e00e901ea5851604315345;hp=0356da087de2853810189b8ea2a20aa8d56dbb3d;hpb=00e9cb703daea4fae4c89b14c8a584677ad74fca;p=ldk-java diff --git a/genbindings.py b/genbindings.py index 0356da08..3f7a12ee 100755 --- a/genbindings.py +++ b/genbindings.py @@ -230,9 +230,6 @@ trait_structs = set() result_types = set() tuple_types = {} -def is_common_base_ext(struct_name): - return struct_name in complex_enums or struct_name in opaque_structs or struct_name in trait_structs or struct_name in result_types - var_is_arr_regex = re.compile("\(\*([A-za-z0-9_]*)\)\[([a-z0-9]*)\]") var_ty_regex = re.compile("([A-za-z_0-9]*)(.*)") java_c_types_none_allowed = True # Unset when we do the real pass that populates the above sets @@ -305,6 +302,11 @@ def java_c_types(fn_arg, ret_arr_len): rust_obj = "LDKCVec_u8Z" assert var_is_arr_regex.match(fn_arg[8:]) arr_access = "data" + elif fn_arg.startswith("LDKTransaction"): + fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]" + rust_obj = "LDKTransaction" + assert var_is_arr_regex.match(fn_arg[8:]) + arr_access = "data" elif fn_arg.startswith("LDKCVecTempl_") or fn_arg.startswith("LDKCVec_"): is_ptr = False if "*" in fn_arg: @@ -520,14 +522,20 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_conv_cleanup = None if not arr_len.isdigit(): arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n" - arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = (*_env)->GetByteArrayElements (_env, " + arr_name + ", NULL);\n" - arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");" - arg_conv_cleanup = "(*_env)->ReleaseByteArrayElements(_env, " + arr_name + ", (int8_t*)" + arr_name + "_ref." + ty_info.arr_access + ", 0);" + arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = (*_env)->GetArrayLength (_env, " + arr_name + ");\n" + if (not ty_info.is_ptr or not holds_ref) and ty_info.rust_obj != "LDKu8slice": + arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = MALLOC(" + arr_name + "_ref." + arr_len + ", \"" + ty_info.rust_obj + " Bytes\");\n" + arg_conv = arg_conv + "(*_env)->GetByteArrayRegion(_env, " + arr_name + ", 0, " + arr_name + "_ref." + arr_len + ", " + arr_name + "_ref." + ty_info.arr_access + ");" + else: + arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = (*_env)->GetByteArrayElements (_env, " + arr_name + ", NULL);" + arg_conv_cleanup = "(*_env)->ReleaseByteArrayElements(_env, " + arr_name + ", (int8_t*)" + arr_name + "_ref." + ty_info.arr_access + ", 0);" + if ty_info.rust_obj == "LDKTransaction": + arg_conv = arg_conv + "\n" + arr_name + "_ref.data_is_owned = " + str(holds_ref).lower() + ";" ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "") ret_conv = (ret_conv[0], ";\njbyteArray " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_name + "_var." + arr_len + ");\n") ret_conv = (ret_conv[0], ret_conv[1] + "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_name + "_var." + arr_len + ", " + arr_name + "_var." + ty_info.arr_access + ");") - if not holds_ref and ty_info.rust_obj == "LDKCVec_u8Z": - ret_conv = (ret_conv[0], ret_conv[1] + "\nCVec_u8Z_free(" + arr_name + "_var);") + if not holds_ref and ty_info.rust_obj != "LDKu8slice": + ret_conv = (ret_conv[0], ret_conv[1] + "\n" + ty_info.rust_obj.replace("LDK", "") + "_free(" + arr_name + "_var);") elif ty_info.rust_obj is not None: arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n" arg_conv = arg_conv + "CHECK((*_env)->GetArrayLength (_env, " + arr_name + ") == " + arr_len + ");\n" @@ -693,7 +701,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: opaque_ret_conv_suf = opaque_ret_conv_suf + "CHECK((((long)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n" opaque_ret_conv_suf = opaque_ret_conv_suf + "CHECK((((long)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n" - if holds_ref or ty_info.is_ptr: + if holds_ref: opaque_ret_conv_suf = opaque_ret_conv_suf + "long " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner & ~1;" else: opaque_ret_conv_suf = opaque_ret_conv_suf + "long " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_var.inner;\n" @@ -726,12 +734,19 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_conv_cleanup = None, ret_conv = ("jclass " + ty_info.var_name + "_conv = " + ty_info.rust_obj + "_to_java(_env, ", ");"), ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None) - base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";"; + base_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + ";" if ty_info.rust_obj in trait_structs: if not is_free: - base_conv = base_conv + "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n" - base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n" - base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}" + needs_full_clone = not is_free and (not ty_info.is_ptr and not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False + if needs_full_clone and (ty_info.java_hu_ty + "_clone") in clone_fns: + base_conv = base_conv + "\n" + ty_info.var_name + "_conv = " + ty_info.java_hu_ty + "_clone(" + ty_info.var_name + ");" + else: + base_conv = base_conv + "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n" + base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n" + base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}" + if needs_full_clone: + base_conv = base_conv + "// Warning: we may need a move here but can't do a full clone!\n" + else: base_conv = base_conv + "\n" + "FREE((void*)" + ty_info.var_name + ");" return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, @@ -741,7 +756,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);", to_hu_conv_name = "ret_hu_conv", from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) - if ty_info.rust_obj != "LDKu8slice" and ty_info.rust_obj != "LDKTransaction": + if ty_info.rust_obj != "LDKu8slice": # Don't bother free'ing slices passed in - Rust doesn't auto-free the # underlying unlike Vecs, and it gives Java more freedom. base_conv = base_conv + "\nFREE((void*)" + ty_info.var_name + ");"; @@ -806,20 +821,12 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");", to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (from_hu_conv + ")", "")) # The manually-defined types - TxOut and Transaction - assert ty_info.rust_obj == "LDKTransaction" or ty_info.rust_obj == "LDKTxOut" - if ty_info.rust_obj == "LDKTransaction": - return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, - arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, - ret_conv = ("LDKTransaction *" + ty_info.var_name + "_copy = MALLOC(sizeof(LDKTransaction), \"LDKTransaction\");\n*" + ty_info.var_name + "_copy = ", ";\nlong " + ty_info.var_name + "_ref = (long)" + ty_info.var_name + "_copy;"), - ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", - to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".ptr", "")) - elif ty_info.rust_obj == "LDKTxOut": - return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, - arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, - ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", - to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".ptr", "")) + assert ty_info.rust_obj == "LDKTxOut" + return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, + arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, + ret_conv = ("long " + ty_info.var_name + "_ref = (long)&", ";"), ret_conv_name = ty_info.var_name + "_ref", + to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", + to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".ptr", "")) elif ty_info.is_ptr: assert(not is_free) if ty_info.rust_obj in complex_enums: @@ -989,7 +996,10 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_struct.write(info.arg_name) out_java_struct.write(");\n") if ret_info.to_hu_conv is not None: - out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n") + if ret_info.rust_obj == "LDK" + struct_meth: + out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t").replace("this", ret_info.to_hu_conv_name) + "\n") + else: + out_java_struct.write("\t\t" + ret_info.to_hu_conv.replace("\n", "\n\t\t") + "\n") for info in arg_names: if info.arg_name == "this_ptr" or info.arg_name == "this_arg": @@ -1323,7 +1333,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: 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): + 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: @@ -1392,7 +1403,6 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: 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: @@ -1793,16 +1803,6 @@ class CommonBase { out_java_struct.write("\tlong to_c_ptr() { return 0; }\n") # TODO: TxOut body out_java_struct.write("}") - elif struct_name == "LDKTransaction": - with open(sys.argv[3] + "/structs/Transaction.java", "w") as out_java_struct: - out_java_struct.write(hu_struct_file_prefix) - out_java_struct.write("public class Transaction extends CommonBase{\n") - out_java_struct.write("\tTransaction(java.lang.Object _dummy, long ptr) { super(ptr); }\n") - out_java_struct.write("\tpublic Transaction(byte[] data) { super(bindings.new_txpointer_copy_data(data)); }\n") - out_java_struct.write("\t@Override public void finalize() throws Throwable { super.finalize(); bindings.txpointer_free(ptr); }\n") - out_java_struct.write("\tpublic byte[] get_contents() { return bindings.txpointer_get_buffer(ptr); }\n") - # TODO: Transaction body - out_java_struct.write("}") else: pass # Everything remaining is a byte[] or some form cur_block_obj = None