@@ -268,20 +268,23 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
268268 opaque_ret_conv_suf = opaque_ret_conv_suf + "\t " + ty_info .var_name + "_ref |= 1;\n "
269269 opaque_ret_conv_suf = opaque_ret_conv_suf + "}"
270270
271+ to_hu_conv_sfx = ""
272+ if not ty_info .is_ptr or holds_ref :
273+ to_hu_conv_sfx = "\n " + ty_info .var_name + "_hu_conv.ptrs_to.add(this);"
271274 if ty_info .is_ptr :
272275 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
273276 arg_conv = opaque_arg_conv , arg_conv_name = "&" + ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
274277 ret_conv = (ty_info .rust_obj + " " + ty_info .var_name + "_var = *" , opaque_ret_conv_suf ),
275278 ret_conv_name = ty_info .var_name + "_ref" ,
276- to_hu_conv = self .consts .to_hu_conv_templates ['ptr' ].replace ('{human_type}' , ty_info .java_hu_ty ).replace ('{var_name}' , ty_info .var_name ),
279+ to_hu_conv = self .consts .to_hu_conv_templates ['ptr' ].replace ('{human_type}' , ty_info .java_hu_ty ).replace ('{var_name}' , ty_info .var_name ) + to_hu_conv_sfx ,
277280 to_hu_conv_name = ty_info .var_name + "_hu_conv" ,
278281 from_hu_conv = from_hu_conv )
279282 else :
280283 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
281284 arg_conv = opaque_arg_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
282285 ret_conv = (ty_info .rust_obj + " " + ty_info .var_name + "_var = " , opaque_ret_conv_suf ),
283286 ret_conv_name = ty_info .var_name + "_ref" ,
284- to_hu_conv = self .consts .to_hu_conv_templates ['default' ].replace ('{human_type}' , ty_info .java_hu_ty ).replace ('{var_name}' , ty_info .var_name ) + " \n " + ty_info . var_name + "_hu_conv.ptrs_to.add(this);" ,
287+ to_hu_conv = self .consts .to_hu_conv_templates ['default' ].replace ('{human_type}' , ty_info .java_hu_ty ).replace ('{var_name}' , ty_info .var_name ) + to_hu_conv_sfx ,
285288 to_hu_conv_name = ty_info .var_name + "_hu_conv" ,
286289 from_hu_conv = from_hu_conv )
287290
@@ -375,60 +378,27 @@ def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_re
375378 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 + ");" ,
376379 to_hu_conv_name = ty_info .var_name + "_hu_conv" , from_hu_conv = (ty_info .var_name + " != null ? " + ty_info .var_name + ".ptr : 0" , "" ))
377380 if ty_info .rust_obj in self .tuple_types :
378- from_hu_conv_sfx = ""
379- from_hu_conv = "bindings." + self .tuple_types [ty_info .rust_obj ][1 ].replace ("LDK" , "" ) + "_new("
380- to_hu_conv_pfx = ""
381- to_hu_conv_sfx = ty_info .java_hu_ty + " " + ty_info .var_name + "_conv = new " + ty_info .java_hu_ty + "("
382- to_hu_conv_refs = ""
383- for idx , conv in enumerate (self .tuple_types [ty_info .rust_obj ][0 ]):
384- if idx != 0 :
385- to_hu_conv_sfx = to_hu_conv_sfx + ", "
386- from_hu_conv = from_hu_conv + ", "
387- conv .var_name = ty_info .var_name + "_" + chr (idx + ord ("a" ))
388- conv_map = self .map_type_with_info (conv , False , None , is_free , holds_ref )
389- to_hu_conv_pfx = to_hu_conv_pfx + conv .java_ty + " " + ty_info .var_name + "_" + chr (idx + ord ("a" )) + " = "
390- if not conv .is_native_primitive and (conv_map .rust_obj .replace ("LDK" , "" ) + "_clone" ) in self .clone_fns and conv_map .rust_obj == "LDKTxOut" :
391- to_hu_conv_pfx = to_hu_conv_pfx + "bindings." + conv_map .rust_obj .replace ("LDK" , "" ) + "_clone("
392- to_hu_conv_pfx = to_hu_conv_pfx + "bindings." + self .tuple_types [ty_info .rust_obj ][1 ] + "_get_" + chr (idx + ord ("a" )) + "(" + ty_info .var_name + ")"
393- if not conv .is_native_primitive and (conv_map .rust_obj .replace ("LDK" , "" ) + "_clone" ) in self .clone_fns and conv_map .rust_obj == "LDKTxOut" : # XXX
394- to_hu_conv_pfx = to_hu_conv_pfx + ")"
395- to_hu_conv_pfx = to_hu_conv_pfx + ";\n "
396- if conv_map .to_hu_conv is not None :
397- to_hu_conv_pfx = to_hu_conv_pfx + conv_map .to_hu_conv + ";\n "
398- to_hu_conv_sfx = to_hu_conv_sfx + conv_map .to_hu_conv_name
399- if to_hu_conv_refs is not None :
400- if conv_map .c_ty .endswith ("Array" ):
401- to_hu_conv_refs = None
402- else :
403- to_hu_conv_refs = to_hu_conv_refs + "\n " + conv_map .to_hu_conv_name + ".ptrs_to.add(" + ty_info .var_name + "_conv);"
404- else :
405- to_hu_conv_sfx = to_hu_conv_sfx + ty_info .var_name + "_" + chr (idx + ord ("a" ))
406- if conv_map .from_hu_conv is not None :
407- 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" )))
408- if conv_map .from_hu_conv [1 ] != "" :
409- 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" )))
410- if idx != len (self .tuple_types [ty_info .rust_obj ][0 ]) - 1 :
411- from_hu_conv_sfx += "; "
381+ ret_conv_name = "((uint64_t)" + ty_info .var_name + "_conv)"
382+ if holds_ref :
383+ # If we're trying to return a ref, we have to clone.
384+ # We just blindly assume its implemented and let the compiler fail if its not.
385+ 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 = " , ";" )
386+ if (ty_info .rust_obj .replace ("LDK" , "" ) + "_clone" ) not in self .clone_fns :
387+ ret_conv = (ret_conv [0 ], ret_conv [1 ] + "\n // Warning: we really need to clone here, but no clone is available for " + ty_info .rust_obj )
388+ ret_conv_name += " | 1"
412389 else :
413- from_hu_conv = from_hu_conv + ty_info .var_name + "." + chr (idx + ord ("a" ))
414-
415- if to_hu_conv_refs is None :
416- to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ");\n // Warning: We may not free the C tuple object!"
390+ ret_conv = (ret_conv [0 ], ret_conv [1 ] + "\n *" + ty_info .var_name + "_conv = " + ty_info .rust_obj .replace ("LDK" , "" ) + "_clone(" + ty_info .var_name + "_conv);" )
417391 else :
418- to_hu_conv = to_hu_conv_pfx + to_hu_conv_sfx + ", () -> {\n "
419- to_hu_conv = to_hu_conv + "\t bindings." + ty_info .rust_obj .replace ("LDK" , "" ) + "_free(" + ty_info .var_name + ");\n "
420- to_hu_conv = to_hu_conv + "});" + to_hu_conv_refs
421- if not ty_info .is_ptr and not holds_ref :
422- ret_conv = (ty_info .rust_obj + "* " + ty_info .var_name + "_ref = MALLOC(sizeof(" + ty_info .rust_obj + "), \" " + ty_info .rust_obj + "\" );\n *" + ty_info .var_name + "_ref = " , ";" )
423- return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
424- arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
425- ret_conv = ret_conv ,
426- ret_conv_name = "(uint64_t)" + ty_info .var_name + "_ref" ,
427- to_hu_conv = to_hu_conv , to_hu_conv_name = ty_info .var_name + "_conv" , from_hu_conv = (from_hu_conv + ")" , from_hu_conv_sfx ))
392+ 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 = " , ";" )
393+ if not ty_info .is_ptr or holds_ref :
394+ to_hu_conv_sfx = "\n " + ty_info .var_name + "_hu_conv.ptrs_to.add(this);"
395+ else :
396+ to_hu_conv_sfx = ""
428397 return ConvInfo (ty_info = ty_info , arg_name = ty_info .var_name ,
429398 arg_conv = base_conv , arg_conv_name = ty_info .var_name + "_conv" , arg_conv_cleanup = None ,
430- ret_conv = ("uint64_t " + ty_info .var_name + "_ref = (uint64_t)(&" , ") | 1;" ), ret_conv_name = ty_info .var_name + "_ref" ,
431- to_hu_conv = to_hu_conv , to_hu_conv_name = ty_info .var_name + "_conv" , from_hu_conv = (from_hu_conv + ")" , from_hu_conv_sfx ))
399+ ret_conv = ret_conv , ret_conv_name = ret_conv_name ,
400+ to_hu_conv = ty_info .java_hu_ty + " " + ty_info .var_name + "_hu_conv = new " + ty_info .java_hu_ty + "(null, " + ty_info .var_name + ");" + to_hu_conv_sfx ,
401+ to_hu_conv_name = ty_info .var_name + "_hu_conv" , from_hu_conv = (ty_info .var_name + " != null ? " + ty_info .var_name + ".ptr : 0" , "" ))
432402
433403 # The manually-defined types - TxOut and u5
434404 if ty_info .rust_obj == "LDKu5" :
0 commit comments