make target argument optional for typescript inputs (undocumented)
[ldk-java] / genbindings.py
index b744da936bf0bc202d6c925b7f9c89c3428b24ff..f022c1b34062ff004dcbef1c25acf1472526edba 100755 (executable)
@@ -1,31 +1,31 @@
 #!/usr/bin/env python3
 import sys, re
 
-import config
-from config import Language
-import os
+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")
+    sys.exit(1)
 
-configuration = config.setup()
+if sys.argv[5] == "false":
+    DEBUG = False
+elif sys.argv[5] == "true":
+    DEBUG = True
+else:
+    print("debug should be true or false and indicates whether to track allocations and ensure we don't leak")
+    sys.exit(1)
 
-output_language = configuration.language
-if output_language == Language.Java:
+target = None
+if sys.argv[6] == "java":
     from java_strings import Consts
-elif output_language == Language.TypeScript:
+elif sys.argv[6] == "typescript":
+    import typescript_strings
     from typescript_strings import Consts
+    target = typescript_strings.Target.NODEJS
+    if len(sys.argv) == 8 and sys.argv[7] == 'browser':
+        target = typescript_strings.Target.BROWSER
 else:
     print("Only java or typescript can be set for lang")
     sys.exit(1)
-
-consts = Consts(configuration.debug)
-
-path_to_lightning_header = configuration.lightning_h_path
-path_to_output_blob = configuration.output_blob_path
-path_to_bindings_directory = configuration.bindings_output_directory_path
-path_to_c_file = configuration.output_c_path
-
-
-
-
+consts = Consts(DEBUG, target=target)
 
 from bindingstypes import *
 
@@ -60,14 +60,6 @@ def camel_to_snake(s):
             lastund = True
     return (ret + lastchar.lower()).strip("_")
 
-
-def recursiveOpenFile(path, *args):
-    output_directory_path = os.path.dirname(path)
-    if not os.path.isdir(output_directory_path):
-        os.makedirs(output_directory_path)
-    return open(path, *args)
-
-
 unitary_enums = set()
 complex_enums = set()
 opaque_structs = set()
@@ -183,6 +175,8 @@ def java_c_types(fn_arg, ret_arr_len):
 
     is_primitive = False
     arr_len = None
+    mapped_type = []
+    java_type_plural = None
     if fn_arg.startswith("void"):
         java_ty = "void"
         c_ty = "void"
@@ -196,26 +190,30 @@ def java_c_types(fn_arg, ret_arr_len):
         fn_arg = fn_arg[4:].strip()
         is_primitive = True
     elif fn_arg.startswith("uint8_t"):
-        java_ty = "byte"
+        mapped_type = consts.c_type_map['uint8_t']
+        java_ty = mapped_type[0]
         c_ty = "int8_t"
         fn_ty_arg = "B"
         fn_arg = fn_arg[7:].strip()
         is_primitive = True
     elif fn_arg.startswith("uint16_t"):
-        java_ty = "short"
+        mapped_type = consts.c_type_map['uint16_t']
+        java_ty = mapped_type[0]
         c_ty = "jshort"
         fn_ty_arg = "S"
         fn_arg = fn_arg[8:].strip()
         is_primitive = True
     elif fn_arg.startswith("uint32_t"):
-        java_ty = "int"
+        mapped_type = consts.c_type_map['uint32_t']
+        java_ty = mapped_type[0]
         c_ty = "int32_t"
         fn_ty_arg = "I"
         fn_arg = fn_arg[8:].strip()
         is_primitive = True
     elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"):
         # TODO: uintptr_t is arch-dependent :(
-        java_ty = "long"
+        mapped_type = consts.c_type_map['long']
+        java_ty = mapped_type[0]
         c_ty = "int64_t"
         fn_ty_arg = "J"
         if fn_arg.startswith("uint64_t"):
