Parse complex enum fields prior to per-language logic, drop java-isms in TS
[ldk-java] / typescript_strings.py
index 70e37331ba56db6230a7e39f6b2511f069ede9e7..a46c4bd65086f194b8e5e10973ab8f5f05b419b7 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,19 +138,15 @@ 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 = ""
         self.c_fn_args_pfx = "void* ctx_TODO"
-        self.file_ext = ""
+        self.file_ext = ".ts"
         self.ptr_c_ty = "uint32_t"
         self.ptr_native_ty = "uint32_t"
         self.result_c_ty = "uint32_t"
@@ -189,9 +194,13 @@ import java.util.Arrays;
         out_c = "static inline " + struct_name + " " + struct_name + "_from_js(int32_t ord) {\n"
         out_c = out_c + "\tswitch (ord) {\n"
         ord_v = 0
+
+        out_typescript_enum_fields = ""
+
         for var in variants:
             out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
             ord_v = ord_v + 1
+            out_typescript_enum_fields += f"{var},\n\t\t\t\t"
         out_c = out_c + "\t}\n"
         out_c = out_c + "\tabort();\n"
         out_c = out_c + "}\n"
@@ -205,16 +214,20 @@ import java.util.Arrays;
         out_c = out_c + "\t\tdefault: abort();\n"
         out_c = out_c + "\t}\n"
         out_c = out_c + "}\n"
-        return (out_c, "", "")
+
+        out_typescript_enum = f"""
+            export enum {struct_name} {{
+                {out_typescript_enum_fields}
+            }}
+        """
+
+        return (out_c, out_typescript_enum, "")
 
     def c_unitary_enum_to_native_call(self, ty_info):
         return (ty_info.rust_obj + "_to_js(", ")")
     def native_unitary_enum_to_c_call(self, ty_info):
         return (ty_info.rust_obj + "_from_js(", ")")
 
-    def c_complex_enum_pfx(self, struct_name, variants, init_meth_jty_strs):
-        return ""
-
     def c_complex_enum_pass_ty(self, struct_name):
         return "uint32_t"
 
@@ -226,3 +239,80 @@ import java.util.Arrays;
 
     def native_c_map_trait(self, struct_name, field_var_convs, field_fn_lines):
         return ("", "")
+
+    def map_complex_enum(self, struct_name, variant_list, camel_to_snake):
+        java_hu_type = struct_name.replace("LDK", "")
+
+        out_java_enum = ""
+        out_java = ""
+        out_c = ""
+
+        out_java_enum += (self.hu_struct_file_prefix)
+        out_java_enum += ("export default class " + java_hu_type + " extends CommonBase {\n")
+        out_java_enum += ("\tprotected constructor(_dummy: object, ptr: number) { super(ptr); }\n")
+        out_java_enum += ("\tprotected finalize() {\n")
+        out_java_enum += ("\t\tsuper.finalize();\n")
+        out_java_enum += ("\t\tif (this.ptr != 0) { bindings." + java_hu_type + "_free(this.ptr); }\n")
+        out_java_enum += ("\t}\n")
+        out_java_enum += f"\tstatic constr_from_ptr(ptr: number): {java_hu_type} {{\n"
+        out_java_enum += (f"\t\tconst raw_val: bindings.{struct_name} = bindings." + struct_name + "_ref_from_ptr(ptr);\n")
+        java_hu_subclasses = ""
+
+        out_java +=  ("\tpublic static class " + struct_name + " {\n")
+        out_java +=  ("\t\tprivate " + struct_name + "() {}\n")
+        for var in variant_list:
+            out_java +=  ("\t\texport class " + var.var_name + " extends " + struct_name + " {\n")
+            java_hu_subclasses = java_hu_subclasses + "export class " + var.var_name + " extends " + java_hu_type + " {\n"
+            out_java_enum += ("\t\tif (raw_val instanceof bindings." + struct_name + "." + var.var_name + ") {\n")
+            out_java_enum += ("\t\t\treturn new " + var.var_name + "(this.ptr, raw_val);\n")
+            init_meth_params = ""
+            init_meth_body = ""
+            hu_conv_body = ""
+            for idx, field_ty in enumerate(var.fields):
+                out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n")
+                java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n"
+                if field_ty.to_hu_conv is not None:
+                    hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n"
+                    hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n"
+                    hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n"
+                else:
+                    hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n"
+                if idx > 0:
+                    init_meth_params = init_meth_params + ", "
+                init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name
+                init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; "
+            out_java +=  ("\t\t\t" + var.var_name + "(" + init_meth_params + ") { ")
+            out_java +=  (init_meth_body)
+            out_java +=  ("}\n")
+            out_java += ("\t\t}\n")
+            out_java_enum += ("\t\t}\n")
+            java_hu_subclasses = java_hu_subclasses + "\tprivate constructor(ptr: number, obj: bindings." + struct_name + "." + var.var_name + ") {\n\t\tsuper(null, ptr);\n"
+            java_hu_subclasses = java_hu_subclasses + hu_conv_body
+            java_hu_subclasses = java_hu_subclasses + "\t}\n}\n"
+        out_java += ("\t\tstatic native void init();\n")
+        out_java += ("\t}\n")
+        out_java_enum += ("\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t}\n\n")
+        out_java += ("\tstatic { " + struct_name + ".init(); }\n")
+        out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
+
+        out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n")
+        out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+        out_c += ("\tswitch(obj->tag) {\n")
+        for var in variant_list:
+            out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
+            c_params = []
+            for idx, field_map in enumerate(var.fields):
+                if field_map.ret_conv is not None:
+                    out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
+                    out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+                    out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
+                    c_params.append(field_map.ret_conv_name)
+                else:
+                    c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+            out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
+            out_c += ("\t\t}\n")
+        out_c += ("\t\tdefault: abort();\n")
+        out_c += ("\t}\n}\n")
+        out_java_enum += ("}\n")
+        out_java_enum += (java_hu_subclasses)
+        return (out_java, out_java_enum, out_c)