]> git.bitcoin.ninja Git - ldk-java/commitdiff
Use file_ext everywhere, common base/header for TS files
authorArik Sosman <git@arik.io>
Tue, 12 Jan 2021 16:26:26 +0000 (11:26 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 12 Jan 2021 16:51:19 +0000 (11:51 -0500)
genbindings.py
java_strings.py
typescript_strings.py

index 24dbf20c547ffddfaccada8b7b5f0c65affd80f2..e684a420fbe39ced3b489900ab3fd6e0278ffb9e 100755 (executable)
@@ -788,7 +788,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
 
         out_java_struct = None
         if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
-            out_java_struct = open(sys.argv[3] + "/structs/" + struct_meth + ".java", "a")
+            out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
             if not args_known:
                 out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n")
                 out_java_struct.close()
@@ -909,7 +909,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
             out_java_struct.close()
 
     def map_unitary_enum(struct_name, field_lines):
-        with open(sys.argv[3] + "/enums/" + struct_name + ".java", "w") as out_java_enum:
+        with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum:
             unitary_enums.add(struct_name)
             for idx, struct_line in enumerate(field_lines):
                 if idx == 0:
@@ -1029,7 +1029,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
             out_java_enum.write("}\n")
 
     def map_trait(struct_name, field_var_lines, trait_fn_lines):
-        with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "w") as out_java_trait:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait:
             field_var_convs = []
             for var_line in field_var_lines:
                 if var_line.group(1) in trait_structs:
@@ -1272,7 +1272,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
     def map_result(struct_name, res_ty, err_ty):
         result_types.add(struct_name)
         human_ty = struct_name.replace("LDKCResult", "Result")
-        with open(sys.argv[3] + "/structs/" + human_ty + consts.file_ext, "w") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct:
             out_java_struct.write(consts.hu_struct_file_prefix)
             out_java_struct.write("public class " + human_ty + " extends CommonBase {\n")
             out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n")
@@ -1495,16 +1495,8 @@ public class bindings {
 
 """)
 
-    with open(sys.argv[3] + "/structs/CommonBase" + consts.file_ext, "a") as out_java_struct:
-        out_java_struct.write("""package org.ldk.structs;
-import java.util.LinkedList;
-class CommonBase {
-       long ptr;
-       LinkedList<Object> ptrs_to = new LinkedList();
-       protected CommonBase(long ptr) { this.ptr = ptr; }
-       public long _test_only_get_ptr() { return this.ptr; }
-}
-""")
+    with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "a") as out_java_struct:
+        out_java_struct.write(consts.common_base)
 
     in_block_comment = False
     cur_block_obj = None
@@ -1594,7 +1586,7 @@ class CommonBase {
 
                 if is_opaque:
                     opaque_structs.add(struct_name)
-                    with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "w") as out_java_struct:
+                    with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_struct:
                         out_java_struct.write(consts.hu_struct_file_prefix)
                         out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase")
                         if struct_name.startswith("LDKLocked"):
@@ -1680,7 +1672,7 @@ class CommonBase {
                     trait_structs.add(struct_name)
                     map_trait(struct_name, field_var_lines, trait_fn_lines)
                 elif struct_name == "LDKTxOut":
-                    with open(sys.argv[3] + "/structs/TxOut" + consts.file_ext, "w") as out_java_struct:
+                    with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct:
                         out_java_struct.write(consts.hu_struct_file_prefix)
                         out_java_struct.write("public class TxOut extends CommonBase{\n")
                         out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n")
@@ -1727,10 +1719,10 @@ class CommonBase {
 
     out_java.write("}\n")
     for struct_name in opaque_structs:
-        with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "a") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct:
             out_java_struct.write("}\n")
     for struct_name in trait_structs:
-        with open(sys.argv[3] + "/structs/" + struct_name.replace("LDK","") + consts.file_ext, "a") as out_java_struct:
+        with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct:
             out_java_struct.write("}\n")
 with open(sys.argv[4], "w") as out_c:
     out_c.write(consts.c_file_pfx)
index 45bb7e8273ff81f8010e717241a1c7f75b77a852..5aefd223a6e8f1eae9a6026a1724f051902382c1 100644 (file)
@@ -2,6 +2,16 @@ from bindingstypes import *
 
 class Consts:
     def __init__(self, DEBUG):
+        self.common_base = """package org.ldk.structs;
+import java.util.LinkedList;
+class CommonBase {
+       long ptr;
+       LinkedList<Object> ptrs_to = new LinkedList();
+       protected CommonBase(long ptr) { this.ptr = ptr; }
+       public long _test_only_get_ptr() { return this.ptr; }
+}
+"""
+
         self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
 #include <rust_types.h>
 #include <lightning.h>
index 70e37331ba56db6230a7e39f6b2511f069ede9e7..dccd8cf3a8d9b0a0133868df8ca5e66818bfe708 100644 (file)
@@ -1,5 +1,14 @@
 class Consts:
     def __init__(self, DEBUG):
+        self.common_base = """
+            export default class CommonBase {
+                ptr: number;
+                ptrs_to: object[] = new Array(); // new LinkedList(); TODO: build linked list implementation
+                protected constructor(ptr: number) { this.ptr = ptr; }
+                public _test_only_get_ptr(): number { return this.ptr; }
+            }
+        """
+
         self.c_file_pfx = """#include <rust_types.h>
 #include <stdatomic.h>
 #include <lightning.h>
@@ -129,14 +138,10 @@ typedef bool jboolean;
 
 """
 
-        self.hu_struct_file_prefix = """package org.ldk.structs;
-
-import org.ldk.impl.bindings;
-import org.ldk.enums.*;
-import org.ldk.util.*;
-import java.util.Arrays;
+        self.hu_struct_file_prefix = f"""
+import CommonBase from './CommonBase';
+import * as bindings from '../bindings' // TODO: figure out location
 
-@SuppressWarnings("unchecked") // We correctly assign various generic arrays
 """
         self.c_fn_ty_pfx = ""
         self.c_fn_name_pfx = ""