@@ -308,7 +306,11 @@ def java_c_types(fn_arg, ret_arr_len):
     if var_is_arr is not None or ret_arr_len is not None:
         assert(not take_by_ptr)
         assert(not is_ptr)
-        java_ty = java_ty + "[]"
+        # is there a special case for plurals?
+        if len(mapped_type) == 2:
+            java_ty = mapped_type[1]
+        else:
+            java_ty = java_ty + "[]"
         c_ty = c_ty + "Array"
         if var_is_arr is not None:
             if var_is_arr.group(1) == "":
@@ -328,7 +330,7 @@ reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
 clone_fns = set()
 constructor_fns = {}
 c_array_class_caches = set()
-with open(path_to_lightning_header) as in_h:
+with open(sys.argv[1]) as in_h:
     for line in in_h:
         reg_fn = reg_fn_regex.match(line)
         if reg_fn is not None:
@@ -352,7 +354,7 @@ write_c("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struc
 
 java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order
 
-with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as out_java:
+with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
     def map_type(fn_arg, print_void, ret_arr_len, is_free, holds_ref):
         ty_info = java_c_types(fn_arg, ret_arr_len)
         return map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref)
@@ -581,7 +583,8 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
                         arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None,
                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = *", opaque_ret_conv_suf),
                         ret_conv_name = ty_info.var_name + "_ref",
-                        to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
+                        # to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
+                        to_hu_conv = consts.to_hu_conv_templates['ptr'].replace('{human_type}', ty_info.java_hu_ty).replace('{var_name}', ty_info.var_name),
                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
                 else:
@@ -589,7 +592,8 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
                         arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
                         ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", opaque_ret_conv_suf),
                         ret_conv_name = ty_info.var_name + "_ref",
-                        to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
+                        # to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");",
+                        to_hu_conv = consts.to_hu_conv_templates['default'].replace('{human_type}', ty_info.java_hu_ty).replace('{var_name}', ty_info.var_name),
                         to_hu_conv_name = ty_info.var_name + "_hu_conv",
                         from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")"))
 
@@ -751,8 +755,6 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
                     to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed
             assert False # We should have handled every case by now.
 
-
-
     def map_fn(line, re_match, ret_arr_len, c_call_string):
         out_java.write("\t// " + line)
         out_java.write("\tpublic static native ")
@@ -803,7 +805,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
 
         out_java_struct = None
         if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
-            out_java_struct = open(f"{path_to_bindings_directory}/structs/{struct_meth}.{consts.file_extension}", "a")
+            out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
             if not args_known:
                 out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n")
                 out_java_struct.close()
@@ -924,7 +926,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
             out_java_struct.close()
 
     def map_unitary_enum(struct_name, field_lines):
-        with recursiveOpenFile(f"{path_to_bindings_directory}/enums/{struct_name}.{consts.file_extension}", "w") as out_java_enum:
+        with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum:
             unitary_enums.add(struct_name)
             for idx, struct_line in enumerate(field_lines):
                 if idx == 0:
@@ -943,108 +945,43 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
     def map_complex_enum(struct_name, union_enum_items):
         java_hu_type = struct_name.replace("LDK", "")
         complex_enums.add(struct_name)
-        with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum:
-            out_java_enum.write(consts.hu_struct_file_prefix)
-            out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n")
-            out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n")
-            out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
-            out_java_enum.write("\tprotected void finalize() throws Throwable {\n")
-            out_java_enum.write("\t\tsuper.finalize();\n")
-            out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n")
-            out_java_enum.write("\t}\n")
-            out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
-            out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
-            java_hu_subclasses = ""
-
-            tag_field_lines = union_enum_items["field_lines"]
-            init_meth_jty_strs = {}
-            for idx, struct_line in enumerate(tag_field_lines):
-                if idx == 0:
-                    assert(struct_line == "typedef enum %s_Tag {" % struct_name)
-                elif idx == len(tag_field_lines) - 3:
-                    assert(struct_line.endswith("_Sentinel,"))
-                elif idx == len(tag_field_lines) - 2:
-                    assert(struct_line == "} %s_Tag;" % struct_name)
-                elif idx == len(tag_field_lines) - 1:
-                    assert(struct_line == "")
 
