X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.py;h=b9189b0695de3f46196495f614974301e688f588;hb=68437301d6d8209c09c7df0b1d11999ca77665d2;hp=d0e080a8b3557efe369dbb396347430d351a9aa1;hpb=739c980d19c6c04538b739c680024c5083a10585;p=ldk-java diff --git a/genbindings.py b/genbindings.py index d0e080a8..b9189b06 100755 --- a/genbindings.py +++ b/genbindings.py @@ -170,6 +170,8 @@ def java_c_types(fn_arg, ret_arr_len): is_primitive = False arr_len = None + mapped_type = [] + java_type_plural = None if fn_arg.startswith("void"): java_ty = "void" c_ty = "void" @@ -183,26 +185,30 @@ def java_c_types(fn_arg, ret_arr_len): fn_arg = fn_arg[4:].strip() is_primitive = True elif fn_arg.startswith("uint8_t"): - java_ty = "byte" + mapped_type = consts.c_type_map['uint8_t'] + java_ty = mapped_type[0] c_ty = "int8_t" fn_ty_arg = "B" fn_arg = fn_arg[7:].strip() is_primitive = True elif fn_arg.startswith("uint16_t"): - java_ty = "short" + mapped_type = consts.c_type_map['uint16_t'] + java_ty = mapped_type[0] c_ty = "jshort" fn_ty_arg = "S" fn_arg = fn_arg[8:].strip() is_primitive = True elif fn_arg.startswith("uint32_t"): - java_ty = "int" + mapped_type = consts.c_type_map['uint32_t'] + java_ty = mapped_type[0] c_ty = "int32_t" fn_ty_arg = "I" fn_arg = fn_arg[8:].strip() is_primitive = True elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"): # TODO: uintptr_t is arch-dependent :( - java_ty = "long" + mapped_type = consts.c_type_map['long'] + java_ty = mapped_type[0] c_ty = "int64_t" fn_ty_arg = "J" if fn_arg.startswith("uint64_t"): @@ -295,7 +301,11 @@ def java_c_types(fn_arg, ret_arr_len): if var_is_arr is not None or ret_arr_len is not None: assert(not take_by_ptr) assert(not is_ptr) - java_ty = java_ty + "[]" + # is there a special case for plurals? + if len(mapped_type) == 2: + java_ty = mapped_type[1] + else: + java_ty = java_ty + "[]" c_ty = c_ty + "Array" if var_is_arr is not None: if var_is_arr.group(1) == "": @@ -568,7 +578,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_conv = opaque_arg_conv, arg_conv_name = "&" + ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = *", opaque_ret_conv_suf), ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", + # to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", + to_hu_conv = consts.to_hu_conv_templates['ptr'].replace('{human_type}', ty_info.java_hu_ty).replace('{var_name}', ty_info.var_name), 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 + ")")) else: @@ -576,7 +587,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: arg_conv = opaque_arg_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None, ret_conv = (ty_info.rust_obj + " " + ty_info.var_name + "_var = ", opaque_ret_conv_suf), ret_conv_name = ty_info.var_name + "_ref", - to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", + # to_hu_conv = ty_info.java_hu_ty + " " + ty_info.var_name + "_hu_conv = new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ");", + to_hu_conv = consts.to_hu_conv_templates['default'].replace('{human_type}', ty_info.java_hu_ty).replace('{var_name}', ty_info.var_name), 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 + ")")) @@ -924,7 +936,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: write_c(c_out) out_java_enum.write(native_file_out) out_java.write(native_out) - + def map_complex_enum(struct_name, union_enum_items): java_hu_type = struct_name.replace("LDK", "") complex_enums.add(struct_name) @@ -1198,41 +1210,9 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: write_c("\treturn tuple->" + e + ";\n") write_c("}\n") - out_java.write("""package org.ldk.impl; -import org.ldk.enums.*; - -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); - init_class_cache(); - } - static native void init(java.lang.Class c, java.lang.Class slicedef); - static native void init_class_cache(); - - 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[] read_bytes(long ptr, long len); - 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 new_txpointer_copy_data(byte[] txdata); - public static native void txpointer_free(long ptr); - public static native byte[] txpointer_get_buffer(long ptr); - public static native long vec_slice_len(long vec); - public static native long new_empty_slice_vec(); - -""") - - with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "a") as out_java_struct: + out_java.write(consts.bindings_header) + + with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.common_base) in_block_comment = False