From c6ea459938480d588c78fdef1f34e8e7fa7c43dd Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 25 Oct 2020 19:06:15 -0400 Subject: [PATCH] clone traits before passing to jcalls and map to human types --- genbindings.py | 27 +++++++++++++------ .../org/ldk/structs/SocketDescriptor.java | 6 +++-- src/main/jni/bindings.c | 6 +++-- .../java/org/ldk/HumanObjectPeerTest.java | 4 +-- src/test/java/org/ldk/PeerTest.java | 4 +-- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/genbindings.py b/genbindings.py index 3f7a12ee..1ed228a5 100755 --- a/genbindings.py +++ b/genbindings.py @@ -753,7 +753,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = (ty_info.rust_obj + "* ret = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n*ret = ", ";"), ret_conv_name = "(long)ret", - to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);", + to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);", to_hu_conv_name = "ret_hu_conv", from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) if ty_info.rust_obj != "LDKu8slice": @@ -838,13 +838,24 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr & ~1", "this.ptrs_to.add(" + ty_info.var_name + ")")) elif ty_info.rust_obj in trait_structs: - return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, - arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";", - arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, - ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name, - to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, ret);\nret_hu_conv.ptrs_to.add(this);", - to_hu_conv_name = "ret_hu_conv", - from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) + if ty_info.java_hu_ty + "_clone" in clone_fns: + return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, + arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";", + arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, + ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" + + "*" + ty_info.var_name + "_clone = " + ty_info.java_hu_ty + "_clone(", ");"), + ret_conv_name = "(long)" + ty_info.var_name + "_clone", + to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);", + to_hu_conv_name = "ret_hu_conv", + from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) + else: + return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, + arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";", + arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, + ret_conv = ("long ret_" + ty_info.var_name + " = (long)", ";"), ret_conv_name = "ret_" + ty_info.var_name, + to_hu_conv = ty_info.java_hu_ty + " ret_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");\nret_hu_conv.ptrs_to.add(this);", + to_hu_conv_name = "ret_hu_conv", + from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".ptr", "this.ptrs_to.add(" + ty_info.var_name + ")")) return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name, arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";", arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, diff --git a/src/main/java/org/ldk/structs/SocketDescriptor.java b/src/main/java/org/ldk/structs/SocketDescriptor.java index f4956896..9670c9da 100644 --- a/src/main/java/org/ldk/structs/SocketDescriptor.java +++ b/src/main/java/org/ldk/structs/SocketDescriptor.java @@ -22,7 +22,7 @@ public class SocketDescriptor extends CommonBase { public static interface SocketDescriptorInterface { long send_data(byte[] data, boolean resume_read); void disconnect_socket(); - boolean eq(long other_arg); + boolean eq(SocketDescriptor other_arg); long hash(); } private static class LDKSocketDescriptorHolder { SocketDescriptor held; } @@ -37,7 +37,9 @@ public class SocketDescriptor extends CommonBase { arg.disconnect_socket(); } @Override public boolean eq(long other_arg) { - boolean ret = arg.eq(other_arg); + SocketDescriptor ret_hu_conv = new SocketDescriptor(null, other_arg); + ret_hu_conv.ptrs_to.add(this); + boolean ret = arg.eq(ret_hu_conv); return ret; } @Override public long hash() { diff --git a/src/main/jni/bindings.c b/src/main/jni/bindings.c index 93ad2e57..dd8ab081 100644 --- a/src/main/jni/bindings.c +++ b/src/main/jni/bindings.c @@ -4346,13 +4346,15 @@ void disconnect_socket_jcall(void* this_arg) { CHECK(obj != NULL); return (*_env)->CallVoidMethod(_env, obj, j_calls->disconnect_socket_meth); } -bool eq_jcall(const void* this_arg, const void *other_arg) { +bool eq_jcall(const void* this_arg, const LDKSocketDescriptor *other_arg) { LDKSocketDescriptor_JCalls *j_calls = (LDKSocketDescriptor_JCalls*) this_arg; JNIEnv *_env; DO_ASSERT((*j_calls->vm)->GetEnv(j_calls->vm, (void**)&_env, JNI_VERSION_1_8) == JNI_OK); + LDKSocketDescriptor *other_arg_clone = MALLOC(sizeof(LDKSocketDescriptor), "LDKSocketDescriptor"); + *other_arg_clone = SocketDescriptor_clone(other_arg); jobject obj = (*_env)->NewLocalRef(_env, j_calls->o); CHECK(obj != NULL); - return (*_env)->CallBooleanMethod(_env, obj, j_calls->eq_meth, other_arg); + return (*_env)->CallBooleanMethod(_env, obj, j_calls->eq_meth, (long)other_arg_clone); } uint64_t hash_jcall(const void* this_arg) { LDKSocketDescriptor_JCalls *j_calls = (LDKSocketDescriptor_JCalls*) this_arg; diff --git a/src/test/java/org/ldk/HumanObjectPeerTest.java b/src/test/java/org/ldk/HumanObjectPeerTest.java index 7993d1b9..bca3e66c 100644 --- a/src/test/java/org/ldk/HumanObjectPeerTest.java +++ b/src/test/java/org/ldk/HumanObjectPeerTest.java @@ -243,7 +243,7 @@ class HumanObjectPeerTestInstance { } @Override public void disconnect_socket() { assert false; } - @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 2; } + @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 2; } @Override public long hash() { return 2; } }); @@ -255,7 +255,7 @@ class HumanObjectPeerTestInstance { } @Override public void disconnect_socket() { assert false; } - @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 1; } + @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == 1; } @Override public long hash() { return 1; } }); diff --git a/src/test/java/org/ldk/PeerTest.java b/src/test/java/org/ldk/PeerTest.java index 0479cdca..9c82543a 100644 --- a/src/test/java/org/ldk/PeerTest.java +++ b/src/test/java/org/ldk/PeerTest.java @@ -174,7 +174,7 @@ public class PeerTest { } @Override public void disconnect_socket() { assert false; } - @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 2; } + @Override public boolean eq(long other_arg) { boolean ret = bindings.SocketDescriptor_hash(other_arg) == 2; bindings.SocketDescriptor_free(other_arg); return ret; } @Override public long hash() { return 2; } }; long descriptor2 = bindings.LDKSocketDescriptor_new(sock1); @@ -187,7 +187,7 @@ public class PeerTest { } @Override public void disconnect_socket() { assert false; } - @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 1; } + @Override public boolean eq(long other_arg) { boolean ret = bindings.SocketDescriptor_hash(other_arg) == 1; bindings.SocketDescriptor_free(other_arg); return ret; } @Override public long hash() { return 1; } }; descriptor1.val = bindings.LDKSocketDescriptor_new(sock2); -- 2.39.5