From 8f8b6aaddcb1dad3d708327c78850cdb30f2a64b Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 5 Apr 2022 18:05:11 +0000 Subject: [PATCH] Fully-Qualify Paths in some contexts instead of assuming enums Instead of assuming enum variants that have a name the same as a field's type, actually build a (simple) qualified-name method and start using it. --- gen_type_mapping.py | 4 +++- java_strings.py | 36 ++++++++++++++++++++++-------------- typescript_strings.py | 3 +++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/gen_type_mapping.py b/gen_type_mapping.py index d3d00d28..9cee9ab4 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -439,10 +439,12 @@ class TypeMappingGenerator: if from_hu_conv is None: from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "") from_hu_conv = (from_hu_conv[0], to_hu_conv_sfx) + fully_qualified_ty = self.consts.fully_qualified_hu_ty_path(ty_info) + to_hu_call = fully_qualified_ty + ".constr_from_ptr(" + ty_info.var_name + ")" return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = ret_conv, ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";", + to_hu_conv = self.consts.var_decl_statement(fully_qualified_ty, ty_info.var_name + "_hu_conv", to_hu_call) + ";\n" + self.consts.add_ref(ty_info.var_name + "_hu_conv", "this") + ";", to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv) if ty_info.rust_obj in self.result_types: if holds_ref: diff --git a/java_strings.py b/java_strings.py index 3a8ba6cb..6abfe427 100644 --- a/java_strings.py +++ b/java_strings.py @@ -701,6 +701,15 @@ import javax.annotation.Nullable; def add_ref(self, holder, referent): return holder + ".ptrs_to.add(" + referent + ")" + def fully_qualified_hu_ty_path(self, ty): + if ty.java_fn_ty_arg.startswith("L") and ty.java_fn_ty_arg.endswith(";"): + return ty.java_fn_ty_arg.strip("L;").replace("/", ".") + if ty.java_hu_ty == "UnqualifiedError" or ty.java_hu_ty == "UInt5": + return "org.ldk.util." + ty.java_hu_ty + if ty.rust_obj is not None and not "[]" in ty.java_hu_ty: + return "org.ldk.structs." + ty.java_hu_ty + return ty.java_hu_ty + def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment): out_java_enum = "package org.ldk.enums;\n\n" out_java = "" @@ -1166,20 +1175,19 @@ import javax.annotation.Nullable; if idx > 0: init_meth_params = init_meth_params + ", " - if field_ty.java_hu_ty == var.var_name: - field_path = field_ty.java_fn_ty_arg.strip("L;").replace("/", ".") - out_java += "\t\t\tpublic " + field_path + " " + field_ty.arg_name + ";\n" - java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_path + " " + field_ty.arg_name + ";\n" - init_meth_params = init_meth_params + field_path + " " + field_ty.arg_name - else: - out_java += "\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n" - if field_docs is not None: - java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n" - java_hu_subclasses += "\t\t" - if field_ty.nullable: - java_hu_subclasses += "@Nullable " - java_hu_subclasses += "public final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" - init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name + java_ty = field_ty.java_ty + if field_ty.java_fn_ty_arg.startswith("L") and field_ty.java_fn_ty_arg.endswith(";"): + # If this is a simple enum, we have to reference it in the low-level bindings differently: + java_ty = field_ty.java_fn_ty_arg.strip("L;").replace("/", ".") + out_java += "\t\t\tpublic " + java_ty + " " + field_ty.arg_name + ";\n" + if field_docs is not None: + java_hu_subclasses += "\t\t/**\n\t\t * " + field_docs.replace("\n", "\n\t\t * ") + "\n\t\t*/\n" + java_hu_subclasses += "\t\t" + if field_ty.nullable: + java_hu_subclasses += "@Nullable " + java_hu_subclasses += "public final " + self.fully_qualified_hu_ty_path(field_ty) + " " + field_ty.arg_name + ";\n" + init_meth_params = init_meth_params + java_ty + " " + field_ty.arg_name + init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " 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" diff --git a/typescript_strings.py b/typescript_strings.py index 102d2256..76e43828 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -722,6 +722,9 @@ import * as bindings from '../bindings.mjs' with open(self.outdir + "/imports.mts.part", 'a') as imports: imports.write(f"import {{ {', '.join(struct_names)} }} from '../{folder}/{struct_names[0]}.mjs';\n") + def fully_qualified_hu_ty_path(self, ty): + return ty.java_hu_ty + def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment): out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_js(int32_t ord) {\n" out_c = out_c + "\tswitch (ord) {\n" -- 2.30.2