Merge pull request #119 from TheBlueMatt/main
[ldk-java] / typescript_strings.py
index 765a93cda50c1247350c1ce6fc959629c8cc088e..84dd9a1565f5d61860bc4edecdfb7fb59b9014a9 100644 (file)
@@ -125,7 +125,8 @@ async function finishInitializeWasm(wasmInstance: WebAssembly.Instance) {
        isWasmInitialized = true;
 }
 
-const fn_list = ["uuuuuu", "buuuuu", "bbbbbb", "ubuuuu", "uubuuu"];
+const fn_list = ["uuuuuu", "buuuuu", "bbuuuu", "bbbuuu", "bbbbuu",
+       "bbbbbb", "ubuubu", "ubuuuu", "ubbuuu", "uubuuu", "uububu"];
 
 /* @internal */
 export async function initializeWasmFromUint8Array(wasmBinary: Uint8Array) {
@@ -253,6 +254,12 @@ export function decodeUint64Array (arrayPointer: number, free = true): bigint[]
 
 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];
+}
+
 /* @internal */
 export function getU32ArrayElem(arrayPointer: number, idx: number): number {
        const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
@@ -322,18 +329,18 @@ export async function initializeWasmFromBinary(bin: Uint8Array) {
         self.common_base = """
 function freer(f: () => void) { f() }
 const finalizer = new FinalizationRegistry(freer);
-function get_freeer(ptr: number, free_fn: (ptr: number) => void) {
+function get_freeer(ptr: bigint, free_fn: (ptr: bigint) => void) {
        return () => {
                free_fn(ptr);
        }
 }
 
 export class CommonBase {
-       protected ptr: number;
+       protected ptr: bigint;
        protected ptrs_to: object[] = [];
-       protected constructor(ptr: number, free_fn: (ptr: number) => void) {
+       protected constructor(ptr: bigint, free_fn: (ptr: bigint) => void) {
                this.ptr = ptr;
-               if (Number.isFinite(ptr) && ptr != 0){
+               if (ptr != 0n){
                        finalizer.register(this, get_freeer(ptr, free_fn), this);
                }
        }
@@ -341,13 +348,13 @@ export class CommonBase {
        // In TypeScript, protected means "any subclass can access parent fields on instances of itself"
        // To work around this, we add accessors for other instances' protected fields here.
        protected static add_ref_from(holder: CommonBase, referent: object) {
-               holder.ptrs_to.push(referent);
+               if (holder !== null) { holder.ptrs_to.push(referent); }
        }
        protected static get_ptr_of(o: CommonBase) {
                return o.ptr;
        }
        protected static set_null_skip_free(o: CommonBase) {
-               o.ptr = 0;
+               o.ptr = 0n;
                // @ts-ignore TypeScript is wrong about the returnvalue of unregister here!
                const did_unregister: boolean = finalizer.unregister(o);
                if (!did_unregister)
@@ -385,7 +392,7 @@ export class UnqualifiedError {
        public value: bigint;
 
        /* @internal */
-       public constructor(_dummy: object, ptr: number) {
+       public constructor(_dummy: object, 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);
@@ -396,6 +403,21 @@ export class UnqualifiedError {
 }"""
         self.obj_defined(["TxOut"], "structs")
 
+        self.scalar_defn = """export class BigEndianScalar extends CommonBase {
+       /** The bytes of the scalar value, in big endian */
+       public scalar_bytes: Uint8Array;
+
+       /* @internal */
+       public constructor(_dummy: object, ptr: bigint) {
+               super(ptr, bindings.BigEndianScalar_free);
+               this.scalar_bytes = bindings.decodeUint8Array(bindings.BigEndianScalar_get_bytes(ptr));
+       }
+       public static constructor_new(scalar_bytes: Uint8Array): BigEndianScalar {
+               return new BigEndianScalar(null, bindings.BigEndianScalar_new(bindings.encodeUint8Array(scalar_bytes)));
+       }
+}"""
+        self.obj_defined(["BigEndianScalar"], "structs")
+
         self.c_file_pfx = """#include "js-wasm.h"
 #include <stdatomic.h>
 #include <lightning.h>
@@ -592,6 +614,7 @@ _Static_assert(sizeof(void*) == 4, "Pointers mut be 32 bits");
        }
 
 DECL_ARR_TYPE(int64_t, int64_t);
+DECL_ARR_TYPE(uint64_t, uint64_t);
 DECL_ARR_TYPE(int8_t, int8_t);
 DECL_ARR_TYPE(uint32_t, uint32_t);
 DECL_ARR_TYPE(void*, ptr);
@@ -650,8 +673,11 @@ import * as bindings from '../bindings.mjs'
         self.util_fn_sfx = "}"
         self.c_fn_ty_pfx = ""
         self.file_ext = ".mts"
-        self.ptr_c_ty = "uint32_t"
-        self.ptr_native_ty = "number"
+        self.ptr_c_ty = "uint64_t"
+        self.ptr_native_ty = "bigint"
+        self.usize_c_ty = "uint32_t"
+        self.usize_native_ty = "number"
+        self.native_zero_ptr = "0n"
         self.result_c_ty = "uint32_t"
         self.ptr_arr = "ptrArray"
         self.is_arr_some_check = ("", " != 0")
@@ -694,7 +720,7 @@ import * as bindings from '../bindings.mjs'
     def map_hu_array_elems(self, arr_name, conv_name, arr_ty, elem_ty):
         if elem_ty.rust_obj == "LDKu5":
             return arr_name + " != null ? bindings.uint5ArrToBytes(" + arr_name + ") : null"
-        assert elem_ty.c_ty == "uint32_t" or elem_ty.c_ty.endswith("Array")
+        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"
 
     def str_ref_to_native_call(self, var_name, str_len):
@@ -715,8 +741,10 @@ import * as bindings from '../bindings.mjs'
     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"):
+        if elem_ty.c_ty.endswith("Array") or elem_ty.c_ty == "uintptr_t":
             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":
             return "bindings.getU8ArrayElem(" + arr_name + ", " + idx + ")"
         else:
@@ -737,7 +765,7 @@ import * as bindings from '../bindings.mjs'
             return ("bindings.encodeUint8Array(" + inner + ")", "")
         elif mapped_ty.c_ty == "uint32_t":
             return ("bindings.encodeUint32Array(" + inner + ")", "")
-        elif mapped_ty.c_ty == "int64_t":
+        elif mapped_ty.c_ty == "int64_t" or mapped_ty.c_ty == "uint64_t":
             return ("bindings.encodeUint64Array(" + inner + ")", "")
         else:
             print(mapped_ty.c_ty)
@@ -843,7 +871,7 @@ export enum {struct_name} {{
                 else:
                     bindings_instantiator += ", " + first_to_lower(var.arg_name)
             else:
-                bindings_instantiator += ", " + first_to_lower(var[1]) + ".bindings_instance"
+                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"
@@ -855,7 +883,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]) + ".bindings_instance"
+                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
@@ -938,7 +966,10 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
        public bindings_instance?: bindings.{struct_name};
 
        /* @internal */
-       constructor(_dummy: object, ptr: number) {{
+       public instance_idx?: number;
+
+       /* @internal */
+       constructor(_dummy: object, ptr: bigint) {{
                super(ptr, bindings.{struct_name.replace("LDK","")}_free);
                this.bindings_instance = null;
        }}
@@ -948,9 +979,10 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                const impl_holder: {struct_name}Holder = new {struct_name}Holder();
                let structImplementation = {{
 {out_interface_implementation_overrides}               }} as bindings.{struct_name};
-{super_constructor_statements}         const ptr: number = bindings.{struct_name}_new(structImplementation{bindings_instantiator});
+{super_constructor_statements}         const ptr_idx: [bigint, number] = bindings.{struct_name}_new(structImplementation{bindings_instantiator});
 
-               impl_holder.held = new {struct_name.replace("LDK", "")}(null, ptr);
+               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;
        }}
@@ -973,14 +1005,18 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
 
         out_typescript_bindings += "}\n\n"
 
+        c_call_extra_args = ""
         out_typescript_bindings += f"/* @internal */\nexport function {struct_name}_new(impl: {struct_name}"
         for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_typescript_bindings += f", {var.arg_name}: {var.java_ty}"
+                c_call_extra_args += f", {var.arg_name}"
             else:
-                out_typescript_bindings += f", {var[1]}: {var[0]}"
+                out_typescript_bindings += f", {var[1]}: number"
+                c_call_extra_args += f", {var[1]}"
+
 
-        out_typescript_bindings += f"""): number {{
+        out_typescript_bindings += f"""): [bigint, number] {{
        if(!isWasmInitialized) {{
                throw new Error("initializeWasm() must be awaited first!");
        }}
@@ -989,7 +1025,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                if (js_objs[i] == null || js_objs[i] == undefined) {{ new_obj_idx = i; break; }}
        }}
        js_objs[i] = new WeakRef(impl);
-       return wasm.TS_{struct_name}_new(i);
+       return [wasm.TS_{struct_name}_new(i{c_call_extra_args}), i];
 }}
 """
 
@@ -1054,15 +1090,19 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                 elif not fn_line.ret_ty_info.passed_as_ptr:
                     out_c += "\treturn js_invoke_function_" + fn_suffix + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
                 else:
-                    out_c += "\tuint32_t ret = js_invoke_function_" + fn_suffix + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
+                    out_c += "\tuint64_t ret = js_invoke_function_" + fn_suffix + "(j_calls->instance_ptr, " + str(self.function_ptr_counter)
 
                 self.function_ptrs[self.function_ptr_counter] = (struct_name, fn_line.fn_name)
                 self.function_ptr_counter += 1
 
                 for idx, arg_info in enumerate(fn_line.args_ty):
                     if arg_info.ret_conv is not None:
-                        out_c += ", (uint32_t)" + arg_info.ret_conv_name
+                        if arg_info.c_ty.endswith("Array"):
+                            out_c += ", (uint32_t)" + arg_info.ret_conv_name
+                        else:
+                            out_c += ", " + arg_info.ret_conv_name
                     else:
+                        assert False # TODO: Would we need some conversion here?
                         out_c += ", (uint32_t)" + arg_info.arg_name
                 for i in range(0, 6 - len(fn_line.args_ty)):
                     out_c += ", 0"
@@ -1133,7 +1173,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
         out_c = out_c + "\treturn ret;\n"
         out_c = out_c + "}\n"
 
-        out_c = out_c + self.c_fn_ty_pfx + "long " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "JSValue o"
+        out_c = out_c + self.c_fn_ty_pfx + "uint64_t " + self.c_fn_name_define_pfx(struct_name + "_new", True) + "JSValue o"
         for var in flattened_field_var_conversions:
             if isinstance(var, ConvInfo):
                 out_c = out_c + ", " + var.c_ty + " " + var.arg_name
@@ -1148,7 +1188,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
             else:
                 out_c = out_c + ", " + var[1]
         out_c = out_c + ");\n"
-        out_c = out_c + "\treturn (long)res_ptr;\n"
+        out_c = out_c + "\treturn tag_ptr(res_ptr, true);\n"
         out_c = out_c + "}\n"
 
         return (out_typescript_bindings, out_typescript_human, out_c)
@@ -1171,12 +1211,12 @@ 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: number) { super(ptr, bindings." + bindings_type + "_free); }\n"
+        java_hu_class += "\tprotected constructor(_dummy: object, 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: number): {java_hu_type} {{\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"
         out_c += self.c_fn_ty_pfx + "uint32_t" + self.c_fn_name_define_pfx(struct_name + "_ty_from_ptr", True) + self.ptr_c_ty + " ptr) {\n"
-        out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n"
+        out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
         out_c += "\tswitch(obj->tag) {\n"
         java_hu_class += "\t\tswitch (raw_ty) {\n"
         java_hu_subclasses = ""
@@ -1202,13 +1242,13 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                 else:
                     hu_conv_body += f"\t\tthis.{field_ty.arg_name} = bindings.{struct_name}_{var.var_name}_get_{field_ty.arg_name}(ptr);\n"
             java_hu_subclasses += "\t/* @internal */\n"
-            java_hu_subclasses += "\tpublic constructor(ptr: number) {\n\t\tsuper(null, ptr);\n"
+            java_hu_subclasses += "\tpublic constructor(ptr: bigint) {\n\t\tsuper(null, ptr);\n"
             java_hu_subclasses = java_hu_subclasses + hu_conv_body
             java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
             var_idx += 1
         out_java += "}\n"
         java_hu_class += "\t\t\tdefault:\n\t\t\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t\t}\n\t}\n\n"
-        out_java += self.fn_call_body(struct_name + "_ty_from_ptr", "uint32_t", "number", "ptr: number", "ptr")
+        out_java += self.fn_call_body(struct_name + "_ty_from_ptr", "uint32_t", "number", "ptr: bigint", "ptr")
         out_c += ("\t\tdefault: abort();\n")
         out_c += ("\t}\n}\n")
 
@@ -1216,7 +1256,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
             for idx, (field_map, _) in enumerate(var.fields):
                 fn_name = f"{struct_name}_{var.var_name}_get_{field_map.arg_name}"
                 out_c += self.c_fn_ty_pfx + field_map.c_ty + self.c_fn_name_define_pfx(fn_name, True) + self.ptr_c_ty + " ptr) {\n"
-                out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n"
+                out_c += "\t" + struct_name + " *obj = (" + struct_name + "*)untag_ptr(ptr);\n"
                 out_c += f"\tassert(obj->tag == {struct_name}_{var.var_name});\n"
                 if field_map.ret_conv is not None:
                     out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
@@ -1232,7 +1272,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
                     else:
                         out_c += "\treturn " + "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name + ";\n"
                 out_c += "}\n"
-                out_java += self.fn_call_body(fn_name, field_map.c_ty, field_map.java_ty, "ptr: number", "ptr")
+                out_java += self.fn_call_body(fn_name, field_map.c_ty, field_map.java_ty, "ptr: bigint", "ptr")
         out_java_enum += java_hu_class
         self.struct_file_suffixes[java_hu_type] = java_hu_subclasses
         self.obj_defined([java_hu_type], "structs")
@@ -1263,7 +1303,7 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
  */
 export class {hu_name} extends CommonBase {implementations}{{
        /* @internal */
-       public constructor(_dummy: object, ptr: number) {{
+       public constructor(_dummy: object, ptr: bigint) {{
                {constructor_body}
        }}{extra_body}
 
@@ -1282,7 +1322,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: number) {{
+       public constructor(_dummy: object, ptr: bigint) {{
                super(_dummy, ptr);
 """
         if res_map.java_hu_ty == "void":
@@ -1300,7 +1340,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: number) {{
+       public constructor(_dummy: object, ptr: bigint) {{
                super(_dummy, ptr);
 """
         if err_map.java_hu_ty == "void":
@@ -1319,11 +1359,11 @@ 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: number) {{
+       protected constructor(_dummy: object, ptr: bigint) {{
                super(ptr, bindings.{struct_name.replace("LDK","")}_free);
        }}
        /* @internal */
-       public static constr_from_ptr(ptr: number): {human_ty} {{
+       public static constr_from_ptr(ptr: bigint): {human_ty} {{
                if (bindings.{struct_name.replace("LDK", "")}_is_ok(ptr)) {{
                        return new {human_ty}_OK(null, ptr);
                }} else {{