X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.py;h=f022c1b34062ff004dcbef1c25acf1472526edba;hb=9f93df58323e1dea5f3900d5d06b8506b8bc3278;hp=ecdb6bd7a59aa479cd0b697047ffbe9ca68d30d2;hpb=b1add05ff30a3ad491d5dbaaa066101756b5093d;p=ldk-java diff --git a/genbindings.py b/genbindings.py index ecdb6bd7..f022c1b3 100755 --- a/genbindings.py +++ b/genbindings.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import sys, re -if len(sys.argv) != 7: +if len(sys.argv) < 7: print("USAGE: /path/to/lightning.h /path/to/bindings/output /path/to/bindings/ /path/to/bindings/output.c debug lang") sys.exit(1) @@ -13,14 +13,19 @@ else: print("debug should be true or false and indicates whether to track allocations and ensure we don't leak") sys.exit(1) +target = None if sys.argv[6] == "java": from java_strings import Consts elif sys.argv[6] == "typescript": + import typescript_strings from typescript_strings import Consts + target = typescript_strings.Target.NODEJS + if len(sys.argv) == 8 and sys.argv[7] == 'browser': + target = typescript_strings.Target.BROWSER else: print("Only java or typescript can be set for lang") sys.exit(1) -consts = Consts(DEBUG) +consts = Consts(DEBUG, target=target) from bindingstypes import * @@ -170,6 +175,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 +190,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 +306,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 +583,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 +592,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 + ")")) @@ -1198,39 +1215,7 @@ 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(); - -""") + 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)