Redo constructor function names to feel more natural in Java
[ldk-java] / java_strings.py
index eda4086cd92b2f4c08048bccb764a0316971ab9a..cc545e8297f97797dc53ce3b91924c12fe6cf7d9 100644 (file)
@@ -37,9 +37,21 @@ public class bindings {
                System.loadLibrary(\"lightningjni\");
                init(java.lang.Enum.class, VecOrSliceDef.class);
                init_class_cache();
+               if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))
+                       throw new IllegalArgumentException("Compiled LDK library and LDK class failes do not match");
+               // Fetching the LDK versions from C also checks that the header and binaries match
+               get_ldk_c_bindings_version();
+               get_ldk_version();
        }
        static native void init(java.lang.Class c, java.lang.Class slicedef);
        static native void init_class_cache();
+       static native String get_lib_version_string();
+
+       public static String get_ldk_java_bindings_version() {
+               return "<git_version_ldk_garbagecollected>";
+       }
+       public static native String get_ldk_c_bindings_version();
+       public static native String get_ldk_version();
 
        public static native boolean deref_bool(long ptr);
        public static native long deref_long(long ptr);
@@ -76,7 +88,6 @@ class CommonBase {
 """
 
         self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
-#include <rust_types.h>
 #include <lightning.h>
 #include <string.h>
 #include <stdatomic.h>
@@ -115,7 +126,7 @@ void __attribute__((constructor)) spawn_stderr_redirection() {
 
         if not DEBUG:
             self.c_file_pfx = self.c_file_pfx + """#define MALLOC(a, _) malloc(a)
-#define FREE(p) if ((long)(p) > 1024) { free(p); }
+#define FREE(p) if ((uint64_t)(p) > 1024) { free(p); }
 #define DO_ASSERT(a) (void)(a)
 #define CHECK(a)
 """
@@ -126,6 +137,14 @@ void __attribute__((constructor)) spawn_stderr_redirection() {
 // Assert a is true or do nothing
 #define CHECK(a) DO_ASSERT(a)
 
+void __attribute__((constructor)) debug_log_version() {
+       if (check_get_ldk_version() == NULL)
+               DEBUG_PRINT("LDK version did not match the header we built against\\n");
+       if (check_get_ldk_bindings_version() == NULL)
+               DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
+       DEBUG_PRINT("Loaded LDK-Java Bindings with LDK %s and LDK-C-Bindings %s\\n", check_get_ldk_version(), check_get_ldk_bindings_version());
+}
+
 // Running a leak check across all the allocations and frees of the JDK is a mess,
 // so instead we implement our own naive leak checker here, relying on the -wrap
 // linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
@@ -237,7 +256,7 @@ static void alloc_freed(void* ptr) {
        __real_free(it);
 }
 static void FREE(void* ptr) {
-       if ((long)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
+       if ((uint64_t)ptr < 1024) return; // Rust loves to create pointers to the NULL page for dummys
        alloc_freed(ptr);
        __real_free(ptr);
 }
@@ -326,14 +345,14 @@ JNIEXPORT int64_t impl_bindings_bytes_1to_1u8_1vec (JNIEnv * env, jclass _b, jby
        vec->datalen = (*env)->GetArrayLength(env, bytes);
        vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
        (*env)->GetByteArrayRegion (env, bytes, 0, vec->datalen, vec->data);
-       return (long)vec;
+       return (uint64_t)vec;
 }
 JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_txpointer_1get_1buffer (JNIEnv * env, jclass _b, jlong ptr) {
        LDKTransaction *txdata = (LDKTransaction*)ptr;
        LDKu8slice slice;
        slice.data = txdata->data;
        slice.datalen = txdata->datalen;
-       return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (long)&slice);
+       return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (uint64_t)&slice);
 }
 JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
        LDKTransaction *txdata = (LDKTransaction*)MALLOC(sizeof(LDKTransaction), "LDKTransaction");
@@ -341,7 +360,7 @@ JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data
        txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
        txdata->data_is_owned = false;
        (*env)->GetByteArrayRegion (env, bytes, 0, txdata->datalen, txdata->data);
-       return (long)txdata;
+       return (uint64_t)txdata;
 }
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_txpointer_1free (JNIEnv * env, jclass _b, jlong ptr) {
        LDKTransaction *tx = (LDKTransaction*)ptr;
@@ -356,7 +375,7 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env
        _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
        _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
        LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
-       return (long)vec->datalen;
+       return (uint64_t)vec->datalen;
 }
 JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * env, jclass _b) {
        // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
@@ -367,7 +386,7 @@ JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNI
        LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
        vec->data = NULL;
        vec->datalen = 0;
-       return (long)vec;
+       return (uint64_t)vec;
 }
 
 // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
@@ -405,6 +424,16 @@ static inline LDKStr java_to_owned_str(JNIEnv *env, jstring str) {
        };
        return res;
 }
+
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, "<git_version_ldk_garbagecollected>", strlen("<git_version_ldk_garbagecollected>"));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1version(JNIEnv *env, jclass _c) {
+       return str_ref_to_java(env, check_get_ldk_version(), strlen(check_get_ldk_version()));
+}
 """
 
         self.hu_struct_file_prefix = """package org.ldk.structs;
@@ -860,7 +889,7 @@ import java.util.Arrays;
             else:
                 out_c = out_c + ", " + var[1]
         out_c = out_c + ");\n"
-        out_c = out_c + "\treturn (long)res_ptr;\n"
+        out_c = out_c + "\treturn (uint64_t)res_ptr;\n"
         out_c = out_c + "}\n"
 
         return (out_java, out_java_trait, out_c)
@@ -1014,8 +1043,12 @@ import java.util.Arrays;
             if doc_comment is not None:
                 out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
             if not takes_self:
-                out_java_struct += (
-                    "\tpublic static " + return_type_info.java_hu_ty + " constructor_" + meth_n + "(")
+                if meth_n == "new":
+                    out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " of("
+                elif meth_n == "default":
+                    out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " with_default("
+                else:
+                    out_java_struct += "\tpublic static " + return_type_info.java_hu_ty + " " + meth_n + "("
             else:
                 out_java_struct += ("\tpublic " + return_type_info.java_hu_ty + " " + meth_n + "(")
             for idx, arg in enumerate(argument_types):