Skip to content

Commit c45b91b

Browse files
authored
Merge pull request #14 from TheBlueMatt/2021-03-fix-enums
Handle tuple variants in complex enums
2 parents ad6b4ed + 963dba5 commit c45b91b

File tree

217 files changed

+7137
-1443
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+7137
-1443
lines changed

bindingstypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, fn_name, self_is_const, ret_ty_info, args_ty, docs):
7575
self.docs = docs
7676

7777
class ComplexEnumVariantInfo:
78-
def __init__(self, var_name, fields):
78+
def __init__(self, var_name, fields, tuple_variant):
7979
self.var_name = var_name
8080
self.fields = fields
81+
self.tuple_variant = tuple_variant

genbindings.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def java_c_types(fn_arg, ret_arr_len):
299299
else:
300300
c_ty = consts.ptr_c_ty
301301
java_ty = consts.ptr_native_ty
302-
java_hu_ty = ma.group(1).strip().replace("LDKCResult", "Result").replace("LDK", "")
302+
java_hu_ty = ma.group(1).strip().replace("LDKCOption", "Option").replace("LDKCResult", "Result").replace("LDK", "")
303303
fn_ty_arg = "J"
304304
fn_arg = ma.group(2).strip()
305305
rust_obj = ma.group(1).strip()
@@ -381,7 +381,10 @@ def map_fn(line, re_match, ret_arr_len, c_call_string, doc_comment):
381381
method_arguments = method_comma_separated_arguments.split(',')
382382

383383
is_free = method_name.endswith("_free")
384-
struct_meth = method_name.split("_")[0]
384+
if method_name.startswith("COption") or method_name.startswith("CResult"):
385+
struct_meth = method_name.rsplit("Z", 1)[0][1:] + "Z"
386+
else:
387+
struct_meth = method_name.split("_")[0]
385388

386389
return_type_info = type_mapping_generator.map_type(method_return_type, True, ret_arr_len, False, False)
387390

@@ -437,7 +440,11 @@ def map_fn(line, re_match, ret_arr_len, c_call_string, doc_comment):
437440
write_c(out_c_delta)
438441

439442
out_java_struct = None
440-
if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
443+
expected_struct = "LDK" + struct_meth
444+
expected_cstruct = "LDKC" + struct_meth
445+
if (expected_struct in opaque_structs or expected_struct in trait_structs
446+
or expected_struct in complex_enums or expected_cstruct in complex_enums
447+
or expected_cstruct in result_types) and not is_free:
441448
out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
442449
elif method_name.startswith("C2Tuple_") and method_name.endswith("_read"):
443450
struct_meth = method_name.rsplit("_", 1)[0]
@@ -462,8 +469,8 @@ def map_unitary_enum(struct_name, field_lines, enum_doc_comment):
462469
out_java_enum.write(native_file_out)
463470
out_java.write(native_out)
464471

465-
def map_complex_enum(struct_name, union_enum_items, enum_doc_comment):
466-
java_hu_type = struct_name.replace("LDK", "")
472+
def map_complex_enum(struct_name, union_enum_items, inline_enum_variants, enum_doc_comment):
473+
java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
467474
complex_enums.add(struct_name)
468475

469476
enum_variants = []
@@ -485,13 +492,12 @@ def map_complex_enum(struct_name, union_enum_items, enum_doc_comment):
485492
for idx, field in enumerate(enum_var_lines):
486493
if idx != 0 and idx < len(enum_var_lines) - 2:
487494
fields.append(type_mapping_generator.map_type(field.strip(' ;'), False, None, False, True))
488-
else:
489-
# TODO: Assert line format
490-
pass
495+
enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False))
496+
elif camel_to_snake(variant_name) in inline_enum_variants:
497+
fields.append(type_mapping_generator.map_type(inline_enum_variants[camel_to_snake(variant_name)] + " " + camel_to_snake(variant_name), False, None, False, True))
498+
enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
491499
else:
492-
# TODO: Assert line format
493-
pass
494-
enum_variants.append(ComplexEnumVariantInfo(variant_name, fields))
500+
enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
495501

