make target argument optional for typescript inputs (undocumented)
[ldk-java] / genbindings.py
index 4b242aab1fca67926cb7019a2d60a95a0c36ce67..f022c1b34062ff004dcbef1c25acf1472526edba 100755 (executable)
@@ -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 *
 
@@ -185,27 +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"):
-        mapped_type = consts.c_type_map['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"):
@@ -575,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:
@@ -583,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 + ")"))
 
@@ -1205,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)