-            out_java.write("\tpublic static class " + struct_name + " {\n")
-            out_java.write("\t\tprivate " + struct_name + "() {}\n")
-            for idx, struct_line in enumerate(tag_field_lines):
-                if idx != 0 and idx < len(tag_field_lines) - 3:
-                    var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
-                    out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n")
-                    java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n"
-                    out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n")
-                    out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n")
-                    init_meth_jty_str = ""
-                    init_meth_params = ""
-                    init_meth_body = ""
-                    hu_conv_body = ""
-                    if "LDK" + var_name in union_enum_items:
-                        enum_var_lines = union_enum_items["LDK" + var_name]
-                        for idx, field in enumerate(enum_var_lines):
-                            if idx != 0 and idx < len(enum_var_lines) - 2:
-                                field_ty = map_type(field.strip(' ;'), False, None, False, True)
-                                out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
-                                java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n"
-                                if field_ty.to_hu_conv is not None:
-                                    hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
-                                    hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
-                                    hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
-                                else:
-                                    hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
-                                init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg
-                                if idx > 1:
-                                    init_meth_params = init_meth_params + ", "
-                                init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
-                                init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
-                        out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ")
-                        out_java.write(init_meth_body)
-                        out_java.write("}\n")
-                    out_java.write("\t\t}\n")
-                    out_java_enum.write("\t\t}\n")
-                    java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n"
-                    java_hu_subclasses = java_hu_subclasses + hu_conv_body
-                    java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n"
-                    init_meth_jty_strs[var_name] = init_meth_jty_str
-            out_java.write("\t\tstatic native void init();\n")
-            out_java.write("\t}\n")
-            out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
-            out_java_enum.write(java_hu_subclasses)
-            out_java.write("\tstatic { " + struct_name + ".init(); }\n")
-            out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
-
-            write_c(consts.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs))
-
-            write_c(consts.c_fn_ty_pfx + consts.c_complex_enum_pass_ty(struct_name) + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " ptr) {\n")
-            write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
-            write_c("\tswitch(obj->tag) {\n")
-            for idx, struct_line in enumerate(tag_field_lines):
-                if idx != 0 and idx < len(tag_field_lines) - 3:
-                    var_name = struct_line.strip(' ,')[len(struct_name) + 1:]
-                    write_c("\t\tcase " + struct_name + "_" + var_name + ": {\n")
-                    c_params = []
-                    if "LDK" + var_name in union_enum_items:
-                        enum_var_lines = union_enum_items["LDK" + var_name]
-                        for idx, field in enumerate(enum_var_lines):
-                            if idx != 0 and idx < len(enum_var_lines) - 2:
-                                field_map = map_type(field.strip(' ;'), False, None, False, True)
-                                if field_map.ret_conv is not None:
-                                    write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
-                                    write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
-                                    write_c(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
-                                    c_params.append(field_map.ret_conv_name)
-                                else:
-                                    c_params.append("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name)
-                    write_c("\t\t\treturn " + consts.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n")
-                    write_c("\t\t}\n")
-            write_c("\t\tdefault: abort();\n")
-            write_c("\t}\n}\n")
-            out_java_enum.write("}\n")
+        enum_variants = []
+        tag_field_lines = union_enum_items["field_lines"]
+        for idx, struct_line in enumerate(tag_field_lines):
+            if idx == 0:
+                assert(struct_line == "typedef enum %s_Tag {" % struct_name)
+            elif idx == len(tag_field_lines) - 3:
+                assert(struct_line.endswith("_Sentinel,"))
+            elif idx == len(tag_field_lines) - 2:
+                assert(struct_line == "} %s_Tag;" % struct_name)
+            elif idx == len(tag_field_lines) - 1:
+                assert(struct_line == "")
+            else:
+                variant_name = struct_line.strip(' ,')[len(struct_name) + 1:]
+                fields = []
+                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:
+                            fields.append(map_type(field.strip(' ;'), False, None, False, True))
+                        else:
+                            # TODO: Assert line format
+                            pass
+                else:
+                    # TODO: Assert line format
+                    pass
+                enum_variants.append(ComplexEnumVariantInfo(variant_name, fields))
+
+        with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum:
+            (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake)
+
+            out_java_enum.write(out_java_enum_addendum)
+            out_java.write(out_java_addendum)
+            write_c(out_c_addendum)
 
     def map_trait(struct_name, field_var_lines, trait_fn_lines):
-        with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "w") as out_java_trait:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait:
             field_var_convs = []
             for var_line in field_var_lines:
                 if var_line.group(1) in trait_structs:
@@ -1065,208 +1002,10 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
                     arg_tys.append(arg_conv_info)
                 field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys))
 
