]> git.bitcoin.ninja Git - ldk-java/commitdiff
Handle supertraits with constructor arguments
authorMatt Corallo <git@bluematt.me>
Thu, 15 Apr 2021 23:56:19 +0000 (23:56 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 16 Apr 2021 00:44:11 +0000 (00:44 +0000)
genbindings.py
java_strings.py
typescript_strings.py

index ab5b0cbf183a1372a250bd47f65de85200243aff..bac54945477f0182e3d378511e6b32962c786243 100755 (executable)
@@ -69,7 +69,7 @@ def camel_to_snake(s):
 unitary_enums = set()
 complex_enums = set()
 opaque_structs = set()
-trait_structs = set()
+trait_structs = {}
 result_types = set()
 tuple_types = {}
 
@@ -514,12 +514,17 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
     def map_trait(struct_name, field_var_lines, trait_fn_lines, trait_doc_comment):
         with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait:
             field_var_convs = []
+            flattened_field_var_convs = []
             for var_line in field_var_lines:
                 if var_line.group(1) in trait_structs:
-                    field_var_convs.append((var_line.group(1), var_line.group(2)))
+                    field_var_convs.append((var_line.group(1), var_line.group(2), trait_structs[var_line.group(1)]))
+                    flattened_field_var_convs.append((var_line.group(1), var_line.group(2), ))
+                    flattened_field_var_convs.extend(trait_structs[var_line.group(1)])
                 else:
-                    field_var_convs.append(
-                        type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False))
+                    mapped = type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False)
+                    field_var_convs.append(mapped)
+                    flattened_field_var_convs.append(mapped)
+            trait_structs[struct_name] = field_var_convs
 
             field_fns = []
             for fn_docs, fn_line in trait_fn_lines:
@@ -534,7 +539,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                     arg_tys.append(arg_conv_info)
                 field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys, fn_docs))
 
-            (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, field_fns, trait_doc_comment)
+            (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, flattened_field_var_convs, field_fns, trait_doc_comment)
             write_c(out_c_addendum)
             out_java_trait.write(out_java_trait_addendum)
             out_java.write(out_java_addendum)
@@ -879,7 +884,6 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
                     map_unitary_enum(struct_name, field_lines, last_block_comment)
                     last_block_comment = None
                 elif len(trait_fn_lines) > 0:
-                    trait_structs.add(struct_name)
                     map_trait(struct_name, field_var_lines, trait_fn_lines, last_block_comment)
                 elif struct_name == "LDKTxOut":
                     with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct:
index 03a1d6e9084db6987861503760677ebb5aa03cf7..af7672b00b53676af0c8c58d6553e034232a4894 100644 (file)
@@ -479,7 +479,7 @@ import java.util.Arrays;
             ret = ret + ", " + param
         return ret + ")"
 
-    def native_c_map_trait(self, struct_name, field_vars, field_fns, trait_doc_comment):
+    def native_c_map_trait(self, struct_name, field_vars, flattened_field_vars, field_fns, trait_doc_comment):
         out_java_trait = ""
         out_java = ""
 
@@ -492,14 +492,14 @@ import java.util.Arrays;
         out_java_trait = out_java_trait + "\tfinal bindings." + struct_name + " bindings_instance;\n"
         out_java_trait = out_java_trait + "\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n"
         out_java_trait = out_java_trait + "\tprivate " + struct_name.replace("LDK", "") + "(bindings." + struct_name + " arg"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 out_java_trait = out_java_trait + ", " + var.java_hu_ty + " " + var.arg_name
             else:
                 out_java_trait = out_java_trait + ", bindings." + var[0] + " " + var[1]
         out_java_trait = out_java_trait + ") {\n"
         out_java_trait = out_java_trait + "\t\tsuper(bindings." + struct_name + "_new(arg"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 if var.from_hu_conv is not None:
                     out_java_trait = out_java_trait + ", " + var.from_hu_conv[0]
@@ -509,7 +509,7 @@ import java.util.Arrays;
                 out_java_trait = out_java_trait + ", " + var[1]
         out_java_trait = out_java_trait + "));\n"
         out_java_trait = out_java_trait + "\t\tthis.ptrs_to.add(arg);\n"
-        for var in field_vars:
+        for var in flattened_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].replace("\n", "\n\t\t") + ";\n"
@@ -524,7 +524,7 @@ import java.util.Arrays;
 
         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 var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 java_trait_constr = java_trait_constr + ", " + var.java_hu_ty + " " + var.arg_name
             else:
@@ -593,14 +593,25 @@ import java.util.Arrays;
             if isinstance(var, ConvInfo):
                 java_trait_constr = java_trait_constr + ", " + var.arg_name
             else:
-                java_trait_constr = java_trait_constr + ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance"
+                java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        java_trait_constr += ", " + suparg.arg_name
+                    else:
+                        java_trait_constr += ", " + suparg[1]
+                java_trait_constr += ").bindings_instance"
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        java_trait_constr += ", " + suparg.arg_name
+                    else:
+                        java_trait_constr += ", " + suparg[1]
         out_java_trait = out_java_trait + "\t}\n"
         out_java_trait = out_java_trait + java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n"
 
         out_java = out_java + "\t}\n"
 
         out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 out_java = out_java + ", " + var.java_ty + " " + var.arg_name
             else:
@@ -612,7 +623,7 @@ import java.util.Arrays;
         out_c = out_c + "\tatomic_size_t refcnt;\n"
         out_c = out_c + "\tJavaVM *vm;\n"
         out_c = out_c + "\tjweak o;\n"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 # We're a regular ol' field
                 pass
