From: Matt Corallo Date: Thu, 4 Aug 2022 22:56:43 +0000 (+0000) Subject: [TS] Support mapping returned uint64[]s from rust functions X-Git-Tag: v0.0.110.1^2~8 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a37d4e67b1ec6666ca9dfda060e4986aa82ee3da;p=ldk-java [TS] Support mapping returned uint64[]s from rust functions --- diff --git a/gen_type_mapping.py b/gen_type_mapping.py index 2ed6faa3..8dd7c6ca 100644 --- a/gen_type_mapping.py +++ b/gen_type_mapping.py @@ -187,8 +187,6 @@ class TypeMappingGenerator: if is_nullable: ret_conv = (ret_conv[0], ret_conv[1] + "\n}") - to_hu_conv = None - to_hu_conv_name = None if subty.to_hu_conv is not None: to_hu_conv = self.consts.var_decl_statement(self.consts.c_type_map["uint32_t"][0], conv_name + "_len", self.consts.get_java_arr_len(arr_name)) + ";\n" to_hu_conv += self.consts.var_decl_statement(ty_info.java_hu_ty, conv_name + "_arr", self.consts.constr_hu_array(ty_info, conv_name + "_len")) @@ -200,6 +198,12 @@ class TypeMappingGenerator: if cleanup is not None: to_hu_conv += "\n" + cleanup to_hu_conv_name = conv_name + "_arr" + else: + to_hu_conv = self.consts.primitive_arr_to_hu(ty_info.subty, None, arr_name, arr_name + "_conv") + if to_hu_conv is not None: + to_hu_conv_name = arr_name + "_conv" + else: + to_hu_conv_name = None from_hu_conv = self.consts.primitive_arr_from_hu(ty_info.subty, None, arr_name) if subty.from_hu_conv is not None: hu_conv_b = "" diff --git a/typescript_strings.py b/typescript_strings.py index 0d303642..24a146a9 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -228,7 +228,23 @@ const decodeUint32Array = (arrayPointer: number, free = true) => { } return actualArray; } - +/* @internal */ +export function decodeUint64Array (arrayPointer: number, free = true): bigint[] { + const arraySize = getArrayLength(arrayPointer); + const actualArrayViewer = new BigUint64Array( + wasm.memory.buffer, // value + arrayPointer + 4, // offset (ignoring length bytes) + arraySize // uint32 count + ); + // 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. + const actualArray = new Array(arraySize); + for (var i = 0; i < arraySize; i++) actualArray[i] = actualArrayViewer[i]; + if (free) { + wasm.TS_free(arrayPointer); + } + return actualArray; +} export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); } @@ -720,8 +736,12 @@ import * as bindings from '../bindings.mjs' 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 + ");" + if mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t": + return "const " + conv_name + ": Uint8Array = bindings.decodeUint8Array(" + arr_name + ");" + elif mapped_ty.c_ty == "uint64_t" or mapped_ty.c_ty == "int64_t": + return "const " + conv_name + ": bigint[] = bindings.decodeUint64Array(" + arr_name + ");" + else: + assert False def var_decl_statement(self, ty_string, var_name, statement): return "const " + var_name + ": " + ty_string + " = " + statement