New manual U5 (from u5) and Witness types
[ldk-java] / typescript_strings.py
index aeb2a4ae8e68b5ac7214eec3a0c5273e844cfb14..46a2991c9f83f355b1c438247eb8ca12e802e55d 100644 (file)
@@ -53,7 +53,7 @@ imports.wasi_snapshot_preview1 = {
                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("[fd " + fd + "]: " + String.fromCharCode(...bytes_view));
-                       bytes_written += ptr_len_view[i*2+1];
+                       bytes_written += ptr_len_view[i*2+1]!;
                }
                const written_view = new Uint32Array(wasm.memory.buffer, bytes_written_ptr, 1);
                written_view[0] = bytes_written;
@@ -82,7 +82,7 @@ imports.wasi_snapshot_preview1 = {
                out_len_view[0] = 0;
                return 0;
        },
-       "environ_get": (environ_ptr: number, environ_buf_ptr: number) => {
+       "environ_get": (_environ_ptr: number, _environ_buf_ptr: number) => {
                // This is called before fd_write to format + print panic messages,
                // but only if we have variables in environ_sizes_get, so shouldn't ever actually happen!
                console.log("wasi_snapshot_preview1:environ_get");
@@ -150,7 +150,7 @@ export async function initializeWasmFetch(uri: string) {
 export function uint5ArrToBytes(inputArray: Array<UInt5>): Uint8Array {
        const arr = new Uint8Array(inputArray.length);
        for (var i = 0; i < inputArray.length; i++) {
-               arr[i] = inputArray[i].getVal();
+               arr[i] = inputArray[i]!.getVal();
        }
        return arr;
 }
@@ -159,13 +159,23 @@ export function uint5ArrToBytes(inputArray: Array<UInt5>): Uint8Array {
 export function WitnessVersionArrToBytes(inputArray: Array<WitnessVersion>): Uint8Array {
        const arr = new Uint8Array(inputArray.length);
        for (var i = 0; i < inputArray.length; i++) {
-               arr[i] = inputArray[i].getVal();
+               arr[i] = inputArray[i]!.getVal();
        }
        return arr;
 }
 
 
 
+/* @internal */
+export function encodeUint128 (inputVal: bigint): number {
+       if (inputVal >= 0x10000000000000000000000000000000n) throw "U128s cannot exceed 128 bits";
+       const cArrayPointer = wasm.TS_malloc(16 + 8);
+       const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
+       arrayLengthView[0] = BigInt(16);
+       const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, 16);
+       for (var i = 0; i < 16; i++) arrayMemoryView[i] = Number((inputVal >> BigInt(i)*8n) & 0xffn);
+       return cArrayPointer;
+}
 /* @internal */
 export function encodeUint8Array (inputArray: Uint8Array|null): number {
        if (inputArray == null) return 0;
@@ -197,7 +207,7 @@ export function encodeUint64Array (inputArray: BigUint64Array|Array<bigint>|null
 }
 
 /* @internal */
-export function check_arr_len(arr: Uint8Array|null, len: number): Uint8Array {
+export function check_arr_len(arr: Uint8Array|null, len: number): Uint8Array|null {
        if (arr !== null && arr.length != len) { throw new Error("Expected array of length " + len + " got " + arr.length); }
        return arr;
 }
@@ -205,11 +215,26 @@ export function check_arr_len(arr: Uint8Array|null, len: number): Uint8Array {
 /* @internal */
 export function getArrayLength(arrayPointer: number): number {
        const arraySizeViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer, 1);
-       const len = arraySizeViewer[0];
+       const len = arraySizeViewer[0]!;
        if (len >= (2n ** 32n)) throw new Error("Bogus Array Size");
        return Number(len % (2n ** 32n));
 }
 /* @internal */
+export function decodeUint128 (arrayPointer: number, free = true): bigint {
+       const arraySize = getArrayLength(arrayPointer);
+       if (arraySize != 16) throw "Need 16 bytes for a uint128";
+       const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
+       var val = 0n;
+       for (var i = 0; i < 16; i++) {
+               val <<= 8n;
+               val |= BigInt(actualArrayViewer[i]!);
+       }
+       if (free) {
+               wasm.TS_free(arrayPointer);
+       }
+       return val;
+}
+/* @internal */
 export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
        const arraySize = getArrayLength(arrayPointer);
        const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
@@ -260,19 +285,19 @@ export function freeWasmMemory(pointer: number) { wasm.TS_free(pointer); }
 /* @internal */
 export function getU64ArrayElem(arrayPointer: number, idx: number): bigint {
        const actualArrayViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
-       return actualArrayViewer[idx];
+       return actualArrayViewer[idx]!;
 }
 
 /* @internal */
 export function getU32ArrayElem(arrayPointer: number, idx: number): number {
        const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
-       return actualArrayViewer[idx];
+       return actualArrayViewer[idx]!;
 }
 
 /* @internal */
 export function getU8ArrayElem(arrayPointer: number, idx: number): number {
        const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
-       return actualArrayViewer[idx];
+       return actualArrayViewer[idx]!;
 }
 
 
@@ -384,7 +409,7 @@ export class WitnessVersion {
 }
 
 export class UnqualifiedError {
-       public constructor(val: number) {}
+       public constructor(_val: number) {}
 }
 """
 
@@ -395,7 +420,7 @@ export class UnqualifiedError {
        public value: bigint;
 
        /* @internal */
-       public constructor(_dummy: object, ptr: bigint) {
+       public constructor(_dummy: null, ptr: bigint) {
                super(ptr, bindings.TxOut_free);
                this.script_pubkey = bindings.decodeUint8Array(bindings.TxOut_get_script_pubkey(ptr));
                this.value = bindings.TxOut_get_value(ptr);
@@ -411,7 +436,7 @@ export class UnqualifiedError {
        public scalar_bytes: Uint8Array;
 
        /* @internal */
-       public constructor(_dummy: object, ptr: bigint) {
+       public constructor(_dummy: null, ptr: bigint) {
                super(ptr, bindings.BigEndianScalar_free);
                this.scalar_bytes = bindings.decodeUint8Array(bindings.BigEndianScalar_get_bytes(ptr));
        }
@@ -678,6 +703,7 @@ import * as bindings from '../bindings.mjs'
         self.file_ext = ".mts"
         self.ptr_c_ty = "uint64_t"
         self.ptr_native_ty = "bigint"
+        self.u128_native_ty = "bigint"
         self.usize_c_ty = "uint32_t"
         self.usize_native_ty = "number"
         self.native_zero_ptr = "0n"
@@ -690,7 +716,7 @@ import * as bindings from '../bindings.mjs'
         return None
     def create_native_arr_call(self, arr_len, ty_info):
         if ty_info.c_ty == "ptrArray":
-            assert ty_info.rust_obj == "LDKCVec_u5Z" or (ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array"))
+            assert ty_info.rust_obj == "LDKCVec_U5Z" or (ty_info.subty is not None and ty_info.subty.c_ty.endswith("Array"))
         return "init_" + ty_info.c_ty + "(" + arr_len + ", __LINE__)"
     def set_native_arr_contents(self, arr_name, arr_len, ty_info):
         if ty_info.c_ty == "int8_tArray":
@@ -721,7 +747,7 @@ import * as bindings from '../bindings.mjs'
             return "FREE(" + arr_name + ")"
 
     def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
-        if elem_ty.rust_obj == "LDKu5":
+        if elem_ty.rust_obj == "LDKU5":
             return arr_name + " != null ? bindings.uint5ArrToBytes(" + arr_name + ") : null"
         assert elem_ty.c_ty == "uint64_t" or elem_ty.c_ty.endswith("Array")
         return arr_name + " != null ? " + arr_name + ".map(" + conv_name + " => " + elem_ty.from_hu_conv[0] + ") : null"
@@ -748,7 +774,7 @@ import * as bindings from '../bindings.mjs'
             return "bindings.getU32ArrayElem(" + arr_name + ", " + idx + ")"
         elif elem_ty.c_ty == "uint64_t":
             return "bindings.getU64ArrayElem(" + arr_name + ", " + idx + ")"
-        elif elem_ty.rust_obj == "LDKu5":
+        elif elem_ty.rust_obj == "LDKU5":
             return "bindings.getU8ArrayElem(" + arr_name + ", " + idx + ")"
         else:
             assert False
@@ -757,8 +783,11 @@ import * as bindings from '../bindings.mjs'
     def cleanup_converted_native_array(self, ty_info, arr_name):
         return "bindings.freeWasmMemory(" + arr_name + ")"
 
-    def primitive_arr_from_hu(self, mapped_ty, fixed_len, arr_name):
+    def primitive_arr_from_hu(self, arr_ty, fixed_len, arr_name):
+        mapped_ty = arr_ty.subty
         inner = arr_name
+        if arr_ty.rust_obj == "LDKU128":
+            return ("bindings.encodeUint128(" + inner + ")", "")
         if fixed_len is not None:
             assert mapped_ty.c_ty == "int8_t"
             inner = "bindings.check_arr_len(" + arr_name + ", " + fixed_len + ")"
@@ -774,8 +803,11 @@ import * as bindings from '../bindings.mjs'
             print(mapped_ty.c_ty)
             assert False
 
-    def primitive_arr_to_hu(self, mapped_ty, fixed_len, arr_name, conv_name):
-        if mapped_ty.c_ty == "uint8_t" or mapped_ty.c_ty == "int8_t":
+    def primitive_arr_to_hu(self, arr_ty, fixed_len, arr_name, conv_name):
+        mapped_ty = arr_ty.subty
+        if arr_ty.rust_obj == "LDKU128":
+            return "const " + conv_name + ": bigint = bindings.decodeUint128(" + arr_name + ");"
+        elif 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 + ");"
@@ -874,7 +906,7 @@ export enum {struct_name} {{
                 else:
                     bindings_instantiator += ", " + first_to_lower(var.arg_name)
             else:
-                bindings_instantiator += ", " + first_to_lower(var[1]) + ".instance_idx"
+                bindings_instantiator += ", " + first_to_lower(var[1]) + ".instance_idx!"
                 super_instantiator += first_to_lower(var[1]) + "_impl, "
                 pointer_to_adder += "\t\timpl_holder.held.ptrs_to.push(" + first_to_lower(var[1]) + ");\n"
                 impl_constructor_arguments += f", {first_to_lower(var[1])}_impl: {var[0].replace('LDK', '')}Interface"
@@ -886,7 +918,7 @@ export enum {struct_name} {{
                 trait_constructor_arguments += ", " + var.arg_name
             else:
                 super_constructor_statements += "\t\tconst " + first_to_lower(var[1]) + " = " + var[1] + ".new_impl(" + super_instantiator + ");\n"
-                trait_constructor_arguments += ", " + first_to_lower(var[1]) + ".instance_idx"
+                trait_constructor_arguments += ", " + first_to_lower(var[1]) + ".instance_idx!"
                 for suparg in var[2]:
                     if isinstance(suparg, ConvInfo):
                         trait_constructor_arguments += ", " + suparg.arg_name
@@ -958,7 +990,7 @@ export interface {struct_name.replace("LDK", "")}Interface {{
 {out_java_interface}}}
 
 class {struct_name}Holder {{
-       held: {struct_name.replace("LDK", "")};
+       held: {struct_name.replace("LDK", "")}|null = null;
 }}
 
 /**
@@ -966,13 +998,13 @@ class {struct_name}Holder {{
  */
 export class {struct_name.replace("LDK","")} extends CommonBase {{
        /* @internal */
-       public bindings_instance?: bindings.{struct_name};
+       public bindings_instance: bindings.{struct_name}|null;
 
        /* @internal */
        public instance_idx?: number;
 
        /* @internal */
-       constructor(_dummy: object, ptr: bigint) {{
+       constructor(_dummy: null, ptr: bigint) {{
                super(ptr, bindings.{struct_name.replace("LDK","")}_free);
                this.bindings_instance = null;
        }}
@@ -987,7 +1019,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                impl_holder.held = new {struct_name.replace("LDK", "")}(null, ptr_idx[0]);
                impl_holder.held.instance_idx = ptr_idx[1];
                impl_holder.held.bindings_instance = structImplementation;
-{pointer_to_adder}             return impl_holder.held;
+{pointer_to_adder}             return impl_holder.held!;
        }}
 
 """
@@ -1214,7 +1246,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
 
         java_hu_class = "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
         java_hu_class += "export class " + java_hu_type + " extends CommonBase {\n"
-        java_hu_class += "\tprotected constructor(_dummy: object, ptr: bigint) { super(ptr, bindings." + bindings_type + "_free); }\n"
+        java_hu_class += "\tprotected constructor(_dummy: null, ptr: bigint) { super(ptr, bindings." + bindings_type + "_free); }\n"
         java_hu_class += "\t/* @internal */\n"
         java_hu_class += f"\tpublic static constr_from_ptr(ptr: bigint): {java_hu_type} {{\n"
         java_hu_class += f"\t\tconst raw_ty: number = bindings." + struct_name + "_ty_from_ptr(ptr);\n"
@@ -1306,7 +1338,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
  */
 export class {hu_name} extends CommonBase {implementations}{{
        /* @internal */
-       public constructor(_dummy: object, ptr: bigint) {{
+       public constructor(_dummy: null, ptr: bigint) {{
                {constructor_body}
        }}{extra_body}
 
@@ -1325,7 +1357,7 @@ export class {hu_name} extends CommonBase {implementations}{{
             suffixes += "\tpublic res: " + res_map.java_hu_ty + ";\n"
         suffixes += f"""
        /* @internal */
-       public constructor(_dummy: object, ptr: bigint) {{
+       public constructor(_dummy: null, ptr: bigint) {{
                super(_dummy, ptr);
 """
         if res_map.java_hu_ty == "void":
@@ -1343,7 +1375,7 @@ export class {hu_name} extends CommonBase {implementations}{{
             suffixes += "\tpublic err: " + err_map.java_hu_ty + ";\n"
         suffixes += f"""
        /* @internal */
-       public constructor(_dummy: object, ptr: bigint) {{
+       public constructor(_dummy: null, ptr: bigint) {{
                super(_dummy, ptr);
 """
         if err_map.java_hu_ty == "void":
@@ -1362,7 +1394,7 @@ export class {hu_name} extends CommonBase {implementations}{{
         return f"""{self.hu_struct_file_prefix}
 
 export class {human_ty} extends CommonBase {{
-       protected constructor(_dummy: object, ptr: bigint) {{
+       protected constructor(_dummy: null, ptr: bigint) {{
                super(ptr, bindings.{struct_name.replace("LDK","")}_free);
        }}
        /* @internal */
@@ -1443,6 +1475,8 @@ export function {method_name}({method_argument_string}): {return_java_ty} {{
                             out_java_struct += arg.arg_name + "_" + explode_arg.arg_name + ": " + explode_arg.java_hu_ty
                     else:
                         out_java_struct += arg.arg_name + ": " + arg.java_hu_ty
+                        if arg.nullable:
+                            out_java_struct += "|null"
 
         out_c += (") {\n")
         if out_java_struct is not None:
@@ -1549,12 +1583,12 @@ export function {method_name}({method_argument_string}): {return_java_ty} {{
             bindings.write("""
 
 js_invoke = function(obj_ptr: number, fn_id: number, arg1: bigint|number, arg2: bigint|number, arg3: bigint|number, arg4: bigint|number, arg5: bigint|number, arg6: bigint|number, arg7: bigint|number, arg8: bigint|number, arg9: bigint|number, arg10: bigint|number) {
-       const weak: WeakRef<object> = js_objs[obj_ptr];
+       const weak: WeakRef<object>|undefined = js_objs[obj_ptr];
        if (weak == null || weak == undefined) {
                console.error("Got function call on unknown/free'd JS object!");
                throw new Error("Got function call on unknown/free'd JS object!");
        }
-       const obj: object = weak.deref();
+       const obj = weak.deref();
        if (obj == null || obj == undefined) {
                console.error("Got function call on GC'd JS object!");
                throw new Error("Got function call on GC'd JS object!");