X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=6b8af357b876455bb5d445abf87b8cfe2127a5bd;hb=ad54249d35928e3fdb321d71318fa6563031a2cd;hp=35331d89355a6f245d31fea201e59aff04a1ce26;hpb=e02f2f6fac64d403ecb4b69236d3f5e1feb7e3df;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index 35331d89..6b8af357 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -10,7 +10,15 @@ class Consts: def __init__(self, DEBUG): self.c_type_map = dict( - byte = ['number', 'Uint8Array'], + uint8_t = ['number', 'Uint8Array'], + uint16_t = ['number', 'Uint16Array'], + uint32_t = ['number', 'Uint32Array'], + long = ['number'], + ) + + self.to_hu_conv_templates = dict( + ptr = 'const {var_name}_hu_conv: {human_type} = new {human_type}(null, {var_name});', + default = 'const {var_name}_hu_conv: {human_type} = new {human_type}(null, {var_name});', ) self.common_base = """ @@ -265,9 +273,11 @@ import * as bindings from '../bindings' // TODO: figure out location constructor_arguments = "" super_instantiator = "" pointer_to_adder = "" + impl_constructor_arguments = "" for var in field_var_conversions: if isinstance(var, ConvInfo): constructor_arguments += f", {first_to_lower(var.arg_name)}?: {var.java_hu_ty}" + impl_constructor_arguments += f", {var.arg_name}: {var.java_hu_ty}" if var.from_hu_conv is not None: super_instantiator += ", " + var.from_hu_conv[0] if var.from_hu_conv[1] != "": @@ -278,18 +288,73 @@ import * as bindings from '../bindings' // TODO: figure out location constructor_arguments += f", {first_to_lower(var[1])}?: bindings.{var[0]}" super_instantiator += ", " + first_to_lower(var[1]) pointer_to_adder += "this.ptrs_to.push(" + first_to_lower(var[1]) + ");\n" + impl_constructor_arguments += f", {first_to_lower(var[1])}_impl: {var[0].replace('LDK', '')}.{var[0].replace('LDK', '')}Interface" # BUILD INTERFACE METHODS out_java_interface = "" + out_interface_implementation_overrides = "" + java_methods = [] for fn_line in field_function_lines: + java_method_descriptor = "" if fn_line.fn_name != "free" and fn_line.fn_name != "clone": out_java_interface += fn_line.fn_name + "(" + out_interface_implementation_overrides += f"{fn_line.fn_name} (" for idx, arg_conv_info in enumerate(fn_line.args_ty): if idx >= 1: out_java_interface += ", " + out_interface_implementation_overrides += ", " out_java_interface += f"{arg_conv_info.arg_name}: {arg_conv_info.java_hu_ty}" + out_interface_implementation_overrides += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}" + java_method_descriptor += arg_conv_info.java_fn_ty_arg out_java_interface += f"): {fn_line.ret_ty_info.java_hu_ty};\n\t\t\t\t" + java_method_descriptor += ")" + fn_line.ret_ty_info.java_fn_ty_arg + java_methods.append((fn_line.fn_name, java_method_descriptor)) + + out_interface_implementation_overrides += f"): {fn_line.ret_ty_info.java_ty} {{\n" + + interface_method_override_inset = "\t\t\t\t\t\t" + interface_implementation_inset = "\t\t\t\t\t\t\t" + for arg_info in fn_line.args_ty: + if arg_info.to_hu_conv is not None: + out_interface_implementation_overrides += interface_implementation_inset + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n" + + if fn_line.ret_ty_info.java_ty != "void": + out_interface_implementation_overrides += interface_implementation_inset + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_line.fn_name + "(" + else: + out_interface_implementation_overrides += f"{interface_implementation_inset}arg." + fn_line.fn_name + "(" + + for idx, arg_info in enumerate(fn_line.args_ty): + if idx != 0: + out_interface_implementation_overrides += ", " + if arg_info.to_hu_conv_name is not None: + out_interface_implementation_overrides += arg_info.to_hu_conv_name + else: + out_interface_implementation_overrides += arg_info.arg_name + + out_interface_implementation_overrides += ");\n" + if fn_line.ret_ty_info.java_ty != "void": + if fn_line.ret_ty_info.from_hu_conv is not None: + out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\t" + f"result: {fn_line.ret_ty_info.java_ty} = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n" + if fn_line.ret_ty_info.from_hu_conv[1] != "": + out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n" + #if fn_line.ret_ty_info.rust_obj in result_types: + # XXX: We need to handle this in conversion logic so that its cross-language! + # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead + # out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\tret.ptr = 0;\n" + out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\treturn result;\n" + else: + out_interface_implementation_overrides = out_interface_implementation_overrides + "\t\t\t\treturn ret;\n" + out_interface_implementation_overrides += f"{interface_method_override_inset}}},\n\n{interface_method_override_inset}" + + trait_constructor_arguments = "" + for var in field_var_conversions: + if isinstance(var, ConvInfo): + trait_constructor_arguments += ", " + var.arg_name + else: + trait_constructor_arguments += ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance" + + out_java_trait = f""" {self.hu_struct_file_prefix} @@ -317,93 +382,48 @@ import * as bindings from '../bindings' // TODO: figure out location super.finalize(); }} + static new_impl(arg: {struct_name.replace("LDK", "")}Interface{impl_constructor_arguments}): {struct_name.replace("LDK", "")} {{ + const impl_holder: {struct_name}Holder = new {struct_name}Holder(); + let structImplementation = {{ + + // todo: in-line interface filling + + {out_interface_implementation_overrides} + }}; + impl_holder.held = new {struct_name.replace("LDK", "")} (null, structImplementation{trait_constructor_arguments}); + }} + }} export interface {struct_name.replace("LDK", "")}Interface {{ {out_java_interface} }} + + class {struct_name}Holder {{ + held: {struct_name.replace("LDK", "")}; + }} + """ - java_trait_constr = "\tprivate static class " + struct_name + "Holder { " + struct_name.replace("LDK", "") + " held; }\n" - java_trait_constr = java_trait_constr + "\tpublic static " + struct_name.replace("LDK", "") + " new_impl(" + struct_name.replace("LDK", "") + "Interface arg" - for var in field_var_conversions: - if isinstance(var, ConvInfo): - java_trait_constr = java_trait_constr + ", " + var.java_hu_ty + " " + var.arg_name - else: - # Ideally we'd be able to take any instance of the interface, but our C code can only represent - # Java-implemented version, so we require users pass a Java implementation here :/ - java_trait_constr = java_trait_constr + ", " + var[0].replace("LDK", "") + "." + var[0].replace("LDK", "") + "Interface " + var[1] + "_impl" - java_trait_constr = java_trait_constr + ") {\n\t\tfinal " + struct_name + "Holder impl_holder = new " + struct_name + "Holder();\n" - java_trait_constr = java_trait_constr + "\t\timpl_holder.held = new " + struct_name.replace("LDK", "") + "(new bindings." + struct_name + "() {\n" - out_java_trait = out_java_trait + "\tpublic static interface " + struct_name.replace("LDK", "") + "Interface {\n" out_java = out_java + "\tpublic interface " + struct_name + " {\n" java_meths = [] for fn_line in field_function_lines: java_meth_descr = "(" if fn_line.fn_name != "free" and fn_line.fn_name != "clone": out_java = out_java + "\t\t " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "(" - java_trait_constr = java_trait_constr + "\t\t\t@Override public " + fn_line.ret_ty_info.java_ty + " " + fn_line.fn_name + "(" - out_java_trait = out_java_trait + "\t\t" + fn_line.ret_ty_info.java_hu_ty + " " + fn_line.fn_name + "(" for idx, arg_conv_info in enumerate(fn_line.args_ty): if idx >= 1: out_java = out_java + ", " - java_trait_constr = java_trait_constr + ", " - out_java_trait = out_java_trait + ", " + # out_java_trait = out_java_trait + ", " out_java = out_java + arg_conv_info.java_ty + " " + arg_conv_info.arg_name - out_java_trait = out_java_trait + arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name - java_trait_constr = java_trait_constr + arg_conv_info.java_ty + " " + arg_conv_info.arg_name - java_meth_descr = java_meth_descr + arg_conv_info.java_fn_ty_arg - java_meth_descr = java_meth_descr + ")" + fn_line.ret_ty_info.java_fn_ty_arg - java_meths.append((fn_line.fn_name, java_meth_descr)) + # out_java_trait = out_java_trait + arg_conv_info.java_hu_ty + " " + arg_conv_info.arg_name out_java = out_java + ");\n" - out_java_trait = out_java_trait + ");\n" - java_trait_constr = java_trait_constr + ") {\n" - - for arg_info in fn_line.args_ty: - if arg_info.to_hu_conv is not None: - java_trait_constr = java_trait_constr + "\t\t\t\t" + arg_info.to_hu_conv.replace("\n", "\n\t\t\t\t") + "\n" - - if fn_line.ret_ty_info.java_ty != "void": - java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_hu_ty + " ret = arg." + fn_line.fn_name + "(" - else: - java_trait_constr = java_trait_constr + "\t\t\t\targ." + fn_line.fn_name + "(" - - for idx, arg_info in enumerate(fn_line.args_ty): - if idx != 0: - java_trait_constr = java_trait_constr + ", " - if arg_info.to_hu_conv_name is not None: - java_trait_constr = java_trait_constr + arg_info.to_hu_conv_name - else: - java_trait_constr = java_trait_constr + arg_info.arg_name - - java_trait_constr = java_trait_constr + ");\n" - if fn_line.ret_ty_info.java_ty != "void": - if fn_line.ret_ty_info.from_hu_conv is not None: - java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.java_ty + " result = " + fn_line.ret_ty_info.from_hu_conv[0] + ";\n" - if fn_line.ret_ty_info.from_hu_conv[1] != "": - java_trait_constr = java_trait_constr + "\t\t\t\t" + fn_line.ret_ty_info.from_hu_conv[1].replace("this", "impl_holder.held") + ";\n" - #if fn_line.ret_ty_info.rust_obj in result_types: - # XXX: We need to handle this in conversion logic so that its cross-language! - # Avoid double-free by breaking the result - we should learn to clone these and then we can be safe instead - # java_trait_constr = java_trait_constr + "\t\t\t\tret.ptr = 0;\n" - java_trait_constr = java_trait_constr + "\t\t\t\treturn result;\n" - else: - java_trait_constr = java_trait_constr + "\t\t\t\treturn ret;\n" - java_trait_constr = java_trait_constr + "\t\t\t}\n" - java_trait_constr = java_trait_constr + "\t\t}" - for var in field_var_conversions: - if isinstance(var, ConvInfo): - java_trait_constr = java_trait_constr + ", " + var.arg_name - else: - java_trait_constr = java_trait_constr + ", " + var[1] + ".new_impl(" + var[1] + "_impl).bindings_instance" - 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" + # out_java_trait = out_java_trait + ");\n" - print(java_trait_constr) out_java = out_java + "\t}\n"