X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=37aba59d595f96d5fe88cb37d807b40aae665a56;hb=f3e3489cf868dc87ba6227ee89feb31a5394354a;hp=6b8af357b876455bb5d445abf87b8cfe2127a5bd;hpb=ad54249d35928e3fdb321d71318fa6563031a2cd;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index 6b8af357..37aba59d 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -1,13 +1,17 @@ from bindingstypes import ConvInfo - +from enum import Enum def first_to_lower(string: str) -> str: first = string[0] return first.lower() + string[1:] +class Target(Enum): + NODEJS = 1, + BROWSER = 2 + class Consts: - def __init__(self, DEBUG): + def __init__(self, DEBUG: bool, target: Target, **kwargs): self.c_type_map = dict( uint8_t = ['number', 'Uint8Array'], @@ -16,11 +20,68 @@ class Consts: long = ['number'], ) + self.wasm_decoding_map = dict( + int8_tArray = 'decodeArray' + ) + + self.wasm_encoding_map = dict( + int8_tArray = 'encodeArray', + ) + 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.bindings_header = self.wasm_import_header(target) + """ +export class VecOrSliceDef { + public dataptr: number; + public datalen: number; + public stride: number; + public constructor(dataptr: number, datalen: number, stride: number) { + this.dataptr = dataptr; + this.datalen = datalen; + this.stride = stride; + } +} + +/* +TODO: load WASM file +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(); +*/ + +""" + + self.bindings_footer = """ + export async function initializeWasm(allowDoubleInitialization: boolean = false): Promise { + if(isWasmInitialized && !allowDoubleInitialization) { + return; + } + const wasmInstance = await WebAssembly.instantiate(wasmModule, imports) + wasm = wasmInstance.exports; + isWasmInitialized = true; + } + """ + self.common_base = """ export default class CommonBase { ptr: number; @@ -172,7 +233,7 @@ import * as bindings from '../bindings' // TODO: figure out location self.c_fn_args_pfx = "void* ctx_TODO" self.file_ext = ".ts" self.ptr_c_ty = "uint32_t" - self.ptr_native_ty = "uint32_t" + self.ptr_native_ty = "number" # "uint32_t" self.result_c_ty = "uint32_t" self.ptr_arr = "uint32_tArray" self.get_native_arr_len_call = ("", ".len") @@ -211,6 +272,106 @@ import * as bindings from '../bindings' // TODO: figure out location else: return None + + def wasm_import_header(self, target): + if target == Target.NODEJS: + return """ + +import * as fs from 'fs'; +const source = fs.readFileSync('./ldk.wasm'); + +const memory = new WebAssembly.Memory({initial: 256}); +const wasmModule = new WebAssembly.Module(source); + +const imports: any = {}; +imports.env = {}; + +imports.env.memoryBase = 0; +imports.env.memory = memory; +imports.env.tableBase = 0; +imports.env.table = new WebAssembly.Table({initial: 4, element: 'anyfunc'}); + +imports.env["abort"] = function () { + console.error("ABORT"); +}; + +let wasm = null; +let isWasmInitialized: boolean = false; + + +// WASM CODEC + +const nextMultipleOfFour = (value: number) => { + return Math.ceil(value / 4) * 4; +} + +const encodeArray = (inputArray) => { + // TODO: (matt) is this correct, or should it go back to length * 4? + // const cArrayPointer = wasm.wasm_malloc(inputArray.length * 4); + const cArrayPointer = wasm.wasm_malloc(nextMultipleOfFour(inputArray.length)); + + const arrayMemoryView = new Uint32Array(memory.buffer, cArrayPointer, inputArray.length); + arrayMemoryView.set(inputArray); + return cArrayPointer; +} + +const decodeArray = (arrayPointer, free = true) => { + const arraySizeViewer = new Uint32Array( + memory.buffer, // value + arrayPointer, // offset + 1 // one int + ); + const arraySize = arraySizeViewer[0]; + const actualArrayViewer = new Uint32Array( + memory.buffer, // value + arrayPointer, // offset + arraySize + 1 + ); + const actualArray = actualArrayViewer.slice(1, arraySize + 1); + if (free) { + // wasm.free_array(arrayPointer); + wasm.wasm_free(arrayPointer); // TODO: check if passing *void still captures remaining values + } + return actualArray; +} + +const encodeString = (string) => { + // make malloc count divisible by 4 + const memoryNeed = nextMultipleOfFour(string.length + 1); + const stringPointer = wasm.wasm_malloc(memoryNeed); + const stringMemoryView = new Uint8Array( + memory.buffer, // value + stringPointer, // offset + string.length + 1 // length + ); + for (let i = 0; i < string.length; i++) { + stringMemoryView[i] = string.charCodeAt(i); + } + stringMemoryView[string.length] = 0; + return stringPointer; +} + +const decodeString = (stringPointer, free = true) => { + const memoryView = new Uint8Array(memory.buffer, stringPointer); + let cursor = 0; + let result = ''; + + while (memoryView[cursor] !== 0) { + result += String.fromCharCode(memoryView[cursor]); + cursor++; + } + + if (free) { + wasm.wasm_free(stringPointer); + } + + return result; +}; + + + """ + return '' + def init_str(self, c_array_class_caches): return "" @@ -262,13 +423,7 @@ import * as bindings from '../bindings' // TODO: figure out location return ret def native_c_map_trait(self, struct_name, field_var_conversions, field_function_lines): - out_java = "out_java:native_c_map_trait" - out_java_trait = "out_java_trait:native_c_map_trait" - out_c = "out_c:native_c_map_trait" - - - out_java_trait = "" - out_java = "" + out_typescript_bindings = "\n\n\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: START\n\n" constructor_arguments = "" super_instantiator = "" @@ -356,7 +511,7 @@ import * as bindings from '../bindings' // TODO: figure out location - out_java_trait = f""" + out_typescript_human = f""" {self.hu_struct_file_prefix} export class {struct_name.replace("LDK","")} extends CommonBase {{ @@ -407,34 +562,38 @@ import * as bindings from '../bindings' // TODO: figure out location - out_java = out_java + "\tpublic interface " + struct_name + " {\n" + out_typescript_bindings += "\t\texport 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 + "(" + out_typescript_bindings += f"\t\t\t{fn_line.fn_name} (" for idx, arg_conv_info in enumerate(fn_line.args_ty): if idx >= 1: - out_java = out_java + ", " - # 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 + out_typescript_bindings = out_typescript_bindings + ", " + out_typescript_bindings += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}" - out_java = out_java + ");\n" - # out_java_trait = out_java_trait + ");\n" + out_typescript_bindings += f"): {fn_line.ret_ty_info.java_ty};\n" + out_typescript_bindings = out_typescript_bindings + "\t\t}\n\n" - out_java = out_java + "\t}\n" - - out_java = out_java + "\tpublic static native long " + struct_name + "_new(" + struct_name + " impl" + out_typescript_bindings += f"\t\texport function {struct_name}_new(impl: {struct_name}" for var in field_var_conversions: if isinstance(var, ConvInfo): - out_java = out_java + ", " + var.java_ty + " " + var.arg_name + out_typescript_bindings += f", {var.arg_name}: {var.java_ty}" else: - out_java = out_java + ", " + var[0] + " " + var[1] - out_java = out_java + ");\n" - out_java = out_java + "\tpublic static native " + struct_name + " " + struct_name + "_get_obj_from_jcalls(long val);\n" + out_typescript_bindings += f", {var[1]}: {var[0]}" + + out_typescript_bindings += f"""): number {{ + throw new Error('unimplemented'); // TODO: bind to WASM + }} + + export function {struct_name}_get_obj_from_jcalls(val: number): {struct_name} {{ + throw new Error('unimplemented'); // TODO: bind to WASM + }} + """ + + out_typescript_bindings += '\n// OUT_TYPESCRIPT_BINDINGS :: MAP_TRAIT :: END\n\n\n' # Now that we've written out our java code (and created java_meths), generate C out_c = "typedef struct " + struct_name + "_JCalls {\n" @@ -590,7 +749,7 @@ import * as bindings from '../bindings' // TODO: figure out location out_c = out_c + "}\n" - return (out_java, out_java_trait, out_c) + return (out_typescript_bindings, out_typescript_human, out_c) def map_complex_enum(self, struct_name, variant_list, camel_to_snake): java_hu_type = struct_name.replace("LDK", "") @@ -668,3 +827,209 @@ import * as bindings from '../bindings' // TODO: figure out location out_java_enum += ("}\n") out_java_enum += (java_hu_subclasses) return (out_java, out_java_enum, out_c) + + def map_opaque_struct(self, struct_name): + + implementations = "" + method_header = "" + if struct_name.startswith("LDKLocked"): + implementations += "implements AutoCloseable " + method_header = """ + public close() { + """ + else: + method_header = """ + protected finalize() { + super.finalize(); + """ + + out_opaque_struct_human = f""" + {self.hu_struct_file_prefix} + + export default class {struct_name.replace("LDK","")} extends CommonBase {implementations}{{ + constructor(_dummy: object, ptr: number) {{ + super(ptr); + }} + + {method_header} + if (this.ptr != 0) {{ + bindings.{struct_name.replace("LDK","")}_free(this.ptr); + }} + }} + """ + return out_opaque_struct_human + + def map_function(self, argument_types, c_call_string, is_free, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, has_out_java_struct: bool, type_mapping_generator): + out_java = "" + out_c = "" + out_java_struct = None + + out_java += ("\tpublic static native ") + out_c += (self.c_fn_ty_pfx) + out_c += (return_type_info.c_ty) + out_java += (return_type_info.java_ty) + if return_type_info.ret_conv is not None: + ret_conv_pfx, ret_conv_sfx = return_type_info.ret_conv + out_java += (" " + method_name + "(") + out_c += (" " + self.c_fn_name_pfx + method_name.replace('_', '_1') + "(" + self.c_fn_args_pfx) + + method_argument_string = "" + native_call_argument_string = "" + for idx, arg_conv_info in enumerate(argument_types): + if idx != 0: + method_argument_string += (", ") + native_call_argument_string += ', ' + if arg_conv_info.c_ty != "void": + out_c += (", ") + if arg_conv_info.c_ty != "void": + out_c += (arg_conv_info.c_ty + " " + arg_conv_info.arg_name) + needs_encoding = arg_conv_info.c_ty in self.wasm_encoding_map + native_argument = arg_conv_info.arg_name + if needs_encoding: + converter = self.wasm_encoding_map[arg_conv_info.c_ty] + native_argument = f"{converter}({arg_conv_info.arg_name})" + method_argument_string += f"{arg_conv_info.arg_name}: {arg_conv_info.java_ty}" + native_call_argument_string += native_argument + + has_return_value = return_type_info.c_ty != 'void' + needs_decoding = return_type_info.c_ty in self.wasm_decoding_map + return_statement = 'return nativeResponseValue;' + if not has_return_value: + return_statement = '// debug statements here' + elif needs_decoding: + converter = self.wasm_decoding_map[return_type_info.c_ty] + return_statement = f"return {converter}(nativeResponseValue);" + + out_java = f"""\texport function {method_name}({method_argument_string}): {return_type_info.java_ty} {{ + if(!isWasmInitialized){{ + throw new Error("initializeWasm() must be awaited first!"); + }} + const nativeResponseValue = wasm.{method_name}({native_call_argument_string}); + {return_statement}\n\t}} + \n""" + + + + if has_out_java_struct: + out_java_struct = "" + if not args_known: + out_java_struct += ("\t// Skipped " + method_name + "\n") + has_out_java_struct = False + else: + meth_n = method_name[len(struct_meth) + 1:] + if not takes_self: + out_java_struct += ( + "\tpublic static " + return_type_info.java_hu_ty + " constructor_" + meth_n + "(") + else: + out_java_struct += ("\tpublic " + return_type_info.java_hu_ty + " " + meth_n + "(") + for idx, arg in enumerate(argument_types): + if idx != 0: + if not takes_self or idx > 1: + out_java_struct += (", ") + elif takes_self: + continue + if arg.java_ty != "void": + if arg.arg_name in default_constructor_args: + for explode_idx, explode_arg in enumerate(default_constructor_args[arg.arg_name]): + if explode_idx != 0: + out_java_struct += (", ") + out_java_struct += ( + explode_arg.java_hu_ty + " " + arg.arg_name + "_" + explode_arg.arg_name) + else: + out_java_struct += (arg.java_hu_ty + " " + arg.arg_name) + + out_c += (") {\n") + if out_java_struct is not None: + out_java_struct += (") {\n") + for info in argument_types: + if info.arg_conv is not None: + out_c += ("\t" + info.arg_conv.replace('\n', "\n\t") + "\n") + if return_type_info.ret_conv is not None: + out_c += ("\t" + ret_conv_pfx.replace('\n', '\n\t')) + elif return_type_info.c_ty != "void": + out_c += ("\t" + return_type_info.c_ty + " ret_val = ") + else: + out_c += ("\t") + if c_call_string is None: + out_c += (method_name + "(") + else: + out_c += (c_call_string) + for idx, info in enumerate(argument_types): + if info.arg_conv_name is not None: + if idx != 0: + out_c += (", ") + elif c_call_string is not None: + continue + out_c += (info.arg_conv_name) + out_c += (")") + if return_type_info.ret_conv is not None: + out_c += (ret_conv_sfx.replace('\n', '\n\t')) + else: + out_c += (";") + for info in argument_types: + if info.arg_conv_cleanup is not None: + out_c += ("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t")) + if return_type_info.ret_conv is not None: + out_c += ("\n\treturn " + return_type_info.ret_conv_name + ";") + elif return_type_info.c_ty != "void": + out_c += ("\n\treturn ret_val;") + out_c += ("\n}\n\n") + + if has_out_java_struct: + out_java_struct += ("\t\t") + if return_type_info.java_ty != "void": + out_java_struct += (return_type_info.java_ty + " ret = ") + out_java_struct += ("bindings." + method_name + "(") + for idx, info in enumerate(argument_types): + if idx != 0: + out_java_struct += (", ") + if idx == 0 and takes_self: + out_java_struct += ("this.ptr") + elif info.arg_name in default_constructor_args: + out_java_struct += ("bindings." + info.java_hu_ty + "_new(") + for explode_idx, explode_arg in enumerate(default_constructor_args[info.arg_name]): + if explode_idx != 0: + out_java_struct += (", ") + expl_arg_name = info.arg_name + "_" + explode_arg.arg_name + if explode_arg.from_hu_conv is not None: + out_java_struct += ( + explode_arg.from_hu_conv[0].replace(explode_arg.arg_name, expl_arg_name)) + else: + out_java_struct += (expl_arg_name) + out_java_struct += (")") + elif info.from_hu_conv is not None: + out_java_struct += (info.from_hu_conv[0]) + else: + out_java_struct += (info.arg_name) + out_java_struct += (");\n") + if return_type_info.to_hu_conv is not None: + if not takes_self: + out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t").replace("this", + return_type_info.to_hu_conv_name) + "\n") + else: + out_java_struct += ("\t\t" + return_type_info.to_hu_conv.replace("\n", "\n\t\t") + "\n") + + for idx, info in enumerate(argument_types): + if idx == 0 and takes_self: + pass + elif info.arg_name in default_constructor_args: + for explode_arg in default_constructor_args[info.arg_name]: + expl_arg_name = info.arg_name + "_" + explode_arg.arg_name + if explode_arg.from_hu_conv is not None and return_type_info.to_hu_conv_name: + out_java_struct += ("\t\t" + explode_arg.from_hu_conv[1].replace(explode_arg.arg_name, + expl_arg_name).replace( + "this", return_type_info.to_hu_conv_name) + ";\n") + elif info.from_hu_conv is not None and info.from_hu_conv[1] != "": + if not takes_self and return_type_info.to_hu_conv_name is not None: + out_java_struct += ( + "\t\t" + info.from_hu_conv[1].replace("this", return_type_info.to_hu_conv_name) + ";\n") + else: + out_java_struct += ("\t\t" + info.from_hu_conv[1] + ";\n") + + if return_type_info.to_hu_conv_name is not None: + out_java_struct += ("\t\treturn " + return_type_info.to_hu_conv_name + ";\n") + elif return_type_info.java_ty != "void" and return_type_info.rust_obj != "LDK" + struct_meth: + out_java_struct += ("\t\treturn ret;\n") + out_java_struct += ("\t}\n\n") + + return (out_java, out_c, out_java_struct)