- out_c.write("\t\t.clone = " + struct_name + "_JCalls_clone,\n")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_c.write("\t\t." + var_line.group(2) + " = " + var_line.group(1) + "_init(env, _a, " + var_line.group(2) + "),\n")
- out_c.write("\t};\n")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_c.write("\tcalls->" + var_line.group(2) + " = ret." + var_line.group(2) + ".this_arg;\n")
- out_c.write("\treturn ret;\n")
- out_c.write("}\n")
-
- out_c.write("JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_" + struct_name.replace("_", "_1") + "_1new (JNIEnv * env, jclass _a, jobject o")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_c.write(", jobject " + var_line.group(2))
- out_c.write(") {\n")
- out_c.write("\t" + struct_name + " *res_ptr = MALLOC(sizeof(" + struct_name + "), \"" + struct_name + "\");\n")
- out_c.write("\t*res_ptr = " + struct_name + "_init(env, _a, o")
- for var_line in field_var_lines:
- if var_line.group(1) in trait_structs:
- out_c.write(", " + var_line.group(2))
- out_c.write(");\n")
- 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")
-
- for fn_line in trait_fn_lines:
- # For now, just disable enabling the _call_log - we don't know how to inverse-map String
- is_log = fn_line.group(2) == "log" and struct_name == "LDKLogger"
- if fn_line.group(2) != "free" and fn_line.group(2) != "clone" and fn_line.group(2) != "eq" and not is_log:
- dummy_line = fn_line.group(1) + struct_name + "_call_" + fn_line.group(2) + " " + struct_name + "* arg" + fn_line.group(4) + "\n"
- map_fn(dummy_line, re.compile("([A-Za-z_0-9]*) *([A-Za-z_0-9]*) *(.*)").match(dummy_line), None, "(arg_conv->" + fn_line.group(2) + ")(arg_conv->this_arg")
-
- out_c.write("""#include \"org_ldk_impl_bindings.h\"
-#include <rust_types.h>
-#include <lightning.h>
-#include <string.h>
-#include <stdatomic.h>
-""")
-
- if sys.argv[4] == "false":
- out_c.write("#define MALLOC(a, _) malloc(a)\n")
- out_c.write("#define FREE free\n")
- out_c.write("#define DO_ASSERT(a) (void)(a)\n")
- else:
- out_c.write("""#include <assert.h>
-#define DO_ASSERT(a) do { bool _assert_val = (a); assert(_assert_val); } while(0)
-
-#include <threads.h>
-static mtx_t allocation_mtx;
-
-void __attribute__((constructor)) init_mtx() {
- DO_ASSERT(mtx_init(&allocation_mtx, mtx_plain) == thrd_success);
-}
-
-typedef struct allocation {
- struct allocation* next;
- void* ptr;
- const char* struct_name;
-} allocation;
-static allocation* allocation_ll = NULL;
-
-void* MALLOC(size_t len, const char* struct_name) {
- void* res = malloc(len);
- allocation* new_alloc = malloc(sizeof(allocation));
- new_alloc->ptr = res;
- new_alloc->struct_name = struct_name;
- DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
- new_alloc->next = allocation_ll;
- allocation_ll = new_alloc;
- DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
- return res;
-}
-
-void FREE(void* ptr) {
- allocation* p = NULL;
- DO_ASSERT(mtx_lock(&allocation_mtx) == thrd_success);
- allocation* it = allocation_ll;
- while (it->ptr != ptr) { p = it; it = it->next; }
- if (p) { p->next = it->next; } else { allocation_ll = it->next; }
- DO_ASSERT(mtx_unlock(&allocation_mtx) == thrd_success);
- DO_ASSERT(it->ptr == ptr);
- free(it);
- free(ptr);
-}
-
-void __attribute__((destructor)) check_leaks() {
- for (allocation* a = allocation_ll; a != NULL; a = a->next) { fprintf(stderr, "%s %p remains\\n", a->struct_name, a->ptr); }
- DO_ASSERT(allocation_ll == NULL);
-}
-""")
- out_java.write("""package org.ldk.impl;
-
-public class bindings {
- public static class VecOrSliceDef {
- public long dataptr;
- public long datalen;
- public long stride;
- public VecOrSliceDef(long dataptr, long datalen, long stride) {
- this.dataptr = dataptr; this.datalen = datalen; this.stride = stride;
- }
- }
- static {
- System.loadLibrary(\"lightningjni\");
- init(java.lang.Enum.class, VecOrSliceDef.class);
- }
- static native void init(java.lang.Class c, java.lang.Class slicedef);
-
- public static native boolean deref_bool(long ptr);
- public static native long deref_long(long ptr);
- public static native void free_heap_ptr(long ptr);
- public static native byte[] get_u8_slice_bytes(long slice_ptr);
- public static native long bytes_to_u8_vec(byte[] bytes);
- public static native long vec_slice_len(long vec);
- public static native long new_empty_slice_vec();
-
-""")
- out_c.write("""
-jmethodID ordinal_meth = NULL;
-jmethodID slicedef_meth = NULL;
-jclass slicedef_cls = NULL;
-JNIEXPORT void Java_org_ldk_impl_bindings_init(JNIEnv * env, jclass _b, jclass enum_class, jclass slicedef_class) {
- ordinal_meth = (*env)->GetMethodID(env, enum_class, "ordinal", "()I");
- DO_ASSERT(ordinal_meth != NULL);
- slicedef_meth = (*env)->GetMethodID(env, slicedef_class, "<init>", "(JJJ)V");
- DO_ASSERT(slicedef_meth != NULL);
- slicedef_cls = (*env)->NewGlobalRef(env, slicedef_class);
- DO_ASSERT(slicedef_cls != NULL);
-}
-
-JNIEXPORT jboolean JNICALL Java_org_ldk_impl_bindings_deref_1bool (JNIEnv * env, jclass _a, jlong ptr) {
- return *((bool*)ptr);
-}
-JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_deref_1long (JNIEnv * env, jclass _a, jlong ptr) {
- return *((long*)ptr);
-}
-JNIEXPORT void JNICALL Java_org_ldk_impl_bindings_free_1heap_1ptr (JNIEnv * env, jclass _a, jlong ptr) {
- FREE((void*)ptr);
-}
-JNIEXPORT jbyteArray JNICALL Java_org_ldk_impl_bindings_get_1u8_1slice_1bytes (JNIEnv * _env, jclass _b, jlong slice_ptr) {
- LDKu8slice *slice = (LDKu8slice*)slice_ptr;
- jbyteArray ret_arr = (*_env)->NewByteArray(_env, slice->datalen);
- (*_env)->SetByteArrayRegion(_env, ret_arr, 0, slice->datalen, slice->data);
- return ret_arr;
-}
-JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec (JNIEnv * _env, jclass _b, jbyteArray bytes) {
- LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "LDKCVec_u8");
- vec->datalen = (*_env)->GetArrayLength(_env, bytes);
- vec->data = (uint8_t*)malloc(vec->datalen); // May be freed by rust, so don't track allocation
- (*_env)->GetByteArrayRegion (_env, bytes, 0, vec->datalen, vec->data);
- return (long)vec;
-}
-JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env, jclass _a, jlong ptr) {
- // Check offsets of a few Vec types are all consistent as we're meant to be generic across types
- _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_SignatureZ, datalen), "Vec<*> needs to be mapped identically");
- _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_MessageSendEventZ, datalen), "Vec<*> needs to be mapped identically");
- _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_EventZ, datalen), "Vec<*> needs to be mapped identically");
- _Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKCVec_C2Tuple_usizeTransactionZZ, datalen), "Vec<*> needs to be mapped identically");
- LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr;
- return (long)vec->datalen;
-}
-JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * _env, jclass _b) {
- // Check sizes of a few Vec types are all consistent as we're meant to be generic across types
- _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically");
- _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically");
- _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically");
- _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically");
- LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec");
- vec->data = NULL;
- vec->datalen = 0;
- return (long)vec;
-}
-
-// We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed)
-_Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec<u8> and [u8] need to have been mapped identically");
-_Static_assert(offsetof(LDKCVec_u8Z, data) == offsetof(LDKu8slice, data), "Vec<u8> and [u8] need to have been mapped identically");
-_Static_assert(offsetof(LDKCVec_u8Z, datalen) == offsetof(LDKu8slice, datalen), "Vec<u8> and [u8] need to have been mapped identically");
-
-""")
-
- # XXX: Temporarily write out a manual SecretKey_new() for testing, we should auto-gen this kind of thing
- out_java.write("\tpublic static native long LDKSecretKey_new();\n\n") # TODO: rm me
- out_c.write("JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_LDKSecretKey_1new(JNIEnv * _env, jclass _b) {\n") # TODO: rm me
- out_c.write("\tLDKSecretKey* key = (LDKSecretKey*)MALLOC(sizeof(LDKSecretKey), \"LDKSecretKey\");\n") # TODO: rm me
- out_c.write("\treturn (long)key;\n") # TODO: rm me
- out_c.write("}\n") # TODO: rm me
-
- in_block_comment = False