From: Matt Corallo Date: Thu, 15 Apr 2021 23:56:19 +0000 (+0000) Subject: Handle supertraits with constructor arguments X-Git-Tag: v0.0.98~2^2~4 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e3b8d99ecdfd932e009456d4eb8ea01365795223;p=ldk-java Handle supertraits with constructor arguments --- diff --git a/genbindings.py b/genbindings.py index ab5b0cbf..bac54945 100755 --- a/genbindings.py +++ b/genbindings.py @@ -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: diff --git a/java_strings.py b/java_strings.py index 03a1d6e9..af7672b0 100644 --- a/java_strings.py +++ b/java_strings.py @@ -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: diff --git a/typescript_strings.py b/typescript_strings.py index a65b52b2..20f639fb 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -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: