Merge pull request #119 from TheBlueMatt/main
[ldk-java] / genbindings.py
index b8e58bc5b0e24a20c1e51a909b52965042650cb7..fd09ba2a264c91fd5c71555242b51782a9b6cf0f 100755 (executable)
@@ -607,7 +607,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
             expected_struct in complex_enums or expected_struct in complex_enums or
             expected_struct in result_types or expected_struct in tuple_types) and not is_free
         impl_on_utils = not impl_on_struct and (not is_free and not method_name.endswith("_clone") and
-            not method_name.startswith("TxOut") and
+            not method_name.startswith("TxOut") and not method_name.startswith("BigEndianScalar") and
             not method_name.startswith("_") and
             method_name != "check_platform" and method_name != "Result_read" and
             not expected_struct in unitary_enums and
@@ -756,9 +756,8 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
                 else:
                     (nullable_params, ret_nullable) = doc_to_params_ret_nullable(fn_docs)
                     if ret_nullable:
-                        assert False # This isn't yet handled on the Java side
-                        ret_ty_info.nullable = True
                         ret_ty_info = type_mapping_generator.map_nullable_type(fn_line.group(2).strip() + " ret", True, None, False, False)
+                        ret_ty_info.nullable = True
                     else:
                         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
@@ -1054,8 +1053,27 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
                         write_c("\treturn thing->value;")
                         write_c("}")
                         map_fn(fn_line + "\n", re.compile("(.*) (TxOut_get_value) \((.*)\)").match(fn_line), None, None, None)
+                elif struct_name == "LDKBigEndianScalar":
+                    with open(f"{sys.argv[3]}/structs/BigEndianScalar{consts.file_ext}", "w") as out_java_struct:
+                        out_java_struct.write(consts.hu_struct_file_prefix)
+                        out_java_struct.write(consts.scalar_defn)
+                        fn_line = "struct LDKThirtyTwoBytes BigEndianScalar_get_bytes (struct LDKBigEndianScalar* thing)"
+                        write_c(fn_line + " {\n")
+                        write_c("\tLDKThirtyTwoBytes ret = { .data = *thing->big_endian_bytes };\n")
+                        write_c("\treturn ret;\n")
+                        write_c("}\n")
+                        map_fn(fn_line + "\n", re.compile("(.*) (BigEndianScalar_get_bytes) \((.*)\)").match(fn_line), None, None, None)
+
+                        # We need to be able to FREE a heap-allocated BigEndianScalar, but because
+                        # there's nothing heap-allocated inside it the C bindings don't bother
+                        # exposing a `_free` method. Instead, we have to manually write one here,
+                        # though it doesn't need to do anything, the autogenerated wrapper will do
+                        # the required FREE.
+                        fn_line = "static void BigEndianScalar_free (struct LDKBigEndianScalar thing)"
+                        write_c(fn_line + " {}\n")
+                        map_fn(fn_line + "\n", re.compile("static (.*) (BigEndianScalar_free) \((.*)\)").match(fn_line), None, None, None)
                 else:
-                    pass # Everything remaining is a byte[] or some form
+                    pass # Everything remaining is a byte[] of some form
                 cur_block_obj = None
         else:
             fn_ptr = fn_ptr_regex.match(line)