Switch 32-bit platforms to using 64-bit "pointers"
[ldk-java] / java_strings.py
index cc1a361702794047eec6026db366d63ff94771a2..bcfc1f37e4fc8a02e87d9d716020c06fb196a81d 100644 (file)
@@ -555,6 +555,9 @@ import javax.annotation.Nullable;
         self.file_ext = ".java"
         self.ptr_c_ty = "int64_t"
         self.ptr_native_ty = "long"
+        self.usize_c_ty = "int64_t"
+        self.usize_native_ty = "long"
+        self.native_zero_ptr = "0"
         self.result_c_ty = "jclass"
         self.ptr_arr = "jobjectArray"
         self.is_arr_some_check = ("", " != NULL")
@@ -632,6 +635,8 @@ import javax.annotation.Nullable;
             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray() : null"
         elif elem_ty.java_hu_ty == "UInt5":
             return arr_name + " != null ? InternalUtils.convUInt5Array(" + arr_name + ") : null"
+        elif elem_ty.java_hu_ty == "WitnessVersion":
+            return arr_name + " != null ? InternalUtils.convWitnessVersionArray(" + arr_name + ") : null"
         else:
             return arr_name + " != null ? Arrays.stream(" + arr_name + ").map(" + conv_name + " -> " + elem_ty.from_hu_conv[0] + ").toArray(" + arr_ty.java_ty + "::new) : null"
 
@@ -701,6 +706,15 @@ import javax.annotation.Nullable;
     def add_ref(self, holder, referent):
         return holder + ".ptrs_to.add(" + referent + ")"
 
+    def fully_qualified_hu_ty_path(self, ty):
+        if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"):
+            return ty.java_fn_ty_arg.strip("L;").replace("/", ".")
+        if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt5" or ty.java_hu_ty == "WitnessVersion":
+            return "org.ldk.util." + ty.java_hu_ty
+        if not ty.is_native_primitive and ty.rust_obj is not None and not "[]" in ty.java_hu_ty:
+            return "org.ldk.structs." + ty.java_hu_ty
+        return ty.java_hu_ty
+
     def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
         out_java_enum = "package org.ldk.enums;\n\n"
         out_java = ""
@@ -882,7 +896,8 @@ import javax.annotation.Nullable;
                     else:
                         java_trait_constr = java_trait_constr + arg_info.arg_name
 
-                java_trait_constr = java_trait_constr + ");\n"
+                java_trait_constr += ");\n"
+                java_trait_constr += "\t\t\t\tReference.reachabilityFence(arg);\n"
                 if fn_line.ret_ty_info.java_ty != "void":
                     if fn_line.ret_ty_info.from_hu_conv is not None:
                         java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n"
@@ -1093,7 +1108,7 @@ import javax.annotation.Nullable;
             else:
                 out_c = out_c + ", " + var[1]
         out_c = out_c + ");\n"
-        out_c = out_c + "\treturn (uint64_t)res_ptr;\n"
+        out_c = out_c + "\treturn tag_ptr(res_ptr, true);\n"
         out_c = out_c + "}\n"
 
         for var in flattened_field_vars:
@@ -1112,10 +1127,8 @@ import javax.annotation.Nullable;
                 out_java += "\tpublic static native long " + struct_name + "_get_" + var[1] + "(long arg);\n"
 
                 out_c += "JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_" + struct_name + "_1get_1" + var[1] + "(JNIEnv *env, jclass clz, int64_t arg) {\n"
-                out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)(arg & ~1);\n"
-                out_c += "\tuint64_t res_ptr = (uint64_t)&inp->" + var[1] + ";\n"
-                out_c += "\tDO_ASSERT((res_ptr & 1) == 0);\n"
-                out_c += "\treturn (int64_t)(res_ptr | 1);\n"
+                out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)untag_ptr(arg);\n"
+                out_c += "\treturn tag_ptr(&inp->" + var[1] + ", false);\n"
                 out_c += "}\n"
 
         return (out_java, out_java_trait, out_c)
@@ -1165,20 +1178,19 @@ import javax.annotation.Nullable;
                 if idx > 0:
                     init_meth_params = init_meth_params + ", "
 
-                if field_ty.java_hu_ty == var.var_name:
-                    field_path = field_ty.java_fn_ty_arg.strip("L;").replace("/", ".")
-                    out_java += "\t\t\tpublic " + field_path + " " + field_ty.arg_name + ";\n"
-                    java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_path + " " + field_ty.arg_name + ";\n"
-                    init_meth_params = init_meth_params + field_path + " " + field_ty.arg_name
-                else:
-                    out_java += "\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n"
-                    if field_docs is not None:
-                        java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n"
-                    java_hu_subclasses += "\t\t"
-                    if field_ty.nullable:
-                        java_hu_subclasses += "@Nullable "
-                    java_hu_subclasses += "public final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
-                    init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
+                java_ty = field_ty.java_ty
+                if field_ty.java_fn_ty_arg.startswith("L") and field_ty.java_fn_ty_arg.endswith(";"):
+                    # If this is a simple enum, we have to reference it in the low-level bindings differently:
+                    java_ty = field_ty.java_fn_ty_arg.strip("L;").replace("/", ".")
+                out_java += "\t\t\tpublic " + java_ty + " " + field_ty.arg_name + ";\n"
+                if field_docs is not None:
+                    java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n"
+                java_hu_subclasses += "\t\t"
+                if field_ty.nullable:
+                    java_hu_subclasses += "@Nullable "
+                java_hu_subclasses += "public final " + self.fully_qualified_hu_ty_path(field_ty) + " " + field_ty.arg_name + ";\n"
+                init_meth_params = init_meth_params + java_ty + " " + field_ty.arg_name
+
                 init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
                 if field_ty.to_hu_conv is not None:
                     hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
@@ -1206,7 +1218,7 @@ import javax.annotation.Nullable;
         out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs))
 
         out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n")
-        out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n")
+        out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n")
         out_c += ("\tswitch(obj->tag) {\n")
         for var in variant_list:
             out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
@@ -1238,11 +1250,11 @@ import javax.annotation.Nullable;
         out_opaque_struct_human += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n"
         hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
         out_opaque_struct_human += ("public class " + hu_name + " extends CommonBase")
-        if struct_name.startswith("LDKLocked"):
+        if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
             out_opaque_struct_human += (" implements AutoCloseable")
         out_opaque_struct_human += (" {\n")
         out_opaque_struct_human += ("\t" + hu_name + "(Object _dummy, long ptr) { super(ptr); }\n")
-        if struct_name.startswith("LDKLocked"):
+        if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
             out_opaque_struct_human += ("\t@Override public void close() {\n")
         else:
             out_opaque_struct_human += ("\t@Override @SuppressWarnings(\"deprecation\")\n")