From: Matt Corallo Date: Tue, 12 Jan 2021 19:18:55 +0000 (-0500) Subject: Parse complex enum fields prior to per-language logic, drop java-isms in TS X-Git-Tag: v0.0.1~57 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=c406e9dff9112e39fe5ec21adfcf2825c415a3fc;p=ldk-java Parse complex enum fields prior to per-language logic, drop java-isms in TS --- diff --git a/bindingstypes.py b/bindingstypes.py index e79b930a..66f34a58 100644 --- a/bindingstypes.py +++ b/bindingstypes.py @@ -71,3 +71,8 @@ class TraitMethInfo: self.self_is_const = self_is_const self.ret_ty_info = ret_ty_info self.args_ty = args_ty + +class ComplexEnumVariantInfo: + def __init__(self, var_name, fields): + self.var_name = var_name + self.fields = fields diff --git a/genbindings.py b/genbindings.py index 0e20b2a2..b434969a 100755 --- a/genbindings.py +++ b/genbindings.py @@ -928,8 +928,36 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: def map_complex_enum(struct_name, union_enum_items): java_hu_type = struct_name.replace("LDK", "") complex_enums.add(struct_name) + + enum_variants = [] + tag_field_lines = union_enum_items["field_lines"] + for idx, struct_line in enumerate(tag_field_lines): + if idx == 0: + assert(struct_line == "typedef enum %s_Tag {" % struct_name) + elif idx == len(tag_field_lines) - 3: + assert(struct_line.endswith("_Sentinel,")) + elif idx == len(tag_field_lines) - 2: + assert(struct_line == "} %s_Tag;" % struct_name) + elif idx == len(tag_field_lines) - 1: + assert(struct_line == "") + else: + variant_name = struct_line.strip(' ,')[len(struct_name) + 1:] + fields = [] + if "LDK" + variant_name in union_enum_items: + enum_var_lines = union_enum_items["LDK" + variant_name] + for idx, field in enumerate(enum_var_lines): + if idx != 0 and idx < len(enum_var_lines) - 2: + fields.append(map_type(field.strip(' ;'), False, None, False, True)) + else: + # TODO: Assert line format + pass + else: + # TODO: Assert line format + pass + enum_variants.append(ComplexEnumVariantInfo(variant_name, fields)) + 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, union_enum_items, map_type, camel_to_snake) + (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake) out_java_enum.write(out_java_enum_addendum) out_java.write(out_java_addendum) diff --git a/java_strings.py b/java_strings.py index 0a20dcf1..481e47ff 100644 --- a/java_strings.py +++ b/java_strings.py @@ -444,7 +444,7 @@ import java.util.Arrays; return ("", out_c) - def map_complex_enum(self, struct_name, union_enum_items, map_type, camel_to_snake): + def map_complex_enum(self, struct_name, variant_list, camel_to_snake): java_hu_type = struct_name.replace("LDK", "") out_java_enum = "" out_java = "" @@ -462,58 +462,42 @@ import java.util.Arrays; out_java_enum += ("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n") java_hu_subclasses = "" - tag_field_lines = union_enum_items["field_lines"] init_meth_jty_strs = {} - for idx, struct_line in enumerate(tag_field_lines): - if idx == 0: - assert(struct_line == "typedef enum %s_Tag {" % struct_name) - elif idx == len(tag_field_lines) - 3: - assert(struct_line.endswith("_Sentinel,")) - elif idx == len(tag_field_lines) - 2: - assert(struct_line == "} %s_Tag;" % struct_name) - elif idx == len(tag_field_lines) - 1: - assert(struct_line == "") out_java += ("\tpublic static class " + struct_name + " {\n") out_java += ("\t\tprivate " + struct_name + "() {}\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_java += ("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n") - java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n" - out_java_enum += ("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n") - out_java_enum += ("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n") - init_meth_jty_str = "" - init_meth_params = "" - init_meth_body = "" - hu_conv_body = "" - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_ty = map_type(field.strip(' ;'), False, None, False, True) - out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") - java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" - 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" - hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" - else: - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg - if idx > 1: - init_meth_params = init_meth_params + ", " - init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name - init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " - out_java += ("\t\t\t" + var_name + "(" + init_meth_params + ") { ") - out_java += (init_meth_body) - out_java += ("}\n") - out_java += ("\t\t}\n") - out_java_enum += ("\t\t}\n") - java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n" - java_hu_subclasses = java_hu_subclasses + hu_conv_body - java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n" - init_meth_jty_strs[var_name] = init_meth_jty_str + 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" + 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 = "" + init_meth_params = "" + init_meth_body = "" + hu_conv_body = "" + for idx, field_ty in enumerate(var.fields): + out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") + java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" + 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" + hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" + hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" + else: + hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" + init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg + if idx > 0: + init_meth_params = init_meth_params + ", " + init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name + init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " + out_java += ("\t\t\t" + var.var_name + "(" + init_meth_params + ") { ") + out_java += (init_meth_body) + out_java += ("}\n") + out_java += ("\t\t}\n") + out_java_enum += ("\t\t}\n") + java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var.var_name + "(long ptr, bindings." + struct_name + "." + var.var_name + " obj) {\n\t\t\tsuper(null, ptr);\n" + java_hu_subclasses = java_hu_subclasses + hu_conv_body + java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n" + init_meth_jty_strs[var.var_name] = init_meth_jty_str out_java += ("\t\tstatic native void init();\n") out_java += ("\t}\n") out_java_enum += ("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n") @@ -521,30 +505,24 @@ import java.util.Arrays; out_java += ("\tstatic { " + struct_name + ".init(); }\n") out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); - out_c += (self.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) + 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_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n") out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") out_c += ("\tswitch(obj->tag) {\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_c += ("\t\tcase " + struct_name + "_" + var_name + ": {\n") - c_params = [] - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_map = map_type(field.strip(' ;'), False, None, False, True) - 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_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_name) + "." + field_map.arg_name) - out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") - out_c += ("\t\t}\n") + for var in variant_list: + out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") + c_params = [] + 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) + 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) + 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") out_c += ("\t}\n}\n") out_java_enum += ("}\n") diff --git a/typescript_strings.py b/typescript_strings.py index c2602f88..a46c4bd6 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -228,9 +228,6 @@ import * as bindings from '../bindings' // TODO: figure out location def native_unitary_enum_to_c_call(self, ty_info): return (ty_info.rust_obj + "_from_js(", ")") - def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs): - return "" - def c_complex_enum_pass_ty(self, struct_name): return "uint32_t" @@ -243,7 +240,7 @@ import * as bindings from '../bindings' // TODO: figure out location def native_c_map_trait(self, struct_name, field_var_convs, field_fn_lines): return ("", "") - def map_complex_enum(self, struct_name, union_enum_items, map_type, camel_to_snake): + def map_complex_enum(self, struct_name, variant_list, camel_to_snake): java_hu_type = struct_name.replace("LDK", "") out_java_enum = "" @@ -261,113 +258,61 @@ import * as bindings from '../bindings' // TODO: figure out location out_java_enum += (f"\t\tconst raw_val: bindings.{struct_name} = bindings." + struct_name + "_ref_from_ptr(ptr);\n") java_hu_subclasses = "" - tag_field_lines = union_enum_items["field_lines"] - init_meth_jty_strs = {} - for idx, struct_line in enumerate(tag_field_lines): - if idx == 0: - assert(struct_line == "typedef enum %s_Tag {" % struct_name) - elif idx == len(tag_field_lines) - 3: - assert(struct_line.endswith("_Sentinel,")) - elif idx == len(tag_field_lines) - 2: - assert(struct_line == "} %s_Tag;" % struct_name) - elif idx == len(tag_field_lines) - 1: - assert(struct_line == "") - out_java += ("\tpublic static class " + struct_name + " {\n") out_java += ("\t\tprivate " + struct_name + "() {}\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_java += ("\t\texport class " + var_name + " extends " + struct_name + " {\n") - java_hu_subclasses = java_hu_subclasses + "export class " + var_name + " extends " + java_hu_type + " {\n" - out_java_enum += ("\t\tif (raw_val instanceof bindings." + struct_name + "." + var_name + ") {\n") - out_java_enum += ("\t\t\treturn new " + var_name + "(this.ptr, raw_val);\n") - init_meth_jty_str = "" - init_meth_params = "" - init_meth_body = "" - hu_conv_body = "" - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_ty = map_type(field.strip(' ;'), False, None, False, True) - out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") - java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n" - if field_ty.to_hu_conv is not None: - hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n" - hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" - hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" - else: - hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg - if idx > 1: - init_meth_params = init_meth_params + ", " - init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name - init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " - out_java += ("\t\t\t" + var_name + "(" + init_meth_params + ") { ") - out_java += (init_meth_body) - out_java += ("}\n") - out_java += ("\t\t}\n") - out_java_enum += ("\t\t}\n") - java_hu_subclasses = java_hu_subclasses + "\tprivate constructor(ptr: number, obj: bindings." + struct_name + "." + var_name + ") {\n\t\tsuper(null, ptr);\n" - java_hu_subclasses = java_hu_subclasses + hu_conv_body - java_hu_subclasses = java_hu_subclasses + "\t}\n}\n" - init_meth_jty_strs[var_name] = init_meth_jty_str + for var in variant_list: + out_java += ("\t\texport class " + var.var_name + " extends " + struct_name + " {\n") + java_hu_subclasses = java_hu_subclasses + "export class " + var.var_name + " extends " + java_hu_type + " {\n" + out_java_enum += ("\t\tif (raw_val instanceof bindings." + struct_name + "." + var.var_name + ") {\n") + out_java_enum += ("\t\t\treturn new " + var.var_name + "(this.ptr, raw_val);\n") + init_meth_params = "" + init_meth_body = "" + hu_conv_body = "" + for idx, field_ty in enumerate(var.fields): + out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") + java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n" + if field_ty.to_hu_conv is not None: + hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n" + hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" + hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" + else: + hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" + if idx > 0: + init_meth_params = init_meth_params + ", " + init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name + init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " + out_java += ("\t\t\t" + var.var_name + "(" + init_meth_params + ") { ") + out_java += (init_meth_body) + out_java += ("}\n") + out_java += ("\t\t}\n") + out_java_enum += ("\t\t}\n") + java_hu_subclasses = java_hu_subclasses + "\tprivate constructor(ptr: number, obj: bindings." + struct_name + "." + var.var_name + ") {\n\t\tsuper(null, ptr);\n" + java_hu_subclasses = java_hu_subclasses + hu_conv_body + java_hu_subclasses = java_hu_subclasses + "\t}\n}\n" out_java += ("\t\tstatic native void init();\n") out_java += ("\t}\n") out_java_enum += ("\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t}\n\n") out_java += ("\tstatic { " + struct_name + ".init(); }\n") out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); - out_c += (self.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) - out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n") out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") out_c += ("\tswitch(obj->tag) {\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_c += ("\t\tcase " + struct_name + "_" + var_name + ": {\n") - c_params = [] - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_map = map_type(field.strip(' ;'), False, None, False, True) - 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_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_name) + "." + field_map.arg_name) - out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") - out_c += ("\t\t}\n") + for var in variant_list: + out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") + c_params = [] + 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) + 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) + 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") out_c += ("\t}\n}\n") out_java_enum += ("}\n") out_java_enum += (java_hu_subclasses) return (out_java, out_java_enum, out_c) - - - -def camel_to_snake_case(str): - res = [str[0].lower()] - for i in range(1, len(str)): - current_char = str[i] - - previous_char = None - next_char = None - if i > 0: - previous_char = str[i - 1] - if i < len(str) - 1: - next_char = str[i + 1] - - if current_char.isupper() and previous_char is not None: - if previous_char.islower() or (next_char is not None and next_char.islower()): - res.append('_') - res.append(current_char.lower()) - continue - res.append(current_char.lower()) - - return ''.join(res)