]> git.bitcoin.ninja Git - ldk-java/commitdiff
Run conversions on JNI -> Java calls, though need to fill them out
authorMatt Corallo <git@bluematt.me>
Fri, 28 Aug 2020 18:22:34 +0000 (14:22 -0400)
committerMatt Corallo <git@bluematt.me>
Fri, 28 Aug 2020 18:22:34 +0000 (14:22 -0400)
genbindings.py
src/main/jni/bindings.c

index 49d22d9589287dcc3ac991999586c0506be98412..609d448344159bffc8770e14ff0f1beaaf554c36 100755 (executable)
@@ -66,7 +66,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
         return (java_ty, c_ty, is_ptr or take_by_ptr, is_ptr, fn_arg)
 
     class TypeInfo:
-        def __init__(self, c_ty, java_ty, arg_name, arg_conv, ret_conv, arg_conv_name):
+        def __init__(self, c_ty, java_ty, arg_name, arg_conv, arg_conv_name, ret_conv, ret_conv_name):
             assert(c_ty is not None)
             assert(java_ty is not None)
             assert(arg_name is not None)
@@ -74,8 +74,9 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
             self.java_ty = java_ty
             self.arg_name = arg_name
             self.arg_conv = arg_conv
-            self.ret_conv = ret_conv
             self.arg_conv_name = arg_conv_name
+            self.ret_conv = ret_conv
+            self.ret_conv_name = ret_conv_name
 
         def print_ty(self):
             out_c.write(self.c_ty)
@@ -101,7 +102,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
         if fn_arg.startswith("void"):
             if not print_void:
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                    arg_conv = None, ret_conv = None, arg_conv_name = None)
+                    arg_conv = None, arg_conv_name = None, ret_conv = None, ret_conv_name = None)
             fn_arg = fn_arg.strip("void ")
         elif not is_ptr:
             split = fn_arg.split(" ", 2)
@@ -127,10 +128,11 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
                 arg_conv = "unsigned char " + arr_name + "_arr[" + arr_len + "];\n" +
                     "(*_env)->GetByteArrayRegion (_env, """ + arr_name + ", 0, " + arr_len + ", " + arr_name + "_arr);\n" +
                     "unsigned char (*""" + arr_name + "_ref)[" + arr_len + "] = &" + arr_name + "_arr;",
+                arg_conv_name = arr_name + "_ref",
                 ret_conv = (c_ty + " " + arr_name + "_arr = (*_env)->NewByteArray(_env, " + arr_len + ");\n" +
                     "(*_env)->SetByteArrayRegion(_env, " + arr_name + "_arr, 0, " + arr_len + ", *",
-                    ");\nreturn ret_arr;"),
-                arg_conv_name = arr_name + "_ref")
+                    ");"),
+                ret_conv_name = arr_name + "_arr")
         elif var_name != "":
             # If we have a parameter name, print it (noting that it may indicate its a pointer)
             if is_ptr_to_obj is not None:
@@ -140,31 +142,35 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
                     if is_ptr_to_obj in opaque_structs:
                         return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
                             arg_conv = base_conv + "\n" + var_name + "_conv._underlying_ref = false;",
-                            ret_conv = None, arg_conv_name = var_name + "_conv")
+                            arg_conv_name = var_name + "_conv",
+                            ret_conv = None, ret_conv_name = None)
                     return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                        arg_conv = base_conv, ret_conv = None, arg_conv_name = var_name + "_conv")
+                        arg_conv = base_conv, arg_conv_name = var_name + "_conv",
+                        ret_conv = None, ret_conv_name = None)
                 else:
                     assert(not is_free)
                     return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
                         arg_conv = is_ptr_to_obj + "* " + var_name + "_conv = (" + is_ptr_to_obj + "*)" + var_name + ";",
