Support (fixed-length) arrays of 16-bit integers
[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))
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), 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                 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))
299                 opaque_arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
300                 opaque_arg_conv = opaque_arg_conv + ty_info.var_name + "_conv.inner = untag_ptr(" + ty_info.var_name + ");\n"
301                 opaque_arg_conv += ty_info.var_name + "_conv.is_owned = ptr_is_owned(" + ty_info.var_name + ");\n"
302                 opaque_arg_conv += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_conv);"
303
304                 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:
305                     if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
306                         # 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.
307                         # arg_conv is used when converting a function argument from java normally (with holds_ref set),
308                         # and when converting a java value being returned from a trait method (with holds_ref unset).
309                         # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
310                         # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
311                         if holds_ref:
312                             opaque_arg_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
313                         else:
314                             from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
315                     elif ty_info.passed_as_ptr:
316                         opaque_arg_conv += "\n// WARNING: we need a move here but no clone is available for " + ty_info.rust_obj + "\n"
317                         # TODO: Once we support features cloning (which just isn't in C yet), we can make this a compile error instead!
318                         from_hu_conv = (from_hu_conv[0], from_hu_conv[1] + ";\n" +
319                             "// Due to rust's strict-ownership memory model, in some cases we need to \"move\"\n" +
320                             "// an object to pass exclusive ownership to the function being called.\n" +
321                             "// In most cases, we avoid this being visible in GC'd languages by cloning the object\n" +
322                             "// at the FFI layer, creating a new object which Rust can claim ownership of\n" +
323                             "// However, in some cases (eg here), there is no way to clone an object, and thus\n" +
324                             "// we actually have to pass full ownership to Rust.\n" +
325                             "// 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))
326                 elif not is_free:
327                     opaque_arg_conv += "\n" + ty_info.var_name + "_conv.is_owned = false;"
328
329                 opaque_ret_conv_suf = ";\n"
330                 opaque_ret_conv_suf += self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = 0;\n"
331                 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
332                     opaque_ret_conv_suf += ty_info.var_name + "_var = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_var);\n"
333                 elif not holds_ref and ty_info.is_ptr:
334                     opaque_ret_conv_suf += "// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n"
335
336                 opaque_ret_conv_suf += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_var);\n"
337                 if holds_ref:
338                     opaque_ret_conv_suf += ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_var.inner, false);"
339                 else:
340                     opaque_ret_conv_suf += ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_var.inner, " + ty_info.var_name + "_var.is_owned);"
341
342                 to_hu_conv_sfx = ""
343                 if not ty_info.is_ptr or holds_ref:
344                     to_hu_conv_sfx = "\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";"
345                 qualified_hu_ty = self.consts.fully_qualified_hu_ty_path(ty_info)
346                 if ty_info.is_ptr:
347                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
348                         arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
349                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = *", opaque_ret_conv_suf),
350                         ret_conv_name = ty_info.var_name + "_ref",
351                         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,
352                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
353                         from_hu_conv = from_hu_conv)
354                 else:
355                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
356                         arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
357                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", opaque_ret_conv_suf),
358                         ret_conv_name = ty_info.var_name + "_ref",
359                         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,
360                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
361                         from_hu_conv = from_hu_conv)
362
363             assert not is_nullable
364             if not ty_info.is_ptr:
365                 if ty_info.rust_obj in self.unitary_enums:
366                     (ret_pfx, ret_sfx) = self.consts.c_unitary_enum_to_native_call(ty_info)
367                     (arg_pfx, arg_sfx) = self.consts.native_unitary_enum_to_c_call(ty_info)
368                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
369                         arg_conv = ty_info.rust_obj + " " + ty_info.var_name + "_conv = " + arg_pfx + ty_info.var_name + arg_sfx + ";",
370                         arg_conv_name = ty_info.var_name + "_conv",
371                         arg_conv_cleanup = None,
372                         ret_conv = (ty_info.c_ty + " " + ty_info.var_name + "_conv = " + ret_pfx, ret_sfx + ";"),
373                         ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
374                 base_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
375                 base_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
376                 base_conv += ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)(" + ty_info.var_name + "_ptr);"
377                 from_hu_conv = None
378                 if ty_info.rust_obj in self.trait_structs:
379                     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 = ", ";")
380                     if holds_ref:
381                         if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
382                             ret_conv = (ret_conv[0] + ty_info.rust_obj.replace("LDK", "") + "_clone(&", ");")
383                         else:
384                             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")
385                             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)"))
386                     if not is_free:
387                         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
388                         if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
389                             # arg_conv is used when converting a function argument from java normally (with holds_ref set),
390                             # and when converting a java value being returned from a trait method (with holds_ref unset).
391                             # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
392                             # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
393                             if holds_ref:
394                                 base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone(&" + ty_info.var_name + "_conv);"
395                             else:
396                                 from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
397                                 base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
398                         else:
399                             base_conv = base_conv + self.consts.trait_struct_inc_refcnt(ty_info)
400                             if needs_full_clone:
401                                 base_conv += "// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n"
402                     else:
403                         base_conv = base_conv + "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
404                     if from_hu_conv is None:
405                         from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), "")
406                     from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name))
407                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
408                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
409                         ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ret, true)",
410                         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") + ";",
411                         to_hu_conv_name = "ret_hu_conv", from_hu_conv = from_hu_conv)
412                 needs_full_clone = not is_free and (not ty_info.is_ptr or ty_info.requires_clone == True) and ty_info.requires_clone != False
413                 if needs_full_clone:
414                     if "res" in ty_info.var_name: # XXX: This is a stupid hack
415                         needs_full_clone = False
416                     if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
417                         # arg_conv is used when converting a function argument from java normally (with holds_ref set),
418                         # and when converting a java value being returned from a trait method (with holds_ref unset).
419                         # In the second case, we need to clone before returning to C (as once we return the GC can free the object),
420                         # whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
421                         if holds_ref:
422                             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 + "));"
423                         else:
424                             from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + ty_info.var_name + ".clone_ptr()", "")
425                             base_conv += "\n" + "FREE(untag_ptr(" + ty_info.var_name + "));"
426                     elif needs_full_clone:
427                         base_conv = base_conv + "\n// WARNING: we may need a move here but no clone is available for " + ty_info.rust_obj
428                 if not needs_full_clone and ty_info.rust_obj != "LDKu8slice" and (not holds_ref or is_free):
429                     # Don't bother free'ing slices passed in - Rust doesn't auto-free the
430                     # underlying unlike Vecs, and it gives Java more freedom.
431                     base_conv = base_conv + "\nFREE(untag_ptr(" + ty_info.var_name + "));"
432                 if ty_info.rust_obj in self.complex_enums:
433                     if needs_full_clone and (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
434                         # We really need a full clone here, but for now we just implement
435                         # a manual clone explicitly for Option<Trait>s
436                         if ty_info.contains_trait:
437                             assert ty_info.rust_obj.startswith("LDKCOption") # We don't support contained traits for anything else yet
438                             optional_ty = ty_info.rust_obj[11:-1]
439                             assert "LDK" + optional_ty in self.trait_structs # We don't support contained traits for anything else yet
440                             base_conv += "\nif (" + ty_info.var_name + "_conv.tag == " + ty_info.rust_obj + "_Some) {"
441                             base_conv += "\n\t// Manually implement clone for Java trait instances"
442                             optional_ty_info = self.java_c_types("LDK" + optional_ty + " " + ty_info.var_name, None)
443                             base_conv += self.consts.trait_struct_inc_refcnt(optional_ty_info).\
444                                 replace("\n", "\n\t").replace(ty_info.var_name + "_conv", ty_info.var_name + "_conv.some")
445                             base_conv += "\n}"
446                     ret_conv = (self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = tag_ptr(&", ", false);")
447                     if not holds_ref:
448                         ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_copy = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n", "")
449                         ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = ", "")
450                         ret_conv = (ret_conv[0], ";\n" + self.consts.ptr_c_ty + " " + ty_info.var_name + "_ref = tag_ptr(" + ty_info.var_name + "_copy, true);")
451                     if from_hu_conv is None:
452                         from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
453                     from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name))
454                     fully_qualified_ty = self.consts.fully_qualified_hu_ty_path(ty_info)
455                     to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info.var_name + ")"
456                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
457                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
458                         ret_conv = ret_conv, ret_conv_name = ty_info.var_name + "_ref",
459                         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") + ";",
460                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
461                 if ty_info.rust_obj in self.result_types:
462                     if holds_ref:
463                         # If we're trying to return a ref, we have to clone.
464                         # We just blindly assume its implemented and let the compiler fail if its not.
465                         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 = ", ";")
466                         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);")
467                     else:
468                         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 = ", ";")
469                     if from_hu_conv is None:
470                         from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
471                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
472                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
473                         ret_conv = ret_conv, ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, true)",
474                         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 + ")") + ";",
475                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
476                 if ty_info.rust_obj in self.tuple_types:
477                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_conv, "
478                     if holds_ref:
479                         # If we're trying to return a ref, we have to clone.
480                         if (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
481                             ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_conv = &", "")
482                             ret_conv = (ret_conv[0], ";\n// WARNING: we really need to clone here, but no clone is available for " + ty_info.rust_obj)
483                             ret_conv_name += "false)"
484                         else:
485                             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 = ", ";")
486                             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);")
487                             ret_conv_name += "true)"
488                     else:
489                         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 = ", ";")
490                         ret_conv_name += "true)"
491                     if not ty_info.is_ptr or holds_ref:
492                         to_hu_conv_sfx = "\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";"
493                     else:
494                         to_hu_conv_sfx = ""
495                     if from_hu_conv is None:
496                         from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : " + self.consts.native_zero_ptr, "")
497                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
498                         arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
499                         ret_conv = ret_conv, ret_conv_name = ret_conv_name,
500                         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,
501                         to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
502
503                 # The manually-defined types - TxOut, BigEndianScalar, U5, and Error
504                 if ty_info.rust_obj == "LDKError":
505                     assert from_hu_conv is None
506                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
507                         arg_conv = "", arg_conv_name = "(LDKError){ ._dummy = 0 }", arg_conv_cleanup = None,
508                         ret_conv = ("/*", "*/"), ret_conv_name = "0",
509                         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 + ")") + ";",
510                         to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = ("0", ""))
511
512                 if ty_info.rust_obj == "LDKU5" or ty_info.rust_obj == "LDKWitnessVersion":
513                     assert from_hu_conv is None
514                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
515                         arg_conv = "", arg_conv_name = "(" + ty_info.rust_obj + "){ ._0 = " + ty_info.var_name + " }", arg_conv_cleanup = None,
516                         ret_conv = ("uint8_t " + ty_info.var_name + "_val = ", "._0;"), ret_conv_name = ty_info.var_name + "_val",
517                         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 + ")") + ";",
518                         to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (ty_info.var_name + ".getVal()", ""))
519
520                 assert ty_info.rust_obj == "LDKTxOut" or ty_info.rust_obj == "LDKBigEndianScalar"
521                 if not ty_info.is_ptr and not holds_ref:
522                     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 = ", ";")
523                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, true)"
524                 else:
525                     ret_conv = (ty_info.rust_obj + "* " + ty_info.var_name + "_ref = &", ";")
526                     ret_conv_name = "tag_ptr(" + ty_info.var_name + "_ref, false)"
527                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
528                     arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
529                     ret_conv = ret_conv, ret_conv_name = ret_conv_name,
530                     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 + ")") + ";",
531                     to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (self.consts.get_ptr(ty_info.var_name), ""))
532             elif ty_info.is_ptr:
533                 assert(not is_free)
534                 if ty_info.rust_obj in self.complex_enums:
535                     ret_conv = (self.consts.ptr_c_ty + " ref_" + ty_info.var_name + " = tag_ptr(", ", false); // WARNING: We should clone here!")
536                     from_hu_sfx = self.consts.add_ref("this", ty_info.var_name)
537                     if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
538                         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"
539                         ret_conv_pfx += "*ret_" + ty_info.var_name + " = " + ty_info.rust_obj.replace("LDK", "") + "_clone("
540                         ret_conv_sfx = ");\n" + self.consts.ptr_c_ty + " ref_" + ty_info.var_name + " = tag_ptr(ret_" + ty_info.var_name + ", true);"
541                         ret_conv = (ret_conv_pfx, ret_conv_sfx)
542                         from_hu_sfx = ""
543                     return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
544                         arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + ");",
545                         arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
546                         ret_conv = ret_conv, ret_conv_name = "ref_" + ty_info.var_name,
547                         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 + ")") + ";",
548                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
549                         from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), from_hu_sfx))
550                 elif ty_info.rust_obj in self.trait_structs:
551                     if ty_info.nonnull_ptr:
552                         arg_conv = "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
553                         arg_conv += "if (ptr_is_owned(" + ty_info.var_name + ")) { CHECK_ACCESS(" + ty_info.var_name + "_ptr); }\n"
554                         arg_conv += ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
555                         arg_conv_name = ty_info.var_name + "_conv"
556                     else:
557                         # We map Option<Trait> as *mut Trait, which we can differentiate from &Trait by the NONNULL_PTR annotation.
558                         # We handle the Option<Trait> case here.
559                         arg_conv = ty_info.rust_obj + " *" + ty_info.var_name + "_conv_ptr = NULL;\n"
560                         arg_conv += "if (" + ty_info.var_name + " != 0) {\n"
561                         arg_conv += "\t" + ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
562                         arg_conv += "void* " + ty_info.var_name + "_ptr = untag_ptr(" + ty_info.var_name + ");\n"
563                         arg_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
564                         arg_conv += "\t" + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
565                         arg_conv += self.consts.trait_struct_inc_refcnt(ty_info).replace("\n", "\n\t")
566                         arg_conv += "\n\t" + ty_info.var_name + "_conv_ptr = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n"
567                         arg_conv += "\t*" + ty_info.var_name + "_conv_ptr = " + ty_info.var_name + "_conv;\n"
568                         arg_conv += "}"
569                         arg_conv_name = ty_info.var_name + "_conv_ptr"
570                     if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
571                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
572                             arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
573                             ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" +
574                                 "*" + ty_info.var_name + "_clone = " + ty_info.rust_obj.replace("LDK", "") + "_clone(", ");"),
575                             ret_conv_name = "tag_ptr(" + ty_info.var_name + "_clone, true)",
576                             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") + ";",
577                             to_hu_conv_name = "ret_hu_conv",
578                             from_hu_conv = (ty_info.var_name + " == null ? " + self.consts.native_zero_ptr + " : " + self.consts.get_ptr(ty_info.var_name), ""))
579                     else:
580                         return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
581                             arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
582                             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);"),
583                             ret_conv_name = "ret_" + ty_info.var_name,
584                             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") + ";",
585                             to_hu_conv_name = "ret_hu_conv",
586                             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)))
587                 ret_conv = (self.consts.ptr_c_ty + " ret_" + ty_info.var_name + " = tag_ptr(", ", true);")
588                 if holds_ref:
589                     ret_conv = (ret_conv[0], ", false);")
590                 return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
591                     arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)untag_ptr(" + ty_info.var_name + ");",
592                     arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
593                     ret_conv = ret_conv, ret_conv_name = "ret_" + ty_info.var_name,
594                     to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed
595             assert False # We should have handled every case by now.