Handle tuple variants in complex enums
authorMatt Corallo <git@bluematt.me>
Tue, 30 Mar 2021 01:53:28 +0000 (21:53 -0400)
committerMatt Corallo <git@bluematt.me>
Tue, 30 Mar 2021 02:49:25 +0000 (22:49 -0400)
bindingstypes.py
genbindings.py
java_strings.py
typescript_strings.py

index bd123ac3a6f603739f9eb8d0d0ea8c1d72d514d5..2ca6c0de11712ed570eca663a8e9b111e22720ee 100644 (file)
@@ -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
index ddbc0956f0719e1180faf79fe5dc7f0842fb64ea..1a0f7dd198e0808364f6224c5e4eeb7bd1cb7f72 100755 (executable)
@@ -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)
index 14c6a9f61fb8c6387f1f688a9ea79faf75b12b72..03a1d6e9084db6987861503760677ebb5aa03cf7 100644 (file)
@@ -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")
index 794f9ce1672a8516ee72b330e3d4eca134c6b9bb..a65b52b2e0e3ea0948eb494e4ed4ba362a674f09 100644 (file)
@@ -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")