Skip to content

Commit 0d48e55

Browse files
Matt CoralloTheBlueMatt
authored andcommitted
Support passing trait instance references from C to Java without clone or double-free
1 parent 5db0283 commit 0d48e55

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

gen_type_mapping.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
335335
base_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
336336
base_conv += ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)(" + ty_info.var_name + "_ptr);"
337337
if ty_info.rust_obj in self.trait_structs:
338-
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 = ", ";")
338+
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 = ", ";")
339339
if holds_ref:
340340
if (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns:
341341
ret_conv = (ret_conv[0] + ty_info.rust_obj.replace("LDK", "") + "_clone(&", ");")
@@ -496,7 +496,8 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
496496
else:
497497
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
498498
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
499-
ret_conv = ("uint64_t ret_" + ty_info.var_name + " = (uint64_t)", ";"), ret_conv_name = "ret_" + ty_info.var_name,
499+
ret_conv = ("// WARNING: This object doesn't live past this scope, needs clone!\nuint64_t ret_" + ty_info.var_name + " = ((uint64_t)", ") | 1;"),
500+
ret_conv_name = "ret_" + ty_info.var_name,
500501
to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);",
501502
to_hu_conv_name = "ret_hu_conv",
502503
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")"))

0 commit comments

Comments
 (0)