@@ -688,7 +699,7 @@ import java.util.Arrays;
         out_c = out_c + "}\n"
 
         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
             else:
@@ -707,7 +718,7 @@ import java.util.Arrays;
                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
 
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo) and var.arg_conv is not None:
                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
@@ -728,16 +739,22 @@ import java.util.Arrays;
                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
             else:
-                out_c = out_c + "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1] + "),\n"
+                out_c += "\t\t." + var[1] + " = " + var[0] + "_init(env, clz, " + var[1]
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        out_c = out_c + ", " + suparg.arg_name
+                    else:
+                        out_c = out_c + ", " + suparg[1]
+                out_c += "),\n"
         out_c = out_c + "\t};\n"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if not isinstance(var, ConvInfo):
                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
         out_c = out_c + "\treturn ret;\n"
         out_c = out_c + "}\n"
 
         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:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
             else:
@@ -745,7 +762,7 @@ import java.util.Arrays;
         out_c = out_c + ") {\n"
         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(env, clz, o"
-        for var in field_vars:
+        for var in flattened_field_vars:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.arg_name
             else:
index a65b52b2e0e3ea0948eb494e4ed4ba362a674f09..20f639fb8d80cf8e7bc8f35bfcefd70f78157826 100644 (file)
@@ -483,14 +483,14 @@ const decodeString = (stringPointer, free = true) => {
             ret = ret + "; (void) " + param
         return ret
 
-    def native_c_map_trait(self, struct_name, field_var_conversions, field_function_lines, trait_doc_comment):
+    def native_c_map_trait(self, struct_name, field_var_conversions, flattened_field_var_conversions, field_function_lines, trait_doc_comment):
         out_typescript_bindings = "\n\n\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: START\n\n"
 
         constructor_arguments = ""
         super_instantiator = ""
         pointer_to_adder = ""
         impl_constructor_arguments = ""
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 constructor_arguments += f", {first_to_lower(var.arg_name)}?: {var.java_hu_ty}"
                 impl_constructor_arguments += f", {var.arg_name}: {var.java_hu_ty}"
@@ -568,7 +568,18 @@ const decodeString = (stringPointer, free = true) => {
             if isinstance(var, ConvInfo):
                 trait_constructor_arguments += ", " + var.arg_name
             else:
-                trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance"
+                trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl"
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        trait_constructor_arguments += ", " + suparg.arg_name
+                    else:
+                        trait_constructor_arguments += ", " + suparg[1]
+                trait_constructor_arguments += ").bindings_instance"
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        trait_constructor_arguments += ", " + suparg.arg_name
+                    else:
+                        trait_constructor_arguments += ", " + suparg[1]
 
         out_typescript_human = f"""
             {self.hu_struct_file_prefix}
@@ -631,7 +642,7 @@ const decodeString = (stringPointer, free = true) => {
         out_typescript_bindings = out_typescript_bindings + "\t\t}\n\n"
 
         out_typescript_bindings += f"\t\texport function {struct_name}_new(impl: {struct_name}"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_typescript_bindings += f", {var.arg_name}: {var.java_ty}"
             else:
@@ -647,7 +658,7 @@ const decodeString = (stringPointer, free = true) => {
         # Now that we've written out our java code (and created java_meths), generate C
         out_c = "typedef struct " + struct_name + "_JCalls {\n"
         out_c = out_c + "\tatomic_size_t refcnt;\n"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 # We're a regular ol' field
                 pass
@@ -722,7 +733,7 @@ const decodeString = (stringPointer, free = true) => {
         out_c = out_c + "}\n"
 
         out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (/*TODO: JS Object Reference */void* o"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
             else:
@@ -738,7 +749,7 @@ const decodeString = (stringPointer, free = true) => {
                 out_c = out_c + "\tcalls->" + fn_name + "_meth = (*env)->GetMethodID(env, c, \"" + fn_name + "\", \"" + java_meth_descr + "\");\n"
                 out_c = out_c + "\tCHECK(calls->" + fn_name + "_meth != NULL);\n"
 
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo) and var.arg_conv is not None:
                 out_c = out_c + "\n\t" + var.arg_conv.replace("\n", "\n\t") +"\n"
         out_c = out_c + "\n\t" + struct_name + " ret = {\n"
@@ -759,16 +770,22 @@ const decodeString = (stringPointer, free = true) => {
                     out_c = out_c + "\t\t." + var.var_name + " = " + var.var_name + ",\n"
                     out_c = out_c + "\t\t.set_" + var.var_name + " = NULL,\n"
             else:
-                out_c = out_c + "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1] + "),\n"
+                out_c += "\t\t." + var[1] + " = " + var[0] + "_init(" + var[1]
+                for suparg in var[2]:
+                    if isinstance(suparg, ConvInfo):
+                        out_c += ", " + suparg.arg_name
+                    else:
+                        out_c += ", " + suparg[1]
+                out_c += "),\n"
         out_c = out_c + "\t};\n"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if not isinstance(var, ConvInfo):
                 out_c = out_c + "\tcalls->" + var[1] + " = ret." + var[1] + ".this_arg;\n"
         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) + "/*TODO: JS Object Reference */void* o"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
             else:
@@ -776,7 +793,7 @@ const decodeString = (stringPointer, free = true) => {
         out_c = out_c + ") {\n"
         out_c = out_c + "\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n"
         out_c = out_c + "\t*res_ptr = " + struct_name + "_init(o"
-        for var in field_var_conversions:
+        for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.arg_name
             else: