From e02f2f6fac64d403ecb4b69236d3f5e1feb7e3df Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Thu, 14 Jan 2021 03:10:01 -0800 Subject: [PATCH] initiate language-dependent type mappings from C --- genbindings.py | 11 +++++++++-- java_strings.py | 5 +++++ typescript_strings.py | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/genbindings.py b/genbindings.py index ecdb6bd7..4b242aab 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,7 +185,8 @@ 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['byte'] + java_ty = mapped_type[0] c_ty = "int8_t" fn_ty_arg = "B" fn_arg = fn_arg[7:].strip() @@ -295,7 +298,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) == "": diff --git a/java_strings.py b/java_strings.py index b66157eb..0a479de3 100644 --- a/java_strings.py +++ b/java_strings.py @@ -2,6 +2,11 @@ from bindingstypes import * class Consts: def __init__(self, DEBUG): + + self.c_type_map = dict( + byte = ['byte'], + ) + self.common_base = """package org.ldk.structs; import java.util.LinkedList; class CommonBase { diff --git a/typescript_strings.py b/typescript_strings.py index 3c44daea..35331d89 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -8,6 +8,11 @@ def first_to_lower(string: str) -> str: class Consts: def __init__(self, DEBUG): + + self.c_type_map = dict( + byte = ['number', 'Uint8Array'], + ) + self.common_base = """ export default class CommonBase { ptr: number; @@ -274,6 +279,18 @@ import * as bindings from '../bindings' // TODO: figure out location super_instantiator += ", " + first_to_lower(var[1]) pointer_to_adder += "this.ptrs_to.push(" + first_to_lower(var[1]) + ");\n" + # BUILD INTERFACE METHODS + out_java_interface = "" + for fn_line in field_function_lines: + if fn_line.fn_name != "free" and fn_line.fn_name != "clone": + out_java_interface += fn_line.fn_name + "(" + + for idx, arg_conv_info in enumerate(fn_line.args_ty): + if idx >= 1: + out_java_interface += ", " + out_java_interface += f"{arg_conv_info.arg_name}: {arg_conv_info.java_hu_ty}" + out_java_interface += f"): {fn_line.ret_ty_info.java_hu_ty};\n\t\t\t\t" + out_java_trait = f""" {self.hu_struct_file_prefix} @@ -301,6 +318,10 @@ import * as bindings from '../bindings' // TODO: figure out location }} }} + + export interface {struct_name.replace("LDK", "")}Interface {{ + {out_java_interface} + }} """ @@ -382,6 +403,8 @@ import * as bindings from '../bindings' // TODO: figure out location out_java_trait = out_java_trait + "\t}\n" out_java_trait = out_java_trait + java_trait_constr + ");\n\t\treturn impl_holder.held;\n\t}\n" + print(java_trait_constr) + out_java = out_java + "\t}\n" out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl" -- 2.30.2