@@ -300,7 +300,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
300300 else :
301301 from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), self .consts .add_ref ("this" , ty_info .var_name ))
302302 opaque_arg_conv = ty_info .rust_obj + " " + ty_info .var_name + "_conv;\n "
303- opaque_arg_conv = opaque_arg_conv + ty_info .var_name + "_conv.inner = untag_ptr(" + ty_info .var_name + ");\n "
303+ opaque_arg_conv += ty_info .var_name + "_conv.inner = untag_ptr(" + ty_info .var_name + ");\n "
304304 opaque_arg_conv += ty_info .var_name + "_conv.is_owned = ptr_is_owned(" + ty_info .var_name + ");\n "
305305 opaque_arg_conv += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info .var_name + "_conv);"
306306
@@ -313,6 +313,8 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
313313 # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
314314 if holds_ref :
315315 opaque_arg_conv += "\n " + ty_info .var_name + "_conv = " + ty_info .rust_obj .replace ("LDK" , "" ) + "_clone(&" + ty_info .var_name + "_conv);"
316+ if not ty_info .pass_by_ref and not ty_info .contains_trait and not ty_info .is_trait :
317+ from_hu_conv = (from_hu_conv [0 ], "" )
316318 elif is_nullable :
317319 from_hu_conv = (ty_info .var_name + " == null ? " + self .consts .native_zero_ptr + " : " + ty_info .var_name + ".clone_ptr()" , "" )
318320 else :
@@ -415,18 +417,23 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
415417 to_hu_conv = self .consts .var_decl_statement (ty_info .java_hu_ty , "ret_hu_conv" , "new " + ty_info .java_hu_ty + "(null, " + ty_info .var_name + ")" ) + ";\n " + self .consts .add_ref ("ret_hu_conv" , "this" ) + ";" ,
416418 to_hu_conv_name = "ret_hu_conv" , from_hu_conv = from_hu_conv )
417419 needs_full_clone = not is_free and (not ty_info .is_ptr or ty_info .requires_clone == True ) and ty_info .requires_clone != False
420+ from_hu_add_ref = ""
421+ if ty_info .contains_trait or ty_info .is_trait or needs_full_clone :
422+ from_hu_add_ref = self .consts .add_ref ("this" , ty_info .var_name )
418423 if needs_full_clone :
419- if "res" in ty_info .var_name : # XXX: This is a stupid hack
420- needs_full_clone = False
421424 if needs_full_clone and (ty_info .rust_obj .replace ("LDK" , "" ) + "_clone" ) in self .clone_fns :
422425 # arg_conv is used when converting a function argument from java normally (with holds_ref set),
423426 # and when converting a java value being returned from a trait method (with holds_ref unset).
424427 # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
425428 # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
426429 if holds_ref :
427430 base_conv += "\n " + ty_info .var_name + "_conv = " + ty_info .rust_obj .replace ("LDK" , "" ) + "_clone((" + ty_info .rust_obj + "*)untag_ptr(" + ty_info .var_name + "));"
431+ if not ty_info .pass_by_ref and not ty_info .contains_trait and not ty_info .is_trait :
432+ from_hu_add_ref = ""
428433 else :
429- from_hu_conv = (ty_info .var_name + ".clone_ptr()" , "" )
434+ if not ty_info .pass_by_ref and not ty_info .contains_trait and not ty_info .is_trait :
435+ from_hu_add_ref = ""
436+ from_hu_conv = (ty_info .var_name + ".clone_ptr()" , from_hu_add_ref )
430437 base_conv += "\n " + "FREE(untag_ptr(" + ty_info .var_name + "));"
431438 elif needs_full_clone :
432439 base_conv = base_conv + "\n // WARNING: we may need a move here but no clone is available for " + ty_info .rust_obj
@@ -454,8 +461,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
454461 ret_conv = (ret_conv [0 ] + "*" + ty_info .var_name + "_copy = " , "" )
455462 ret_conv = (ret_conv [0 ], ";\n " + self .consts .ptr_c_ty + " " + ty_info .var_name + "_ref = tag_ptr(" + ty_info .var_name + "_copy, true);" )
456463 if from_hu_conv is None :
457- from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), "" )
458- from_hu_conv = (from_hu_conv [0 ], self .consts .add_ref ("this" , ty_info .var_name ))
464+ from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), from_hu_add_ref )
459465 fully_qualified_ty = self .consts .fully_qualified_hu_ty_path (ty_info )
460466 to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info .var_name + ")"
461467 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
@@ -472,7 +478,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
472478 else :
473479 ret_conv = (ty_info .rust_obj + "* " + ty_info .var_name + "_conv = MALLOC(sizeof(" + ty_info .rust_obj + "), \" " + ty_info .rust_obj + "\" );\n *" + ty_info .var_name + "_conv = " , ";" )
474480 if from_hu_conv is None :
475- from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), "" )
481+ from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), from_hu_add_ref )
476482 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
477483 arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
478484 ret_conv = ret_conv , ret_conv_name = "tag_ptr(" + ty_info .var_name + "_conv, true)" ,
@@ -498,7 +504,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
498504 else :
499505 to_hu_conv_sfx = ""
500506 if from_hu_conv is None :
501- from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), "" )
507+ from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), from_hu_add_ref )
502508 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
503509 arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
504510 ret_conv = ret_conv , ret_conv_name = ret_conv_name ,
0 commit comments