Add deserialization/init helper class that overtorment asked for
[ldk-java] / java_strings.py
index f7aa1b02afab5d631b246be28181ca874fd13462..e6a17ebd5dde9756eac644d05481b8e1b6be0154 100644 (file)
@@ -1,7 +1,13 @@
 from bindingstypes import *
+from enum import Enum
+
+class Target(Enum):
+    JAVA = 1,
+    ANDROID = 2
 
 class Consts:
-    def __init__(self, DEBUG: bool, **kwargs):
+    def __init__(self, DEBUG: bool, target: Target, **kwargs):
+        self.target = target
         self.c_array_class_caches = set()
         self.c_type_map = dict(
             uint8_t = ['byte'],
@@ -61,7 +67,7 @@ public class UtilMethods {
         self.common_base = """package org.ldk.structs;
 import java.util.LinkedList;
 class CommonBase {
-       final long ptr;
+       long ptr;
        LinkedList<Object> ptrs_to = new LinkedList();
        protected CommonBase(long ptr) { this.ptr = ptr; }
        public long _test_only_get_ptr() { return this.ptr; }
@@ -235,7 +241,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (J
        (*env)->SetByteArrayRegion(env, ret_arr, 0, slice->datalen, slice->data);
        return ret_arr;
 }
-JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * env, jclass _b, jbyteArray bytes) {
+JNIEXPORT int64_t impl_bindings_bytes_1to_1u8_1vec (JNIEnv * env, jclass _b, jbyteArray bytes) {
        LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
        vec->datalen = (*env)->GetArrayLength(env, bytes);
        vec->data = (uint8_t*)MALLOC(vec->datalen, "LDKCVec_u8Z Bytes");
@@ -249,7 +255,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_txpointer_1get_1buffer (
        slice.datalen = txdata->datalen;
        return Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes(env, _b, (long)&slice);
 }
-JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1txpointer_1copy_1data (JNIEnv * env, jclass _b, jbyteArray bytes) {
+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");
        txdata->datalen = (*env)->GetArrayLength(env, bytes);
        txdata->data = (uint8_t*)MALLOC(txdata->datalen, "Tx Data Bytes");
@@ -272,7 +278,7 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env
        LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
        return (long)vec->datalen;
 }
-JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * env, jclass _b) {
+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
        _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
        _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
@@ -449,7 +455,10 @@ import java.util.Arrays;
         out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_impl_bindings_00024" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
         for var_name in variants:
             out_c = out_c + "\t" + struct_name + "_" + var_name + "_class =\n"
-            out_c = out_c + "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n"
+            if self.target == Target.ANDROID:
+                out_c = out_c + "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"org/ldk/impl/bindings$" + struct_name + "$" + var_name + "\"));\n"
+            else:
+                out_c = out_c + "\t\t(*env)->NewGlobalRef(env, (*env)->FindClass(env, \"Lorg/ldk/impl/bindings$" + struct_name + "$" + var_name + ";\"));\n"
             out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_class != NULL);\n"
             out_c = out_c + "\t" + struct_name + "_" + var_name + "_meth = (*env)->GetMethodID(env, " + struct_name + "_" + var_name + "_class, \"<init>\", \"(" + init_meth_jty_strs[var_name] + ")V\");\n"
             out_c = out_c + "\tCHECK(" + struct_name + "_" + var_name + "_meth != NULL);\n"
@@ -495,7 +504,7 @@ import java.util.Arrays;
         for var in field_vars:
             if isinstance(var, ConvInfo):
                 if var.from_hu_conv is not None and var.from_hu_conv[1] != "":
-                    out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1] + ";\n"
+                    out_java_trait = out_java_trait + "\t\t" + var.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n"
             else:
                 out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(" + var[1] + ");\n"
         out_java_trait = out_java_trait + "\t\tthis.bindings_instance = arg;\n"
@@ -612,7 +621,7 @@ import java.util.Arrays;
                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
                 out_c = out_c + "\tif (atomic_fetch_sub_explicit(&j_calls->refcnt, 1, memory_order_acquire) == 1) {\n"
                 out_c = out_c + "\t\tJNIEnv *env;\n"
-                out_c = out_c + "\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
+                out_c = out_c + "\t\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_6) == JNI_OK);\n"
                 out_c = out_c + "\t\t(*env)->DeleteWeakGlobalRef(env, j_calls->o);\n"
                 out_c = out_c + "\t\tFREE(j_calls);\n"
                 out_c = out_c + "\t}\n}\n"
@@ -632,7 +641,7 @@ import java.util.Arrays;
                 out_c = out_c + ") {\n"
                 out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n"
                 out_c = out_c + "\tJNIEnv *env;\n"
-                out_c = out_c + "\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_8) == JNI_OK);\n"
+                out_c = out_c + "\tDO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&env, JNI_VERSION_1_6) == JNI_OK);\n"
 
                 for arg_info in fn_line.args_ty:
                     if arg_info.ret_conv is not None:
@@ -718,7 +727,7 @@ import java.util.Arrays;
         out_c = out_c + "\treturn ret;\n"
         out_c = out_c + "}\n"
 
-        out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
+        out_c = out_c + self.c_fn_ty_pfx + "int64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "jobject o"
         for var in field_vars:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
@@ -980,9 +989,9 @@ import java.util.Arrays;
                 elif info.from_hu_conv is not None and info.from_hu_conv[1] != "":
                     if not takes_self and return_type_info.to_hu_conv_name is not None:
                         out_java_struct += (
-                            "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name) + ";\n")
+                            "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name).replace("\n", "\n\t\t") + ";\n")
                     else:
-                        out_java_struct += ("\t\t" + info.from_hu_conv[1] + ";\n")
+                        out_java_struct += ("\t\t" + info.from_hu_conv[1].replace("\n", "\n\t\t") + ";\n")
 
             if return_type_info.to_hu_conv_name is not None:
                 out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n")