X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.py;h=a03fa4c4d2baa4751ecc34af81592bb6a8c96dfd;hb=5f5d273d63baca66e94d6f34f485fc2398ee12f7;hp=12649658afe476ce32405a95c0d44f139f931cda;hpb=a9a6f1747f8466e99abd5972bf675d310f6d7222;p=ldk-java diff --git a/genbindings.py b/genbindings.py index 12649658..a03fa4c4 100755 --- a/genbindings.py +++ b/genbindings.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -import sys, re +import sys, re, subprocess if len(sys.argv) < 7: print("USAGE: /path/to/lightning.h /path/to/bindings/output /path/to/bindings/ /path/to/bindings/output.c debug lang") @@ -33,6 +33,8 @@ else: consts = Consts(DEBUG, target=target) +local_git_version = subprocess.check_output(["git", "describe", '--tag', '--dirty']).decode("utf-8").strip() + from bindingstypes import * c_file = "" @@ -69,7 +71,7 @@ def camel_to_snake(s): unitary_enums = set() complex_enums = set() opaque_structs = set() -trait_structs = set() +trait_structs = {} result_types = set() tuple_types = {} @@ -101,6 +103,11 @@ def java_c_types(fn_arg, ret_arr_len): assert var_is_arr_regex.match(fn_arg[8:]) rust_obj = "LDKThirtyTwoBytes" arr_access = "data" + elif fn_arg.startswith("LDKTxid"): + fn_arg = "uint8_t (*" + fn_arg[8:] + ")[32]" + assert var_is_arr_regex.match(fn_arg[8:]) + rust_obj = "LDKThirtyTwoBytes" + arr_access = "data" elif fn_arg.startswith("LDKPublicKey"): fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]" assert var_is_arr_regex.match(fn_arg[8:]) @@ -116,6 +123,11 @@ def java_c_types(fn_arg, ret_arr_len): assert var_is_arr_regex.match(fn_arg[8:]) rust_obj = "LDKSignature" arr_access = "compact_form" + elif fn_arg.startswith("LDKRecoverableSignature"): + fn_arg = "uint8_t (*" + fn_arg[25:] + ")[68]" + assert var_is_arr_regex.match(fn_arg[8:]) + rust_obj = "LDKRecoverableSignature" + arr_access = "serialized_form" elif fn_arg.startswith("LDKThreeBytes"): fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]" assert var_is_arr_regex.match(fn_arg[8:]) @@ -131,6 +143,11 @@ def java_c_types(fn_arg, ret_arr_len): assert var_is_arr_regex.match(fn_arg[8:]) rust_obj = "LDKSixteenBytes" arr_access = "data" + elif fn_arg.startswith("LDKTwentyBytes"): + fn_arg = "uint8_t (*" + fn_arg[15:] + ")[20]" + assert var_is_arr_regex.match(fn_arg[8:]) + rust_obj = "LDKTwentyBytes" + arr_access = "data" elif fn_arg.startswith("LDKTenBytes"): fn_arg = "uint8_t (*" + fn_arg[12:] + ")[10]" assert var_is_arr_regex.match(fn_arg[8:]) @@ -146,7 +163,7 @@ def java_c_types(fn_arg, ret_arr_len): rust_obj = "LDKCVec_u8Z" assert var_is_arr_regex.match(fn_arg[8:]) arr_access = "data" - elif fn_arg.startswith("LDKTransaction"): + elif fn_arg.startswith("LDKTransaction ") or fn_arg == "LDKTransaction": fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]" rust_obj = "LDKTransaction" assert var_is_arr_regex.match(fn_arg[8:]) @@ -205,6 +222,13 @@ def java_c_types(fn_arg, ret_arr_len): fn_ty_arg = "B" fn_arg = fn_arg[7:].strip() is_primitive = True + elif fn_arg.startswith("LDKu5"): + java_ty = consts.c_type_map['uint8_t'][0] + java_hu_ty = "UInt5" + rust_obj = "LDKu5" + c_ty = "int8_t" + fn_ty_arg = "B" + fn_arg = fn_arg[6:].strip() elif fn_arg.startswith("uint16_t"): mapped_type = consts.c_type_map['uint16_t'] java_ty = mapped_type[0] @@ -238,6 +262,7 @@ def java_c_types(fn_arg, ret_arr_len): fn_ty_arg = "Ljava/lang/String;" fn_arg = fn_arg[6:].strip() elif fn_arg.startswith("LDKStr"): + rust_obj = "LDKStr" java_ty = "String" c_ty = "jstring" fn_ty_arg = "Ljava/lang/String;" @@ -386,17 +411,20 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: else: struct_meth = method_name.split("_")[0] - return_type_info = type_mapping_generator.map_type(method_return_type, True, ret_arr_len, False, False) + return_type_info = type_mapping_generator.map_type(method_return_type.strip() + " ret", True, ret_arr_len, False, False) argument_types = [] default_constructor_args = {} takes_self = False + takes_self_ptr = False args_known = True for argument_index, argument in enumerate(method_arguments): argument_conversion_info = type_mapping_generator.map_type(argument, False, None, is_free, True) if argument_index == 0 and argument_conversion_info.java_hu_ty == struct_meth: takes_self = True + if argument_conversion_info.ty_info.is_ptr: + takes_self_ptr = True if argument_conversion_info.arg_conv is not None and "Warning" in argument_conversion_info.arg_conv: if argument_conversion_info.rust_obj in constructor_fns: assert not is_free @@ -414,7 +442,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java.write("\t// " + line) (out_java_delta, out_c_delta, out_java_struct_delta) = \ - consts.map_function(argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator, doc_comment) + consts.map_function(argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, takes_self_ptr, args_known, type_mapping_generator, doc_comment) out_java.write(out_java_delta) if is_free: @@ -446,8 +474,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: or expected_struct in complex_enums or expected_cstruct in complex_enums or expected_cstruct in result_types) and not is_free: out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a") - elif method_name.startswith("C2Tuple_") and method_name.endswith("_read"): - struct_meth = method_name.rsplit("_", 1)[0] + elif (method_name.startswith("C2Tuple_") and method_name.endswith("_read")) or \ + (return_type_info.rust_obj is not None and "Result" in return_type_info.rust_obj and "from" in method_name): out_java_struct = open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") if out_java_struct is not None: out_java_struct.write(out_java_struct_delta) @@ -490,7 +518,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: 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: + if idx != 0 and idx < len(enum_var_lines) - 2 and field.strip() != "": fields.append(type_mapping_generator.map_type(field.strip(' ;'), False, None, False, True)) enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False)) elif camel_to_snake(variant_name) in inline_enum_variants: @@ -509,16 +537,21 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: def map_trait(struct_name, field_var_lines, trait_fn_lines, trait_doc_comment): with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait: field_var_convs = [] + flattened_field_var_convs = [] for var_line in field_var_lines: if var_line.group(1) in trait_structs: - field_var_convs.append((var_line.group(1), var_line.group(2))) + field_var_convs.append((var_line.group(1), var_line.group(2), trait_structs[var_line.group(1)])) + flattened_field_var_convs.append((var_line.group(1), var_line.group(2), )) + flattened_field_var_convs.extend(trait_structs[var_line.group(1)]) else: - field_var_convs.append( - type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False)) + mapped = type_mapping_generator.map_type(var_line.group(1) + " " + var_line.group(2), False, None, False, False) + field_var_convs.append(mapped) + flattened_field_var_convs.append(mapped) + trait_structs[struct_name] = field_var_convs field_fns = [] for fn_docs, fn_line in trait_fn_lines: - ret_ty_info = type_mapping_generator.map_type(fn_line.group(2), True, None, False, False) + ret_ty_info = type_mapping_generator.map_type(fn_line.group(2).strip() + " ret", True, None, False, False) is_const = fn_line.group(4) is not None arg_tys = [] @@ -529,7 +562,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_tys.append(arg_conv_info) field_fns.append(TraitMethInfo(fn_line.group(3), is_const, ret_ty_info, arg_tys, fn_docs)) - (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, field_fns, trait_doc_comment) + (out_java_addendum, out_java_trait_addendum, out_c_addendum) = consts.native_c_map_trait(struct_name, field_var_convs, flattened_field_var_convs, field_fns, trait_doc_comment) write_c(out_c_addendum) out_java_trait.write(out_java_trait_addendum) out_java.write(out_java_addendum) @@ -665,7 +698,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: write_c("\tret->" + e + " = " + e + ";\n") if ty_info.arg_conv_cleanup is not None: write_c("\t//TODO: Really need to call " + ty_info.arg_conv_cleanup + " here\n") - write_c("\treturn (long)ret;\n") + write_c("\treturn (uint64_t)ret;\n") write_c("}\n") for idx, ty_info in enumerate(ty_list): @@ -681,7 +714,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: write_c("\treturn tuple->" + e + ";\n") write_c("}\n") - out_java.write(consts.bindings_header) + out_java.write(consts.bindings_header.replace('', local_git_version)) with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.common_base) @@ -825,7 +858,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: if cleanup is not None: write_c("\t\t" + cleanup + ";\n") write_c("\t}\n") - write_c("\treturn (long)ret;\n") + write_c("\treturn (uint64_t)ret;\n") write_c("}\n") if ty_info.is_native_primitive: @@ -861,6 +894,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: line = line.strip() if line.startswith("struct "): line = line[7:] + elif line.startswith("enum "): + line = line[5:] split = line.split(" ") assert len(split) == 2 tuple_variants[split[1].strip(";")] = split[0] @@ -874,7 +909,6 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: map_unitary_enum(struct_name, field_lines, last_block_comment) last_block_comment = None elif len(trait_fn_lines) > 0: - trait_structs.add(struct_name) map_trait(struct_name, field_var_lines, trait_fn_lines, last_block_comment) elif struct_name == "LDKTxOut": with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct: @@ -939,7 +973,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: out_java_struct.write("}\n") with open(sys.argv[4], "w") as out_c: - out_c.write(consts.c_file_pfx) + out_c.write(consts.c_file_pfx.replace('', local_git_version)) out_c.write(consts.init_str()) out_c.write(c_file) with open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") as util: