@@ -300,7 +300,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
300
300
else :
301
301
from_hu_conv = (self .consts .get_ptr (ty_info .var_name ), self .consts .add_ref ("this" , ty_info .var_name ))
302
302
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 "
304
304
opaque_arg_conv += ty_info .var_name + "_conv.is_owned = ptr_is_owned(" + ty_info .var_name + ");\n "
305
305
opaque_arg_conv += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info .var_name + "_conv);"
306
306
@@ -313,6 +313,8 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
313
313
# whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
314
314
if holds_ref :
315
315
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 ], "" )
316
318
elif is_nullable :
317
319
from_hu_conv = (ty_info .var_name + " == null ? " + self .consts .native_zero_ptr + " : " + ty_info .var_name + ".clone_ptr()" , "" )
318
320
else :
@@ -415,18 +417,23 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
415
417
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" ) + ";" ,
416
418
to_hu_conv_name = "ret_hu_conv" , from_hu_conv = from_hu_conv )
417
419
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 )
418
423
if needs_full_clone :
419
- if "res" in ty_info .var_name : # XXX: This is a stupid hack
420
- needs_full_clone = False
421
424
if needs_full_clone and (ty_info .rust_obj .replace ("LDK" , "" ) + "_clone" ) in self .clone_fns :
422
425
# arg_conv is used when converting a function argument from java normally (with holds_ref set),
423
426
# and when converting a java value being returned from a trait method (with holds_ref unset).
424
427
# In the second case, we need to clone before returning to C (as once we return the GC can free the object),
425
428
# whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
426
429
if holds_ref :
427
430
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 = ""
428
433
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 )
430
437
base_conv += "\n " + "FREE(untag_ptr(" + ty_info .var_name + "));"
431
438
elif needs_full_clone :
432
439
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
454
461
ret_conv = (ret_conv [0 ] + "*" + ty_info .var_name + "_copy = " , "" )
455
462
ret_conv = (ret_conv [0 ], ";\n " + self .consts .ptr_c_ty + " " + ty_info .var_name + "_ref = tag_ptr(" + ty_info .var_name + "_copy, true);" )
456
463
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 )
459
465
fully_qualified_ty = self .consts .fully_qualified_hu_ty_path (ty_info )
460
466
to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info .var_name + ")"
461
467
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
472
478
else :
473
479
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 = " , ";" )
474
480
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 )
476
482
return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
477
483
arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
478
484
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
498
504
else :
499
505
to_hu_conv_sfx = ""
500
506
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 )
502
508
return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
503
509
arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
504
510
ret_conv = ret_conv , ret_conv_name = ret_conv_name ,
0 commit comments