From 1668e21d15257d59a739a188a6e85d6e57f8f2cc Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 31 Jan 2021 19:36:00 -0500 Subject: [PATCH] Various straightforward cleanups --- gen_type_mapping.py | 19 ++++++++++++------- genbindings.py | 4 ++-- java_strings.py | 6 +++--- typescript_strings.py | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 05cf751e..aebd7d01 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -77,6 +77,12 @@ class TypeMappingGenerator: #if ty_info.is_ptr or holds_ref: # ty_info.subty.requires_clone = False ty_info.subty.requires_clone = not ty_info.is_ptr or not holds_ref + if ty_info.subty.rust_obj is not None and ty_info.subty.rust_obj == "LDKChannelMonitor": + # We take a Vec of references to ChannelMonitors as input to ChannelManagerReadArgs, if we clone them, + # we end up freeing the clones after creating the ChannelManagerReadArgs before calling the read + # function itself, resulting in a segfault. Thus, we manually check and ensure we don't clone for + # ChannelMonitors inside of vecs. + ty_info.subty.requires_clone = False subty = self.map_type_with_info(ty_info.subty, False, None, is_free, holds_ref) if arr_name == "": arr_name = "arg" @@ -209,15 +215,13 @@ class TypeMappingGenerator: if not is_free and (not ty_info.is_ptr or not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False: if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns: # TODO: This is a bit too naive, even with the checks above, we really need to know if rust wants a ref or not, not just if its pass as a ptr. - opaque_arg_conv = opaque_arg_conv + "\nif (" + ty_info.var_name + "_conv.inner != NULL)\n" - opaque_arg_conv = opaque_arg_conv + "\t" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);" + opaque_arg_conv = opaque_arg_conv + "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);" elif ty_info.passed_as_ptr: opaque_arg_conv = opaque_arg_conv + "\n// Warning: we may need a move here but no clone is available for " + ty_info.rust_obj opaque_ret_conv_suf = ";\n" if not holds_ref and ty_info.is_ptr and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns: # is_ptr, not holds_ref implies passing a pointed-to value to java, which needs copied - opaque_ret_conv_suf = opaque_ret_conv_suf + "if (" + ty_info.var_name + "->inner != NULL)\n" - opaque_ret_conv_suf = opaque_ret_conv_suf + "\t" + ty_info.var_name + "_var = " + ty_info.rust_obj.replace("LDK", "") + "_clone(" + ty_info.var_name + ");\n" + opaque_ret_conv_suf = opaque_ret_conv_suf + ty_info.var_name + "_var = " + ty_info.rust_obj.replace("LDK", "") + "_clone(" + ty_info.var_name + ");\n" elif not holds_ref and ty_info.is_ptr: opaque_ret_conv_suf = opaque_ret_conv_suf + "// Warning: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n" @@ -325,6 +329,7 @@ class TypeMappingGenerator: to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = " + ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ");", to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + " != null ? " + ty_info.var_name + ".ptr : 0", "")) if ty_info.rust_obj in self.tuple_types: + from_hu_conv_sfx = "" from_hu_conv = "bindings." + self.tuple_types[ty_info.rust_obj][1].replace("LDK", "") + "_new(" to_hu_conv_pfx = "" to_hu_conv_sfx = ty_info.java_hu_ty + " " + ty_info.var_name + "_conv = new " + ty_info.java_hu_ty + "(" @@ -350,7 +355,7 @@ class TypeMappingGenerator: if conv_map.from_hu_conv is not None: from_hu_conv = from_hu_conv + conv_map.from_hu_conv[0].replace(ty_info.var_name + "_" + chr(idx + ord("a")), ty_info.var_name + "." + chr(idx + ord("a"))) if conv_map.from_hu_conv[1] != "": - from_hu_conv = from_hu_conv + "/*XXX: " + conv_map.from_hu_conv[1] + "*/" + from_hu_conv_sfx = from_hu_conv_sfx + conv_map.from_hu_conv[1].replace(conv.var_name, ty_info.var_name + "." + chr(idx + ord("a"))) else: from_hu_conv = from_hu_conv + ty_info.var_name + "." + chr(idx + ord("a")) @@ -369,11 +374,11 @@ class TypeMappingGenerator: arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = ret_conv, ret_conv_name = "(long)" + ty_info.var_name + "_ref", - 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 + ")", "")) + 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 + ")", from_hu_conv_sfx)) 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)(&", ") | 1;"), ret_conv_name = ty_info.var_name + "_ref", - 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 + ")", "")) + 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 + ")", from_hu_conv_sfx)) # The manually-defined types - TxOut and Transaction assert ty_info.rust_obj == "LDKTxOut" diff --git a/genbindings.py b/genbindings.py index eefc9937..d6e431f7 100755 --- a/genbindings.py +++ b/genbindings.py @@ -597,7 +597,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: if res_map.from_hu_conv is not None: out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n") if res_map.from_hu_conv[1] != "": - out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1] + ";\n") + out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n") else: out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n") out_java_struct.write("\t\t}\n\t}\n\n") @@ -635,7 +635,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: if err_map.from_hu_conv is not None: out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n") if err_map.from_hu_conv[1] != "": - out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1] + ";\n") + out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n") else: out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n") out_java_struct.write("\t\t}\n\t}\n}\n") diff --git a/java_strings.py b/java_strings.py index f7aa1b02..fc1e4099 100644 --- a/java_strings.py +++ b/java_strings.py @@ -495,7 +495,7 @@ import java.util.Arrays; for var in field_vars: if isinstance(var, ConvInfo): if var.from_hu_conv is not None and var.from_hu_conv[1] != "": - out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1] + ";\n" + out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n" else: out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(" + var[1] + ");\n" out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n" @@ -980,9 +980,9 @@ import java.util.Arrays; elif info.from_hu_conv is not None and info.from_hu_conv[1] != "": if not takes_self and return_type_info.to_hu_conv_name is not None: out_java_struct += ( - "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name) + ";\n") + "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n") else: - out_java_struct += ("\t\t" + info.from_hu_conv[1] + ";\n") + out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n") if return_type_info.to_hu_conv_name is not None: out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n") diff --git a/typescript_strings.py b/typescript_strings.py index 82412e01..542487bc 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -1056,9 +1056,9 @@ const decodeString = (stringPointer, free = true) => { elif info.from_hu_conv is not None and info.from_hu_conv[1] != "": if not takes_self and return_type_info.to_hu_conv_name is not None: out_java_struct += ( - "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name) + ";\n") + "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n") else: - out_java_struct += ("\t\t" + info.from_hu_conv[1] + ";\n") + out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n") if return_type_info.to_hu_conv_name is not None: out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n") -- 2.39.5