Handle methods taking non-ref self by using standard conversion
[ldk-java] / genbindings.py
index 32980aa3c4e7cf8071ebcff248bac9d1ca9993d2..a03fa4c4d2baa4751ecc34af81592bb6a8c96dfd 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-import sys, re
+import sys, re, subprocess
 
 if len(sys.argv) < 7:
     print("USAGE: /path/to/lightning.h /path/to/bindings/output /path/to/bindings/ /path/to/bindings/output.c debug lang")
@@ -33,6 +33,8 @@ else:
 
 consts = Consts(DEBUG, target=target)
 
+local_git_version = subprocess.check_output(["git", "describe", '--tag', '--dirty']).decode("utf-8").strip()
+
 from bindingstypes import *
 
 c_file = ""
@@ -260,6 +262,7 @@ def java_c_types(fn_arg, ret_arr_len):
         fn_ty_arg = "Ljava/lang/String;"
         fn_arg = fn_arg[6:].strip()
     elif fn_arg.startswith("LDKStr"):
+        rust_obj = "LDKStr"
         java_ty = "String"
         c_ty = "jstring"
         fn_ty_arg = "Ljava/lang/String;"
@@ -408,17 +411,20 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
         else:
             struct_meth = method_name.split("_")[0]
 
-        return_type_info = type_mapping_generator.map_type(method_return_type, True, ret_arr_len, False, False)
+        return_type_info = type_mapping_generator.map_type(method_return_type.strip() + " ret", True, ret_arr_len, False, False)
 
         argument_types = []
         default_constructor_args = {}
         takes_self = False
+        takes_self_ptr = False
         args_known = True
 
         for argument_index, argument in enumerate(method_arguments):
             argument_conversion_info = type_mapping_generator.map_type(argument, False, None, is_free, True)
             if argument_index == 0 and argument_conversion_info.java_hu_ty == struct_meth:
                 takes_self = True
+                if argument_conversion_info.ty_info.is_ptr:
+                    takes_self_ptr = True
             if argument_conversion_info.arg_conv is not None and "Warning" in argument_conversion_info.arg_conv:
                 if argument_conversion_info.rust_obj in constructor_fns:
                     assert not is_free
@@ -436,7 +442,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
 
         out_java.write("\t// " + line)
         (out_java_delta, out_c_delta, out_java_struct_delta) = \
-            consts.map_function(argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator, doc_comment)
+            consts.map_function(argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_ptr, args_known, type_mapping_generator, doc_comment)
         out_java.write(out_java_delta)
 
         if is_free:
@@ -468,8 +474,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                 or expected_struct in complex_enums or expected_cstruct in complex_enums
                 or expected_cstruct in result_types) and not is_free:
             out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
-        elif method_name.startswith("C2Tuple_") and method_name.endswith("_read"):
-            struct_meth = method_name.rsplit("_", 1)[0]
+        elif (method_name.startswith("C2Tuple_") and method_name.endswith("_read")) or \
+                (return_type_info.rust_obj is not None and "Result" in return_type_info.rust_obj and "from" in method_name):
             out_java_struct = open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a")
         if out_java_struct is not None:
             out_java_struct.write(out_java_struct_delta)
@@ -512,7 +518,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                 if "LDK" + variant_name in union_enum_items:
                     enum_var_lines = union_enum_items["LDK" + variant_name]
                     for idx, field in enumerate(enum_var_lines):
-                        if idx != 0 and idx < len(enum_var_lines) - 2:
+                        if idx != 0 and idx < len(enum_var_lines) - 2 and field.strip() != "":
                             fields.append(type_mapping_generator.map_type(field.strip(' ;'), False, None, False, True))
                     enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False))
                 elif camel_to_snake(variant_name) in inline_enum_variants:
@@ -545,7 +551,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
 
             field_fns = []
             for fn_docs, fn_line in trait_fn_lines:
-                ret_ty_info = type_mapping_generator.map_type(fn_line.group(2), True, None, False, False)
+                ret_ty_info = type_mapping_generator.map_type(fn_line.group(2).strip() + " ret", True, None, False, False)
                 is_const = fn_line.group(4) is not None
 
                 arg_tys = []
@@ -692,7 +698,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                     write_c("\tret->" + e + " = " + e + ";\n")
                 if ty_info.arg_conv_cleanup is not None:
                     write_c("\t//TODO: Really need to call " + ty_info.arg_conv_cleanup + " here\n")
-        write_c("\treturn (long)ret;\n")
+        write_c("\treturn (uint64_t)ret;\n")
         write_c("}\n")
 
         for idx, ty_info in enumerate(ty_list):
@@ -708,7 +714,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                 write_c("\treturn tuple->" + e + ";\n")
             write_c("}\n")
 
-    out_java.write(consts.bindings_header)
+    out_java.write(consts.bindings_header.replace('<git_version_ldk_garbagecollected>', local_git_version))
 
     with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct:
         out_java_struct.write(consts.common_base)
@@ -852,7 +858,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                         if cleanup is not None:
                             write_c("\t\t" + cleanup + ";\n")
                         write_c("\t}\n")
-                        write_c("\treturn (long)ret;\n")
+                        write_c("\treturn (uint64_t)ret;\n")
                         write_c("}\n")
 
                     if ty_info.is_native_primitive:
@@ -888,6 +894,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                             line = line.strip()
                             if line.startswith("struct "):
                                 line = line[7:]
+                            elif line.startswith("enum "):
+                                line = line[5:]
                             split = line.split(" ")
                             assert len(split) == 2
                             tuple_variants[split[1].strip(";")] = split[0]
@@ -965,7 +973,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
             out_java_struct.write("}\n")
 
 with open(sys.argv[4], "w") as out_c:
-    out_c.write(consts.c_file_pfx)
+    out_c.write(consts.c_file_pfx.replace('<git_version_ldk_garbagecollected>', local_git_version))
     out_c.write(consts.init_str())
     out_c.write(c_file)
 with open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") as util: