#!/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)
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 *
is_primitive = False
arr_len = None
+ mapped_type = []
+ java_type_plural = None
if fn_arg.startswith("void"):
java_ty = "void"
c_ty = "void"
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"):
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) == "":
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:
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 + ")"))
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)
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