Add ability to check if a CResult<> is OK, and get its value pointer
[ldk-java] / genbindings.py
index 07de75e8edf56784477264a42e3c5d24f58bef5b..deea360c406a12bed4b4e31e263e6c5684752739 100755 (executable)
@@ -446,6 +446,10 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
         out_c.write("\treturn (long)res_ptr;\n")
         out_c.write("}\n")
 
+        out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n")
+        out_c.write("JNIEXPORT jobject JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1get_1obj_1from_1jcalls (JNIEnv * env, jclass _a, jlong val) {\n")
+        out_c.write("\treturn ((" + struct_name + "_JCalls*)val)->o;\n")
+        out_c.write("}\n")
 
 
     out_java.write("""package org.ldk.impl;
@@ -528,6 +532,7 @@ public class bindings {
     reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
     const_val_regex = re.compile("^extern const ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$")
 
+    line_indicates_result_regex = re.compile("^   bool result_ok;$")
     line_indicates_opaque_regex = re.compile("^   bool is_owned;$")
     line_indicates_trait_regex = re.compile("^   ([A-Za-z_0-9]* \*?)\(\*([A-Za-z_0-9]*)\)\((const )?void \*this_arg(.*)\);$")
     assert(line_indicates_trait_regex.match("   uintptr_t (*send_data)(void *this_arg, LDKu8slice data, bool resume_read);"))
@@ -538,7 +543,10 @@ public class bindings {
     struct_name_regex = re.compile("^typedef (struct|enum|union) (MUST_USE_STRUCT )?(LDK[A-Za-z_0-9]*) {$")
     assert(struct_name_regex.match("typedef struct LDKCVecTempl_u8 {"))
     assert(struct_name_regex.match("typedef enum LDKNetwork {"))
+    struct_alias_regex = re.compile("^typedef (LDK[A-Za-z_0-9]*) (LDK[A-Za-z_0-9]*);$")
+    assert(struct_alias_regex.match("typedef LDKCResultTempl_bool__PeerHandleError LDKCResult_boolPeerHandleErrorZ;"))
 
+    result_templ_structs = set()
     for line in in_h:
         if in_block_comment:
             #out_java.write("\t" + line)
@@ -551,6 +559,7 @@ public class bindings {
                 struct_name = None
                 obj_lines = cur_block_obj.split("\n")
                 is_opaque = False
+                is_result = False
                 is_unitary_enum = False
                 is_union_enum = False
                 is_union = False
@@ -576,6 +585,8 @@ public class bindings {
                                 is_union = True
                         if line_indicates_opaque_regex.match(struct_line):
                             is_opaque = True
+                        elif line_indicates_result_regex.match(struct_line):
+                            is_result = True
                         trait_fn_match = line_indicates_trait_regex.match(struct_line)
                         if trait_fn_match is not None:
                             trait_fn_lines.append(trait_fn_match)
@@ -592,6 +603,8 @@ public class bindings {
                 assert(not is_union or not (len(trait_fn_lines) != 0 or is_unitary_enum or is_union_enum or is_opaque))
                 if is_opaque:
                     opaque_structs.add(struct_name)
+                elif is_result:
+                    result_templ_structs.add(struct_name)
                 elif is_unitary_enum:
                     unitary_enums.add(struct_name)
                     out_c.write("static inline " + struct_name + " " + struct_name + "_from_java(JNIEnv *env, jclass val) {\n")
@@ -655,6 +668,19 @@ public class bindings {
             elif line.startswith("typedef union "):
                 cur_block_obj = line
             elif line.startswith("typedef "):
+                alias_match =  struct_alias_regex.match(line)
+                if alias_match.group(1) in result_templ_structs:
+                    out_java.write("\tpublic static native boolean " + alias_match.group(2) + "_result_ok(long arg);\n")
+                    out_java.write("\tpublic static native long " + alias_match.group(2) + "_get_inner(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_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_" + alias_match.group(2).replace("_", "_1") + "_1get_1inner (JNIEnv * env, jclass _a, jlong arg) {\n")
+                    out_c.write("\tif (((" + alias_match.group(2) + "*)arg)->result_ok) {\n")
+                    out_c.write("\t\treturn (long)((" + alias_match.group(2) + "*)arg)->contents.result;\n")
+                    out_c.write("\t} else {\n")
+                    out_c.write("\t\treturn (long)((" + alias_match.group(2) + "*)arg)->contents.err;\n")
+                    out_c.write("\t}\n}\n")
                 pass
             elif fn_ptr is not None:
                 map_fn(fn_ptr, None)