From 831ad69f1f65d26c224fe18003f1694806effa5a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 3 Mar 2023 01:20:33 +0000 Subject: [PATCH] Support traits with supertraits of supertraits --- genbindings.py | 13 ++++++++++--- java_strings.py | 22 +++++++++++++--------- typescript_strings.py | 8 +++++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/genbindings.py b/genbindings.py index 77f9b472..10416ab9 100755 --- a/genbindings.py +++ b/genbindings.py @@ -765,13 +765,20 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", 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), 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)]) + flattened_field_var_convs.append((var_line.group(1), var_line.group(2), var_line.group(2))) + for field_var in trait_structs[var_line.group(1)]: + if isinstance(field_var, ConvInfo): + flattened_field_var_convs.append(field_var) + else: + path = var_line.group(2) + if len(field_var) > 2: + path = var_line.group(2) + "." + field_var[2] + flattened_field_var_convs.append((field_var[0], field_var[1], path)) else: 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 + trait_structs[struct_name] = flattened_field_var_convs field_fns = [] for fn_docs, fn_line in trait_fn_lines: diff --git a/java_strings.py b/java_strings.py index 650e36bf..c44dca65 100644 --- a/java_strings.py +++ b/java_strings.py @@ -857,17 +857,21 @@ import javax.annotation.Nullable; java_trait_constr = java_trait_constr + ", " + var.arg_name else: java_trait_constr += ", " + var[1] + ".new_impl(" + var[1] + "_impl" + suptrait_constr = "" for suparg in var[2]: if isinstance(suparg, ConvInfo): - java_trait_constr += ", " + suparg.arg_name + suptrait_constr += ", " + suparg.arg_name else: - java_trait_constr += ", " + suparg[1] - java_trait_constr += ").bindings_instance" + suptrait_constr += ", " + suparg[1] + "_impl" + java_trait_constr += suptrait_constr + ").bindings_instance" for suparg in var[2]: if isinstance(suparg, ConvInfo): java_trait_constr += ", " + suparg.arg_name else: - java_trait_constr += ", " + suparg[1] + java_trait_constr += ", " + suparg[1] + ".new_impl(" + # Blindly assume that we can just strip the first arg to build the args for the supertrait + java_trait_constr += suptrait_constr.split(", ", 1)[1] + java_trait_constr += ").bindings_instance" 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" @@ -976,9 +980,9 @@ import javax.annotation.Nullable; out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n" out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n" out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n" - for var in field_vars: + for var in flattened_field_vars: if not isinstance(var, ConvInfo): - out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n" + out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[2].replace(".", "->") + "->refcnt, 1, memory_order_release);\n" out_c = out_c + "}\n" out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (" + self.c_fn_args_pfx + ", jobject o" @@ -1032,7 +1036,7 @@ import javax.annotation.Nullable; out_c = out_c + "\t};\n" 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 + "\tcalls->" + var[1] + " = ret." + var[2] + ".this_arg;\n" out_c = out_c + "\treturn ret;\n" out_c = out_c + "}\n" @@ -1062,7 +1066,7 @@ import javax.annotation.Nullable; underscore_name = ''.join('_' + c.lower() if c.isupper() else c for c in var[1]).strip('_') out_java_trait += "\tpublic " + var[1] + " get_" + underscore_name + "() {\n" out_java_trait += "\t\t" + var[1] + " res = new " + var[1] + "(null, bindings." + struct_name + "_get_" + var[1] + "(this.ptr));\n" - out_java_trait += "\t\tthis.ptrs_to.add(res);\n" + out_java_trait += "\t\tres.ptrs_to.add(this);\n" out_java_trait += "\t\treturn res;\n" out_java_trait += "\t}\n" out_java_trait += "\n" @@ -1071,7 +1075,7 @@ import javax.annotation.Nullable; out_c += "JNIEXPORT int64_t JNICALL Java_org_ldk_impl_bindings_" + struct_name + "_1get_1" + var[1] + "(JNIEnv *env, jclass clz, int64_t arg) {\n" out_c += "\t" + struct_name + " *inp = (" + struct_name + " *)untag_ptr(arg);\n" - out_c += "\treturn tag_ptr(&inp->" + var[1] + ", false);\n" + out_c += "\treturn tag_ptr(&inp->" + var[2] + ", false);\n" out_c += "}\n" return (out_java, out_java_trait, out_c) diff --git a/typescript_strings.py b/typescript_strings.py index 6706ff39..b387fd4d 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -952,6 +952,8 @@ export enum {struct_name} {{ if isinstance(suparg, ConvInfo): trait_constructor_arguments += ", " + suparg.arg_name else: + # Blindly assume that we can just strip the first arg to build the args for the supertrait + super_constructor_statements += "\t\tconst " + first_to_lower(suparg[1]) + " = " + suparg[1] + ".new_impl(" + super_instantiator.split(", ", 1)[1] + ");\n" trait_constructor_arguments += ", " + suparg[1] # BUILD INTERFACE METHODS @@ -1180,9 +1182,9 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_c = out_c + "static void " + struct_name + "_JCalls_cloned(" + struct_name + "* new_obj) {\n" out_c = out_c + "\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) new_obj->this_arg;\n" out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->refcnt, 1, memory_order_release);\n" - for var in field_var_conversions: + for var in flattened_field_var_conversions: if not isinstance(var, ConvInfo): - out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[1] + "->refcnt, 1, memory_order_release);\n" + out_c = out_c + "\tatomic_fetch_add_explicit(&j_calls->" + var[2].replace(".", "->") + "->refcnt, 1, memory_order_release);\n" out_c = out_c + "}\n" out_c = out_c + "static inline " + struct_name + " " + struct_name + "_init (JSValue o" @@ -1233,7 +1235,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_c = out_c + "\t};\n" 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 + "\tcalls->" + var[1] + " = ret." + var[2] + ".this_arg;\n" out_c = out_c + "\treturn ret;\n" out_c = out_c + "}\n" -- 2.39.5