From: Matt Corallo Date: Wed, 8 Mar 2023 01:58:42 +0000 (+0000) Subject: Ensure enums generate a ref in owning structs X-Git-Tag: v0.0.114.0~17 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=f800f9f0b95aa0f37671414397dbe7f876e298c8;p=ldk-java Ensure enums generate a ref in owning structs Previously we only added a ref in an owning struct to an enum if that enum contained a trait in a field. This isn't sufficient as an enum could still contain a reference to an opaque object, which needs to stick around as long as the enum exists. For simplicity, simply add a ref in all cases as it seems to still work in our own internal tests. --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index c5a434ac..d24ed230 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -429,7 +429,6 @@ class TypeMappingGenerator: # underlying unlike Vecs, and it gives Java more freedom. base_conv = base_conv + "\nFREE(untag_ptr(" + ty_info.var_name + "));" if ty_info.rust_obj in self.complex_enums: - to_hu_conv_sfx = "" if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns: # We really need a full clone here, but for now we just implement # a manual clone explicitly for Options @@ -437,7 +436,6 @@ class TypeMappingGenerator: assert ty_info.rust_obj.startswith("LDKCOption") # We don't support contained traits for anything else yet optional_ty = ty_info.rust_obj[11:-1] assert "LDK" + optional_ty in self.trait_structs # We don't support contained traits for anything else yet - to_hu_conv_sfx = self.consts.add_ref("this", ty_info.var_name) base_conv += "\nif (" + ty_info.var_name + "_conv.tag == " + ty_info.rust_obj + "_Some) {" base_conv += "\n\t// Manually implement clone for Java trait instances" optional_ty_info = self.java_c_types("LDK" + optional_ty + " " + ty_info.var_name, None) @@ -451,7 +449,7 @@ class TypeMappingGenerator: ret_conv = (ret_conv[0], ";\n" + self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_copy, true);") if from_hu_conv is None: from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "") - from_hu_conv = (from_hu_conv[0], to_hu_conv_sfx) + from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name)) fully_qualified_ty = self.consts.fully_qualified_hu_ty_path(ty_info) to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info.var_name + ")" return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,