X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=java_strings.py;h=03a1d6e9084db6987861503760677ebb5aa03cf7;hb=20ee8c36407f25547ac60339007cbf6fbe61deee;hp=4b2ae77fe7f308cfddf7d278071c7bf6ad06a14a;hpb=fd59673b53bd5e74459132e685a10258b3c1d5a6;p=ldk-java diff --git a/java_strings.py b/java_strings.py index 4b2ae77f..03a1d6e9 100644 --- a/java_strings.py +++ b/java_strings.py @@ -197,6 +197,9 @@ void __wrap_reallocarray(void* ptr, size_t new_sz) { void __attribute__((destructor)) check_leaks() { size_t alloc_count = 0; size_t alloc_size = 0; + fprintf(stderr, "The following LDK-allocated blocks still remain.\\n"); + fprintf(stderr, "Note that this is only accurate if System.gc(); System.runFinalization()\\n"); + fprintf(stderr, "was called prior to exit after all LDK objects were out of scope.\\n"); for (allocation* a = allocation_ll; a != NULL; a = a->next) { fprintf(stderr, "%s %p (%lu bytes) remains:\\n", a->struct_name, a->ptr, a->alloc_len); backtrace_symbols_fd(a->bt, a->bt_len, STDERR_FILENO); @@ -205,7 +208,8 @@ void __attribute__((destructor)) check_leaks() { alloc_size += a->alloc_len; } fprintf(stderr, "%lu allocations remained for %lu bytes.\\n", alloc_count, alloc_size); - DO_ASSERT(allocation_ll == NULL); + fprintf(stderr, "Note that this is only accurate if System.gc(); System.runFinalization()\\n"); + fprintf(stderr, "was called prior to exit after all LDK objects were out of scope.\\n"); } """ self.c_file_pfx = self.c_file_pfx + """ @@ -320,7 +324,6 @@ import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; -@SuppressWarnings("unchecked") // We correctly assign various generic arrays """ self.c_fn_ty_pfx = "JNIEXPORT " self.c_fn_args_pfx = "JNIEnv *env, jclass clz" @@ -388,24 +391,26 @@ import java.util.Arrays; def init_str(self): res = "" - for ty in self.c_array_class_caches: + for ty in sorted(self.c_array_class_caches): res = res + "static jclass " + ty + "_clz = NULL;\n" res = res + "JNIEXPORT void Java_org_ldk_impl_bindings_init_1class_1cache(JNIEnv * env, jclass clz) {\n" - for ty in self.c_array_class_caches: + for ty in sorted(self.c_array_class_caches): res = res + "\t" + ty + "_clz = (*env)->FindClass(env, \"" + ty.replace("arr_of_", "[") + "\");\n" res = res + "\tCHECK(" + ty + "_clz != NULL);\n" res = res + "\t" + ty + "_clz = (*env)->NewGlobalRef(env, " + ty + "_clz);\n" res = res + "}\n" return res - def native_c_unitary_enum_map(self, struct_name, variants): + def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment): out_java_enum = "package org.ldk.enums;\n\n" out_java = "" out_c = "" out_c = out_c + "static inline " + struct_name + " " + struct_name + "_from_java(" + self.c_fn_args_pfx + ") {\n" out_c = out_c + "\tswitch ((*env)->CallIntMethod(env, clz, ordinal_meth)) {\n" - out_java_enum = out_java_enum + "public enum " + struct_name + " {\n" + if enum_doc_comment is not None: + out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n" + out_java_enum += "public enum " + struct_name + " {\n" ord_v = 0 for var in variants: out_java_enum = out_java_enum + "\t" + var + ",\n" @@ -474,12 +479,15 @@ import java.util.Arrays; ret = ret + ", " + param return ret + ")" - def native_c_map_trait(self, struct_name, field_vars, field_fns): + def native_c_map_trait(self, struct_name, field_vars, field_fns, trait_doc_comment): out_java_trait = "" out_java = "" # First generate most of the Java code, note that we need information about java method argument strings for C out_java_trait = out_java_trait + self.hu_struct_file_prefix + if trait_doc_comment is not None: + out_java_trait += "/**\n * " + trait_doc_comment.replace("\n", "\n * ") + "\n */\n" + out_java_trait += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n" out_java_trait = out_java_trait + "public class " + struct_name.replace("LDK","") + " extends CommonBase {\n" out_java_trait = out_java_trait + "\tfinal bindings." + struct_name + " bindings_instance;\n" out_java_trait = out_java_trait + "\t" + struct_name.replace("LDK", "") + "(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }\n" @@ -533,6 +541,7 @@ import java.util.Arrays; if fn_line.fn_name != "free" and fn_line.fn_name != "clone": out_java = out_java + "\t\t " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "(" java_trait_constr = java_trait_constr + "\t\t\t@Override public " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "(" + out_java_trait += "\t\t/**\n\t\t * " + fn_line.docs.replace("\n", "\n\t\t * ") + "\n\t\t */\n" out_java_trait = out_java_trait + "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_line.fn_name + "(" for idx, arg_conv_info in enumerate(fn_line.args_ty): @@ -629,7 +638,7 @@ import java.util.Arrays; for idx, fn_line in enumerate(field_fns): if fn_line.fn_name != "free" and fn_line.fn_name != "clone": assert fn_line.ret_ty_info.ty_info.get_full_rust_ty()[1] == "" - out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_jcall(" + out_c = out_c + fn_line.ret_ty_info.ty_info.get_full_rust_ty()[0] + " " + fn_line.fn_name + "_" + struct_name + "_jcall(" if fn_line.self_is_const: out_c = out_c + "const void* this_arg" else: @@ -705,7 +714,7 @@ import java.util.Arrays; out_c = out_c + "\t\t.this_arg = (void*) calls,\n" for fn_line in field_fns: if fn_line.fn_name != "free" and fn_line.fn_name != "clone": - out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_jcall,\n" + out_c = out_c + "\t\t." + fn_line.fn_name + " = " + fn_line.fn_name + "_" + struct_name + "_jcall,\n" elif fn_line.fn_name == "free": out_c = out_c + "\t\t.free = " + struct_name + "_JCalls_free,\n" else: @@ -753,19 +762,21 @@ import java.util.Arrays; base_conv = base_conv + "\t" + ty_info.rust_obj + "_JCalls_clone(" + ty_info.var_name + "_conv.this_arg);\n}" return base_conv - def map_complex_enum(self, struct_name, variant_list, camel_to_snake): - java_hu_type = struct_name.replace("LDK", "") + def map_complex_enum(self, struct_name, variant_list, camel_to_snake, enum_doc_comment): + java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option") out_java_enum = "" out_java = "" out_c = "" out_java_enum += (self.hu_struct_file_prefix) + out_java_enum += "\n/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n" + out_java_enum += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n" out_java_enum += ("public class " + java_hu_type + " extends CommonBase {\n") out_java_enum += ("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n") out_java_enum += ("\t@Override @SuppressWarnings(\"deprecation\")\n") out_java_enum += ("\tprotected void finalize() throws Throwable {\n") out_java_enum += ("\t\tsuper.finalize();\n") - out_java_enum += ("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n") + out_java_enum += ("\t\tif (ptr != 0) { bindings." + struct_name.replace("LDK", "") + "_free(ptr); }\n") out_java_enum += ("\t}\n") out_java_enum += ("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n") out_java_enum += ("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n") @@ -817,7 +828,7 @@ import java.util.Arrays; out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs)) out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n") - out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") + out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n") out_c += ("\tswitch(obj->tag) {\n") for var in variant_list: out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n") @@ -825,21 +836,28 @@ import java.util.Arrays; for idx, field_map in enumerate(var.fields): if field_map.ret_conv is not None: out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) - out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + out_c += "obj->" + camel_to_snake(var.var_name) + else: + out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") c_params.append(field_map.ret_conv_name) else: - c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) + if var.tuple_variant: + c_params.append("obj->" + camel_to_snake(var.var_name)) + else: + c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name) out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n") out_c += ("\t\t}\n") out_c += ("\t\tdefault: abort();\n") out_c += ("\t}\n}\n") - out_java_enum += ("}\n") return (out_java, out_java_enum, out_c) - def map_opaque_struct(self, struct_name): + def map_opaque_struct(self, struct_name, struct_doc_comment): out_opaque_struct_human = "" out_opaque_struct_human += self.hu_struct_file_prefix + out_opaque_struct_human += "\n/**\n * " + struct_doc_comment.replace("\n", "\n * ") + "\n */\n" + out_opaque_struct_human += "@SuppressWarnings(\"unchecked\") // We correctly assign various generic arrays\n" out_opaque_struct_human += ("public class " + struct_name.replace("LDK","") + " extends CommonBase") if struct_name.startswith("LDKLocked"): out_opaque_struct_human += (" implements AutoCloseable") @@ -856,7 +874,7 @@ import java.util.Arrays; return out_opaque_struct_human - def map_function(self, argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator): + def map_function(self, argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator, doc_comment): out_java = "" out_c = "" out_java_struct = None @@ -883,7 +901,9 @@ import java.util.Arrays; if not args_known: out_java_struct += ("\t// Skipped " + method_name + "\n") else: - meth_n = method_name[len(struct_meth) + 1:] + meth_n = method_name[len(struct_meth) + 1:].strip("_") + if doc_comment is not None: + out_java_struct += "\t/**\n\t * " + doc_comment.replace("\n", "\n\t * ") + "\n\t */\n" if not takes_self: out_java_struct += ( "\tpublic static " + return_type_info.java_hu_ty + " constructor_" + meth_n + "(")