From: Matt Corallo Date: Tue, 30 Mar 2021 01:53:28 +0000 (-0400) Subject: Handle tuple variants in complex enums X-Git-Tag: v0.0.98~5^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=5c95cad6c8e9b710cbaaf273b639064e80b697dc;p=ldk-java Handle tuple variants in complex enums --- diff --git a/bindingstypes.py b/bindingstypes.py index bd123ac3..2ca6c0de 100644 --- a/bindingstypes.py +++ b/bindingstypes.py @@ -75,6 +75,7 @@ class TraitMethInfo: self.docs = docs class ComplexEnumVariantInfo: - def __init__(self, var_name, fields): + def __init__(self, var_name, fields, tuple_variant): self.var_name = var_name self.fields = fields + self.tuple_variant = tuple_variant diff --git a/genbindings.py b/genbindings.py index ddbc0956..1a0f7dd1 100755 --- a/genbindings.py +++ b/genbindings.py @@ -467,7 +467,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_enum.write(native_file_out) out_java.write(native_out) - def map_complex_enum(struct_name, union_enum_items, enum_doc_comment): + def map_complex_enum(struct_name, union_enum_items, inline_enum_variants, enum_doc_comment): java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option") complex_enums.add(struct_name) @@ -490,13 +490,12 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: for idx, field in enumerate(enum_var_lines): if idx != 0 and idx < len(enum_var_lines) - 2: fields.append(type_mapping_generator.map_type(field.strip(' ;'), False, None, False, True)) - else: - # TODO: Assert line format - pass + enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False)) + elif camel_to_snake(variant_name) in inline_enum_variants: + fields.append(type_mapping_generator.map_type(inline_enum_variants[camel_to_snake(variant_name)] + " " + camel_to_snake(variant_name), False, None, False, True)) + enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True)) else: - # TODO: Assert line format - pass - enum_variants.append(ComplexEnumVariantInfo(variant_name, fields)) + enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True)) with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum: (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake, enum_doc_comment) @@ -849,7 +848,25 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: enum_var_name = struct_name.split("_") union_enum_items[enum_var_name[0]][enum_var_name[1]] = field_lines elif struct_name in union_enum_items: - map_complex_enum(struct_name, union_enum_items[struct_name], last_block_comment) + tuple_variants = {} + elem_items = -1 + for line in field_lines: + if line == " struct {": + elem_items = 0 + elif line == " };": + elem_items = -1 + elif elem_items > -1: + line = line.strip() + if line.startswith("struct "): + line = line[7:] + split = line.split(" ") + assert len(split) == 2 + tuple_variants[split[1].strip(";")] = split[0] + elem_items += 1 + if elem_items > 1: + # We don't currently support tuple variant with more than one element + assert False + map_complex_enum(struct_name, union_enum_items[struct_name], tuple_variants, last_block_comment) last_block_comment = None elif is_unitary_enum: map_unitary_enum(struct_name, field_lines, last_block_comment) diff --git a/java_strings.py b/java_strings.py index 14c6a9f6..03a1d6e9 100644 --- a/java_strings.py +++ b/java_strings.py @@ -828,7 +828,7 @@ import java.util.Arrays; out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs)) out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n") - out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") + out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n") out_c += ("\tswitch(obj->tag) {\n") for var in variant_list: out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") @@ -836,11 +836,17 @@ import java.util.Arrays; for idx, field_map in enumerate(var.fields): if field_map.ret_conv is not None: out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) - out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + out_c += "obj->" + camel_to_snake(var.var_name) + else: + out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") c_params.append(field_map.ret_conv_name) else: - c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + c_params.append("obj->" + camel_to_snake(var.var_name)) + else: + c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n") out_c += ("\t\t}\n") out_c += ("\t\tdefault: abort();\n") diff --git a/typescript_strings.py b/typescript_strings.py index 794f9ce1..a65b52b2 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -846,7 +846,7 @@ const decodeString = (stringPointer, free = true) => { out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n") - out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") + out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n") out_c += ("\tswitch(obj->tag) {\n") for var in variant_list: out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") @@ -854,11 +854,17 @@ const decodeString = (stringPointer, free = true) => { for idx, field_map in enumerate(var.fields): if field_map.ret_conv is not None: out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) - out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + out_c += "obj->" + camel_to_snake(var.var_name) + else: + out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") c_params.append(field_map.ret_conv_name) else: - c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + c_params.append("obj->" + camel_to_snake(var.var_name)) + else: + c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n") out_c += ("\t\t}\n") out_c += ("\t\tdefault: abort();\n")