X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=typescript_strings.py;h=462a77fe951a135ab20a8727b5a0b2ebb9d19938;hb=7d372e434499e36b8be0d090ef733a03650dec6e;hp=8002aa7c16ad3309cc32b5fe81a512c8488c55ac;hpb=1f10a8405a1e6fb2c133a31909a1601a5691ef77;p=ldk-java diff --git a/typescript_strings.py b/typescript_strings.py index 8002aa7c..462a77fe 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -17,18 +17,16 @@ class Consts: self.function_ptr_counter = 0 self.function_ptrs = {} self.c_type_map = dict( - uint8_t = ['number', 'Uint8Array'], - uint16_t = ['number', 'Uint16Array'], - uint32_t = ['number', 'Uint32Array'], - uint64_t = ['BigInt'], + uint8_t = ['number', 'number', 'Uint8Array'], + uint16_t = ['number', 'number', 'Uint16Array'], + uint32_t = ['number', 'number', 'Uint32Array'], + uint64_t = ['bigint', 'bigint', 'BigUint64Array'], ) - - self.wasm_decoding_map = dict( - int8_tArray = 'decodeUint8Array' + self.java_type_map = dict( + String = "number" ) - - self.wasm_encoding_map = dict( - int8_tArray = 'encodeUint8Array', + self.java_hu_type_map = dict( + String = "string" ) self.to_hu_conv_templates = dict( @@ -80,16 +78,16 @@ export default class CommonBase { /** The script_pubkey in this output */ public script_pubkey: Uint8Array; /** The value, in satoshis, of this output */ - public value: BigInt; + public value: bigint; /* @internal */ public constructor(_dummy: object, ptr: number) { super(ptr, bindings.TxOut_free); - this.script_pubkey = bindings.TxOut_get_script_pubkey(ptr); + this.script_pubkey = bindings.decodeUint8Array(bindings.TxOut_get_script_pubkey(ptr)); this.value = bindings.TxOut_get_value(ptr); } - public constructor_new(value: BigInt, script_pubkey: Uint8Array): TxOut { - return new TxOut(null, bindings.TxOut_new(script_pubkey, value)); + public constructor_new(value: bigint, script_pubkey: Uint8Array): TxOut { + return new TxOut(null, bindings.TxOut_new(bindings.encodeUint8Array(script_pubkey), value)); } }""" self.obj_defined(["TxOut"], "structs") @@ -292,7 +290,6 @@ void __attribute__((export_name("TS_free"))) TS_free(uint32_t ptr) { self.hu_struct_file_prefix = """ import CommonBase from './CommonBase.mjs'; import * as bindings from '../bindings.mjs' -import * as InternalUtils from '../InternalUtils.mjs' """ self.util_fn_pfx = self.hu_struct_file_prefix + "\nexport class UtilMethods extends CommonBase {\n" @@ -306,12 +303,6 @@ import * as InternalUtils from '../InternalUtils.mjs' self.is_arr_some_check = ("", " != 0") self.get_native_arr_len_call = ("", "->arr_len") - with open(outdir + "/InternalUtils.mts", "w") as f: - f.write("export function check_arr_len(arr: Uint8Array, len: number): Uint8Array {\n") - f.write("\tif (arr.length != len) { throw new Error(\"Expected array of length \" + len + \"got \" + arr.length); }\n") - f.write("\treturn arr;\n") - f.write("}") - def release_native_arr_ptr_call(self, ty_info, arr_var, arr_ptr_var): return None def create_native_arr_call(self, arr_len, ty_info): @@ -347,12 +338,17 @@ import * as InternalUtils from '../InternalUtils.mjs' return None def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty): + assert elem_ty.c_ty == "uint32_t" or elem_ty.c_ty.endswith("Array") return arr_name + " != null ? " + arr_name + ".map(" + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null" def str_ref_to_native_call(self, var_name, str_len): return "str_ref_to_ts(" + var_name + ", " + str_len + ")" def str_ref_to_c_call(self, var_name): return "str_ref_to_owned_c(" + var_name + ")" + def str_to_hu_conv(self, var_name): + return "const " + var_name + "_conv: string = bindings.decodeString(" + var_name + ");" + def str_from_hu_conv(self, var_name): + return ("bindings.encodeString(" + var_name + ")", "") def c_fn_name_define_pfx(self, fn_name, have_args): return " __attribute__((export_name(\"TS_" + fn_name + "\"))) TS_" + fn_name + "(" @@ -366,21 +362,44 @@ var js_objs: Array> = []; var js_invoke: Function; imports.wasi_snapshot_preview1 = { - "fd_write" : () => { - console.log("ABORT"); + "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => { + // This should generally only be used to print panic messages + console.log("FD_WRITE to " + fd + " in " + iovec_array_len + " chunks."); + const ptr_len_view = new Uint32Array(wasm.memory.buffer, iovec_array_ptr, iovec_array_len * 2); + for (var i = 0; i < iovec_array_len; i++) { + const bytes_view = new Uint8Array(wasm.memory.buffer, ptr_len_view[i*2], ptr_len_view[i*2+1]); + console.log(String.fromCharCode(...bytes_view)); + } + return 0; }, - "random_get" : () => { - console.log("RAND GET"); + "random_get": (buf_ptr: number, buf_len: number) => { + const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len); + crypto.getRandomValues(buf); + return 0; }, - "environ_sizes_get" : () => { + "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => { + // This is called before fd_write to format + print panic messages console.log("wasi_snapshot_preview1:environ_sizes_get"); + const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1); + out_count_view[0] = 1; + const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1); + out_len_view[0] = "RUST_BACKTRACE=1".length + 1; // Note that string must be NULL-terminated + return 0; + }, + "environ_get": (environ_ptr: number, environ_buf_ptr: number) => { + // This is called before fd_write to format + print panic messages + console.log("wasi_snapshot_preview1:environ_get"); + const out_ptrs = new Uint32Array(wasm.memory.buffer, environ_ptr, 2); + out_ptrs[0] = environ_buf_ptr; + out_ptrs[1] = "RUST_BACKTRACE=1".length; + const out_environ = new Uint8Array(wasm.memory.buffer, environ_buf_ptr, out_ptrs[1]); + for (var i = 0; i < out_ptrs[1]; i++) { out_environ[i] = "RUST_BACKTRACE=1".codePointAt(i); } + out_environ[out_ptrs[1]] = 0; + return 0; }, "proc_exit" : () => { console.log("wasi_snapshot_preview1:proc_exit"); }, - "environ_get" : () => { - console.log("wasi_snapshot_preview1:environ_get"); - }, }; var wasm: any = null; @@ -389,6 +408,7 @@ let isWasmInitialized: boolean = false; if target == Target.NODEJS: res += """import * as fs from 'fs'; +import { webcrypto as crypto } from 'crypto'; export async function initializeWasm(path: string) { const source = fs.readFileSync(path); imports.env["js_invoke_function"] = js_invoke; @@ -424,7 +444,7 @@ const nextMultipleOfFour = (value: number) => { return Math.ceil(value / 4) * 4; } -const encodeUint8Array = (inputArray: Uint8Array) => { +export function encodeUint8Array (inputArray: Uint8Array): number { const cArrayPointer = wasm.TS_malloc(inputArray.length + 4); const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1); arrayLengthView[0] = inputArray.length; @@ -432,32 +452,37 @@ const encodeUint8Array = (inputArray: Uint8Array) => { arrayMemoryView.set(inputArray); return cArrayPointer; } - -const encodeUint32Array = (inputArray: Uint32Array) => { +export function encodeUint32Array (inputArray: Uint32Array|Array): number { const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4); const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length); arrayMemoryView.set(inputArray, 1); arrayMemoryView[0] = inputArray.length; return cArrayPointer; } +export function encodeUint64Array (inputArray: BigUint64Array|Array): number { + const cArrayPointer = wasm.TS_malloc(inputArray.length * 8 + 1); + const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1); + arrayLengthView[0] = inputArray.length; + const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length); + arrayMemoryView.set(inputArray); + return cArrayPointer; +} -const getArrayLength = (arrayPointer: number) => { - const arraySizeViewer = new Uint32Array( - wasm.memory.buffer, // value - arrayPointer, // offset - 1 // one int - ); +export function check_arr_len(arr: Uint8Array, len: number): Uint8Array { + if (arr.length != len) { throw new Error("Expected array of length " + len + "got " + arr.length); } + return arr; +} + +export function getArrayLength(arrayPointer: number): number { + const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1); return arraySizeViewer[0]; } -const decodeUint8Array = (arrayPointer: number, free = true) => { +export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array { const arraySize = getArrayLength(arrayPointer); - const actualArrayViewer = new Uint8Array( - wasm.memory.buffer, // value - arrayPointer + 4, // offset (ignoring length bytes) - arraySize // uint8 count - ); + const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, arraySize); // Clone the contents, TODO: In the future we should wrap the Viewer in a class that // will free the underlying memory when it becomes unreachable instead of copying here. + // Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer). const actualArray = actualArrayViewer.slice(0, arraySize); if (free) { wasm.TS_free(arrayPointer); @@ -480,49 +505,69 @@ const decodeUint32Array = (arrayPointer: number, free = true) => { return actualArray; } -const encodeString = (string: string) => { - // make malloc count divisible by 4 - const memoryNeed = nextMultipleOfFour(string.length + 1); - const stringPointer = wasm.TS_malloc(memoryNeed); - const stringMemoryView = new Uint8Array( - wasm.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; +export function getU32ArrayElem(arrayPointer: number, idx: number): number { + const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1); + return actualArrayViewer[idx]; } -const decodeString = (stringPointer: number, free = true) => { - const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer); - let cursor = 0; - let result = ''; +export function encodeString(str: string): number { + const charArray = new TextEncoder().encode(str); + return encodeUint8Array(charArray); +} - while (memoryView[cursor] !== 0) { - result += String.fromCharCode(memoryView[cursor]); - cursor++; - } +export function decodeString(stringPointer: number, free = true): string { + const arraySize = getArrayLength(stringPointer); + const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize); + const result = new TextDecoder("utf-8").decode(memoryView); if (free) { - wasm.wasm_free(stringPointer); + wasm.TS_free(stringPointer); } return result; -}; +} """ def init_str(self): return "" + def get_java_arr_len(self, arr_name): + return "bindings.getArrayLength(" + arr_name + ")" + def get_java_arr_elem(self, elem_ty, arr_name, idx): + if elem_ty.c_ty == "uint32_t" or elem_ty.c_ty == "uintptr_t" or elem_ty.c_ty.endswith("Array"): + return "bindings.getU32ArrayElem(" + arr_name + ", " + idx + ")" + else: + assert False def constr_hu_array(self, ty_info, arr_len): return "new Array(" + arr_len + ").fill(null)" + def primitive_arr_from_hu(self, mapped_ty, fixed_len, arr_name): + inner = arr_name + if fixed_len is not None: + assert mapped_ty.c_ty == "int8_t" + inner = "bindings.check_arr_len(" + arr_name + ", " + fixed_len + ")" + if mapped_ty.c_ty.endswith("Array"): + return ("bindings.encodeUint32Array(" + inner + ")", "") + elif mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t": + return ("bindings.encodeUint8Array(" + inner + ")", "") + elif mapped_ty.c_ty == "uint32_t": + return ("bindings.encodeUint32Array(" + inner + ")", "") + elif mapped_ty.c_ty == "int64_t": + return ("bindings.encodeUint64Array(" + inner + ")", "") + else: + print(mapped_ty.c_ty) + assert False + + def primitive_arr_to_hu(self, mapped_ty, fixed_len, arr_name, conv_name): + assert mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t" + return "const " + conv_name + ": Uint8Array = bindings.decodeUint8Array(" + arr_name + ");" + def var_decl_statement(self, ty_string, var_name, statement): return "const " + var_name + ": " + ty_string + " = " + statement + def java_arr_ty_str(self, elem_ty_str): + return "number" + def for_n_in_range(self, n, minimum, maximum): return "for (var " + n + " = " + minimum + "; " + n + " < " + maximum + "; " + n + "++) {" def for_n_in_arr(self, n, arr_name, arr_elem_ty): @@ -792,7 +837,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{ out_c += "js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) elif fn_line.ret_ty_info.java_ty == "void": out_c = out_c + "\tjs_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) - elif fn_line.ret_ty_info.java_ty == "String": + elif fn_line.ret_ty_info.java_hu_ty == "string": out_c = out_c + "\tjstring ret = (jstring)js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) elif not fn_line.ret_ty_info.passed_as_ptr: out_c = out_c + "\treturn js_invoke_function_" + str(len(fn_line.args_ty)) + "(j_calls->instance_ptr, " + str(self.function_ptr_counter) @@ -1055,13 +1100,9 @@ export class {human_ty} extends CommonBase {{ def fn_call_body(self, method_name, return_c_ty, return_java_ty, method_argument_string, native_call_argument_string): has_return_value = return_c_ty != 'void' - needs_decoding = return_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_c_ty] - return_statement = f"return {converter}(nativeResponseValue);" return f"""export function {method_name}({method_argument_string}): {return_java_ty} {{ if(!isWasmInitialized) {{ @@ -1094,13 +1135,8 @@ export class {human_ty} extends CommonBase {{ 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 + native_call_argument_string += arg_conv_info.arg_name out_java = self.fn_call_body(method_name, return_type_info.c_ty, return_type_info.java_ty, method_argument_string, native_call_argument_string) out_java_struct = ""