496502
with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum:
497503
(out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake, enum_doc_comment)
@@ -600,17 +606,7 @@ def map_result(struct_name, res_ty, err_ty):
600606
else:
601607
out_java_struct.write("\t\t\tthis.res = bindings." + struct_name + "_get_ok(ptr);\n")
602608
out_java_struct.write("\t\t}\n")
603-
if struct_name.startswith("LDKCResult_None"):
604-
out_java_struct.write("\t\tpublic " + human_ty + "_OK() {\n\t\t\tthis(null, bindings.C" + human_ty + "_ok());\n")
605-
else:
606-
out_java_struct.write("\t\tpublic " + human_ty + "_OK(" + res_map.java_hu_ty + " res) {\n")
607-
if res_map.from_hu_conv is not None:
608-
out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n")
609-
if res_map.from_hu_conv[1] != "":
610-
out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n")
611-
else:
612-
out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n")
613-
out_java_struct.write("\t\t}\n\t}\n\n")
609+
out_java_struct.write("\t}\n\n")
614610

615611
out_java.write("\tpublic static native " + err_map.java_ty + " " + struct_name + "_get_err(long arg);\n")
616612
write_c(consts.c_fn_ty_pfx + err_map.c_ty + " " + consts.c_fn_name_define_pfx(struct_name + "_get_err", True) + consts.ptr_c_ty + " arg) {\n")
@@ -638,17 +634,7 @@ def map_result(struct_name, res_ty, err_ty):
638634
out_java_struct.write("\t\t\tthis.err = bindings." + struct_name + "_get_err(ptr);\n")
639635
out_java_struct.write("\t\t}\n")
640636

641-
if struct_name.endswith("NoneZ"):
642-
out_java_struct.write("\t\tpublic " + human_ty + "_Err() {\n\t\t\tthis(null, bindings.C" + human_ty + "_err());\n")
643-
else:
644-
out_java_struct.write("\t\tpublic " + human_ty + "_Err(" + err_map.java_hu_ty + " err) {\n")
645-
if err_map.from_hu_conv is not None:
646-
out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n")
647-
if err_map.from_hu_conv[1] != "":
648-
out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1].replace("\n", "\n\t\t\t") + ";\n")
649-
else:
650-
out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n")
651-
out_java_struct.write("\t\t}\n\t}\n}\n")
637+
out_java_struct.write("\t}\n\n")
652638

653639
def map_tuple(struct_name, field_lines):
654640
out_java.write("\tpublic static native long " + struct_name + "_new(")
@@ -864,7 +850,25 @@ def map_tuple(struct_name, field_lines):
864850
enum_var_name = struct_name.split("_")
865851
union_enum_items[enum_var_name[0]][enum_var_name[1]] = field_lines
866852
elif struct_name in union_enum_items:
867-
map_complex_enum(struct_name, union_enum_items[struct_name], last_block_comment)
853+
tuple_variants = {}
854+
elem_items = -1
855+
for line in field_lines:
856+
if line == " struct {":
857+
elem_items = 0
858+
elif line == " };":
859+
elem_items = -1
860+
elif elem_items > -1:
861+
line = line.strip()
862+
if line.startswith("struct "):
863+
line = line[7:]
864+
split = line.split(" ")
865+
assert len(split) == 2
866+
tuple_variants[split[1].strip(";")] = split[0]
867+
elem_items += 1
868+
if elem_items > 1:
869+
# We don't currently support tuple variant with more than one element
870+
assert False
871+
map_complex_enum(struct_name, union_enum_items[struct_name], tuple_variants, last_block_comment)
868872
last_block_comment = None
869873
elif is_unitary_enum:
870874
map_unitary_enum(struct_name, field_lines, last_block_comment)
@@ -927,6 +931,12 @@ def map_tuple(struct_name, field_lines):
927931
for struct_name in trait_structs:
928932
with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct:
929933
out_java_struct.write("}\n")
934+
for struct_name in complex_enums:
935+
with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '').replace('COption', 'Option')}{consts.file_ext}", "a") as out_java_struct:
936+
out_java_struct.write("}\n")
937+
for struct_name in result_types:
938+
with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDKCResult', 'Result')}{consts.file_ext}", "a") as out_java_struct:
939+
out_java_struct.write("}\n")
930940

