From: Matt Corallo Date: Sun, 28 Nov 2021 23:17:52 +0000 (+0000) Subject: Add Variant-Level docs on complex and unitary enums. X-Git-Tag: v0.0.103.1^2~14 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f95cc8ba7d61d65aa37e6448e6a083edc6e72fc1;p=ldk-java Add Variant-Level docs on complex and unitary enums. Fixes #60. --- diff --git a/bindingstypes.py b/bindingstypes.py index 44b4182e..8c3f0db8 100644 --- a/bindingstypes.py +++ b/bindingstypes.py @@ -78,7 +78,8 @@ class TraitMethInfo: self.docs = docs class ComplexEnumVariantInfo: - def __init__(self, var_name, fields, tuple_variant): + def __init__(self, var_name, var_docs, fields, tuple_variant): self.var_name = var_name + self.var_docs = var_docs self.fields = fields self.tuple_variant = tuple_variant diff --git a/genbindings.py b/genbindings.py index a2c66863..042f5b92 100755 --- a/genbindings.py +++ b/genbindings.py @@ -539,7 +539,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", elif idx == len(field_lines) - 1: assert(struct_line == "") assert struct_name.startswith("LDK") - (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name[3:], [x.strip().strip(",") for x, _ in field_lines[1:-3]], enum_doc_comment) + (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name[3:], [(x.strip().strip(","), y) for x, y in field_lines[1:-3]], enum_doc_comment) write_c(c_out) out_java_enum.write(native_file_out) out_java.write(native_out) @@ -550,7 +550,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", enum_variants = [] tag_field_lines = union_enum_items["field_lines"] contains_trait = False - for idx, (struct_line, _) in enumerate(tag_field_lines): + for idx, (struct_line, variant_docs) in enumerate(tag_field_lines): if idx == 0: assert(struct_line == "typedef enum %s_Tag {" % struct_name) elif idx == len(tag_field_lines) - 3: @@ -573,16 +573,16 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}", else: field_conv = type_mapping_generator.map_type_with_info(field_ty, False, None, False, True, False) fields.append((field_conv, field_docs)) - enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False)) + enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, False)) elif camel_to_snake(variant_name) in inline_enum_variants: # TODO: If we ever have a rust enum Variant(Option) we need to pipe # docs through to there, and then potentially mark the field nullable. mapped = type_mapping_generator.map_type(inline_enum_variants[camel_to_snake(variant_name)] + " " + camel_to_snake(variant_name), False, None, False, True) contains_trait |= mapped.ty_info.contains_trait fields.append((mapped, None)) - enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True)) + enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, True)) else: - enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True)) + enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, True)) complex_enums[struct_name] = contains_trait with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum: diff --git a/java_strings.py b/java_strings.py index 15c3ac47..a83671db 100644 --- a/java_strings.py +++ b/java_strings.py @@ -614,8 +614,10 @@ import javax.annotation.Nullable; out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n" out_java_enum += "public enum " + struct_name + " {\n" ord_v = 0 - for var in variants: - out_java_enum = out_java_enum + "\t" + var + ",\n" + for var, var_docs in variants: + if var_docs is not None: + out_java_enum += "\t/**\n\t * " + var_docs.replace("\n", "\n\t * ") + "\n\t */\n" + out_java_enum += "\t" + var + ",\n" out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var) ord_v = ord_v + 1 out_java_enum = out_java_enum + "\t; static native void init();\n" @@ -627,19 +629,19 @@ import javax.annotation.Nullable; out_c = out_c + "}\n" out_c = out_c + "static jclass " + struct_name + "_class = NULL;\n" - for var in variants: + for var, _ in variants: out_c = out_c + "static jfieldID " + struct_name + "_" + var + " = NULL;\n" out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n" out_c = out_c + "\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n" out_c = out_c + "\tCHECK(" + struct_name + "_class != NULL);\n" - for var in variants: + for var, _ in variants: out_c = out_c + "\t" + struct_name + "_" + var + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + var + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n" out_c = out_c + "\tCHECK(" + struct_name + "_" + var + " != NULL);\n" out_c = out_c + "}\n" out_c = out_c + "static inline jclass LDK" + struct_name + "_to_java(JNIEnv *env, LDK" + struct_name + " val) {\n" out_c = out_c + "\tswitch (val) {\n" ord_v = 0 - for var in variants: + for var, _ in variants: out_c = out_c + "\t\tcase " + var + ":\n" out_c = out_c + "\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + var + ");\n" ord_v = ord_v + 1 @@ -1049,7 +1051,9 @@ import javax.annotation.Nullable; out_java += ("\t\tprivate " + struct_name + "() {}\n") for var in variant_list: out_java += ("\t\tpublic final static class " + var.var_name + " extends " + struct_name + " {\n") - java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var.var_name + " extends " + java_hu_type + " {\n" + if var.var_docs is not None: + java_hu_subclasses += "\t/**\n\t * " + var.var_docs.replace("\n", "\n\t * ") + "\n\t */\n" + java_hu_subclasses += "\tpublic final static class " + var.var_name + " extends " + java_hu_type + " {\n" out_java_enum += ("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var.var_name + ".class) {\n") out_java_enum += ("\t\t\treturn new " + var.var_name + "(ptr, (bindings." + struct_name + "." + var.var_name + ")raw_val);\n") init_meth_jty_str = "" diff --git a/typescript_strings.py b/typescript_strings.py index 54159d8d..270eeccc 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -472,9 +472,11 @@ const decodeString = (stringPointer, free = true) => { out_typescript_enum_fields = "" - for var in variants: + for var, var_docs in variants: out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var) ord_v = ord_v + 1 + if var_docs is not None: + out_typescript_enum_fields += f"/**\n * {var_docs}\n */\n" out_typescript_enum_fields += f"{var},\n\t\t\t\t" out_c = out_c + "\t}\n" out_c = out_c + "\tabort();\n" @@ -483,7 +485,7 @@ const decodeString = (stringPointer, free = true) => { out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_js(LDK" + struct_name + " val) {\n" out_c = out_c + "\tswitch (val) {\n" ord_v = 0 - for var in variants: + for var, _ in variants: out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v ord_v = ord_v + 1 out_c = out_c + "\t\tdefault: abort();\n"