dc9899bdb1f9edbbaaa487a96bb6ed9b4fc02314
[ldk-java] / gen_type_mapping.py
1 from bindingstypes import ConvInfo
2
3 class TypeMappingGenerator:
4
5     def __init__(self, java_c_types, consts, opaque_structs, clone_fns, unitary_enums, trait_structs, complex_enums, result_types, tuple_types):
6         # trick our way around circular imports
7         self.java_c_types = java_c_types
8         self.consts = consts
9         self.opaque_structs = opaque_structs
10         self.clone_fns = clone_fns
11         self.unitary_enums = unitary_enums
12         self.trait_structs = trait_structs
13         self.complex_enums = complex_enums
14         self.result_types = result_types
15         self.tuple_types = tuple_types
16
17     def map_type(self, fn_arg, print_void, ret_arr_len, is_free, holds_ref):
18         ty_info = self.java_c_types(fn_arg, ret_arr_len)
19         mapped_info = self.map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref, False)
20         return mapped_info
21
22     def map_nullable_type(self, fn_arg, print_void, ret_arr_len, is_free, holds_ref):
23         ty_info = self.java_c_types(fn_arg, ret_arr_len)
24         mapped_info = self.map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref, True)
25         return mapped_info
26
27     def map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_ref, is_nullable):
28         mapped_info = self._do_map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref, is_nullable)
29         if is_nullable:
30             mapped_info.nullable = True
31         return mapped_info
32
33     def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, holds_ref, is_nullable):
34         if ty_info.c_ty == "void":
35             if not print_void:
36                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
37                     arg_conv = None, arg_conv_name = None, arg_conv_cleanup = None,
38                     ret_conv = None, ret_conv_name = None, to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
39         if ty_info.c_ty.endswith("Array"):
40             arr_len = ty_info.arr_len
41             if arr_len is not None:
42                 arr_name = ty_info.var_name
43             else:
44                 arr_name = "ret"
45                 arr_len = ret_arr_len
46             if ty_info.c_ty == "int8_tArray" or ty_info.c_ty == "int16_tArray":
47                 (set_pfx, set_sfx) = self.consts.set_native_arr_contents(arr_name + "_arr", arr_len, ty_info)
48                 ret_conv = (ty_info.c_ty + " " + arr_name + "_arr = " + self.consts.create_native_arr_call(arr_len, ty_info) + ";\n" + set_pfx, "")
49                 arg_conv_cleanup = None
50                 if not arr_len.isdigit():
51                     arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
52                     arg_conv = arg_conv + arr_name + "_ref." + arr_len + " = " +  self.consts.get_native_arr_len_call[0] + arr_name + self.consts.get_native_arr_len_call[1] + ";\n"
53                     if (not ty_info.is_ptr or not holds_ref) and (ty_info.rust_obj != "LDKu8slice" and ty_info.rust_obj != "LDKu16slice"):
54                         arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = MALLOC(" + arr_name + "_ref." + arr_len + ", \"" + ty_info.rust_obj + " Bytes\");\n"
55                         arg_conv = arg_conv + self.consts.get_native_arr_contents(arr_name, arr_name + "_ref." + ty_info.arr_access, arr_name + "_ref." + arr_len, ty_info, True) + ";"
56                     else:
57                         arg_conv = arg_conv + arr_name + "_ref." + ty_info.arr_access + " = " + self.consts.get_native_arr_contents(arr_name, "NO_DEST", arr_name + "_ref." + arr_len, ty_info, False) + ";"
58                         arg_conv_cleanup = self.consts.cleanup_native_arr_ref_contents(arr_name, arr_name + "_ref." + ty_info.arr_access, arr_name + "_ref." + arr_len, ty_info)
59                     if ty_info.rust_obj == "LDKTransaction" or ty_info.rust_obj == "LDKWitness":
60                         arg_conv = arg_conv + "\n" + arr_name + "_ref.data_is_owned = " + str(holds_ref).lower() + ";"
61                     ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
62                     ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = " + self.consts.create_native_arr_call(arr_name + "_var." + arr_len, ty_info) + ";\n")
63                     (pfx, sfx) = self.consts.set_native_arr_contents(arr_name + "_arr", arr_name + "_var." + arr_len, ty_info)
64                     ret_conv = (ret_conv[0], ret_conv[1] + pfx + arr_name + "_var." + ty_info.arr_access + sfx + ";")
65                     if not holds_ref and (ty_info.rust_obj != "LDKu8slice" and ty_info.rust_obj != "LDKu16slice"):
66                         ret_conv = (ret_conv[0], ret_conv[1] + "\n" + ty_info.rust_obj.replace("LDK", "") + "_free(" + arr_name + "_var);")
67                     from_hu_conv = self.consts.primitive_arr_from_hu(ty_info, None, arr_name)
68                     to_hu_conv = self.consts.primitive_arr_to_hu(ty_info, None, arr_name, arr_name + "_conv")
69                 elif ty_info.rust_obj is not None:
70                     arg_conv = ty_info.rust_obj + " " + arr_name + "_ref;\n"
71                     arg_conv = arg_conv + "CHECK(" + self.consts.get_native_arr_len_call[0] + arr_name + self.consts.get_native_arr_len_call[1] + " == " + arr_len + ");\n"
72                     arg_conv = arg_conv + self.consts.get_native_arr_contents(arr_name, arr_name + "_ref." + ty_info.arr_access, arr_len, ty_info, True) + ";"
73                     ret_conv = (ret_conv[0], "." + ty_info.arr_access + set_sfx + ";")
74                     from_hu_conv = self.consts.primitive_arr_from_hu(ty_info, arr_len, arr_name)
75                     to_hu_conv = self.consts.primitive_arr_to_hu(ty_info, None, arr_name, arr_name + "_conv")
76                 else:
77                     # Note that we just blindly assume we should be using unsigned integers here.
78                     arg_conv = "u" + ty_info.subty.c_ty + " " + arr_name + "_arr[" + arr_len + "];\n"
79                     arg_conv = arg_conv + "CHECK(" + self.consts.get_native_arr_len_call[0] + arr_name + self.consts.get_native_arr_len_call[1] + " == " + arr_len + ");\n"
80                     arg_conv = arg_conv + self.consts.get_native_arr_contents(arr_name, arr_name + "_arr", arr_len, ty_info, True) + ";\n"
81                     arg_conv = arg_conv + "u" + ty_info.subty.c_ty + " (*" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;"
82                     ret_conv = (ret_conv[0] + "*", set_sfx + ";")
83                     from_hu_conv = self.consts.primitive_arr_from_hu(ty_info, arr_len, arr_name)
84                     to_hu_conv = self.consts.primitive_arr_to_hu(ty_info, None, arr_name, arr_name + "_conv")
85                 to_hu_conv_name = None
86                 if to_hu_conv is not None:
87                     to_hu_conv_name = arr_name + "_conv"
88                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
89                     arg_conv = arg_conv, arg_conv_name = arr_name + "_ref", arg_conv_cleanup = arg_conv_cleanup,
90                     ret_conv = ret_conv, ret_conv_name = arr_name + "_arr",
91                     to_hu_conv = to_hu_conv, to_hu_conv_name = to_hu_conv_name,
92                     from_hu_conv = from_hu_conv)
93             else:
94                 assert not arr_len.isdigit() # fixed length arrays not implemented
95                 assert ty_info.java_hu_ty[len(ty_info.java_hu_ty) - 2:] == "[]"
96                 if arr_name == "":
97                     arr_name = "ret"
98                 conv_name = arr_name + "_conv_" + str(len(ty_info.java_hu_ty))
99                 idxc = chr(ord('a') + (len(ty_info.java_hu_ty) % 26))
100                 ty_info.subty.var_name = conv_name
101                 #XXX: We'd really prefer to only ever set to False, avoiding lots of clone, but need smarter free logic
102                 #if ty_info.is_ptr or holds_ref:
103                 #    ty_info.subty.requires_clone = False
104                 ty_info.subty.requires_clone = not ty_info.is_ptr or not holds_ref
105                 if not ty_info.subty.is_native_primitive and ty_info.subty.rust_obj == "LDKChannelMonitor":
106                     # We take a Vec of references to ChannelMonitors as input to ChannelManagerReadArgs, if we clone them,
107                     # we end up freeing the clones after creating the ChannelManagerReadArgs before calling the read
108                     # function itself, resulting in a segfault. Thus, we manually check and ensure we don't clone for
109                     # ChannelMonitors inside of vecs.
110                     ty_info.subty.requires_clone = False
111                 subty = self.map_type_with_info(ty_info.subty, False, None, is_free, holds_ref, False)
112                 arg_conv = ty_info.rust_obj + " " + arr_name + "_constr;\n"
113                 pf = ""
114                 if ty_info.is_ptr:
115                     pf = "\t"
116                     arg_conv += ty_info.rust_obj + " *" + arr_name + "_ptr = NULL;\n"
117                     arg_conv += "if (" + self.consts.is_arr_some_check[0] + arr_name + self.consts.is_arr_some_check[1] + ") {\n"
118                 arg_conv += pf + arr_name + "_constr." + arr_len + " = " + self.consts.get_native_arr_len_call[0] + arr_name + self.consts.get_native_arr_len_call[1] + ";\n"
119                 arg_conv += pf + "if (" + arr_name + "_constr." + arr_len + " > 0)\n"
120                 if subty.is_native_primitive:
121                     szof = subty.c_ty
122                 else:
123                     szof = subty.rust_obj
124                 arg_conv += pf + "\t" + arr_name + "_constr." + ty_info.arr_access + " = MALLOC(" + arr_name + "_constr." + arr_len + " * sizeof(" + szof + "), \"" + ty_info.rust_obj + " Elements\");\n"
125                 arg_conv += pf + "else\n"
126                 arg_conv += pf + "\t" + arr_name + "_constr." + ty_info.arr_access + " = NULL;\n"
127                 get_arr = self.consts.get_native_arr_contents(arr_name, "NO_DEST", arr_name + "_constr." + arr_len, ty_info, False)
128                 if get_arr != None:
129                     arg_conv += pf + subty.c_ty + "* " + arr_name + "_vals = " + get_arr + ";\n"
130                 arg_conv += pf + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_constr." + arr_len + "; " + idxc + "++) {\n"
131                 if get_arr != None:
132                     arg_conv += pf + "\t" + subty.c_ty + " " + conv_name + " = " + arr_name + "_vals[" + idxc + "];"
133                     if subty.arg_conv is not None:
134                         arg_conv += "\n\t" + pf + subty.arg_conv.replace("\n", "\n\t" + pf)
135                 else:
136                     arg_conv += pf + "\t" + subty.c_ty + " " + conv_name + " = " + self.consts.get_native_arr_elem(arr_name, idxc, ty_info) + ";\n"
137                     arg_conv += pf + "\t" + subty.arg_conv.replace("\n", "\n\t" + pf)
138                 arg_conv += "\n\t" + pf + arr_name + "_constr." + ty_info.arr_access + "[" + idxc + "] = " + subty.arg_conv_name + ";\n" + pf + "}"
139                 if get_arr != None:
140                     cleanup = self.consts.cleanup_native_arr_ref_contents(arr_name, arr_name + "_vals", arr_name + "_constr." + arr_len, ty_info)
141                     if cleanup is not None:
142                         arg_conv += "\n" + pf + cleanup + ";"
143                 if ty_info.is_ptr:
144                     arg_conv_name = arr_name + "_ptr"
145                 else:
146                     arg_conv_name = arr_name + "_constr"
147                 arg_conv_cleanup = None
148                 if ty_info.is_ptr:
149                     arg_conv_cleanup = "if (" + arr_name + "_ptr != NULL) { FREE(" + arr_name + "_constr." + ty_info.arr_access + "); }"
150                 if ty_info.is_ptr:
151                     arg_conv += "\n\t" + arr_name + "_ptr = &" + arr_name + "_constr;\n}"
152
153                 if is_nullable:
154                     ret_conv = (ty_info.rust_obj + " *" + arr_name + "_var_ptr = ", "")
155                 else:
156                     ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "")
157                 if subty.ret_conv is None:
158                     ret_conv = ("DUMMY", "DUMMY")
159                 else:
160                     ret_conv = (ret_conv[0], ";\n" + ty_info.c_ty + " " + arr_name + "_arr = NULL;\n")
161                     indent = ""
162                     if is_nullable:
163                         ret_conv = (ret_conv[0], ret_conv[1] + "if (" + arr_name + " != NULL) {\n")
164                         ret_conv = (ret_conv[0], ret_conv[1] + "\t" + ty_info.rust_obj + " " + arr_name + "_var = *" + arr_name + "_var_ptr;\n")
165                         indent = "\t"
166                     ret_conv = (ret_conv[0], ret_conv[1] + indent + arr_name + "_arr = " + self.consts.create_native_arr_call(arr_name + "_var." + arr_len, ty_info) + ";\n")
167                     get_ptr_call = self.consts.get_native_arr_ptr_call(ty_info)
168                     if get_ptr_call is not None:
169                         ret_conv = (ret_conv[0], ret_conv[1] + indent + subty.c_ty + " *" + arr_name + "_arr_ptr = " + get_ptr_call[0] + arr_name + "_arr" + get_ptr_call[1] + ";\n")
170                     ret_conv = (ret_conv[0], ret_conv[1] + indent + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_var." + arr_len + "; " + idxc + "++) {\n")
171                     ret_conv = (ret_conv[0], ret_conv[1] + indent + "\t" + subty.ret_conv[0].replace("\n", "\n\t" + indent))
172                     ret_conv = (ret_conv[0], ret_conv[1] + indent + arr_name + "_var." + ty_info.arr_access + "[" + idxc + "]" + subty.ret_conv[1].replace("\n", "\n\t" + indent) + "\n")
173                     if get_ptr_call is not None:
174                         ret_conv = (ret_conv[0], ret_conv[1] + indent + "\t" + arr_name + "_arr_ptr[" + idxc + "] = " + subty.ret_conv_name + ";\n" + indent + "}\n")
175                     else:
176                         ret_conv = (ret_conv[0], ret_conv[1] + indent + "\t" + self.consts.get_native_arr_entry_call(ty_info, arr_name + "_arr", idxc, subty.ret_conv_name) + ";\n" + indent + "}\n")
177                     cleanup = self.consts.release_native_arr_ptr_call(ty_info, arr_name + "_arr", arr_name + "_arr_ptr")
178                     if cleanup is not None:
179                         ret_conv = (ret_conv[0], ret_conv[1] + indent + cleanup + ";")
180                     if not holds_ref and not is_nullable:
181                         # XXX: The commented if's are a bit smarter freeing, but we need to be a nudge smarter still
182                         # Note that we don't drop the full vec here - we're passing ownership to java (or have cloned) or free'd by now!
183                         ret_conv = (ret_conv[0], ret_conv[1] + "\n" + indent + "FREE(" + arr_name + "_var." + ty_info.arr_access + ");")
184                         #if subty.rust_obj is not None and subty.rust_obj in self.opaque_structs:
185                         #    ret_conv = (ret_conv[0], ret_conv[1] + "\nFREE(" + arr_name + "_var." + ty_info.arr_access + ");")
186                         #else:
187                         #    ret_conv = (ret_conv[0], ret_conv[1] + "\n" + ty_info.rust_obj.replace("LDK", "") + "_free(" + arr_name + "_var);")
188                     if is_nullable:
189                         ret_conv = (ret_conv[0], ret_conv[1] + "\n}")
190
191                 if subty.to_hu_conv is not None:
192                     to_hu_conv = self.consts.var_decl_statement(self.consts.c_type_map["uint32_t"][0], conv_name + "_len", self.consts.get_java_arr_len(arr_name)) + ";\n"
193                     to_hu_conv += self.consts.var_decl_statement(ty_info.java_hu_ty, conv_name + "_arr", self.consts.constr_hu_array(ty_info, conv_name + "_len"))
194                     to_hu_conv += ";\n"
195                     pfx = ""
196                     if is_nullable:
197                         to_hu_conv += "if (" + arr_name + " != null) {\n"
198                         pfx = "\t"
199                     to_hu_conv += pfx + self.consts.for_n_in_range(idxc, "0", conv_name + "_len") + "\n"
200
201                     to_hu_conv += pfx + "\t" + self.consts.var_decl_statement(subty.java_ty, conv_name, self.consts.get_java_arr_elem(subty, arr_name, idxc)) + ";\n"
202                     to_hu_conv += pfx + "\t" + subty.to_hu_conv.replace("\n", "\n\t" + pfx) + "\n"
203                     to_hu_conv += pfx + "\t" + conv_name + "_arr[" + idxc + "] = " + subty.to_hu_conv_name + ";\n"
204                     to_hu_conv += pfx + "}"
205                     if is_nullable:
206                         to_hu_conv += "\n}"
207                     cleanup = self.consts.cleanup_converted_native_array(ty_info, arr_name)
208                     if cleanup is not None:
209                         to_hu_conv += "\n" + cleanup
210                     to_hu_conv_name = conv_name + "_arr"
211                 else:
212                     to_hu_conv = self.consts.primitive_arr_to_hu(ty_info, None, arr_name, arr_name + "_conv")
213                     if to_hu_conv is not None:
214                         to_hu_conv_name = arr_name + "_conv"
215                     else:
216                         to_hu_conv_name = None
217                 from_hu_conv = self.consts.primitive_arr_from_hu(ty_info, None, arr_name)
218                 if subty.from_hu_conv is not None:
219                     hu_conv_b = ""
220                     if subty.from_hu_conv[1] != "":
221                         iterator = self.consts.for_n_in_arr(conv_name, arr_name, subty)
222                         if is_nullable:
223                             hu_conv_b = "if (" + arr_name + " != null) { " + iterator[0] + subty.from_hu_conv[1] + ";" + iterator[1] + " }"
224                         else:
225                             hu_conv_b = iterator[0] + subty.from_hu_conv[1] + ";" + iterator[1]
226                     if from_hu_conv is not None:
227                         arr_conv = self.consts.primitive_arr_from_hu(ty_info, None, self.consts.map_hu_array_elems(arr_name, conv_name, ty_info, subty, is_nullable))
228                         assert arr_conv[1] == ""
229                         from_hu_conv = (arr_conv[0], hu_conv_b)
230                     else:
231                         from_hu_conv = (self.consts.map_hu_array_elems(arr_name, conv_name, ty_info, subty, is_nullable), hu_conv_b)
232
233                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
234                     arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = arg_conv_cleanup,
235                     ret_conv = ret_conv, ret_conv_name = arr_name + "_arr", to_hu_conv = to_hu_conv, to_hu_conv_name = to_hu_conv_name, from_hu_conv = from_hu_conv)
236         elif ty_info.java_fn_ty_arg == "Ljava/lang/String;":
237             assert not is_nullable
238             if not is_free:
239                 arg_conv = "LDKStr " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_c_call(ty_info.var_name) + ";"
240                 arg_conv_name = ty_info.var_name + "_conv"
241             else:
242                 arg_conv = "LDKStr dummy = { .chars = NULL, .len = 0, .chars_is_owned = false };"
243                 arg_conv_name = "dummy"
244             if ty_info.arr_access is None:
245                 assert False
246                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
247                     arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
248                     ret_conv = ("const char* " + ty_info.var_name + "_str = ",
249                         ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str", "strlen(" + ty_info.var_name + "_str)") + ";"),
250                     ret_conv_name = ty_info.var_name + "_conv",
251                     to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
252             else:
253                 free_str = ""
254                 if not holds_ref:
255                     free_str = "\nStr_free(" + ty_info.var_name + "_str);"
256                 to_hu_conv = self.consts.str_to_hu_conv(ty_info.var_name)
257                 to_hu_conv_name = None
258                 if to_hu_conv is not None:
259                     to_hu_conv_name = ty_info.var_name + "_conv"
260                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
261                     arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
262                     ret_conv = ("LDKStr " + ty_info.var_name + "_str = ",
263                         ";\njstring " + ty_info.var_name + "_conv = " + self.consts.str_ref_to_native_call(ty_info.var_name + "_str." + ty_info.arr_access, ty_info.var_name + "_str." + ty_info.arr_len) + ";" + free_str),
264                     ret_conv_name = ty_info.var_name + "_conv",
265                     to_hu_conv=to_hu_conv, to_hu_conv_name=to_hu_conv_name,
266                     from_hu_conv = self.consts.str_from_hu_conv(ty_info.var_name))
267         elif ty_info.var_name == "" and not print_void:
268             # We don't have a parameter name, and want one, just call it arg
269             if not ty_info.is_native_primitive:
270                 assert(not is_free or ty_info.rust_obj not in self.opaque_structs)
271                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
272                     arg_conv = ty_info.rust_obj + " arg_conv = *(" + ty_info.rust_obj + "*)untag_ptr(arg);\nFREE(untag_ptr(arg));",
273                     arg_conv_name = "arg_conv", arg_conv_cleanup = None,
274                     ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 7", to_hu_conv_name = None, from_hu_conv = None)
275             else:
276                 assert(not is_free)
277                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
278                     arg_conv = None, arg_conv_name = "arg", arg_conv_cleanup = None,
279                     ret_conv = None, ret_conv_name = None, to_hu_conv = "TODO 8", to_hu_conv_name = None, from_hu_conv = None)
280         elif ty_info.is_native_primitive and ty_info.c_ty != "void":
281             assert not is_nullable
282             return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
283                 arg_conv = None, arg_conv_name =  ty_info.var_name, arg_conv_cleanup = None,
284                 ret_conv = (ty_info.c_ty + " " + ty_info.var_name + "_conv = ", ";"), ret_conv_name = ty_info.var_name + "_conv",
285                 to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
286         elif ty_info.c_ty == "void":
287             assert ty_info.is_native_primitive
288             assert not is_nullable
289             return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
290                 arg_conv = None, arg_conv_name =  ty_info.var_name, arg_conv_cleanup = None,
291                 ret_conv = None, ret_conv_name = ty_info.var_name,
292                 to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
293         else:
294             if ty_info.var_name == "":
295                 ty_info.var_name = "ret"
296
297             if ty_info.rust_obj in self.opaque_structs:
298                 if is_nullable:
299                     from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
300                 else:
301                     from_hu_conv = (self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name))
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"
304                 opaque_arg_conv += ty_info.var_name + "_conv.is_owned = ptr_is_owned(" + ty_info.var_name + ");\n"
305                 opaque_arg_conv += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_conv);"
306
307                 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:
308                     if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
309                         # 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.
310                         # arg_conv is used when converting a function argument from java normally (with holds_ref set),
311                         # and when converting a java value being returned from a trait method (with holds_ref unset).
312                         # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
313                         # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
314                         if holds_ref:
315                             opaque_arg_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
316                         elif is_nullable:
317                             from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
318                         else:
319                             from_hu_conv = (ty_info.var_name + ".clone_ptr()", "")
320                     elif ty_info.passed_as_ptr:
321                         opaque_arg_conv += "\n// WARNING: we need a move here but no clone is available for " + ty_info.rust_obj + "\n"
322                         # TODO: Once we support features cloning (which just isn't in C yet), we can make this a compile error instead!
323                         from_hu_conv = (from_hu_conv[0], from_hu_conv[1] + ";\n" +
324                             "// Due to rust's strict-ownership memory model, in some cases we need to \"move\"\n" +
325                             "// an object to pass exclusive ownership to the function being called.\n" +
326                             "// In most cases, we avoid this being visible in GC'd languages by cloning the object\n" +
327                             "// at the FFI layer, creating a new object which Rust can claim ownership of\n" +
328                             "// However, in some cases (eg here), there is no way to clone an object, and thus\n" +
329                             "// we actually have to pass full ownership to Rust.\n" +
330                             "// Thus, after this call, " + ty_info.var_name + " is reset to null and is now a dummy object.\n" + self.consts.set_null_skip_free(ty_info.var_name))
331                 elif not is_free:
332                     opaque_arg_conv += "\n" + ty_info.var_name + "_conv.is_owned = false;"
333
334                 opaque_ret_conv_suf = ";\n"
335                 opaque_ret_conv_suf += self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = 0;\n"
336                 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
337                     opaque_ret_conv_suf += ty_info.var_name + "_var = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_var);\n"
338                 elif not holds_ref and ty_info.is_ptr:
339                     opaque_ret_conv_suf += "// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n"
340
341                 opaque_ret_conv_suf += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_var);\n"
342                 if holds_ref:
343                     opaque_ret_conv_suf += ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_var.inner, false);"
344                 else:
345                     opaque_ret_conv_suf += ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_var.inner, " + ty_info.var_name + "_var.is_owned);"
346
347                 to_hu_conv_sfx = ""
348                 if not ty_info.is_ptr or holds_ref:
349                     to_hu_conv_sfx = "\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";"
350                 qualified_hu_ty = self.consts.fully_qualified_hu_ty_path(ty_info)
351                 if ty_info.is_ptr:
352                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
353                         arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
354                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = *", opaque_ret_conv_suf),
355                         ret_conv_name = ty_info.var_name + "_ref",
356                         to_hu_conv = self.consts.to_hu_conv_templates['ptr'].replace('{human_type}', qualified_hu_ty).replace('{var_name}', ty_info.var_name) + to_hu_conv_sfx,
357                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
358                         from_hu_conv = from_hu_conv)
359                 else:
360                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
361                         arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
362                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", opaque_ret_conv_suf),
363                         ret_conv_name = ty_info.var_name + "_ref",
364                         to_hu_conv = self.consts.to_hu_conv_templates['default'].replace('{human_type}', qualified_hu_ty).replace('{var_name}', ty_info.var_name) + to_hu_conv_sfx,
365                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
366                         from_hu_conv = from_hu_conv)
367
368             assert not is_nullable
369             if not ty_info.is_ptr:
370                 if ty_info.rust_obj in self.unitary_enums:
371                     (ret_pfx, ret_sfx) = self.consts.c_unitary_enum_to_native_call(ty_info)
372                     (arg_pfx, arg_sfx) = self.consts.native_unitary_enum_to_c_call(ty_info)
373                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
374                         arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = " + arg_pfx + ty_info.var_name + arg_sfx + ";",
375                         arg_conv_name = ty_info.var_name + "_conv",
376                         arg_conv_cleanup = None,
377                         ret_conv = (ty_info.c_ty + " " + ty_info.var_name + "_conv = " + ret_pfx, ret_sfx + ";"),
378                         ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
379                 base_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
380                 base_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
381                 base_conv += ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)(" + ty_info.var_name + "_ptr);"
382                 from_hu_conv = None
383                 if ty_info.rust_obj in self.trait_structs:
384                     ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*" + ty_info.var_name + "_ret = ", ";")
385                     if holds_ref:
386                         if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
387                             ret_conv = (ret_conv[0] + ty_info.rust_obj.replace("LDK", "") + "_clone(&", ");")
388                         else:
389                             ret_conv = (ret_conv[0], ";\n// WARNING: We likely need to clone here, but no clone is available, so we just do it for Java instances")
390                             ret_conv = (ret_conv[0], ret_conv[1] + "" + self.consts.trait_struct_inc_refcnt(ty_info).replace(ty_info.var_name + "_conv", "(*" + ty_info.var_name + "_ret)"))
391                     if not is_free:
392                         needs_full_clone = not is_free and (not ty_info.is_ptr and not holds_ref or ty_info.requires_clone == True) and ty_info.requires_clone != False
393                         if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
394                             # arg_conv is used when converting a function argument from java normally (with holds_ref set),
395                             # and when converting a java value being returned from a trait method (with holds_ref unset).
396                             # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
397                             # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
398                             if holds_ref:
399                                 base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
400                             else:
401                                 from_hu_conv = (ty_info.var_name + ".clone_ptr()", "")
402                                 base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
403                         else:
404                             base_conv = base_conv + self.consts.trait_struct_inc_refcnt(ty_info)
405                             if needs_full_clone:
406                                 base_conv += "// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n"
407                     else:
408                         base_conv = base_conv + "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
409                     if from_hu_conv is None:
410                         from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
411                     from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name))
412                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
413                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
414                         ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ret, true)",
415                         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                         to_hu_conv_name = "ret_hu_conv", from_hu_conv = from_hu_conv)
417                 needs_full_clone = not is_free and (not ty_info.is_ptr or ty_info.requires_clone == True) and ty_info.requires_clone != False
418                 if needs_full_clone:
419                     if "res" in ty_info.var_name: # XXX: This is a stupid hack
420                         needs_full_clone = False
421                     if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
422                         # arg_conv is used when converting a function argument from java normally (with holds_ref set),
423                         # and when converting a java value being returned from a trait method (with holds_ref unset).
424                         # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
425                         # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
426                         if holds_ref:
427                             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 + "));"
428                         else:
429                             from_hu_conv = (ty_info.var_name + ".clone_ptr()", "")
430                             base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
431                     elif needs_full_clone:
432                         base_conv = base_conv + "\n// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj
433                 if not needs_full_clone and ty_info.rust_obj != "LDKu8slice" and (not holds_ref or is_free):
434                     # Don't bother free'ing slices passed in - Rust doesn't auto-free the
435                     # underlying unlike Vecs, and it gives Java more freedom.
436                     base_conv = base_conv + "\nFREE(untag_ptr(" + ty_info.var_name + "));"
437                 if ty_info.rust_obj in self.complex_enums:
438                     if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
439                         # We really need a full clone here, but for now we just implement
440                         # a manual clone explicitly for Option<Trait>s
441                         if ty_info.contains_trait:
442                             assert ty_info.rust_obj.startswith("LDKCOption") # We don't support contained traits for anything else yet
443                             optional_ty = ty_info.rust_obj[11:-1]
444                             assert "LDK" + optional_ty in self.trait_structs # We don't support contained traits for anything else yet
445                             base_conv += "\nif (" + ty_info.var_name + "_conv.tag == " + ty_info.rust_obj + "_Some) {"
446                             base_conv += "\n\t// Manually implement clone for Java trait instances"
447                             optional_ty_info = self.java_c_types("LDK" + optional_ty + " " + ty_info.var_name, None)
448                             base_conv += self.consts.trait_struct_inc_refcnt(optional_ty_info).\
449                                 replace("\n", "\n\t").replace(ty_info.var_name + "_conv", ty_info.var_name + "_conv.some")
450                             base_conv += "\n}"
451                     ret_conv = (self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = tag_ptr(&", ", false);")
452                     if not holds_ref:
453                         ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_copy = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n", "")
454                         ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = ", "")
455                         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                     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))
459                     fully_qualified_ty = self.consts.fully_qualified_hu_ty_path(ty_info)
460                     to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info.var_name + ")"
461                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
462                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
463                         ret_conv = ret_conv, ret_conv_name = ty_info.var_name + "_ref",
464                         to_hu_conv = self.consts.var_decl_statement(fully_qualified_ty, ty_info.var_name + "_hu_conv", to_hu_call) + ";\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";",
465                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
466                 if ty_info.rust_obj in self.result_types:
467                     if holds_ref:
468                         # If we're trying to return a ref, we have to clone.
469                         # We just blindly assume its implemented and let the compiler fail if its not.
470                         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 = ", ";")
471                         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);")
472                     else:
473                         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                     if from_hu_conv is None:
475                         from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
476                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
477                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
478                         ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, true)",
479                         to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
480                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
481                 if ty_info.rust_obj in self.tuple_types:
482                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, "
483                     if holds_ref:
484                         # If we're trying to return a ref, we have to clone.
485                         if (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
486                             ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = &", "")
487                             ret_conv = (ret_conv[0], ";\n// WARNING: we really need to clone here, but no clone is available for " + ty_info.rust_obj)
488                             ret_conv_name += "false)"
489                         else:
490                             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 = ", ";")
491                             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);")
492                             ret_conv_name += "true)"
493                     else:
494                         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 = ", ";")
495                         ret_conv_name += "true)"
496                     if not ty_info.is_ptr or holds_ref:
497                         to_hu_conv_sfx = "\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";"
498                     else:
499                         to_hu_conv_sfx = ""
500                     if from_hu_conv is None:
501                         from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
502                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
503                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
504                         ret_conv = ret_conv, ret_conv_name = ret_conv_name,
505                         to_hu_conv = self.consts.var_decl_statement(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,
506                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
507
508                 # The manually-defined types - TxIn, TxOut, BigEndianScalar, U5, and Error
509                 if ty_info.rust_obj == "LDKError":
510                     assert from_hu_conv is None
511                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
512                         arg_conv = "", arg_conv_name = "(LDKError){ ._dummy = 0 }", arg_conv_cleanup = None,
513                         ret_conv = ("/*", "*/"), ret_conv_name = "0",
514                         to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " + ty_info.java_hu_ty + "(" + ty_info.var_name + ")") + ";",
515                         to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = ("0", ""))
516
517                 if ty_info.rust_obj == "LDKU5" or ty_info.rust_obj == "LDKWitnessVersion":
518                     assert from_hu_conv is None
519                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
520                         arg_conv = "", arg_conv_name = "(" + ty_info.rust_obj + "){ ._0 = " + ty_info.var_name + " }", arg_conv_cleanup = None,
521                         ret_conv = ("uint8_t " + ty_info.var_name + "_val = ", "._0;"), ret_conv_name = ty_info.var_name + "_val",
522                         to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " + ty_info.java_hu_ty + "(" + ty_info.var_name + ")") + ";",
523                         to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".getVal()", ""))
524
525                 assert ty_info.rust_obj == "LDKWitnessProgram" or ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKTxIn" or ty_info.rust_obj == "LDKBigEndianScalar"
526                 if not ty_info.is_ptr and not holds_ref:
527                     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 = ", ";")
528                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, true)"
529                 else:
530                     ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = &", ";")
531                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, false)"
532                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
533                     arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
534                     ret_conv = ret_conv, ret_conv_name = ret_conv_name,
535                     to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";",
536                     to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (self.consts.get_ptr(ty_info.var_name), ""))
537             elif ty_info.is_ptr:
538                 assert(not is_free)
539                 if ty_info.rust_obj in self.complex_enums:
540                     ret_conv = (self.consts.ptr_c_ty + " ref_" + ty_info.var_name + " = tag_ptr(", ", false); // WARNING: We should clone here!")
541                     from_hu_sfx = self.consts.add_ref("this", ty_info.var_name)
542                     if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
543                         ret_conv_pfx = ty_info.rust_obj + " *ret_" + ty_info.var_name + " = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + " ret conversion\");\n"
544                         ret_conv_pfx += "*ret_" + ty_info.var_name + " = " + ty_info.rust_obj.replace("LDK", "") + "_clone("
545                         ret_conv_sfx = ");\n" + self.consts.ptr_c_ty + " ref_" + ty_info.var_name + " = tag_ptr(ret_" + ty_info.var_name + ", true);"
546                         ret_conv = (ret_conv_pfx, ret_conv_sfx)
547                         from_hu_sfx = ""
548                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
549                         arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + ");",
550                         arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
551                         ret_conv = ret_conv, ret_conv_name = "ref_" + ty_info.var_name,
552                         to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
553                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
554                         from_hu_conv = (self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
555                 elif ty_info.rust_obj in self.trait_structs:
556                     if ty_info.nonnull_ptr:
557                         arg_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
558                         arg_conv += "if (ptr_is_owned(" + ty_info.var_name + ")) { CHECK_ACCESS(" + ty_info.var_name + "_ptr); }\n"
559                         arg_conv += ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
560                         arg_conv_name = ty_info.var_name + "_conv"
561                         from_hu_conv_pfx = self.consts.get_ptr(ty_info.var_name)
562                     else:
563                         # We map Option<Trait> as *mut Trait, which we can differentiate from &Trait by the NONNULL_PTR annotation.
564                         # We handle the Option<Trait> case here.
565                         arg_conv = ty_info.rust_obj + " *" + ty_info.var_name + "_conv_ptr = NULL;\n"
566                         arg_conv += "if (" + ty_info.var_name + " != 0) {\n"
567                         arg_conv += "\t" + ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
568                         arg_conv += "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
569                         arg_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
570                         arg_conv += "\t" + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
571                         arg_conv += self.consts.trait_struct_inc_refcnt(ty_info).replace("\n", "\n\t")
572                         arg_conv += "\n\t" + ty_info.var_name + "_conv_ptr = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n"
573                         arg_conv += "\t*" + ty_info.var_name + "_conv_ptr = " + ty_info.var_name + "_conv;\n"
574                         arg_conv += "}"
575                         arg_conv_name = ty_info.var_name + "_conv_ptr"
576                         from_hu_conv_pfx = self.consts.get_ptr(ty_info.var_name)
577                     if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
578                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
579                             arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
580                             ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" +
581                                 "*" + ty_info.var_name + "_clone = " + ty_info.rust_obj.replace("LDK", "") + "_clone(", ");"),
582                             ret_conv_name = "tag_ptr(" + ty_info.var_name + "_clone, true)",
583                             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") + ";",
584                             to_hu_conv_name = "ret_hu_conv", from_hu_conv = (from_hu_conv_pfx, ""))
585                     else:
586                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
587                             arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
588                             ret_conv = ("// WARNING: This object doesn't live past this scope, needs clone!\n" + self.consts.ptr_c_ty + " ret_" + ty_info.var_name + " = tag_ptr(", ", false);"),
589                             ret_conv_name = "ret_" + ty_info.var_name,
590                             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") + ";",
591                             to_hu_conv_name = "ret_hu_conv", from_hu_conv = (from_hu_conv_pfx, self.consts.add_ref("this", ty_info.var_name)))
592                 ret_conv = (self.consts.ptr_c_ty + " ret_" + ty_info.var_name + " = tag_ptr(", ", true);")
593                 if holds_ref:
594                     ret_conv = (ret_conv[0], ", false);")
595                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
596                     arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + ");",
597                     arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
598                     ret_conv = ret_conv, ret_conv_name = "ret_" + ty_info.var_name,
599                     to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed
600             assert False # We should have handled every case by now.