-                            ret_conv = None, arg_conv_name = var_name + "_conv")
+                        arg_conv_name = var_name + "_conv",
+                        ret_conv = None, ret_conv_name = None)
             elif rust_takes_ptr:
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                    arg_conv = None, ret_conv = None, arg_conv_name = var_name)
+                    arg_conv = None, arg_conv_name = var_name, ret_conv = None, ret_conv_name = None)
             else:
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                    arg_conv = None, ret_conv = None, arg_conv_name = var_name)
+                    arg_conv = None, arg_conv_name = var_name, ret_conv = None, ret_conv_name = None)
         elif not print_void:
             # We don't have a parameter name, and want one, just call it arg
             if is_ptr_to_obj is not None:
                 assert(not is_free or is_ptr_to_obj not in opaque_structs);
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
                     arg_conv = is_ptr_to_obj + " arg_conv = *(" + is_ptr_to_obj + "*)arg;\nfree((void*)arg);",
-                    ret_conv = None, arg_conv_name = "arg_conv")
+                    arg_conv_name = "arg_conv",
+                    ret_conv = None, ret_conv_name = None)
             else:
                 assert(not is_free)
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                    arg_conv = None, ret_conv = None, arg_conv_name = "arg")
+                    arg_conv = None, arg_conv_name = "arg", ret_conv = None, ret_conv_name = None)
         else:
             # We don't have a parameter name, and don't want one (cause we're returning)
             if is_ptr_to_obj is not None:
@@ -175,19 +181,21 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
                         # any _free function.
                         # To avoid any issues, we first assert that the incoming object is non-ref.
                         return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                            ret_conv = (is_ptr_to_obj + "* ret = malloc(sizeof(" + is_ptr_to_obj + "));\n*ret = ", ";\nassert(!ret->_underlying_ref);\nret->_underlying_ref = true;\nreturn (long)ret;"),
+                            ret_conv = (is_ptr_to_obj + "* ret = malloc(sizeof(" + is_ptr_to_obj + "));\n*ret = ", ";\nassert(!ret->_underlying_ref);\nret->_underlying_ref = true;"),
+                            ret_conv_name = "(long)ret",
                             arg_conv = None, arg_conv_name = None)
                     else:
                         return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                            ret_conv = (is_ptr_to_obj + "* ret = malloc(sizeof(" + is_ptr_to_obj + "));\n*ret = ", ";\nreturn (long)ret;"),
+                            ret_conv = (is_ptr_to_obj + "* ret = malloc(sizeof(" + is_ptr_to_obj + "));\n*ret = ", ";"),
+                            ret_conv_name = "(long)ret",
                             arg_conv = None, arg_conv_name = None)
                 else:
                     return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                        ret_conv = ("return (long) ", ";"),
+                        ret_conv = ("long ret = (long)", ";"), ret_conv_name = "ret",
                         arg_conv = None, arg_conv_name = None)
             else:
                 return TypeInfo(c_ty = c_ty, java_ty = java_ty, arg_name = var_name,
-                    arg_conv = None, ret_conv = None, arg_conv_name = None)
+                    arg_conv = None, arg_conv_name = None, ret_conv = None, ret_conv_name = None)
 
     def map_fn(re_match, ret_arr_len):
         out_java.write("\t/// " + line)
@@ -235,6 +243,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg
         out_c.write(")")
         if ret_info.ret_conv is not None:
             out_c.write(ret_conv_sfx.replace('\n', '\n\t'))
+            out_c.write("\n\treturn " + ret_info.ret_conv_name + ";")
         else:
             out_c.write(";")
         out_c.write("\n}\n\n")