-            write_c(consts.native_c_map_trait(struct_name, field_var_convs, field_fns)[1])
-
-            out_java_trait.write(consts.hu_struct_file_prefix)
-            out_java_trait.write("public class " + struct_name.replace("LDK","") + " extends CommonBase {\n")
-            out_java_trait.write("\tfinal bindings." + struct_name + " bindings_instance;\n")
-            out_java_trait.write("\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n")
-            out_java_trait.write("\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    out_java_trait.write(", bindings." + var_line.group(1) + " " + var_line.group(2))
-                else:
-                    out_java_trait.write(", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2))
-            out_java_trait.write(") {\n")
-            out_java_trait.write("\t\tsuper(bindings." + struct_name + "_new(arg")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    out_java_trait.write(", " + var_line.group(2))
-                elif field_var_convs[idx].from_hu_conv is not None:
-                    out_java_trait.write(", " + field_var_convs[idx].from_hu_conv[0])
-                else:
-                    out_java_trait.write(", " + var_line.group(2))
-            out_java_trait.write("));\n")
-            out_java_trait.write("\t\tthis.ptrs_to.add(arg);\n")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    out_java_trait.write("\t\tthis.ptrs_to.add(" + var_line.group(2) + ");\n")
-                elif field_var_convs[idx].from_hu_conv is not None and field_var_convs[idx].from_hu_conv[1] != "":
-                    out_java_trait.write("\t\t" + field_var_convs[idx].from_hu_conv[1] + ";\n")
-            out_java_trait.write("\t\tthis.bindings_instance = arg;\n")
-            out_java_trait.write("\t}\n")
-            out_java_trait.write("\t@Override @SuppressWarnings(\"deprecation\")\n")
-            out_java_trait.write("\tprotected void finalize() throws Throwable {\n")
-            out_java_trait.write("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK","") + "_free(ptr); } super.finalize();\n")
-            out_java_trait.write("\t}\n\n")
-
-            java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n"
-            java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg"
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    # Ideally we'd be able to take any instance of the interface, but our C code can only represent
-                    # Java-implemented version, so we require users pass a Java implementation here :/
-                    java_trait_constr = java_trait_constr + ", " + var_line.group(1).replace("LDK", "") + "." + var_line.group(1).replace("LDK", "") + "Interface " + var_line.group(2) + "_impl"
-                else:
-                    java_trait_constr = java_trait_constr + ", " + field_var_convs[idx].java_hu_ty + " " + var_line.group(2)
-            java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n"
-            java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n"
-            out_java_trait.write("\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n")
-            out_java.write("\tpublic interface " + struct_name + " {\n")
-            java_meths = []
-            for fn_line in trait_fn_lines:
-                java_meth_descr = "("
-                if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
-                    ret_ty_info = map_type(fn_line.group(2), True, None, False, False)
-
-                    out_java.write("\t\t " + ret_ty_info.java_ty + " " + fn_line.group(3) + "(")
-                    java_trait_constr = java_trait_constr + "\t\t\t@Override public " + ret_ty_info.java_ty + " " + fn_line.group(3) + "("
-                    out_java_trait.write("\t\t" + ret_ty_info.java_hu_ty + " " + fn_line.group(3) + "(")
-                    is_const = fn_line.group(4) is not None
-
-                    arg_names = []
-                    for idx, arg in enumerate(fn_line.group(5).split(',')):
-                        if arg == "":
-                            continue
-                        if idx >= 2:
-                            out_java.write(", ")
-                            java_trait_constr = java_trait_constr + ", "
-                            out_java_trait.write(", ")
-                        arg_conv_info = map_type(arg, True, None, False, False)
-                        out_java.write(arg_conv_info.java_ty + " " + arg_conv_info.arg_name)
-                        out_java_trait.write(arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name)
-                        java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name
-                        arg_names.append(arg_conv_info)
-                        java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg
-                    java_meth_descr = java_meth_descr + ")" + ret_ty_info.java_fn_ty_arg
-                    java_meths.append((fn_line, java_meth_descr))
-
-                    out_java.write(");\n")
-                    out_java_trait.write(");\n")
-                    java_trait_constr = java_trait_constr + ") {\n"
-
-                    for arg_info in arg_names:
-                        if arg_info.to_hu_conv is not None:
-                            java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n"
-
-                    if ret_ty_info.java_ty != "void":
-                        java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_hu_ty + " ret = arg." + fn_line.group(3) + "("
-                    else:
-                        java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.group(3) + "("
-
-                    for idx, arg_info in enumerate(arg_names):
-                        if idx != 0:
-                            java_trait_constr = java_trait_constr + ", "
-                        if arg_info.to_hu_conv_name is not None:
-                            java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name
-                        else:
-                            java_trait_constr = java_trait_constr + arg_info.arg_name
-
-                    java_trait_constr = java_trait_constr + ");\n"
-                    if ret_ty_info.java_ty != "void":
-                        if ret_ty_info.from_hu_conv is not None:
-                            java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.java_ty + " result = " + ret_ty_info.from_hu_conv[0] + ";\n"
-                            if ret_ty_info.from_hu_conv[1] != "":
-                                java_trait_constr = java_trait_constr + "\t\t\t\t" + ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n"
-                            if ret_ty_info.rust_obj in result_types:
-                                # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead
-                                java_trait_constr = java_trait_constr + "\t\t\t\tret.ptr = 0;\n"
-                            java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n"
-                        else:
-                            java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n"
-                    java_trait_constr = java_trait_constr + "\t\t\t}\n"
-            java_trait_constr = java_trait_constr + "\t\t}"
-            for var_line in field_var_lines:
-                if var_line.group(1) in trait_structs:
-                    java_trait_constr = java_trait_constr + ", " + var_line.group(2) + ".new_impl(" + var_line.group(2) + "_impl).bindings_instance"
-                else:
-                    java_trait_constr = java_trait_constr + ", " + var_line.group(2)
-            out_java_trait.write("\t}\n")
-            out_java_trait.write(java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n")
-
-            # Write out a clone function whether we need one or not, as we use them in moving to rust
-            write_c("static void* " + struct_name + "_JCalls_clone(const void* this_arg) {\n")
-            write_c("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
-            write_c("\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n")
-            for var_line in field_var_lines:
-                if var_line.group(1) in trait_structs:
-                    write_c("\tatomic_fetch_add_explicit(&j_calls->" + var_line.group(2) + "->refcnt, 1, memory_order_release);\n")
-            write_c("\treturn (void*) this_arg;\n")
-            write_c("}\n")
-
-            out_java.write("\t}\n")
-
-            out_java.write("\tpublic static native long " + struct_name + "_new(" + struct_name + " impl")
-            write_c("static inline " + struct_name + " " + struct_name + "_init (" + consts.c_fn_args_pfx + ", jobject o")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    out_java.write(", " + var_line.group(1) + " " + var_line.group(2))
-                    write_c(", jobject " + var_line.group(2))
-                else:
-                    out_java.write(", " + field_var_convs[idx].java_ty + " " + var_line.group(2))
-                    write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
-            out_java.write(");\n")
-            write_c(") {\n")
-
-            write_c("\tjclass c = (*env)->GetObjectClass(env, o);\n")
-            write_c("\tCHECK(c != NULL);\n")
-            write_c("\t" + struct_name + "_JCalls *calls = MALLOC(sizeof(" + struct_name + "_JCalls), \"" + struct_name + "_JCalls\");\n")
-            write_c("\tatomic_init(&calls->refcnt, 1);\n")
-            write_c("\tDO_ASSERT((*env)->GetJavaVM(env, &calls->vm) == 0);\n")
-            write_c("\tcalls->o = (*env)->NewWeakGlobalRef(env, o);\n")
-            for (fn_line, java_meth_descr) in java_meths:
-                if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
-                    write_c("\tcalls->" + fn_line.group(3) + "_meth = (*env)->GetMethodID(env, c, \"" + fn_line.group(3) + "\", \"" + java_meth_descr + "\");\n")
-                    write_c("\tCHECK(calls->" + fn_line.group(3) + "_meth != NULL);\n")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) not in trait_structs and field_var_convs[idx].arg_conv is not None:
-                    write_c("\n\t" + field_var_convs[idx].arg_conv.replace("\n", "\n\t") +"\n")
-            write_c("\n\t" + struct_name + " ret = {\n")
-            write_c("\t\t.this_arg = (void*) calls,\n")
-            for fn_line in trait_fn_lines:
-                if fn_line.group(3) != "free" and fn_line.group(3) != "clone":
-                    write_c("\t\t." + fn_line.group(3) + " = " + fn_line.group(3) + "_jcall,\n")
-                elif fn_line.group(3) == "free":
-                    write_c("\t\t.free = " + struct_name + "_JCalls_free,\n")
-                else:
-                    write_c("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    write_c("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, clz, " + var_line.group(2) + "),\n")
-                elif field_var_convs[idx].arg_conv_name is not None:
-                    write_c("\t\t." + var_line.group(2) + " = " + field_var_convs[idx].arg_conv_name + ",\n")
-                    write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
-                else:
-                    write_c("\t\t." + var_line.group(2) + " = " + var_line.group(2) + ",\n")
-                    write_c("\t\t.set_" + var_line.group(2) + " = NULL,\n")
-            write_c("\t};\n")
-            for var_line in field_var_lines:
-                if var_line.group(1) in trait_structs:
-                    write_c("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
-            write_c("\treturn ret;\n")
-            write_c("}\n")
-
-            write_c(consts.c_fn_ty_pfx + "long " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1new (" + consts.c_fn_args_pfx + ", jobject o")
-            for idx, var_line in enumerate(field_var_lines):
-                if var_line.group(1) in trait_structs:
-                    write_c(", jobject " + var_line.group(2))
-                else:
-                    write_c(", " + field_var_convs[idx].c_ty + " " + var_line.group(2))
-            write_c(") {\n")
-            write_c("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
-            write_c("\t*res_ptr = " + struct_name + "_init(env, clz, o")
-            for var_line in field_var_lines:
-                write_c(", " + var_line.group(2))
-            write_c(");\n")
-            write_c("\treturn (long)res_ptr;\n")
-            write_c("}\n")
-
-            out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
-            write_c(consts.c_fn_ty_pfx + "jobject " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " val) {\n")
-            write_c("\tjobject ret = (*env)->NewLocalRef(env, ((" + struct_name + "_JCalls*)val)->o);\n")
-            write_c("\tCHECK(ret != NULL);\n")
-            write_c("\treturn ret;\n")
-            write_c("}\n")
+            (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, field_fns)
+            write_c(out_c_addendum)
+            out_java_trait.write(out_java_trait_addendum)
+            out_java.write(out_java_addendum)
 
         for fn_line in trait_fn_lines:
             # For now, just disable enabling the _call_log - we don't know how to inverse-map String
@@ -1287,7 +1026,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
     def map_result(struct_name, res_ty, err_ty):
         result_types.add(struct_name)
         human_ty = struct_name.replace("LDKCResult", "Result")
-        with open(f"{path_to_bindings_directory}/structs/{human_ty}.{consts.file_extension}", "w") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct:
             out_java_struct.write(consts.hu_struct_file_prefix)
             out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
             out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
@@ -1476,41 +1215,9 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o
                 write_c("\treturn tuple->" + e + ";\n")
             write_c("}\n")
 
-    out_java.write("""package org.ldk.impl;
-import org.ldk.enums.*;
-
-public class bindings {
-       public static class VecOrSliceDef {
-               public long dataptr;
-               public long datalen;
-               public long stride;
-               public VecOrSliceDef(long dataptr, long datalen, long stride) {
-                       this.dataptr = dataptr; this.datalen = datalen; this.stride = stride;
-               }
-       }
-       static {
-               System.loadLibrary(\"lightningjni\");
-               init(java.lang.Enum.class, VecOrSliceDef.class);
-               init_class_cache();
-       }
-       static native void init(java.lang.Class c, java.lang.Class slicedef);
-       static native void init_class_cache();
-
-       public static native boolean deref_bool(long ptr);
-       public static native long deref_long(long ptr);
-       public static native void free_heap_ptr(long ptr);
-       public static native byte[] read_bytes(long ptr, long len);
-       public static native byte[] get_u8_slice_bytes(long slice_ptr);
-       public static native long bytes_to_u8_vec(byte[] bytes);
-       public static native long new_txpointer_copy_data(byte[] txdata);
-       public static native void txpointer_free(long ptr);
-       public static native byte[] txpointer_get_buffer(long ptr);
-       public static native long vec_slice_len(long vec);
-       public static native long new_empty_slice_vec();
-
-""")
-
-    with recursiveOpenFile(f"{path_to_bindings_directory}/structs/CommonBase.{consts.file_extension}", "w") as out_java_struct:
+    out_java.write(consts.bindings_header)
+
+    with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct:
         out_java_struct.write(consts.common_base)
 
     in_block_comment = False
@@ -1601,7 +1308,7 @@ public class bindings {
 
                 if is_opaque:
                     opaque_structs.add(struct_name)
-                    with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "w") as out_java_struct:
+                    with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_struct:
                         out_java_struct.write(consts.hu_struct_file_prefix)
                         out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase")
                         if struct_name.startswith("LDKLocked"):
@@ -1687,7 +1394,7 @@ public class bindings {
                     trait_structs.add(struct_name)
                     map_trait(struct_name, field_var_lines, trait_fn_lines)
                 elif struct_name == "LDKTxOut":
-                    with open(f"{path_to_bindings_directory}/structs/TxOut.{consts.file_extension}", "w") as out_java_struct:
+                    with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct:
                         out_java_struct.write(consts.hu_struct_file_prefix)
                         out_java_struct.write("public class TxOut extends CommonBase{\n")
                         out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n")
@@ -1734,13 +1441,12 @@ public class bindings {
 
     out_java.write("}\n")
     for struct_name in opaque_structs:
-        with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "a") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct:
             out_java_struct.write("}\n")
     for struct_name in trait_structs:
-        with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "a") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct:
             out_java_struct.write("}\n")
-with open(path_to_c_file, "w") as out_c:
+with open(sys.argv[4], "w") as out_c:
     out_c.write(consts.c_file_pfx)
     out_c.write(consts.init_str(c_array_class_caches))
     out_c.write(c_file)
-