]> git.bitcoin.ninja Git - ldk-java/commitdiff
[TS] Fix generation for traits that have supertraits
authorMatt Corallo <git@bluematt.me>
Mon, 3 Oct 2022 20:07:28 +0000 (20:07 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 3 Oct 2022 20:38:23 +0000 (20:38 +0000)
When we have a trait with a supertrait, we have to pass both the
main- and super-trait instance indxes through to C so that it can
call both. We failed to do this, missing an argument entirely when
calling the C code.

Instead of trying to allocate a new instance index, we opt to store
instance indexes in trait structs (assuming there is one) and then
reuse the one in the constructed object.

typescript_strings.py

index 52dd12ce03bf0822023beb18390e429e2f0bd65a..84dd9a1565f5d61860bc4edecdfb7fb59b9014a9 100644 (file)
@@ -871,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"
@@ -883,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
@@ -965,6 +965,9 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
        /* @internal */
        public bindings_instance?: bindings.{struct_name};
 
+       /* @internal */
+       public instance_idx?: number;
+
        /* @internal */
        constructor(_dummy: object, ptr: bigint) {{
                super(ptr, bindings.{struct_name.replace("LDK","")}_free);
@@ -976,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: bigint = 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;
        }}
@@ -1001,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"""): bigint {{
+        out_typescript_bindings += f"""): [bigint, number] {{
        if(!isWasmInitialized) {{
                throw new Error("initializeWasm() must be awaited first!");
        }}
@@ -1017,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];
 }}
 """