Move native string conversion to per-lang files
[ldk-java] / java_strings.py
index 2e9ae67052d7d06fe6873065da9004d0a72fc4a2..04855ce9b864e41f5d39f36db2dae5b83e56228b 100644 (file)
@@ -7,7 +7,7 @@ class Consts:
             uint8_t = ['byte'],
             uint16_t = ['short'],
             uint32_t = ['int'],
-            long = ['long'],
+            uint64_t = ['long'],
         )
 
         self.to_hu_conv_templates = dict(
@@ -297,6 +297,7 @@ import java.util.Arrays;
         self.ptr_c_ty = "int64_t"
         self.ptr_native_ty = "long"
         self.result_c_ty = "jclass"
+        self.owned_str_to_c_call = ("(*env)->NewStringUTF(env, ", ")")
         self.ptr_arr = "jobjectArray"
         self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")
         self.get_native_arr_ptr_call = ("(*env)->GetPrimitiveArrayCritical(env, ", ", NULL)")
@@ -545,7 +546,6 @@ import java.util.Arrays;
             else:
                 out_java = out_java + ", " + var[0] + " " + var[1]
         out_java = out_java + ");\n"
-        out_java = out_java + "\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n"
 
         # Now that we've written out our java code (and created java_meths), generate C
         out_c = "typedef struct " + struct_name + "_JCalls {\n"
@@ -694,14 +694,14 @@ import java.util.Arrays;
         out_c = out_c + "\treturn (long)res_ptr;\n"
         out_c = out_c + "}\n"
 
-        out_c = out_c + self.c_fn_ty_pfx + "jobject " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " val) {\n"
-        out_c = out_c + "\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n"
-        out_c = out_c + "\tCHECK(ret != NULL);\n"
-        out_c = out_c + "\treturn ret;\n"
-        out_c = out_c + "}\n"
-
         return (out_java, out_java_trait, out_c)
 
+    def trait_struct_inc_refcnt(self, ty_info):
+        base_conv = "\nif (" + ty_info.var_name + "_conv.free == " + ty_info.rust_obj + "_JCalls_free) {\n"
+        base_conv = base_conv + "\t// If this_arg is a JCalls struct, then we need to increment the refcnt in it.\n"
+        base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}"
+        return base_conv
+
     def map_complex_enum(self, struct_name, variant_list, camel_to_snake):
         java_hu_type = struct_name.replace("LDK", "")
         out_java_enum = ""