@@ -344,16 +353,22 @@ public class bindings {
                             out_c.write(") {\n")
                             out_c.write("\t" + struct_name + "_JCalls *j_calls = (" + struct_name + "_JCalls*) this_arg;\n")
 
+                            for arg_info in arg_names:
+                                if arg_info.ret_conv is not None:
+                                    out_c.write("\t" + arg_info.ret_conv[0].replace('\n', '\n\t').replace("_env", "j_calls->env"));
+                                    out_c.write(arg_info.arg_name)
+                                    out_c.write(arg_info.ret_conv[1].replace('\n', '\n\t').replace("_env", "j_calls->env") + "\n")
+
                             if not is_ptr:
                                 out_c.write("\treturn (*j_calls->env)->Call" + java_ty.title() + "Method(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth")
                             else:
                                 out_c.write("\t" + fn_line.group(1).strip() + "* ret = (" + fn_line.group(1).strip() + "*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, j_calls->" + fn_line.group(2) + "_meth");
-                            for arg in fn_line.group(4).split(','):
-                                if arg == "":
-                                    continue
-                                (arg_java_ty, arg_c_ty, arg_is_ptr, _, arg_name) = java_c_types(arg, None)
-                                # TODO: Run conversion here!
-                                out_c.write(", " + arg_name)
+
+                            for arg_info in arg_names:
+                                if arg_info.ret_conv is not None:
+                                    out_c.write(", " + arg_info.ret_conv_name)
+                                else:
+                                    out_c.write(", " + arg_info.arg_name)
                             out_c.write(");\n");
 
                             if is_ptr:
index 57f0089c86036cdc67947ce47ce7f1b0a5769ef2..1614fef91d554523ad57eaa99d5501a0fb488375 100644 (file)
@@ -113,7 +113,9 @@ typedef struct LDKChainWatchInterface_JCalls {
 } LDKChainWatchInterface_JCalls;
 void install_watch_tx_jcall(const void* this_arg, const uint8_t (*txid)[32], LDKu8slice script_pub_key) {
        LDKChainWatchInterface_JCalls *j_calls = (LDKChainWatchInterface_JCalls*) this_arg;
-       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->install_watch_tx_meth, txid, script_pub_key);
+       jbyteArray txid_arr = (*j_calls->env)->NewByteArray(j_calls->env, 32);
+       (*j_calls->env)->SetByteArrayRegion(j_calls->env, txid_arr, 0, 32, *txid);
+       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->install_watch_tx_meth, txid_arr, script_pub_key);
 }
 void install_watch_outpoint_jcall(const void* this_arg, LDKC2Tuple_Txidu32Z outpoint, LDKu8slice out_script) {
        LDKChainWatchInterface_JCalls *j_calls = (LDKChainWatchInterface_JCalls*) this_arg;
@@ -218,11 +220,15 @@ typedef struct LDKChainListener_JCalls {
 } LDKChainListener_JCalls;
 void block_connected_jcall(const void* this_arg, const uint8_t (*header)[80], uint32_t height, LDKCVec_TransactionZ txn_matched, LDKusizeslice indexes_of_txn_matched) {
        LDKChainListener_JCalls *j_calls = (LDKChainListener_JCalls*) this_arg;
-       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->block_connected_meth, header, height, txn_matched, indexes_of_txn_matched);
+       jbyteArray header_arr = (*j_calls->env)->NewByteArray(j_calls->env, 80);
+       (*j_calls->env)->SetByteArrayRegion(j_calls->env, header_arr, 0, 80, *header);
+       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->block_connected_meth, header_arr, height, txn_matched, indexes_of_txn_matched);
 }
 void block_disconnected_jcall(const void* this_arg, const uint8_t (*header)[80], uint32_t disconnected_height) {
        LDKChainListener_JCalls *j_calls = (LDKChainListener_JCalls*) this_arg;
-       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->block_disconnected_meth, header, disconnected_height);
+       jbyteArray header_arr = (*j_calls->env)->NewByteArray(j_calls->env, 80);
+       (*j_calls->env)->SetByteArrayRegion(j_calls->env, header_arr, 0, 80, *header);
+       return (*j_calls->env)->CallVoidMethod(j_calls->env, j_calls->o, j_calls->block_disconnected_meth, header_arr, disconnected_height);
 }
 void LDKChainListener_JCalls_free(void* this_arg) {
        LDKChainListener_JCalls *j_calls = (LDKChainListener_JCalls*) this_arg;
@@ -337,7 +343,9 @@ LDKCResult_CVec_SignatureZNoneZ sign_local_commitment_htlc_transactions_jcall(co
 }
 LDKCResult_SignatureNoneZ sign_justice_transaction_jcall(const void* this_arg, LDKTransaction justice_tx, uintptr_t input, uint64_t amount, const uint8_t (*per_commitment_key)[32], const LDKHTLCOutputInCommitment *htlc) {
        LDKChannelKeys_JCalls *j_calls = (LDKChannelKeys_JCalls*) this_arg;
-       LDKCResult_SignatureNoneZ* ret = (LDKCResult_SignatureNoneZ*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, j_calls->sign_justice_transaction_meth, justice_tx, input, amount, per_commitment_key, htlc);
+       jbyteArray per_commitment_key_arr = (*j_calls->env)->NewByteArray(j_calls->env, 32);
+       (*j_calls->env)->SetByteArrayRegion(j_calls->env, per_commitment_key_arr, 0, 32, *per_commitment_key);
+       LDKCResult_SignatureNoneZ* ret = (LDKCResult_SignatureNoneZ*)(*j_calls->env)->CallLongMethod(j_calls->env, j_calls->o, j_calls->sign_justice_transaction_meth, justice_tx, input, amount, per_commitment_key_arr, htlc);
        LDKCResult_SignatureNoneZ res = *ret;
        free(ret);
        return res;
@@ -2536,7 +2544,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1free(J
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1get_1keys_1manager(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKChannelManagerReadArgs* this_ptr_conv = (LDKChannelManagerReadArgs*)this_ptr;
-       return (long) ChannelManagerReadArgs_get_keys_manager(this_ptr_conv);
+       long ret = (long)ChannelManagerReadArgs_get_keys_manager(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1keys_1manager(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -2548,7 +2557,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1k
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1get_1fee_1estimator(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKChannelManagerReadArgs* this_ptr_conv = (LDKChannelManagerReadArgs*)this_ptr;
-       return (long) ChannelManagerReadArgs_get_fee_estimator(this_ptr_conv);
+       long ret = (long)ChannelManagerReadArgs_get_fee_estimator(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1fee_1estimator(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -2560,7 +2570,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1f
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1get_1monitor(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKChannelManagerReadArgs* this_ptr_conv = (LDKChannelManagerReadArgs*)this_ptr;
-       return (long) ChannelManagerReadArgs_get_monitor(this_ptr_conv);
+       long ret = (long)ChannelManagerReadArgs_get_monitor(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1monitor(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -2572,7 +2583,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1m
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1get_1tx_1broadcaster(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKChannelManagerReadArgs* this_ptr_conv = (LDKChannelManagerReadArgs*)this_ptr;
-       return (long) ChannelManagerReadArgs_get_tx_broadcaster(this_ptr_conv);
+       long ret = (long)ChannelManagerReadArgs_get_tx_broadcaster(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1tx_1broadcaster(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -2584,7 +2596,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1t
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1get_1logger(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKChannelManagerReadArgs* this_ptr_conv = (LDKChannelManagerReadArgs*)this_ptr;
-       return (long) ChannelManagerReadArgs_get_logger(this_ptr_conv);
+       long ret = (long)ChannelManagerReadArgs_get_logger(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_ChannelManagerReadArgs_1set_1logger(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -5130,7 +5143,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_MessageHandler_1free(JNIEnv *
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_MessageHandler_1get_1chan_1handler(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKMessageHandler* this_ptr_conv = (LDKMessageHandler*)this_ptr;
-       return (long) MessageHandler_get_chan_handler(this_ptr_conv);
+       long ret = (long)MessageHandler_get_chan_handler(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_MessageHandler_1set_1chan_1handler(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {
@@ -5142,7 +5156,8 @@ JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_MessageHandler_1set_1chan_1han
 
 JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_MessageHandler_1get_1route_1handler(JNIEnv * _env, jclass _b, jlong this_ptr) {
        LDKMessageHandler* this_ptr_conv = (LDKMessageHandler*)this_ptr;
-       return (long) MessageHandler_get_route_handler(this_ptr_conv);
+       long ret = (long)MessageHandler_get_route_handler(this_ptr_conv);
+       return ret;
 }
 
 JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_MessageHandler_1set_1route_1handler(JNIEnv * _env, jclass _b, jlong this_ptr, jlong val) {