[TS] Support lock structs by requiring manual free calls
authorMatt Corallo <git@bluematt.me>
Fri, 5 Aug 2022 00:54:08 +0000 (00:54 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 5 Aug 2022 01:56:49 +0000 (01:56 +0000)
typescript_strings.py

index c5d9f47747f13143c4e9503b7437bc10cb95e6df..6eb5a10a186f9fb7cde4d2d3933363d97ccc69ed 100644 (file)
@@ -1238,18 +1238,28 @@ export class {struct_name.replace("LDK","")} extends CommonBase {{
 
         hu_name = struct_name.replace("LDKC2Tuple", "TwoTuple").replace("LDKC3Tuple", "ThreeTuple").replace("LDK", "")
         out_opaque_struct_human = f"{self.hu_struct_file_prefix}"
+        constructor_body = "super(ptr, bindings." + struct_name.replace("LDK","") + "_free);"
+        extra_docs = ""
+        extra_body = ""
         if struct_name.startswith("LDKLocked") or struct_name.startswith("LDKReadOnly"):
-            out_opaque_struct_human += "/** XXX: DO NOT USE THIS - it remains locked until the GC runs (if that ever happens */"
+            extra_docs = "\n * This type represents a lock and MUST BE MANUALLY FREE'd!"
+            constructor_body = 'super(ptr, () => { throw new Error("Locks must be manually freed with free()"); });'
+            extra_body = f"""
+       /** Releases this lock */
+       public free() {{
+               bindings.{struct_name.replace("LDK","")}_free(this.ptr);
+               CommonBase.set_null_skip_free(this);
+       }}"""
         formatted_doc_comment = struct_doc_comment.replace("\n", "\n * ")
         out_opaque_struct_human += f"""
-/**
+/**{extra_docs}
  * {formatted_doc_comment}
  */
 export class {hu_name} extends CommonBase {implementations}{{
        /* @internal */
        public constructor(_dummy: object, ptr: number) {{
-               super(ptr, bindings.{struct_name.replace("LDK","")}_free);
-       }}
+               {constructor_body}
+       }}{extra_body}
 
 """
         self.obj_defined([hu_name], "structs")