From: Matt Corallo Date: Fri, 6 Aug 2021 17:10:19 +0000 (+0000) Subject: Correctly handle passing a null to Rust for an array X-Git-Tag: v0.0.99.2~11 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=44f51979975ddeac03a4b270c30b9e0059c24d9b;p=ldk-java Correctly handle passing a null to Rust for an array --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 3fb0ba7c..9fcdc402 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -87,38 +87,45 @@ class TypeMappingGenerator: ty_info.subty.requires_clone = False subty = self.map_type_with_info(ty_info.subty, False, None, is_free, holds_ref) arg_conv = ty_info.rust_obj + " " + arr_name + "_constr;\n" - arg_conv = arg_conv + arr_name + "_constr." + arr_len + " = " + self.consts.get_native_arr_len_call[0] + arr_name + self.consts.get_native_arr_len_call[1] + ";\n" - arg_conv = arg_conv + "if (" + arr_name + "_constr." + arr_len + " > 0)\n" + pf = "" + if ty_info.is_ptr: + pf = "\t" + arg_conv += ty_info.rust_obj + " *" + arr_name + "_ptr = NULL;\n" + arg_conv += "if (" + self.consts.is_arr_some_check[0] + arr_name + self.consts.is_arr_some_check[1] + ") {\n" + 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" + arg_conv += pf + "if (" + arr_name + "_constr." + arr_len + " > 0)\n" if subty.is_native_primitive: szof = subty.c_ty else: szof = subty.rust_obj - arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = MALLOC(" + arr_name + "_constr." + arr_len + " * sizeof(" + szof + "), \"" + ty_info.rust_obj + " Elements\");\n" - arg_conv = arg_conv + "else\n" - arg_conv = arg_conv + "\t" + arr_name + "_constr." + ty_info.arr_access + " = NULL;\n" + 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" + arg_conv += pf + "else\n" + arg_conv += pf + "\t" + arr_name + "_constr." + ty_info.arr_access + " = NULL;\n" get_arr = self.consts.get_native_arr_contents(arr_name, "NO_DEST", arr_name + "_constr." + arr_len, ty_info, False) if get_arr != None: - arg_conv = arg_conv + subty.c_ty + "* " + arr_name + "_vals = " + get_arr + ";\n" - arg_conv = arg_conv + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_constr." + arr_len + "; " + idxc + "++) {\n" + arg_conv += pf + subty.c_ty + "* " + arr_name + "_vals = " + get_arr + ";\n" + arg_conv += pf + "for (size_t " + idxc + " = 0; " + idxc + " < " + arr_name + "_constr." + arr_len + "; " + idxc + "++) {\n" if get_arr != None: - arg_conv = arg_conv + "\t" + subty.c_ty + " " + conv_name + " = " + arr_name + "_vals[" + idxc + "];" + arg_conv += pf + "\t" + subty.c_ty + " " + conv_name + " = " + arr_name + "_vals[" + idxc + "];" if subty.arg_conv is not None: - arg_conv = arg_conv + "\n\t" + subty.arg_conv.replace("\n", "\n\t") + arg_conv += "\n\t" + pf + subty.arg_conv.replace("\n", "\n\t" + pf) else: - arg_conv = arg_conv + "\t" + subty.c_ty + " " + conv_name + " = " + self.consts.get_native_arr_elem(arr_name, idxc, ty_info) + ";\n" - arg_conv = arg_conv + "\t" + subty.arg_conv.replace("\n", "\n\t") - arg_conv = arg_conv + "\n\t" + arr_name + "_constr." + ty_info.arr_access + "[" + idxc + "] = " + subty.arg_conv_name + ";\n}" + arg_conv += pf + "\t" + subty.c_ty + " " + conv_name + " = " + self.consts.get_native_arr_elem(arr_name, idxc, ty_info) + ";\n" + arg_conv += pf + "\t" + subty.arg_conv.replace("\n", "\n\t" + pf) + arg_conv += "\n\t" + pf + arr_name + "_constr." + ty_info.arr_access + "[" + idxc + "] = " + subty.arg_conv_name + ";\n" + pf + "}" if get_arr != None: cleanup = self.consts.cleanup_native_arr_ref_contents(arr_name, arr_name + "_vals", arr_name + "_constr." + arr_len, ty_info) if cleanup is not None: - arg_conv = arg_conv + "\n" + cleanup + ";" + arg_conv += "\n" + pf + cleanup + ";" if ty_info.is_ptr: - arg_conv_name = "&" + arr_name + "_constr" + arg_conv_name = arr_name + "_ptr" else: arg_conv_name = arr_name + "_constr" arg_conv_cleanup = None if ty_info.is_ptr: - arg_conv_cleanup = "FREE(" + arr_name + "_constr." + ty_info.arr_access + ");" + arg_conv_cleanup = "if (" + arr_name + "_ptr != NULL) { FREE(" + arr_name + "_constr." + ty_info.arr_access + "); }" + if ty_info.is_ptr: + arg_conv += "\n\t" + arr_name + "_ptr = &" + arr_name + "_constr;\n}" ret_conv = (ty_info.rust_obj + " " + arr_name + "_var = ", "") if subty.ret_conv is None: @@ -159,11 +166,11 @@ class TypeMappingGenerator: from_hu_conv = None if subty.from_hu_conv is not None: if subty.java_ty == "long" and subty.java_hu_ty != "long": - from_hu_conv = ("Arrays.stream(" + arr_name + ").mapToLong(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + " */") + from_hu_conv = (arr_name + " != null ? Arrays.stream(" + arr_name + ").mapToLong(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray() : null", "/* TODO 2 " + subty.java_hu_ty + " */") elif subty.java_ty == "long": - from_hu_conv = ("Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray()", "/* TODO 2 " + subty.java_hu_ty + " */") + from_hu_conv = (arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray() : null", "/* TODO 2 " + subty.java_hu_ty + " */") else: - from_hu_conv = ("Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray(" + ty_info.java_ty + "::new)", "/* TODO 2 " + subty.java_hu_ty + " */") + from_hu_conv = (arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + subty.from_hu_conv[0] + ").toArray(" + ty_info.java_ty + "::new) : null", "/* TODO 2 " + subty.java_hu_ty + " */") return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = arg_conv_cleanup, diff --git a/java_strings.py b/java_strings.py index 82efd47b..10fd2b3b 100644 --- a/java_strings.py +++ b/java_strings.py @@ -484,6 +484,7 @@ import javax.annotation.Nullable; self.ptr_native_ty = "long" self.result_c_ty = "jclass" self.ptr_arr = "jobjectArray" + self.is_arr_some_check = ("", " != NULL") self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")") def construct_jenv(self): diff --git a/typescript_strings.py b/typescript_strings.py index f90e5a60..a98ffbff 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -272,6 +272,7 @@ import * as bindings from '../bindings' // TODO: figure out location self.ptr_native_ty = "number" self.result_c_ty = "uint32_t" self.ptr_arr = "ptrArray" + self.is_arr_some_check = ("", " != 0") self.get_native_arr_len_call = ("*((uint32_t*)", ")") def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var):