931941
with open(sys.argv[4], "w") as out_c:
932942
out_c.write(consts.c_file_pfx)

java_strings.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def trait_struct_inc_refcnt(self, ty_info):
763763
return base_conv
764764

765765
def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment):
766-
java_hu_type = struct_name.replace("LDK", "")
766+
java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
767767
out_java_enum = ""
768768
out_java = ""
769769
out_c = ""
@@ -776,7 +776,7 @@ def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_c
776776
out_java_enum += ("\t@Override @SuppressWarnings(\"deprecation\")\n")
777777
out_java_enum += ("\tprotected void finalize() throws Throwable {\n")
778778
out_java_enum += ("\t\tsuper.finalize();\n")
779-
out_java_enum += ("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n")
779+
out_java_enum += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK", "") + "_free(ptr); }\n")
780780
out_java_enum += ("\t}\n")
781781
out_java_enum += ("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n")
782782
out_java_enum += ("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
@@ -828,24 +828,29 @@ def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_c
828828
out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs))
829829

830830
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")
831-
out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
831+
out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n")
832832
out_c += ("\tswitch(obj->tag) {\n")
833833
for var in variant_list:
834834
out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
835835
c_params = []
836836
for idx, field_map in enumerate(var.fields):
837837
if field_map.ret_conv is not None:
838838
out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
839-
out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
839+
if var.tuple_variant:
840+
out_c += "obj->" + camel_to_snake(var.var_name)
841+
else:
842+
out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
840843
out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
841844
c_params.append(field_map.ret_conv_name)
842845
else:
843-
c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
846+
if var.tuple_variant:
847+
c_params.append("obj->" + camel_to_snake(var.var_name))
848+
else:
849+
c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
844850
out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
845851
out_c += ("\t\t}\n")
846852
out_c += ("\t\tdefault: abort();\n")
847853
out_c += ("\t}\n}\n")
848-
out_java_enum += ("}\n")
849854
return (out_java, out_java_enum, out_c)
850855

851856
def map_opaque_struct(self, struct_name, struct_doc_comment):
@@ -896,7 +901,7 @@ def map_function(self, argument_types, c_call_string, method_name, return_type_i
896901
if not args_known:
897902
out_java_struct += ("\t// Skipped " + method_name + "\n")
898903
else:
899-
meth_n = method_name[len(struct_meth) + 1:]
904+
meth_n = method_name[len(struct_meth) + 1:].strip("_")
900905
if doc_comment is not None:
901906
out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n"
902907
if not takes_self:

liblightningjni_debug.so

361 KB
Binary file not shown.

liblightningjni_release.so

57 KB
Binary file not shown.

src/main/java/org/ldk/enums/LDKNetwork.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.ldk.enums;
22

3+
/**
4+
* An enum representing the possible Bitcoin or test networks which we can run on
5+
*/
36
public enum LDKNetwork {
47
LDKNetwork_Bitcoin,
58
LDKNetwork_Testnet,

src/main/java/org/ldk/enums/LDKSecp256k1Error.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.ldk.enums;
22

3+
/**
4+
* Represents an error returned from libsecp256k1 during validation of some secp256k1 data
5+
*/
36
public enum LDKSecp256k1Error {
47
LDKSecp256k1Error_IncorrectSignature,
58
LDKSecp256k1Error_InvalidMessage,

0 commit comments

Comments
 (0)