- elif line.startswith("typedef "):
- alias_match = struct_alias_regex.match(line)
- if alias_match.group(1) in tuple_types:
- tuple_types[alias_match.group(2)] = (tuple_types[alias_match.group(1)][0], alias_match.group(2))
- tuple_types[alias_match.group(1)] = (tuple_types[alias_match.group(1)][0], alias_match.group(2))
- for idx, ty_info in enumerate(tuple_types[alias_match.group(1)][0]):
- e = chr(ord('a') + idx)
- out_java.write("\tpublic static native " + ty_info.java_ty + " " + alias_match.group(2) + "_get_" + e + "(long ptr);\n")
- # XXX: Write C method!
- elif alias_match.group(1) in result_templ_structs:
- result_types.add(alias_match.group(2))
- human_ty = alias_match.group(2).replace("LDKCResult", "Result")
- with open(sys.argv[3] + "/structs/" + human_ty + ".java", "w") as out_java_struct:
- out_java_struct.write(hu_struct_file_prefix)
- out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
- out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
- out_java_struct.write("\tprotected void finalize() throws Throwable {\n")
- out_java_struct.write("\t\tif (ptr != 0) { bindings." + alias_match.group(2).replace("LDK","") + "_free(ptr); } super.finalize();\n")
- out_java_struct.write("\t}\n\n")
- out_java_struct.write("\tstatic " + human_ty + " constr_from_ptr(long ptr) {\n")
- out_java_struct.write("\t\tif (bindings." + alias_match.group(2) + "_result_ok(ptr)) {\n")
- out_java_struct.write("\t\t\treturn new " + human_ty + "_OK(null, ptr);\n")
- out_java_struct.write("\t\t} else {\n")
- out_java_struct.write("\t\t\treturn new " + human_ty + "_Err(null, ptr);\n")
- out_java_struct.write("\t\t}\n")
- out_java_struct.write("\t}\n")
-
- contents_ty = alias_match.group(1).replace("LDKCResultTempl", "LDKCResultPtr")
- res_ty, err_ty = result_ptr_struct_items[contents_ty]
- res_map = map_type(res_ty + " res", True, None, False, True)
- err_map = map_type(err_ty + " err", True, None, False, True)
-
- out_java.write("\tpublic static native boolean " + alias_match.group(2) + "_result_ok(long arg);\n")
- out_c.write("JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1result_1ok (JNIEnv * env, jclass _a, jlong arg) {\n")
- out_c.write("\treturn ((" + alias_match.group(2) + "*)arg)->result_ok;\n")
- out_c.write("}\n")
-
- out_java.write("\tpublic static native " + res_map.java_ty + " " + alias_match.group(2) + "_get_ok(long arg);\n")
- out_c.write("JNIEXPORT " + res_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1get_1ok (JNIEnv * _env, jclass _a, jlong arg) {\n")
- out_c.write("\t" + alias_match.group(2) + " *val = (" + alias_match.group(2) + "*)arg;\n")
- out_c.write("\tCHECK(val->result_ok);\n\t")
- out_java_struct.write("\tpublic static final class " + human_ty + "_OK extends " + human_ty + " {\n")
- if res_map.ret_conv is not None:
- out_c.write(res_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.result)")
- out_c.write(res_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + res_map.ret_conv_name)
- else:
- out_c.write("return *val->contents.result")
- out_c.write(";\n}\n")
-
- out_java_struct.write("\t\tpublic final " + res_map.java_hu_ty + " res;\n")
- out_java_struct.write("\t\tprivate " + human_ty + "_OK(Object _dummy, long ptr) {\n")
- out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
- if res_map.to_hu_conv is not None:
- out_java_struct.write("\t\t\t" + res_map.java_ty + " res = bindings." + alias_match.group(2) + "_get_ok(ptr);\n")
- out_java_struct.write("\t\t\t" + res_map.to_hu_conv.replace("\n", "\n\t\t\t"))
- out_java_struct.write("\n\t\t\tthis.res = " + res_map.to_hu_conv_name + ";\n")
- else:
- out_java_struct.write("\t\t\tthis.res = bindings." + alias_match.group(2) + "_get_ok(ptr);\n")
- out_java_struct.write("\t\t}\n")
- if alias_match.group(2).startswith("LDKCResult_None"):
- out_java_struct.write("\t\tpublic " + human_ty + "_OK() {\n\t\t\tthis(null, bindings.C" + human_ty + "_ok());\n")
- else:
- out_java_struct.write("\t\tpublic " + human_ty + "_OK(" + res_map.java_hu_ty + " res) {\n")
- if res_map.from_hu_conv is not None:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(" + res_map.from_hu_conv[0] + "));\n")
- if res_map.from_hu_conv[1] != "":
- out_java_struct.write("\t\t\t" + res_map.from_hu_conv[1] + ";\n")
- else:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_ok(res));\n")
- out_java_struct.write("\t\t}\n\t}\n\n")
-
- out_java.write("\tpublic static native " + err_map.java_ty + " " + alias_match.group(2) + "_get_err(long arg);\n")
- out_c.write("JNIEXPORT " + err_map.c_ty + " JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1get_1err (JNIEnv * _env, jclass _a, jlong arg) {\n")
- out_c.write("\t" + alias_match.group(2) + " *val = (" + alias_match.group(2) + "*)arg;\n")
- out_c.write("\tCHECK(!val->result_ok);\n\t")
- out_java_struct.write("\tpublic static final class " + human_ty + "_Err extends " + human_ty + " {\n")
- if err_map.ret_conv is not None:
- out_c.write(err_map.ret_conv[0].replace("\n", "\n\t") + "(*val->contents.err)")
- out_c.write(err_map.ret_conv[1].replace("\n", "\n\t") + "\n\treturn " + err_map.ret_conv_name)
- else:
- out_c.write("return *val->contents.err")
- out_c.write(";\n}\n")
-
- out_java_struct.write("\t\tpublic final " + err_map.java_hu_ty + " err;\n")
- out_java_struct.write("\t\tprivate " + human_ty + "_Err(Object _dummy, long ptr) {\n")
- out_java_struct.write("\t\t\tsuper(_dummy, ptr);\n")
- if err_map.to_hu_conv is not None:
- out_java_struct.write("\t\t\t" + err_map.java_ty + " err = bindings." + alias_match.group(2) + "_get_err(ptr);\n")
- out_java_struct.write("\t\t\t" + err_map.to_hu_conv.replace("\n", "\n\t\t\t"))
- out_java_struct.write("\n\t\t\tthis.err = " + err_map.to_hu_conv_name + ";\n")
- else:
- out_java_struct.write("\t\t\tthis.err = bindings." + alias_match.group(2) + "_get_err(ptr);\n")
- out_java_struct.write("\t\t}\n")
-
- if alias_match.group(2).endswith("NoneZ"):
- out_java_struct.write("\t\tpublic " + human_ty + "_Err() {\n\t\t\tthis(null, bindings.C" + human_ty + "_err());\n")
- else:
- out_java_struct.write("\t\tpublic " + human_ty + "_Err(" + err_map.java_hu_ty + " err) {\n")
- if err_map.from_hu_conv is not None:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(" + err_map.from_hu_conv[0] + "));\n")
- if err_map.from_hu_conv[1] != "":
- out_java_struct.write("\t\t\t" + err_map.from_hu_conv[1] + ";\n")
- else:
- out_java_struct.write("\t\t\tthis(null, bindings.C" + human_ty + "_err(err));\n")
- out_java_struct.write("\t\t}\n\t}\n}\n")