[TS] Resolve locally-defined structs in other structs, create index.mts
authorMatt Corallo <git@bluematt.me>
Fri, 7 Jan 2022 18:36:54 +0000 (18:36 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 8 Jan 2022 04:06:53 +0000 (04:06 +0000)
genbindings.sh
typescript_strings.py

index 6c7f6501210dd816317a58b0150488c48680b791..77c936d9da129f46eae65670109100b2b0228a68 100755 (executable)
@@ -180,7 +180,7 @@ if [ "$2" != "wasm" ]; then
 else
        echo "Creating TS bindings..."
        mkdir -p ts/{enums,structs}
-       rm -f ts/{enums,structs,}/*.{mjs,mts}
+       rm -f ts/{enums,structs,}/*.{mjs,mts,mts.part}
        if [ "$4" = "false" ]; then
                ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node
        else
@@ -214,7 +214,11 @@ else
 
        if [ -x "$(which tsc)" ]; then
                cd ts
-       rm -r structs # TODO: Make the human-types compile
+               for F in structs/*; do
+                       cat imports.mts.part | grep -v " $(basename -s .mts $F)[ ,]" | cat - $F > $F.tmp
+                       mv $F.tmp $F
+               done
+               rm imports.mts.part
                if [ "$4" = "true" ]; then
                        tsc
                else
index ab7bd54a60d74e9491cce4344d9195e28afb40a6..379eaa6bd3f48c58421153c9e1c86307a2fd43c4 100644 (file)
@@ -522,6 +522,12 @@ const decodeString = (stringPointer, free = true) => {
     def add_ref(self, holder, referent):
         return "CommonBase.add_ref_from(" + holder + ", " + referent + ")"
 
+    def obj_defined(self, struct_names, folder):
+        with open(self.outdir + "/index.mts", 'a') as index:
+            index.write(f"export * from './{folder}/{struct_names[0]}.mjs';\n")
+        with open(self.outdir + "/imports.mts.part", 'a') as imports:
+            imports.write(f"import {{ {', '.join(struct_names)} }} from '../{folder}/{struct_names[0]}.mjs';\n")
+
     def native_c_unitary_enum_map(self, struct_name, variants, enum_doc_comment):
         out_c = "static inline LDK" + struct_name + " LDK" + struct_name + "_from_js(int32_t ord) {\n"
         out_c = out_c + "\tswitch (ord) {\n"
@@ -555,6 +561,7 @@ const decodeString = (stringPointer, free = true) => {
             }}
 """
         out_typescript_enum = f"export {{ {struct_name} }} from \"../bindings.mjs\";"
+        self.obj_defined([struct_name], "enums")
         return (out_c, out_typescript_enum, out_typescript)
 
     def c_unitary_enum_to_native_call(self, ty_info):
@@ -966,6 +973,7 @@ const decodeString = (stringPointer, free = true) => {
         out_c += ("\t}\n}\n")
         out_java_enum += java_hu_class
         self.struct_file_suffixes[java_hu_type] = java_hu_subclasses
+        self.obj_defined([java_hu_type], "structs")
         return (out_java, out_java_enum, out_c)
 
     def map_opaque_struct(self, struct_name, struct_doc_comment):
@@ -984,6 +992,7 @@ export class {hu_name} extends CommonBase {implementations}{{
        }}
 
 """
+        self.obj_defined([hu_name], "structs")
         return out_opaque_struct_human
 
     def map_tuple(self, struct_name):